/
test_css_select.txt
162 lines (159 loc) · 5.2 KB
/
test_css_select.txt
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
This is a test of CSS selectors. We setup a document we'll use for
all our selections, and a function make querying simpler:
>>> from lxml.cssselect import CSSSelector
>>> from lxml.etree import HTML
>>> doc = HTML('''
... <html id="html"><head></head><body>
... <div id="outer-div">
... <a id="name-anchor" name="foo"></a>
... <a id="tag-anchor" rel="tag" href="http://localhost/foo">link</a>
... <a id="nofollow-anchor" rel="nofollow" href="https://example.org">link</a>
... <ol id="first-ol" class="a b c">
... <li id="first-li">content</li>
... <li id="second-li" lang="en-US">
... <div id="li-div">
... </div>
... </li>
... <li id="third-li" class="ab c"></li>
... <li id="fourth-li" class="ab
... c"></li>
... <li id="fifth-li"></li>
... <li id="sixth-li"></li>
... <li id="seventh-li"> </li>
... </ol>
... <p id="paragraph">
... <b id="p-b">hi</b> <em id="p-em">there</em>
... <b id="p-b2">guy</b></p>
... <ol id="second-ol">
... </ol>
... </div>
... <div id="foobar-div" foobar="ab bc
... cde"><span id="foobar-span"></span></div>
... </body></html>''')
>>> order = {}
>>> for count, el in enumerate(doc.getiterator()):
... order[el] = count
>>> def select_ids(selector):
... items = CSSSelector(selector)(doc)
... if not items:
... return 'empty'
... items = CSSSelector(selector)(doc)
... items.sort(key=lambda el: order[el])
... return ', '.join([el.get('id', 'nil') for el in items])
>>> def pcss(main, *selectors):
... result = select_ids(main)
... for selector in selectors:
... sel_result = select_ids(selector)
... if sel_result != result:
... print('Selector %r returns %s' % (selector, sel_result))
... print(result)
Now, the tests:
>>> pcss('*') # doctest: +ELLIPSIS
html, nil, nil, outer-div, ... foobar-span
>>> pcss('div')
outer-div, li-div, foobar-div
>>> pcss('a[name]')
name-anchor
>>> pcss('a[rel]')
tag-anchor, nofollow-anchor
>>> pcss('a[rel="tag"]')
tag-anchor
>>> pcss('a[href*="localhost"]')
tag-anchor
>>> pcss('a[href^="http"]')
tag-anchor, nofollow-anchor
>>> pcss('a[href^="http:"]')
tag-anchor
>>> pcss('a[href$="org"]')
nofollow-anchor
>>> pcss('div[foobar~="bc"]', 'div[foobar~="cde"]')
foobar-div
>>> pcss('div[foobar~="cd"]')
empty
>>> pcss('*[lang|="en"]', '*[lang|="en-US"]')
second-li
>>> pcss('*[lang|="e"]')
empty
>>> pcss('li:nth-child(3)')
third-li
>>> pcss('li:nth-child(10)')
empty
>>> pcss('li:nth-child(2n)', 'li:nth-child(even)', 'li:nth-child(2n+0)')
second-li, fourth-li, sixth-li
>>> pcss('li:nth-child(+2n+1)', 'li:nth-child(odd)')
first-li, third-li, fifth-li, seventh-li
>>> pcss('li:nth-child(2n+4)')
fourth-li, sixth-li
>>> # FIXME: I'm not 100% sure this is right:
>>> pcss('li:nth-child(3n+1)')
first-li, fourth-li, seventh-li
>>> # FIXME: I'm not sure if nth-last-child(1) or nth-last-child(1)
>>> # should be equivalent to nth-last-child()
>>> pcss('li:nth-last-child()', 'li:nth-last-child(0)')
seventh-li
>>> pcss('li:nth-last-child(2n)', 'li:nth-last-child(even)')
second-li, fourth-li, sixth-li
>>> pcss('li:nth-last-child(2n+2)')
second-li, fourth-li
>>> pcss('ol:first-of-type')
first-ol
>>> pcss('ol:nth-child(1)')
empty
>>> pcss('ol:nth-of-type(2)')
second-ol
>>> # FIXME: like above, (1) or (2)?
>>> pcss('ol:nth-last-of-type(1)')
first-ol
>>> pcss('span:only-child')
foobar-span
>>> pcss('li div:only-child')
li-div
>>> pcss('div *:only-child')
li-div, foobar-span
>>> pcss('p *:only-of-type')
Traceback (most recent call last):
...
NotImplementedError: *:only-of-type is not implemented
>>> pcss('p:only-of-type')
paragraph
>>> pcss('a:empty')
name-anchor
>>> pcss('li:empty')
third-li, fourth-li, fifth-li, sixth-li, seventh-li
>>> pcss(':root', 'html:root')
html
>>> pcss('li:root', '* :root')
empty
>>> pcss('*:contains("link")')
html, nil, outer-div, tag-anchor, nofollow-anchor
>>> pcss('*:contains("e")')
html, nil, outer-div, first-ol, first-li, paragraph, p-em
>>> pcss('.a', '.b', '*.a', 'ol.a')
first-ol
>>> pcss('.c', '*.c')
first-ol, third-li, fourth-li
>>> pcss('ol *.c', 'ol li.c', 'li ~ li.c', 'ol > li.c')
third-li, fourth-li
>>> pcss('#first-li', 'li#first-li', '*#first-li')
first-li
>>> # Need some tests of :not()
>>> pcss('li div', 'li > div', 'div div')
li-div
>>> pcss('div > div')
empty
>>> pcss('div>.c', 'div > .c')
first-ol
>>> pcss('div + div')
foobar-div
>>> pcss('a ~ a')
tag-anchor, nofollow-anchor
>>> pcss('a[rel="tag"] ~ a')
nofollow-anchor
>>> pcss('ol#first-ol li:last-child')
seventh-li
>>> pcss('ol#first-ol *:last-child')
li-div, seventh-li
>>> pcss('#outer-div:first-child')
outer-div
>>> pcss('#outer-div :first-child')
name-anchor, first-li, li-div, p-b