arbre 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +4 -2
- data/.travis.yml +1 -2
- data/CHANGELOG.md +13 -0
- data/Gemfile +8 -5
- data/LICENSE +20 -0
- data/README.md +110 -0
- data/Rakefile +11 -0
- data/arbre.gemspec +1 -0
- data/lib/arbre/element.rb +9 -4
- data/lib/arbre/element/proxy.rb +28 -0
- data/lib/arbre/element_collection.rb +1 -1
- data/lib/arbre/html/attributes.rb +11 -2
- data/lib/arbre/html/class_list.rb +7 -0
- data/lib/arbre/html/tag.rb +12 -4
- 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 +62 -43
- data/spec/arbre/unit/html/class_list_spec.rb +16 -0
- data/spec/arbre/unit/html/tag_attributes_spec.rb +19 -17
- data/spec/arbre/unit/html/tag_spec.rb +23 -16
- 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 +6 -7
- 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 +27 -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
|
|
@@ -164,9 +170,22 @@ describe Arbre::Element do
|
|
164
170
|
|
165
171
|
describe "rendering to html" do
|
166
172
|
|
173
|
+
before { @separator = $, }
|
174
|
+
after { $, = @separator }
|
175
|
+
let(:collection){ element + "hello world" }
|
176
|
+
|
167
177
|
it "should render the children collection" do
|
168
|
-
element.children.
|
169
|
-
element.to_s.
|
178
|
+
expect(element.children).to receive(:to_s).and_return("content")
|
179
|
+
expect(element.to_s).to eq("content")
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should render collection when is set the default separator" do
|
183
|
+
$, = "_"
|
184
|
+
expect(collection.to_s).to eq("hello world")
|
185
|
+
end
|
186
|
+
|
187
|
+
it "should render collection when is not set the default separator" do
|
188
|
+
expect(collection.to_s).to eq("hello world")
|
170
189
|
end
|
171
190
|
|
172
191
|
end
|
@@ -179,13 +198,13 @@ describe Arbre::Element do
|
|
179
198
|
let(:collection){ first + second }
|
180
199
|
|
181
200
|
it "should return an instance of Collection" do
|
182
|
-
collection.
|
201
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
183
202
|
end
|
184
203
|
|
185
204
|
it "should return the elements in the collection" do
|
186
|
-
collection.size.
|
187
|
-
collection.first.
|
188
|
-
collection[1].
|
205
|
+
expect(collection.size).to eq(2)
|
206
|
+
expect(collection.first).to eq(first)
|
207
|
+
expect(collection[1]).to eq(second)
|
189
208
|
end
|
190
209
|
end
|
191
210
|
|
@@ -196,14 +215,14 @@ describe Arbre::Element do
|
|
196
215
|
let(:collection){ Arbre::ElementCollection.new([first, second]) + third}
|
197
216
|
|
198
217
|
it "should return an instance of Collection" do
|
199
|
-
collection.
|
218
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
200
219
|
end
|
201
220
|
|
202
221
|
it "should return the elements in the collection flattened" do
|
203
|
-
collection.size.
|
204
|
-
collection[0].
|
205
|
-
collection[1].
|
206
|
-
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)
|
207
226
|
end
|
208
227
|
end
|
209
228
|
|
@@ -214,14 +233,14 @@ describe Arbre::Element do
|
|
214
233
|
let(:collection){ first + Arbre::ElementCollection.new([second,third]) }
|
215
234
|
|
216
235
|
it "should return an instance of Collection" do
|
217
|
-
collection.
|
236
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
218
237
|
end
|
219
238
|
|
220
239
|
it "should return the elements in the collection flattened" do
|
221
|
-
collection.size.
|
222
|
-
collection[0].
|
223
|
-
collection[1].
|
224
|
-
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)
|
225
244
|
end
|
226
245
|
end
|
227
246
|
|
@@ -230,13 +249,13 @@ describe Arbre::Element do
|
|
230
249
|
let(:collection){ element + "Hello World"}
|
231
250
|
|
232
251
|
it "should return an instance of Collection" do
|
233
|
-
collection.
|
252
|
+
expect(collection).to be_an_instance_of(Arbre::ElementCollection)
|
234
253
|
end
|
235
254
|
|
236
255
|
it "should return the elements in the collection" do
|
237
|
-
collection.size.
|
238
|
-
collection[0].
|
239
|
-
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)
|
240
259
|
end
|
241
260
|
end
|
242
261
|
|
@@ -244,7 +263,7 @@ describe Arbre::Element do
|
|
244
263
|
let(:collection){ "hello World" + Arbre::Element.new}
|
245
264
|
|
246
265
|
it "should return a string" do
|
247
|
-
collection.strip.chomp.
|
266
|
+
expect(collection.strip.chomp).to eq("hello World")
|
248
267
|
end
|
249
268
|
end
|
250
269
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Arbre::HTML::ClassList do
|
4
|
+
|
5
|
+
describe ".build_from_string" do
|
6
|
+
|
7
|
+
it "should build a new list from a string of classes" do
|
8
|
+
list = Arbre::HTML::ClassList.build_from_string("first second")
|
9
|
+
expect(list.size).to eq(2)
|
10
|
+
|
11
|
+
expect(list.to_a.sort).to eq(%w{first second})
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|