element_factory 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
data/lib/element_factory.rb
CHANGED
@@ -2,12 +2,17 @@ module ElementFactory
|
|
2
2
|
class Element
|
3
3
|
attr_accessor :name, :attributes
|
4
4
|
|
5
|
+
CONTENT_ATTRIBUTES = {text: Elements::TextElement, inner_html: Elements::InnerHtmlElement}
|
6
|
+
|
5
7
|
def initialize(name, attributes_or_string=nil)
|
6
8
|
@name = name.to_s
|
7
9
|
@attributes = coerce_attributes(attributes_or_string)
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
+
CONTENT_ATTRIBUTES.each do |attribute, klass|
|
12
|
+
if value = attributes[attribute]
|
13
|
+
add_child klass.new(value)
|
14
|
+
attributes.delete(attribute)
|
15
|
+
end
|
11
16
|
end
|
12
17
|
end
|
13
18
|
|
@@ -50,10 +55,8 @@ module ElementFactory
|
|
50
55
|
|
51
56
|
def coerce_attributes(attributes)
|
52
57
|
case attributes
|
53
|
-
when String
|
54
|
-
|
55
|
-
when Hash
|
56
|
-
attributes
|
58
|
+
when String then {text: attributes}
|
59
|
+
when Hash then attributes
|
57
60
|
else
|
58
61
|
{}
|
59
62
|
end
|
data/spec/lib/element_spec.rb
CHANGED
@@ -19,13 +19,18 @@ describe ElementFactory::Element do
|
|
19
19
|
|
20
20
|
specify "with a string" do
|
21
21
|
tag = subject.new(:span, "Some text")
|
22
|
-
expect(tag.
|
22
|
+
expect(tag.children.first).to be_kind_of ElementFactory::Elements::TextElement
|
23
23
|
end
|
24
24
|
|
25
25
|
specify "with a string adds a child" do
|
26
26
|
tag = subject.new(:span, "Some text")
|
27
27
|
expect(tag).to have(1).children
|
28
28
|
end
|
29
|
+
|
30
|
+
it "accepts inner html" do
|
31
|
+
tag = subject.new(:span, inner_html: "<p>I'm inside and safe!</p>".html_safe)
|
32
|
+
expect(tag).to have_tag "p"
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
context "tag pieces" do
|
@@ -91,6 +96,14 @@ describe ElementFactory::Element do
|
|
91
96
|
expect(element.text).to eq("Text")
|
92
97
|
end
|
93
98
|
end
|
99
|
+
|
100
|
+
context "omitting attributes" do
|
101
|
+
subject { described_class.new(:span, inner_html: "<p>Whut</p>") }
|
102
|
+
|
103
|
+
it "does not include an inner_html attribute" do
|
104
|
+
expect(subject).to_not have_xpath "//span[@inner_html]"
|
105
|
+
end
|
106
|
+
end
|
94
107
|
end
|
95
108
|
|
96
109
|
context "children" do
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "nokogiri"
|
2
|
+
|
3
|
+
RSpec::Matchers.define :have_tag do |tag|
|
4
|
+
match do |string|
|
5
|
+
document = Nokogiri::HTML(string.to_s)
|
6
|
+
!!document.at_css(tag)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Matchers.define :have_xpath do |tag|
|
11
|
+
match do |string|
|
12
|
+
document = Nokogiri::HTML(string.to_s)
|
13
|
+
!!document.at_xpath(tag)
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: element_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -178,6 +178,7 @@ files:
|
|
178
178
|
- element_factory.gemspec
|
179
179
|
- lib/element_factory.rb
|
180
180
|
- lib/element_factory/element.rb
|
181
|
+
- lib/element_factory/elements/inner_html_element.rb
|
181
182
|
- lib/element_factory/elements/text_element.rb
|
182
183
|
- lib/element_factory/html_attributes.rb
|
183
184
|
- lib/element_factory/version.rb
|
@@ -186,6 +187,7 @@ files:
|
|
186
187
|
- spec/lib/html_attributes_spec.rb
|
187
188
|
- spec/spec_helper.rb
|
188
189
|
- spec/support/element_helpers.rb
|
190
|
+
- spec/support/matchers/elements.rb
|
189
191
|
homepage: ''
|
190
192
|
licenses: []
|
191
193
|
post_install_message:
|
@@ -216,4 +218,5 @@ test_files:
|
|
216
218
|
- spec/lib/html_attributes_spec.rb
|
217
219
|
- spec/spec_helper.rb
|
218
220
|
- spec/support/element_helpers.rb
|
221
|
+
- spec/support/matchers/elements.rb
|
219
222
|
has_rdoc:
|