@@ -284,72 +284,72 @@ def xpath(css):
284
284
return str (GenericTranslator ().css_to_xpath (css , prefix = '' ))
285
285
286
286
assert xpath ('*' ) == "*"
287
- assert xpath ('E ' ) == "e"
288
- assert xpath ('E [foo]' ) == "e[@foo]"
289
- assert xpath ('E [foo="bar"]' ) == "e[@foo = 'bar']"
290
- assert xpath ('E [foo~="bar"]' ) == (
287
+ assert xpath ('e ' ) == "e"
288
+ assert xpath ('e [foo]' ) == "e[@foo]"
289
+ assert xpath ('e [foo="bar"]' ) == "e[@foo = 'bar']"
290
+ assert xpath ('e [foo~="bar"]' ) == (
291
291
"e[@foo and contains("
292
292
"concat(' ', normalize-space(@foo), ' '), ' bar ')]" )
293
- assert xpath ('E [foo^="bar"]' ) == (
293
+ assert xpath ('e [foo^="bar"]' ) == (
294
294
"e[@foo and starts-with(@foo, 'bar')]" )
295
- assert xpath ('E [foo$="bar"]' ) == (
295
+ assert xpath ('e [foo$="bar"]' ) == (
296
296
"e[@foo and substring(@foo, string-length(@foo)-2) = 'bar']" )
297
- assert xpath ('E [foo*="bar"]' ) == (
297
+ assert xpath ('e [foo*="bar"]' ) == (
298
298
"e[@foo and contains(@foo, 'bar')]" )
299
- assert xpath ('E [hreflang|="en"]' ) == (
299
+ assert xpath ('e [hreflang|="en"]' ) == (
300
300
"e[@hreflang and ("
301
301
"@hreflang = 'en' or starts-with(@hreflang, 'en-'))]" )
302
- assert xpath ('E :nth-child(1)' ) == (
302
+ assert xpath ('e :nth-child(1)' ) == (
303
303
"*/*[name() = 'e' and (position() = 1)]" )
304
- assert xpath ('E :nth-last-child(1)' ) == (
304
+ assert xpath ('e :nth-last-child(1)' ) == (
305
305
"*/*[name() = 'e' and (position() = last() - 1)]" )
306
- assert xpath ('E :nth-last-child(2n+2)' ) == (
306
+ assert xpath ('e :nth-last-child(2n+2)' ) == (
307
307
"*/*[name() = 'e' and ("
308
308
"(position() +2) mod -2 = 0 and position() < (last() -2))]" )
309
- assert xpath ('E :nth-of-type(1)' ) == (
309
+ assert xpath ('e :nth-of-type(1)' ) == (
310
310
"*/e[position() = 1]" )
311
- assert xpath ('E :nth-last-of-type(1)' ) == (
311
+ assert xpath ('e :nth-last-of-type(1)' ) == (
312
312
"*/e[position() = last() - 1]" )
313
- assert xpath ('E :nth-last-of-type(1)' ) == (
313
+ assert xpath ('e :nth-last-of-type(1)' ) == (
314
314
"*/e[position() = last() - 1]" )
315
- assert xpath ('div E :nth-last-of-type(1) .aclass' ) == (
315
+ assert xpath ('div e :nth-last-of-type(1) .aclass' ) == (
316
316
"div/descendant-or-self::*/e[position() = last() - 1]"
317
317
"/descendant-or-self::*/*[@class and contains("
318
318
"concat(' ', normalize-space(@class), ' '), ' aclass ')]" )
319
- assert xpath ('E :first-child' ) == (
319
+ assert xpath ('e :first-child' ) == (
320
320
"*/*[name() = 'e' and (position() = 1)]" )
321
- assert xpath ('E :last-child' ) == (
321
+ assert xpath ('e :last-child' ) == (
322
322
"*/*[name() = 'e' and (position() = last())]" )
323
- assert xpath ('E :first-of-type' ) == (
323
+ assert xpath ('e :first-of-type' ) == (
324
324
"*/e[position() = 1]" )
325
- assert xpath ('E :last-of-type' ) == (
325
+ assert xpath ('e :last-of-type' ) == (
326
326
"*/e[position() = last()]" )
327
- assert xpath ('E :only-child' ) == (
327
+ assert xpath ('e :only-child' ) == (
328
328
"*/*[name() = 'e' and (last() = 1)]" )
329
- assert xpath ('E :only-of-type' ) == (
329
+ assert xpath ('e :only-of-type' ) == (
330
330
"e[last() = 1]" )
331
- assert xpath ('E :empty' ) == (
331
+ assert xpath ('e :empty' ) == (
332
332
"e[not(*) and not(normalize-space())]" )
333
- assert xpath ('E :root' ) == (
333
+ assert xpath ('e :root' ) == (
334
334
"e[not(parent::*)]" )
335
- assert xpath ('E :contains("foo")' ) == (
335
+ assert xpath ('e :contains("foo")' ) == (
336
336
"e[contains(string(.), 'foo')]" )
337
- assert xpath ('E :contains(foo)' ) == (
337
+ assert xpath ('e :contains(foo)' ) == (
338
338
"e[contains(string(.), 'foo')]" )
339
- assert xpath ('E .warning' ) == (
339
+ assert xpath ('e .warning' ) == (
340
340
"e[@class and contains("
341
341
"concat(' ', normalize-space(@class), ' '), ' warning ')]" )
342
- assert xpath ('E #myid' ) == (
342
+ assert xpath ('e #myid' ) == (
343
343
"e[@id = 'myid']" )
344
- assert xpath ('E :not(:nth-child(odd))' ) == (
344
+ assert xpath ('e :not(:nth-child(odd))' ) == (
345
345
"e[not((position() -1) mod 2 = 0 and position() >= 1)]" )
346
- assert xpath ('E F ' ) == (
346
+ assert xpath ('e f ' ) == (
347
347
"e/descendant-or-self::*/f" )
348
- assert xpath ('E > F ' ) == (
348
+ assert xpath ('e > f ' ) == (
349
349
"e/f" )
350
- assert xpath ('E + F ' ) == (
350
+ assert xpath ('e + f ' ) == (
351
351
"e/following-sibling::*[name() = 'f' and (position() = 1)]" )
352
- assert xpath ('E ~ F ' ) == (
352
+ assert xpath ('e ~ f ' ) == (
353
353
"e/following-sibling::f" )
354
354
assert xpath ('div#container p' ) == (
355
355
"div[@id = 'container']/descendant-or-self::*/p" )
@@ -426,12 +426,17 @@ def pcss(main, *selectors, **kwargs):
426
426
return result
427
427
428
428
all_ids = pcss ('*' )
429
+ assert len (all_ids ) == 27
429
430
assert all_ids [:4 ] == ['html' , 'nil' , 'nil' , 'outer-div' ]
430
431
assert all_ids [- 1 :] == ['foobar-span' ]
431
432
assert pcss ('div' ) == ['outer-div' , 'li-div' , 'foobar-div' ]
433
+ assert pcss ('DIV' , html_only = True ) == [
434
+ 'outer-div' , 'li-div' , 'foobar-div' ] # case-insensitive in HTML
432
435
assert pcss ('div div' ) == ['li-div' ]
433
436
assert pcss ('div, div div' ) == ['outer-div' , 'li-div' , 'foobar-div' ]
434
437
assert pcss ('a[name]' ) == ['name-anchor' ]
438
+ assert pcss ('a[NAme]' , html_only = True ) == [
439
+ 'name-anchor' ] # case-insensitive in HTML:
435
440
assert pcss ('a[rel]' ) == ['tag-anchor' , 'nofollow-anchor' ]
436
441
assert pcss ('a[rel="tag"]' ) == ['tag-anchor' ]
437
442
assert pcss ('a[href*="localhost"]' ) == ['tag-anchor' ]
@@ -441,7 +446,7 @@ def pcss(main, *selectors, **kwargs):
441
446
assert pcss ('div[foobar~="bc"]' , 'div[foobar~="cde"]' ) == [
442
447
'foobar-div' ]
443
448
assert pcss ('div[foobar~="cd"]' ) == []
444
- assert pcss ('*[lang|="en"]' , '* [lang|="en-US"]' ) == ['second-li' ]
449
+ assert pcss ('*[lang|="en"]' , '[lang|="en-US"]' ) == ['second-li' ]
445
450
assert pcss ('*[lang|="e"]' ) == []
446
451
assert pcss ('li:nth-child(3)' ) == ['third-li' ]
447
452
assert pcss ('li:nth-child(10)' ) == []
@@ -471,12 +476,12 @@ def pcss(main, *selectors, **kwargs):
471
476
self .assertRaises (ExpressionError , pcss , 'p *:only-of-type' )
472
477
self .assertRaises (ExpressionError , pcss , 'p:lang(fr)' )
473
478
assert pcss ('p:only-of-type' ) == ['paragraph' ]
474
- assert pcss ('a:empty' ) == ['name-anchor' ]
479
+ assert pcss ('a:empty' , 'a:EMpty' ) == ['name-anchor' ]
475
480
assert pcss ('li:empty' ) == [
476
481
'third-li' , 'fourth-li' , 'fifth-li' , 'sixth-li' , 'seventh-li' ]
477
482
assert pcss (':root' , 'html:root' ) == ['html' ]
478
483
assert pcss ('li:root' , '* :root' ) == []
479
- assert pcss ('*:contains("link")' ) == [
484
+ assert pcss ('*:contains("link")' , ':CONtains("link")' ) == [
480
485
'html' , 'nil' , 'outer-div' , 'tag-anchor' , 'nofollow-anchor' ]
481
486
assert pcss ('*:contains("LInk")' ) == [] # case sensitive
482
487
assert pcss ('*:contains("e")' ) == [
@@ -488,7 +493,6 @@ def pcss(main, *selectors, **kwargs):
488
493
assert pcss ('ol *.c' , 'ol li.c' , 'li ~ li.c' , 'ol > li.c' ) == [
489
494
'third-li' , 'fourth-li' ]
490
495
assert pcss ('#first-li' , 'li#first-li' , '*#first-li' ) == ['first-li' ]
491
- # Need some tests of :not()']
492
496
assert pcss ('li div' , 'li > div' , 'div div' ) == ['li-div' ]
493
497
assert pcss ('div > div' ) == []
494
498
assert pcss ('div>.c' , 'div > .c' ) == ['first-ol' ]
@@ -507,6 +511,10 @@ def pcss(main, *selectors, **kwargs):
507
511
'fieldset' , 'checkbox-disabled' ]
508
512
assert pcss (':enabled' , html_only = True ) == [
509
513
'checkbox-unchecked' , 'checkbox-checked' ]
514
+ assert pcss ('a:not([href])' ) == ['name-anchor' ]
515
+ assert pcss ('ol :Not(li[class])' ) == [
516
+ 'first-li' , 'second-li' , 'li-div' ,
517
+ 'fifth-li' , 'sixth-li' , 'seventh-li' ]
510
518
511
519
def test_select_shakespeare (self ):
512
520
document = html .document_fromstring (HTML_SHAKESPEARE )
0 commit comments