arbre 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -2
- data/.travis.yml +1 -2
- data/CHANGELOG.md +6 -0
- data/Gemfile +8 -6
- data/LICENSE +20 -0
- data/README.md +110 -0
- data/Rakefile +11 -0
- data/arbre.gemspec +1 -0
- data/lib/arbre/element.rb +8 -3
- data/lib/arbre/element/proxy.rb +28 -0
- data/lib/arbre/html/attributes.rb +11 -2
- data/lib/arbre/html/tag.rb +2 -3
- data/lib/arbre/html/text_node.rb +4 -0
- data/lib/arbre/rails/template_handler.rb +5 -5
- data/lib/arbre/version.rb +1 -1
- data/spec/arbre/integration/html_spec.rb +46 -39
- data/spec/arbre/unit/component_spec.rb +5 -5
- data/spec/arbre/unit/context_spec.rb +8 -8
- data/spec/arbre/unit/element_finder_methods_spec.rb +36 -21
- data/spec/arbre/unit/element_spec.rb +51 -45
- data/spec/arbre/unit/html/class_list_spec.rb +2 -2
- data/spec/arbre/unit/html/tag_attributes_spec.rb +19 -17
- data/spec/arbre/unit/html/tag_spec.rb +17 -17
- data/spec/rails/integration/forms_spec.rb +9 -9
- data/spec/rails/integration/rendering_spec.rb +20 -10
- data/spec/rails/rails_spec_helper.rb +5 -12
- data/spec/rails/templates/arbre/_partial_with_assignment.arb +1 -0
- data/spec/rails/templates/arbre/page_with_arb_partial_and_assignment.arb +3 -0
- metadata +25 -17
- data/README.rdoc +0 -71
@@ -20,21 +20,21 @@ describe Arbre::Component do
|
|
20
20
|
let(:component){ component_class.new }
|
21
21
|
|
22
22
|
it "should be a subclass of an html div" do
|
23
|
-
Arbre::Component.ancestors.
|
23
|
+
expect(Arbre::Component.ancestors).to include(Arbre::HTML::Div)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should render to a div, even as a subclass" do
|
27
|
-
component.tag_name.
|
27
|
+
expect(component.tag_name).to eq('div')
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should add a class by default" do
|
31
|
-
component.class_list.
|
31
|
+
expect(component.class_list).to include("mock_component")
|
32
32
|
end
|
33
33
|
|
34
34
|
it "should render the object using the builder method name" do
|
35
|
-
comp = arbre {
|
35
|
+
comp = expect(arbre {
|
36
36
|
mock_component
|
37
|
-
}.to_s.
|
37
|
+
}.to_s).to eq <<-HTML
|
38
38
|
<div class="mock_component">
|
39
39
|
<h2>Hello World</h2>
|
40
40
|
</div>
|
@@ -10,26 +10,26 @@ describe Arbre::Context do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should not increment the indent_level" do
|
13
|
-
context.indent_level.
|
13
|
+
expect(context.indent_level).to eq(-1)
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should return a bytesize" do
|
17
|
-
context.bytesize.
|
17
|
+
expect(context.bytesize).to eq(25)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should return a length" do
|
21
|
-
context.length.
|
21
|
+
expect(context.length).to eq(25)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "should delegate missing methods to the html string" do
|
25
|
-
context.
|
26
|
-
context.index('<').
|
25
|
+
expect(context).to respond_to(:index)
|
26
|
+
expect(context.index('<')).to eq(0)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should use a cached version of the HTML for method delegation" do
|
30
|
-
context.
|
31
|
-
context.index('<').
|
32
|
-
context.index('<').
|
30
|
+
expect(context).to receive(:to_s).once.and_return("<h1>札幌市北区</h1>")
|
31
|
+
expect(context.index('<')).to eq(0)
|
32
|
+
expect(context.index('<')).to eq(0)
|
33
33
|
end
|
34
34
|
|
35
35
|
end
|
@@ -7,9 +7,9 @@ describe Arbre::Element, "Finder Methods" do
|
|
7
7
|
describe "finding elements by tag name" do
|
8
8
|
|
9
9
|
it "should return 0 when no elements exist" do
|
10
|
-
arbre {
|
10
|
+
expect(arbre {
|
11
11
|
div
|
12
|
-
}.get_elements_by_tag_name("li").size.
|
12
|
+
}.get_elements_by_tag_name("li").size).to eq(0)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should return a child element" do
|
@@ -19,8 +19,8 @@ describe Arbre::Element, "Finder Methods" do
|
|
19
19
|
ul
|
20
20
|
end
|
21
21
|
elements = html.get_elements_by_tag_name("li")
|
22
|
-
elements.size.
|
23
|
-
elements[0].
|
22
|
+
expect(elements.size).to eq(1)
|
23
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Li)
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should return multple child elements" do
|
@@ -31,9 +31,9 @@ describe Arbre::Element, "Finder Methods" do
|
|
31
31
|
li
|
32
32
|
end
|
33
33
|
elements = html.get_elements_by_tag_name("li")
|
34
|
-
elements.size.
|
35
|
-
elements[0].
|
36
|
-
elements[1].
|
34
|
+
expect(elements.size).to eq(2)
|
35
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Li)
|
36
|
+
expect(elements[1]).to be_instance_of(Arbre::HTML::Li)
|
37
37
|
end
|
38
38
|
|
39
39
|
it "should return children's child elements" do
|
@@ -44,10 +44,10 @@ describe Arbre::Element, "Finder Methods" do
|
|
44
44
|
end
|
45
45
|
end
|
46
46
|
elements = html.get_elements_by_tag_name("li")
|
47
|
-
elements.size.
|
48
|
-
elements[0].
|
49
|
-
elements[1].
|
50
|
-
elements[1].parent.
|
47
|
+
expect(elements.size).to eq(2)
|
48
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Li)
|
49
|
+
expect(elements[1]).to be_instance_of(Arbre::HTML::Li)
|
50
|
+
expect(elements[1].parent).to eq(elements[0])
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
@@ -56,9 +56,15 @@ describe Arbre::Element, "Finder Methods" do
|
|
56
56
|
describe "finding an element by a class name" do
|
57
57
|
|
58
58
|
it "should return 0 when no elements exist" do
|
59
|
-
arbre {
|
59
|
+
expect(arbre {
|
60
60
|
div
|
61
|
-
}.get_elements_by_class_name("my_class").size.
|
61
|
+
}.get_elements_by_class_name("my_class").size).to eq(0)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should allow text nodes on tree" do
|
65
|
+
expect(arbre {
|
66
|
+
text_node "text"
|
67
|
+
}.get_elements_by_class_name("my_class").size).to eq(0)
|
62
68
|
end
|
63
69
|
|
64
70
|
it "should return a child element" do
|
@@ -67,8 +73,8 @@ describe Arbre::Element, "Finder Methods" do
|
|
67
73
|
div :class => "my_class"
|
68
74
|
end
|
69
75
|
elements = html.get_elements_by_class_name("my_class")
|
70
|
-
elements.size.
|
71
|
-
elements[0].
|
76
|
+
expect(elements.size).to eq(1)
|
77
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Div)
|
72
78
|
end
|
73
79
|
|
74
80
|
it "should return multple child elements" do
|
@@ -78,9 +84,9 @@ describe Arbre::Element, "Finder Methods" do
|
|
78
84
|
div :class => "my_class"
|
79
85
|
end
|
80
86
|
elements = html.get_elements_by_class_name("my_class")
|
81
|
-
elements.size.
|
82
|
-
elements[0].
|
83
|
-
elements[1].
|
87
|
+
expect(elements.size).to eq(2)
|
88
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Div)
|
89
|
+
expect(elements[1]).to be_instance_of(Arbre::HTML::Div)
|
84
90
|
end
|
85
91
|
|
86
92
|
it "should return elements that match one of several classes" do
|
@@ -91,11 +97,20 @@ describe Arbre::Element, "Finder Methods" do
|
|
91
97
|
|
92
98
|
end
|
93
99
|
elements = html.get_elements_by_class_name("this_class")
|
94
|
-
elements.size.
|
95
|
-
elements[0].
|
100
|
+
expect(elements.size).to eq(1)
|
101
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Div)
|
96
102
|
end
|
97
103
|
|
98
|
-
|
104
|
+
it "should return a grandchild element" do
|
105
|
+
html = arbre do
|
106
|
+
div :class => "some_class" do
|
107
|
+
div :class => "my_class"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
elements = html.get_elements_by_class_name("my_class")
|
111
|
+
expect(elements.size).to eq(1)
|
112
|
+
expect(elements[0]).to be_instance_of(Arbre::HTML::Div)
|
113
|
+
end
|
99
114
|
|
100
115
|
end
|
101
116
|
end
|
@@ -7,25 +7,25 @@ describe Arbre::Element do
|
|
7
7
|
context "when initialized" do
|
8
8
|
|
9
9
|
it "should have no children" do
|
10
|
-
element.children.
|
10
|
+
expect(element.children).to be_empty
|
11
11
|
end
|
12
12
|
|
13
13
|
it "should have no parent" do
|
14
|
-
element.parent.
|
14
|
+
expect(element.parent).to be_nil
|
15
15
|
end
|
16
16
|
|
17
17
|
it "should respond to the HTML builder methods" do
|
18
|
-
element.
|
18
|
+
expect(element).to respond_to(:span)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should have a set of local assigns" do
|
22
22
|
context = Arbre::Context.new :hello => "World"
|
23
23
|
element = Arbre::Element.new(context)
|
24
|
-
element.assigns[:hello].
|
24
|
+
expect(element.assigns[:hello]).to eq("World")
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should have an empty hash with no local assigns" do
|
28
|
-
element.assigns.
|
28
|
+
expect(element.assigns).to eq({})
|
29
29
|
end
|
30
30
|
|
31
31
|
end
|
@@ -43,28 +43,34 @@ describe Arbre::Element do
|
|
43
43
|
let(:element){ Arbre::Element.new(Arbre::Context.new(nil, helper.new)) }
|
44
44
|
|
45
45
|
it "should call methods on the helper object and return TextNode objects" do
|
46
|
-
element.helper_method.
|
46
|
+
expect(element.helper_method).to eq("helper method")
|
47
47
|
end
|
48
48
|
|
49
49
|
it "should raise a NoMethodError if not found" do
|
50
|
-
|
50
|
+
expect {
|
51
51
|
element.a_method_that_doesnt_exist
|
52
|
-
}.
|
52
|
+
}.to raise_error(NoMethodError)
|
53
53
|
end
|
54
54
|
|
55
55
|
end
|
56
56
|
|
57
57
|
describe "passing in assigns" do
|
58
|
-
let(:post){
|
58
|
+
let(:post){ double }
|
59
59
|
let(:assigns){ {:post => post} }
|
60
60
|
|
61
61
|
it "should be accessible via a method call" do
|
62
62
|
element = Arbre::Element.new(Arbre::Context.new(assigns))
|
63
|
-
element.post.
|
63
|
+
expect(element.post).to eq(post)
|
64
64
|
end
|
65
65
|
|
66
66
|
end
|
67
67
|
|
68
|
+
it "to_a.flatten should not infinitely recurse" do
|
69
|
+
Timeout.timeout(1) do
|
70
|
+
element.to_a.flatten
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
68
74
|
describe "adding a child" do
|
69
75
|
|
70
76
|
let(:child){ Arbre::Element.new }
|
@@ -74,11 +80,11 @@ describe Arbre::Element do
|
|
74
80
|
end
|
75
81
|
|
76
82
|
it "should add the child to the parent" do
|
77
|
-
element.children.first.
|
83
|
+
expect(element.children.first).to eq(child)
|
78
84
|
end
|
79
85
|
|
80
86
|
it "should set the parent of the child" do
|
81
|
-
child.parent.
|
87
|
+
expect(child.parent).to eq(element)
|
82
88
|
end
|
83
89
|
|
84
90
|
context "when the child is nil" do
|
@@ -86,7 +92,7 @@ describe Arbre::Element do
|
|
86
92
|
let(:child){ nil }
|
87
93
|
|
88
94
|
it "should not add the child" do
|
89
|
-
element.children.
|
95
|
+
expect(element.children).to be_empty
|
90
96
|
end
|
91
97
|
|
92
98
|
end
|
@@ -96,8 +102,8 @@ describe Arbre::Element do
|
|
96
102
|
let(:child){ "Hello World" }
|
97
103
|
|
98
104
|
it "should add as a TextNode" do
|
99
|
-
element.children.first.
|
100
|
-
element.children.first.to_s.
|
105
|
+
expect(element.children.first).to be_instance_of(Arbre::HTML::TextNode)
|
106
|
+
expect(element.children.first.to_s).to eq("Hello World")
|
101
107
|
end
|
102
108
|
|
103
109
|
end
|
@@ -113,17 +119,17 @@ describe Arbre::Element do
|
|
113
119
|
end
|
114
120
|
|
115
121
|
it "should clear the existing children" do
|
116
|
-
element.children.size.
|
122
|
+
expect(element.children.size).to eq(1)
|
117
123
|
end
|
118
124
|
|
119
125
|
it "should add the string as a child" do
|
120
|
-
element.children.first.to_s.
|
126
|
+
expect(element.children.first.to_s).to eq("Goodbye")
|
121
127
|
end
|
122
128
|
|
123
129
|
it "should html escape the string" do
|
124
130
|
string = "Goodbye <br />"
|
125
131
|
element.content = string
|
126
|
-
element.content.to_s.
|
132
|
+
expect(element.content.to_s).to eq("Goodbye <br />")
|
127
133
|
end
|
128
134
|
end
|
129
135
|
|
@@ -135,11 +141,11 @@ describe Arbre::Element do
|
|
135
141
|
end
|
136
142
|
|
137
143
|
it "should set the content tag" do
|
138
|
-
element.children.first.
|
144
|
+
expect(element.children.first).to eq(content_element)
|
139
145
|
end
|
140
146
|
|
141
147
|
it "should set the tags parent" do
|
142
|
-
content_element.parent.
|
148
|
+
expect(content_element.parent).to eq(element)
|
143
149
|
end
|
144
150
|
end
|
145
151
|
|
@@ -152,11 +158,11 @@ describe Arbre::Element do
|
|
152
158
|
end
|
153
159
|
|
154
160
|
it "should set the content tag" do
|
155
|
-
element.children.first.
|
161
|
+
expect(element.children.first).to eq(first)
|
156
162
|
end
|
157
163
|
|
158
164
|
it "should set the tags parent" do
|
159
|
-
element.children.first.parent.
|
165
|
+
expect(element.children.first.parent).to eq(element)
|
160
166
|
end
|
161
167
|
end
|
162
168
|
|
@@ -169,17 +175,17 @@ describe Arbre::Element do
|
|
169
175
|
let(:collection){ element + "hello world" }
|
170
176
|
|
171
177
|
it "should render the children collection" do
|
172
|
-
element.children.
|
173
|
-
element.to_s.
|
178
|
+
expect(element.children).to receive(:to_s).and_return("content")
|
179
|
+
expect(element.to_s).to eq("content")
|
174
180
|
end
|
175
181
|
|
176
182
|
it "should render collection when is set the default separator" do
|
177
183
|
$, = "_"
|
178
|
-
collection.to_s.
|
184
|
+
expect(collection.to_s).to eq("hello world")
|
179
185
|
end
|
180
186
|
|
181
187
|
it "should render collection when is not set the default separator" do
|
182
|
-
collection.to_s.
|
188
|
+
expect(collection.to_s).to eq("hello world")
|
183
189
|
end
|
184
190
|
|
185
191
|
end
|
@@ -192,13 +198,13 @@ describe Arbre::Element do
|
|
192
198
|
let(:collection){ first + second }
|
193
199
|
|
194
200
|
it "should return an instance of Collection" do
|
195
|
-
collection.
|
201
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
196
202
|
end
|
197
203
|
|
198
204
|
it "should return the elements in the collection" do
|
199
|
-
collection.size.
|
200
|
-
collection.first.
|
201
|
-
collection[1].
|
205
|
+
expect(collection.size).to eq(2)
|
206
|
+
expect(collection.first).to eq(first)
|
207
|
+
expect(collection[1]).to eq(second)
|
202
208
|
end
|
203
209
|
end
|
204
210
|
|
@@ -209,14 +215,14 @@ describe Arbre::Element do
|
|
209
215
|
let(:collection){ Arbre::ElementCollection.new([first, second]) + third}
|
210
216
|
|
211
217
|
it "should return an instance of Collection" do
|
212
|
-
collection.
|
218
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
213
219
|
end
|
214
220
|
|
215
221
|
it "should return the elements in the collection flattened" do
|
216
|
-
collection.size.
|
217
|
-
collection[0].
|
218
|
-
collection[1].
|
219
|
-
collection[2].
|
222
|
+
expect(collection.size).to eq(3)
|
223
|
+
expect(collection[0]).to eq(first)
|
224
|
+
expect(collection[1]).to eq(second)
|
225
|
+
expect(collection[2]).to eq(third)
|
220
226
|
end
|
221
227
|
end
|
222
228
|
|
@@ -227,14 +233,14 @@ describe Arbre::Element do
|
|
227
233
|
let(:collection){ first + Arbre::ElementCollection.new([second,third]) }
|
228
234
|
|
229
235
|
it "should return an instance of Collection" do
|
230
|
-
collection.
|
236
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
231
237
|
end
|
232
238
|
|
233
239
|
it "should return the elements in the collection flattened" do
|
234
|
-
collection.size.
|
235
|
-
collection[0].
|
236
|
-
collection[1].
|
237
|
-
collection[2].
|
240
|
+
expect(collection.size).to eq(3)
|
241
|
+
expect(collection[0]).to eq(first)
|
242
|
+
expect(collection[1]).to eq(second)
|
243
|
+
expect(collection[2]).to eq(third)
|
238
244
|
end
|
239
245
|
end
|
240
246
|
|
@@ -243,13 +249,13 @@ describe Arbre::Element do
|
|
243
249
|
let(:collection){ element + "Hello World"}
|
244
250
|
|
245
251
|
it "should return an instance of Collection" do
|
246
|
-
collection.
|
252
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
247
253
|
end
|
248
254
|
|
249
255
|
it "should return the elements in the collection" do
|
250
|
-
collection.size.
|
251
|
-
collection[0].
|
252
|
-
collection[1].
|
256
|
+
expect(collection.size).to eq(2)
|
257
|
+
expect(collection[0]).to eq(element)
|
258
|
+
expect(collection[1]).to be_an_instance_of(Arbre::HTML::TextNode)
|
253
259
|
end
|
254
260
|
end
|
255
261
|
|
@@ -257,7 +263,7 @@ describe Arbre::Element do
|
|
257
263
|
let(:collection){ "hello World" + Arbre::Element.new}
|
258
264
|
|
259
265
|
it "should return a string" do
|
260
|
-
collection.strip.chomp.
|
266
|
+
expect(collection.strip.chomp).to eq("hello World")
|
261
267
|
end
|
262
268
|
end
|
263
269
|
end
|
@@ -6,9 +6,9 @@ describe Arbre::HTML::ClassList do
|
|
6
6
|
|
7
7
|
it "should build a new list from a string of classes" do
|
8
8
|
list = Arbre::HTML::ClassList.build_from_string("first second")
|
9
|
-
list.size.
|
9
|
+
expect(list.size).to eq(2)
|
10
10
|
|
11
|
-
list.to_a.sort.
|
11
|
+
expect(list.to_a.sort).to eq(%w{first second})
|
12
12
|
end
|
13
13
|
|
14
14
|
end
|
@@ -9,52 +9,54 @@ describe Arbre::HTML::Tag, "Attributes" do
|
|
9
9
|
before { tag.build :id => "my_id" }
|
10
10
|
|
11
11
|
it "should have an attributes hash" do
|
12
|
-
tag.attributes.
|
12
|
+
expect(tag.attributes).to eq({:id => "my_id"})
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should render the attributes to html" do
|
16
|
-
tag.to_s.
|
17
|
-
|
18
|
-
|
16
|
+
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "shouldn't render attributes that are empty" do
|
20
|
+
tag.class_list # initializes an empty ClassList
|
21
|
+
tag.set_attribute :foo, ''
|
22
|
+
tag.set_attribute :bar, nil
|
23
|
+
|
24
|
+
expect(tag.to_s).to eq "<tag id=\"my_id\"></tag>\n"
|
19
25
|
end
|
20
26
|
|
21
27
|
it "should get an attribute value" do
|
22
|
-
tag.attr(:id).
|
28
|
+
expect(tag.attr(:id)).to eq("my_id")
|
23
29
|
end
|
24
30
|
|
25
31
|
describe "#has_attribute?" do
|
26
32
|
context "when the attribute exists" do
|
27
33
|
it "should return true" do
|
28
|
-
tag.has_attribute?(:id).
|
34
|
+
expect(tag.has_attribute?(:id)).to eq(true)
|
29
35
|
end
|
30
36
|
end
|
31
37
|
|
32
38
|
context "when the attribute does not exist" do
|
33
39
|
it "should return false" do
|
34
|
-
tag.has_attribute?(:class).
|
40
|
+
expect(tag.has_attribute?(:class)).to eq(false)
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
38
44
|
|
39
45
|
it "should remove an attribute" do
|
40
|
-
tag.attributes.
|
41
|
-
tag.remove_attribute(:id).
|
42
|
-
tag.attributes.
|
46
|
+
expect(tag.attributes).to eq({:id => "my_id"})
|
47
|
+
expect(tag.remove_attribute(:id)).to eq("my_id")
|
48
|
+
expect(tag.attributes).to eq({})
|
43
49
|
end
|
44
50
|
end
|
45
51
|
|
46
52
|
describe "rendering attributes" do
|
47
53
|
it "should html safe the attribute values" do
|
48
|
-
tag.set_attribute(:class, "
|
49
|
-
tag.to_s.
|
50
|
-
<tag class="">bad things!"></tag>
|
51
|
-
HTML
|
54
|
+
tag.set_attribute(:class, '">bad things!')
|
55
|
+
expect(tag.to_s).to eq "<tag class=\"">bad things!\"></tag>\n"
|
52
56
|
end
|
53
57
|
it "should should escape the attribute names" do
|
54
58
|
tag.set_attribute(">bad", "things")
|
55
|
-
tag.to_s.
|
56
|
-
<tag >bad="things"></tag>
|
57
|
-
HTML
|
59
|
+
expect(tag.to_s).to eq "<tag >bad=\"things\"></tag>\n"
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|