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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
require "helper"
module Nokogiri
module CSS
class TestParser < Nokogiri::TestCase
def setup
super
@parser = Nokogiri::CSS::Parser.new
@parser_with_ns = Nokogiri::CSS::Parser.new({
"xmlns" => "http://default.example.com/",
"hoge" => "http://hoge.example.com/",
})
end
def test_extra_single_quote
assert_raises(CSS::SyntaxError) { @parser.parse("'") }
end
def test_syntax_error_raised
assert_raises(CSS::SyntaxError) { @parser.parse("a[x=]") }
end
def test_function_and_pseudo
assert_xpath '//child::text()[position() = 99]', @parser.parse('text():nth-of-type(99)')
end
def test_find_by_type
ast = @parser.parse("a:nth-child(2)").first
matches = ast.find_by_type(
[:CONDITIONAL_SELECTOR,
[:ELEMENT_NAME],
[:PSEUDO_CLASS,
[:FUNCTION]
]
]
)
assert_equal(1, matches.length)
assert_equal(ast, matches.first)
end
def test_to_type
ast = @parser.parse("a:nth-child(2)").first
assert_equal(
[:CONDITIONAL_SELECTOR,
[:ELEMENT_NAME],
[:PSEUDO_CLASS,
[:FUNCTION]
]
], ast.to_type
)
end
def test_to_a
asts = @parser.parse("a:nth-child(2)")
assert_equal(
[:CONDITIONAL_SELECTOR,
[:ELEMENT_NAME, ["a"]],
[:PSEUDO_CLASS,
[:FUNCTION, ["nth-child("], ["2"]]
]
], asts.first.to_a
)
end
def test_has
assert_xpath "//a[b]", @parser.parse("a:has(b)")
assert_xpath "//a[b/c]", @parser.parse("a:has(b > c)")
end
def test_dashmatch
assert_xpath "//a[@class = 'bar' or starts-with(@class, concat('bar', '-'))]",
@parser.parse("a[@class|='bar']")
assert_xpath "//a[@class = 'bar' or starts-with(@class, concat('bar', '-'))]",
@parser.parse("a[@class |= 'bar']")
end
def test_includes
assert_xpath "//a[contains(concat(\" \", @class, \" \"),concat(\" \", 'bar', \" \"))]",
@parser.parse("a[@class~='bar']")
assert_xpath "//a[contains(concat(\" \", @class, \" \"),concat(\" \", 'bar', \" \"))]",
@parser.parse("a[@class ~= 'bar']")
end
def test_function_with_arguments
assert_xpath "//a[count(preceding-sibling::*) = 1]",
@parser.parse("a[2]")
assert_xpath "//a[count(preceding-sibling::*) = 1]",
@parser.parse("a:nth-child(2)")
end
def test_carrot
assert_xpath "//a[starts-with(@id, 'Boing')]",
@parser.parse("a[id^='Boing']")
assert_xpath "//a[starts-with(@id, 'Boing')]",
@parser.parse("a[id ^= 'Boing']")
end
def test_suffix_match
assert_xpath "//a[substring(@id, string-length(@id) - string-length('Boing') + 1, string-length('Boing')) = 'Boing']",
@parser.parse("a[id$='Boing']")
assert_xpath "//a[substring(@id, string-length(@id) - string-length('Boing') + 1, string-length('Boing')) = 'Boing']",
@parser.parse("a[id $= 'Boing']")
end
def test_attributes_with_at
## This is non standard CSS
assert_xpath "//a[@id = 'Boing']",
@parser.parse("a[@id='Boing']")
assert_xpath "//a[@id = 'Boing']",
@parser.parse("a[@id = 'Boing']")
end
def test_attributes_with_at_and_stuff
## This is non standard CSS
assert_xpath "//a[@id = 'Boing']//div",
@parser.parse("a[@id='Boing'] div")
end
def test_not_equal
## This is non standard CSS
assert_xpath "//a[child::text() != 'Boing']",
@parser.parse("a[text()!='Boing']")
assert_xpath "//a[child::text() != 'Boing']",
@parser.parse("a[text() != 'Boing']")
end
def test_function
## This is non standard CSS
assert_xpath "//a[child::text()]",
@parser.parse("a[text()]")
## This is non standard CSS
assert_xpath "//child::text()",
@parser.parse("text()")
## This is non standard CSS
assert_xpath "//a[contains(child::text(), 'Boing')]",
@parser.parse("a[text()*='Boing']")
assert_xpath "//a[contains(child::text(), 'Boing')]",
@parser.parse("a[text() *= 'Boing']")
## This is non standard CSS
assert_xpath "//script//comment()",
@parser.parse("script comment()")
end
def test_nonstandard_nth_selectors
## These are non standard CSS
assert_xpath '//a[position() = 1]', @parser.parse('a:first()')
assert_xpath '//a[position() = 1]', @parser.parse('a:first') # no parens
assert_xpath '//a[position() = 99]', @parser.parse('a:eq(99)')
assert_xpath '//a[position() = 99]', @parser.parse('a:nth(99)')
assert_xpath '//a[position() = last()]', @parser.parse('a:last()')
assert_xpath '//a[position() = last()]', @parser.parse('a:last') # no parens
assert_xpath '//a[node()]', @parser.parse('a:parent')
end
def test_standard_nth_selectors
assert_xpath '//a[position() = 1]', @parser.parse('a:first-of-type()')
assert_xpath '//a[position() = 1]', @parser.parse('a:first-of-type') # no parens
assert_xpath "//a[contains(concat(' ', normalize-space(@class), ' '), ' b ')][position() = 1]",
@parser.parse('a.b:first-of-type') # no parens
assert_xpath '//a[position() = 99]', @parser.parse('a:nth-of-type(99)')
assert_xpath "//a[contains(concat(' ', normalize-space(@class), ' '), ' b ')][position() = 99]",
@parser.parse('a.b:nth-of-type(99)')
assert_xpath '//a[position() = last()]', @parser.parse('a:last-of-type()')
assert_xpath '//a[position() = last()]', @parser.parse('a:last-of-type') # no parens
assert_xpath "//a[contains(concat(' ', normalize-space(@class), ' '), ' b ')][position() = last()]",
@parser.parse('a.b:last-of-type') # no parens
assert_xpath '//a[position() = last()]', @parser.parse('a:nth-last-of-type(1)')
assert_xpath '//a[position() = last() - 98]', @parser.parse('a:nth-last-of-type(99)')
assert_xpath "//a[contains(concat(' ', normalize-space(@class), ' '), ' b ')][position() = last() - 98]",
@parser.parse('a.b:nth-last-of-type(99)')
end
def test_nth_child_selectors
assert_xpath '//a[count(preceding-sibling::*) = 0]', @parser.parse('a:first-child')
assert_xpath '//a[count(preceding-sibling::*) = 98]', @parser.parse('a:nth-child(99)')
assert_xpath '//a[count(following-sibling::*) = 0]', @parser.parse('a:last-child')
assert_xpath '//a[count(following-sibling::*) = 0]', @parser.parse('a:nth-last-child(1)')
assert_xpath '//a[count(following-sibling::*) = 98]', @parser.parse('a:nth-last-child(99)')
end
def test_miscellaneous_selectors
assert_xpath '//a[count(preceding-sibling::*) = 0 and count(following-sibling::*) = 0]', @parser.parse('a:only-child')
assert_xpath '//a[last() = 1]', @parser.parse('a:only-of-type')
assert_xpath '//a[not(node())]', @parser.parse('a:empty')
end
def test_nth_a_n_plus_b
assert_xpath '//a[(position() mod 2) = 0]', @parser.parse('a:nth-of-type(2n)')
assert_xpath '//a[(position() >= 1) and (((position()-1) mod 2) = 0)]', @parser.parse('a:nth-of-type(2n+1)')
assert_xpath '//a[(position() mod 2) = 0]', @parser.parse('a:nth-of-type(even)')
assert_xpath '//a[(position() >= 1) and (((position()-1) mod 2) = 0)]', @parser.parse('a:nth-of-type(odd)')
assert_xpath '//a[(position() >= 3) and (((position()-3) mod 4) = 0)]', @parser.parse('a:nth-of-type(4n+3)')
assert_xpath '//a[position() <= 3]', @parser.parse('a:nth-of-type(-1n+3)')
assert_xpath '//a[position() <= 3]', @parser.parse('a:nth-of-type(-n+3)')
assert_xpath '//a[position() >= 3]', @parser.parse('a:nth-of-type(1n+3)')
assert_xpath '//a[position() >= 3]', @parser.parse('a:nth-of-type(n+3)')
assert_xpath '//a[((last()-position()+1) mod 2) = 0]', @parser.parse('a:nth-last-of-type(2n)')
assert_xpath '//a[((last()-position()+1) >= 1) and ((((last()-position()+1)-1) mod 2) = 0)]', @parser.parse('a:nth-last-of-type(2n+1)')
assert_xpath '//a[((last()-position()+1) mod 2) = 0]', @parser.parse('a:nth-last-of-type(even)')
assert_xpath '//a[((last()-position()+1) >= 1) and ((((last()-position()+1)-1) mod 2) = 0)]', @parser.parse('a:nth-last-of-type(odd)')
assert_xpath '//a[((last()-position()+1) >= 3) and ((((last()-position()+1)-3) mod 4) = 0)]', @parser.parse('a:nth-last-of-type(4n+3)')
assert_xpath '//a[(last()-position()+1) <= 3]', @parser.parse('a:nth-last-of-type(-1n+3)')
assert_xpath '//a[(last()-position()+1) <= 3]', @parser.parse('a:nth-last-of-type(-n+3)')
assert_xpath '//a[(last()-position()+1) >= 3]', @parser.parse('a:nth-last-of-type(1n+3)')
assert_xpath '//a[(last()-position()+1) >= 3]', @parser.parse('a:nth-last-of-type(n+3)')
end
def test_preceding_selector
assert_xpath "//E/following-sibling::F",
@parser.parse("E ~ F")
assert_xpath "//E/following-sibling::F//G",
@parser.parse("E ~ F G")
end
def test_direct_preceding_selector
assert_xpath "//E/following-sibling::*[1]/self::F",
@parser.parse("E + F")
assert_xpath "//E/following-sibling::*[1]/self::F//G",
@parser.parse("E + F G")
end
def test_child_selector
assert_xpath("//a//b/i", @parser.parse('a b>i'))
assert_xpath("//a//b/i", @parser.parse('a b > i'))
assert_xpath("//a/b/i", @parser.parse('a > b > i'))
end
def test_prefixless_child_selector
assert_xpath("./a", @parser.parse('>a'))
assert_xpath("./a", @parser.parse('> a'))
assert_xpath("./a//b/i", @parser.parse('>a b>i'))
assert_xpath("./a/b/i", @parser.parse('> a > b > i'))
end
def test_prefixless_preceding_sibling_selector
assert_xpath("./following-sibling::a", @parser.parse('~a'))
assert_xpath("./following-sibling::a", @parser.parse('~ a'))
assert_xpath("./following-sibling::a//b/following-sibling::i", @parser.parse('~a b~i'))
assert_xpath("./following-sibling::a//b/following-sibling::i", @parser.parse('~ a b ~ i'))
end
def test_prefixless_direct_adjacent_selector
assert_xpath("./following-sibling::*[1]/self::a", @parser.parse('+a'))
assert_xpath("./following-sibling::*[1]/self::a", @parser.parse('+ a'))
assert_xpath("./following-sibling::*[1]/self::a/following-sibling::*[1]/self::b", @parser.parse('+a+b'))
assert_xpath("./following-sibling::*[1]/self::a/following-sibling::*[1]/self::b", @parser.parse('+ a + b'))
end
def test_attribute
assert_xpath "//h1[@a = 'Tender Lovemaking']",
@parser.parse("h1[a='Tender Lovemaking']")
assert_xpath "//h1[@a]",
@parser.parse("h1[a]")
assert_xpath %q{//h1[@a = 'gnewline\n']},
@parser.parse("h1[a='\\gnew\\\nline\\\\n']")
assert_xpath "//h1[@a = 'test']",
@parser.parse(%q{h1[a=\te\st]})
assert_xpath %q{//h1[@a = "'"]},
@parser.parse(%q{h1[a="'"]})
assert_xpath %q{//h1[@a = concat("'", "")]},
@parser.parse(%q{h1[a='\\'']})
assert_xpath %q{//h1[@a = concat("", '"', "'", "")]},
@parser.parse(%q{h1[a='"\'']})
end
def test_attribute_with_number_or_string
assert_xpath "//img[@width = '200']", @parser.parse("img[width='200']")
assert_xpath "//img[@width = '200']", @parser.parse("img[width=200]")
end
def test_id
assert_xpath "//*[@id = 'foo']", @parser.parse('#foo')
assert_xpath "//*[@id = 'escape:needed,']", @parser.parse('#escape\:needed\,')
assert_xpath "//*[@id = 'escape:needed,']", @parser.parse('#escape\3Aneeded\,')
assert_xpath "//*[@id = 'escape:needed,']", @parser.parse('#escape\3A needed\2C')
assert_xpath "//*[@id = 'escape:needed']", @parser.parse('#escape\00003Aneeded')
end
def test_pseudo_class_no_ident
assert_xpath "//*[link(.)]", @parser.parse(':link')
end
def test_pseudo_class
assert_xpath "//a[link(.)]", @parser.parse('a:link')
assert_xpath "//a[visited(.)]", @parser.parse('a:visited')
assert_xpath "//a[hover(.)]", @parser.parse('a:hover')
assert_xpath "//a[active(.)]", @parser.parse('a:active')
assert_xpath "//a[active(.) and contains(concat(' ', normalize-space(@class), ' '), ' foo ')]",
@parser.parse('a:active.foo')
end
def test_significant_space
assert_xpath "//x//*[count(preceding-sibling::*) = 0]//*[@a]//*[@b]", @parser.parse("x :first-child [a] [b]")
assert_xpath "//*[@a]//*[@b]", @parser.parse(" [a] [b]")
end
def test_star
assert_xpath "//*", @parser.parse('*')
assert_xpath "//*[contains(concat(' ', normalize-space(@class), ' '), ' pastoral ')]",
@parser.parse('*.pastoral')
end
def test_class
assert_xpath "//*[contains(concat(' ', normalize-space(@class), ' '), ' a ') and contains(concat(' ', normalize-space(@class), ' '), ' b ')]",
@parser.parse('.a.b')
assert_xpath "//*[contains(concat(' ', normalize-space(@class), ' '), ' awesome ')]",
@parser.parse('.awesome')
assert_xpath "//foo[contains(concat(' ', normalize-space(@class), ' '), ' awesome ')]",
@parser.parse('foo.awesome')
assert_xpath "//foo//*[contains(concat(' ', normalize-space(@class), ' '), ' awesome ')]",
@parser.parse('foo .awesome')
assert_xpath "//foo//*[contains(concat(' ', normalize-space(@class), ' '), ' awe.some ')]",
@parser.parse('foo .awe\.some')
end
def test_bare_not
assert_xpath "//*[not(contains(concat(' ', normalize-space(@class), ' '), ' a '))]",
@parser.parse(':not(.a)')
end
def test_not_so_simple_not
assert_xpath "//*[@id = 'p' and not(contains(concat(' ', normalize-space(@class), ' '), ' a '))]",
@parser.parse('#p:not(.a)')
assert_xpath "//p[contains(concat(' ', normalize-space(@class), ' '), ' a ') and not(contains(concat(' ', normalize-space(@class), ' '), ' b '))]",
@parser.parse('p.a:not(.b)')
assert_xpath "//p[@a = 'foo' and not(contains(concat(' ', normalize-space(@class), ' '), ' b '))]",
@parser.parse("p[a='foo']:not(.b)")
end
def test_multiple_not
assert_xpath "//p[not(contains(concat(' ', normalize-space(@class), ' '), ' a ')) and not(contains(concat(' ', normalize-space(@class), ' '), ' b ')) and not(contains(concat(' ', normalize-space(@class), ' '), ' c '))]",
@parser.parse("p:not(.a):not(.b):not(.c)")
end
def test_ident
assert_xpath '//x', @parser.parse('x')
end
def test_parse_space
assert_xpath '//x//y', @parser.parse('x y')
end
def test_parse_descendant
assert_xpath '//x/y', @parser.parse('x > y')
end
def test_parse_slash
## This is non standard CSS
assert_xpath '//x/y', @parser.parse('x/y')
end
def test_parse_doubleslash
## This is non standard CSS
assert_xpath '//x//y', @parser.parse('x//y')
end
def test_multi_path
assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y, y > z')
assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y,y > z')
###
# TODO: should we make this work?
# assert_xpath ['//x/y', '//y/z'], @parser.parse('x > y | y > z')
end
def test_attributes_with_namespace
## Default namespace is not applied to attributes.
## So this must be @class, not @xmlns:class.
assert_xpath "//xmlns:a[@class = 'bar']", @parser_with_ns.parse("a[class='bar']")
assert_xpath "//xmlns:a[@hoge:class = 'bar']", @parser_with_ns.parse("a[hoge|class='bar']")
end
def assert_xpath expecteds, asts
expecteds = [expecteds].flatten
expecteds.zip(asts).each do |expected, actual|
assert_equal expected, actual.to_xpath
end
end
end
end
end
|