aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/template/html-scanner/tag_node_test.rb
blob: d1d466737840a4e87efb3c8726992742fa76bd6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require 'abstract_unit'

class TagNodeTest < Test::Unit::TestCase
  def test_open_without_attributes
    node = tag("<tag>")
    assert_equal "tag", node.name
    assert_equal Hash.new, node.attributes
    assert_nil node.closing
  end
  
  def test_open_with_attributes
    node = tag("<TAG1 foo=hey_ho x:bar=\"blah blah\" BAZ='blah blah blah' >")
    assert_equal "tag1", node.name
    assert_equal "hey_ho", node["foo"]
    assert_equal "blah blah", node["x:bar"]
    assert_equal "blah blah blah", node["baz"]
  end
  
  def test_self_closing_without_attributes
    node = tag("<tag/>")
    assert_equal "tag", node.name
    assert_equal Hash.new, node.attributes
    assert_equal :self, node.closing
  end
  
  def test_self_closing_with_attributes
    node = tag("<tag a=b/>")
    assert_equal "tag", node.name
    assert_equal( { "a" => "b" }, node.attributes )
    assert_equal :self, node.closing
  end
  
  def test_closing_without_attributes
    node = tag("</tag>")
    assert_equal "tag", node.name
    assert_nil node.attributes
    assert_equal :close, node.closing
  end
  
  def test_bracket_op_when_no_attributes
    node = tag("</tag>")
    assert_nil node["foo"]
  end

  def test_bracket_op_when_attributes
    node = tag("<tag a=b/>")
    assert_equal "b", node["a"]
  end
  
  def test_attributes_with_escaped_quotes
    node = tag("<tag a='b\\'c' b=\"bob \\\"float\\\"\">")
    assert_equal "b\\'c", node["a"]
    assert_equal "bob \\\"float\\\"", node["b"]
  end
  
  def test_to_s
    node = tag("<a b=c d='f' g=\"h 'i'\" />")
    assert_equal %(<a b='c' d='f' g='h \\'i\\'' />), node.to_s
  end
  
  def test_tag
    assert tag("<tag>").tag?
  end
  
  def test_match_tag_as_string
    assert tag("<tag>").match(:tag => "tag")
    assert !tag("<tag>").match(:tag => "b")
  end
  
  def test_match_tag_as_regexp
    assert tag("<tag>").match(:tag => /t.g/)
    assert !tag("<tag>").match(:tag => /t[bqs]g/)
  end

  def test_match_attributes_as_string
    t = tag("<tag a=something b=else />")
    assert t.match(:attributes => {"a" => "something"})
    assert t.match(:attributes => {"b" => "else"})
  end
  
  def test_match_attributes_as_regexp
    t = tag("<tag a=something b=else />")
    assert t.match(:attributes => {"a" => /^something$/})
    assert t.match(:attributes => {"b" =>  /e.*e/})
    assert t.match(:attributes => {"a" => /me..i/, "b" => /.ls.$/})
  end
  
  def test_match_attributes_as_number
    t = tag("<tag a=15 b=3.1415 />")
    assert t.match(:attributes => {"a" => 15})
    assert t.match(:attributes => {"b" => 3.1415})
    assert t.match(:attributes => {"a" => 15, "b" => 3.1415})
  end
  
  def test_match_attributes_exist
    t = tag("<tag a=15 b=3.1415 />")
    assert t.match(:attributes => {"a" => true})
    assert t.match(:attributes => {"b" => true})
    assert t.match(:attributes => {"a" => true, "b" => true})
  end
  
  def test_match_attributes_not_exist
    t = tag("<tag a=15 b=3.1415 />")
    assert t.match(:attributes => {"c" => false})
    assert t.match(:attributes => {"c" => nil})
    assert t.match(:attributes => {"a" => true, "c" => false})
  end
  
  def test_match_parent_success
    t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
    assert t.match(:parent => {:tag => "foo", :attributes => {"k" => /v.l/, "j" => false}})
  end
  
  def test_match_parent_fail
    t = tag("<tag a=15 b='hello'>", tag("<foo k='value'>"))
    assert !t.match(:parent => {:tag => /kafka/})
  end
  
  def test_match_child_success
    t = tag("<tag x:k='something'>")
    tag("<child v=john a=kelly>", t)
    tag("<sib m=vaughn v=james>", t)
    assert t.match(:child => { :tag => "sib", :attributes => {"v" => /j/}})
    assert t.match(:child => { :attributes => {"a" => "kelly"}})
  end
  
  def test_match_child_fail
    t = tag("<tag x:k='something'>")
    tag("<child v=john a=kelly>", t)
    tag("<sib m=vaughn v=james>", t)
    assert !t.match(:child => { :tag => "sib", :attributes => {"v" => /r/}})
    assert !t.match(:child => { :attributes => {"v" => false}})
  end
  
  def test_match_ancestor_success
    t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
    assert t.match(:ancestor => {:tag => "parent", :attributes => {"a" => /ll/}})
    assert t.match(:ancestor => {:attributes => {"m" => "vaughn"}})
  end
  
  def test_match_ancestor_fail
    t = tag("<tag x:k='something'>", tag("<parent v=john a=kelly>", tag("<grandparent m=vaughn v=james>")))
    assert !t.match(:ancestor => {:tag => /^parent/, :attributes => {"v" => /m/}})
    assert !t.match(:ancestor => {:attributes => {"v" => false}})
  end

  def test_match_descendant_success
    tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
    assert t.match(:descendant => {:tag => "child", :attributes => {"a" => /ll/}})
    assert t.match(:descendant => {:attributes => {"m" => "vaughn"}})
  end
  
  def test_match_descendant_fail
    tag("<grandchild m=vaughn v=james>", tag("<child v=john a=kelly>", t = tag("<tag x:k='something'>")))
    assert !t.match(:descendant => {:tag => /^child/, :attributes => {"v" => /m/}})
    assert !t.match(:descendant => {:attributes => {"v" => false}})
  end
  
  def test_match_child_count
    t = tag("<tag x:k='something'>")
    tag("hello", t)
    tag("<child v=john a=kelly>", t)
    tag("<sib m=vaughn v=james>", t)
    assert t.match(:children => { :count => 2 })
    assert t.match(:children => { :count => 2..4 })
    assert t.match(:children => { :less_than => 4 })
    assert t.match(:children => { :greater_than => 1 })
    assert !t.match(:children => { :count => 3 })
  end

  def test_conditions_as_strings
    t = tag("<tag x:k='something'>")
    assert t.match("tag" => "tag")
    assert t.match("attributes" => { "x:k" => "something" })
    assert !t.match("tag" => "gat")
    assert !t.match("attributes" => { "x:j" => "something" })
  end

  def test_attributes_as_symbols
    t = tag("<child v=john a=kelly>")
    assert t.match(:attributes => { :v => /oh/ })
    assert t.match(:attributes => { :a => /ll/ })
  end

  def test_match_sibling
    t = tag("<tag x:k='something'>")
    tag("hello", t)
    tag("<span a=b>", t)
    tag("world", t)
    m = tag("<span k=r>", t)
    tag("<span m=l>", t)

    assert m.match(:sibling => {:tag => "span", :attributes => {:a => true}})
    assert m.match(:sibling => {:tag => "span", :attributes => {:m => true}})
    assert !m.match(:sibling => {:tag => "span", :attributes => {:k => true}})
  end

  def test_match_sibling_before
    t = tag("<tag x:k='something'>")
    tag("hello", t)
    tag("<span a=b>", t)
    tag("world", t)
    m = tag("<span k=r>", t)
    tag("<span m=l>", t)

    assert m.match(:before => {:tag => "span", :attributes => {:m => true}})
    assert !m.match(:before => {:tag => "span", :attributes => {:a => true}})
    assert !m.match(:before => {:tag => "span", :attributes => {:k => true}})
  end

  def test_match_sibling_after
    t = tag("<tag x:k='something'>")
    tag("hello", t)
    tag("<span a=b>", t)
    tag("world", t)
    m = tag("<span k=r>", t)
    tag("<span m=l>", t)

    assert m.match(:after => {:tag => "span", :attributes => {:a => true}})
    assert !m.match(:after => {:tag => "span", :attributes => {:m => true}})
    assert !m.match(:after => {:tag => "span", :attributes => {:k => true}})
  end

  def test_to_s
    t = tag("<b x='foo'>")
    tag("hello", t)
    tag("<hr />", t)
    assert_equal %(<b x="foo">hello<hr /></b>), t.to_s
  end

  private
  
    def tag(content, parent=nil)
      node = HTML::Node.parse(parent,0,0,content)
      parent.children << node if parent
      node
    end
end