forked from urnest/urnest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpq.py.test
396 lines (348 loc) · 10.9 KB
/
pq.py.test
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
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
# coding: utf-8
#
# Copyright (c) 2018 Trevor Taylor
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that all
# copyright notices and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# jquery-like python library
#
# parse a HTML text into a tree, with search, manipulation
# and output as HTML text
#
# see test1() near the bottom for usage examples
#
# REVISIT: need better diags, eg:
# track selectors used to find/filter nodes and report them
# on failure eg replace/html
#
from sys import path
from os.path import dirname
if path[0]==dirname(__file__): path.pop(0)
from xju.pq import parse,hasClass,encodeEntities,tagName,Selection,loadFile,parseFile
from xju.pq import attrEquals,hasAttr
from xju.assert_ import Assert
from xju.xn import readable_repr
from xju.patch import PatchAttr
from html.parser import HTMLParser
html1='''<html>
<body>
<p>The best thing about html is simplicity
<ul class="list">
<li class="i">item 1
<li class="i">item 2</li>
</ul>
</body>
</html>'''
#note the spacing is carefully chosen to get the format of html2 (below)
newitems='''
<li>item 4
<li class="item5">item 5
<li>item 6
'''
html2='''<html>
<body>
<p>The best thing about html is simplicity
<ul class="list">
<li>item 4
<li class="item5">item 5
<li>item 6
</ul>
</body>
</html>'''
html3='''<html>
<body>
<p>The best thing about html is simplicity
<ul class="list">
<li>item 4
<li>item <b>
<li>item 6
</ul>
</body>
</html>'''
#note the spacing is carefully chosen to get the format of html2 (below)
newitem5='''<li>item <b>
'''
def test1():
s=parse(html1, 'html1')
Assert(str(s))==html1
s2=s.find(hasClass('list'))
s2.html(parse(newitems, 'newitems'))
Assert(str(s))==html2
s2.html(newitems)
Assert(str(s))==html2
s3=s.find(hasClass('item5'))
parse(newitem5, 'newitem5').replace(s3)
Assert(str(s))==html3
Assert(s.utf8())==str(s).encode('utf-8')
pass
html4='''<html>
<body>
<p>The best thing about html is simplicity
</body>
</html>'''
def test2():
s=parse(html1, 'html1')
sc=s.clone()
sc.find(hasClass('list')).detach()
Assert(str(s))==html1
Assert(str(sc))==html4
html5='''<html>
<body>
<p>The best thing about html is simplicity
<ul class="list">
<li class="i">item 1
<li class="i">item 2</li>
<li>item <b>
</ul>
</body>
</html>'''
def test3():
s=parse(html1, 'html1')
s2=parse(newitem5, 'newitem5')
Assert(str(s2.appendTo(s.find(hasClass('list')))))==newitem5
Assert(str(s))==html5
script='''
$(document).ready(function(){
});
'''
def test4():
a=parse('<head></head>')
parse(encodeEntities(script)).appendTo(a)
Assert(str(a))=='<head>'+script+'</head>'
pass
def test5():
s=parse('<p>fred</p>')
s.text('jock')
Assert(str(s))==u'<p>jock</p>'
pass
def test6():
s=parse('<p>fred</p>')
s.text(u'30x40”')
Assert(str(s))==u'<p>30x40”</p>'
pass
def test7():
s=parse('<p>fred</p>')
s.text(u'30x40”')
Assert(str(s))==u'<p>30x40”</p>'
pass
def test8():
s=parse('<html><p>fred</p><p>jock</p></html>')
s=s.children()
assert len(s)==2, str(s).encode('utf-8')
Assert(str(s.first()))==u'<p>fred</p>'
pass
def test9():
s=parse('<td><a href="fred">jock</a> and fred</td>')
Assert(s.text())==u'jock and fred'
pass
def test10():
s=parse('<td><a href="fred">jock</a> and fred</td>')
Assert(s.text())==u'jock\xa0and fred'
s1=parse('<td><a href="fred">jock</a> ')
s2=parse('and fred</td>')
Assert((s1+s2).text())==u'jock\xa0and fred'
pass
def test12():
s=parse('<ul><li class="a">1<li class="b">2</ul>')
parse('<li>1.5').addBefore(s.find(hasClass('b')))
Assert(str(s))==u'<ul><li class="a">1<li>1.5<li class="b">2</ul>'
Assert(str(s.find(hasClass('b')).nodeList[0].pos))=='unknown:1:19'
def test13():
s=parse('<ul><li class="a">1<li class="b">2</ul>')
parse('<li>0').addBefore(s.find(hasClass('a')))
Assert(str(s))==u'<ul><li>0<li class="a">1<li class="b">2</ul>'
def test14():
s=parse('<li>')
s.attr('x','"fred&jock"')
Assert(str(s))==u'<li x=""fred&jock"">'
Assert(s.attr('x'))=='"fred&jock"'
s=parse('<div><p a="fred"><bold>a</bold>x</p><p a="jock">b</p></div>').find(tagName('p'))
assert len(s)==2, s.utf8()
Assert(s.attr('a'))=='fredjock'
Assert(s.attrs('a'))==['fred','jock']
pass
def test15():
s=parse('λ')
Assert(s.text())==chr(955), s.text()
s=parse('λ')
Assert(s.text())==chr(955), s.text()
def test16():
s=parse('<div><p><bold>a</bold>x</p><p>b</p></div>').find(tagName('p'))
Assert(len(s))==2
Assert(Selection(s[0]).find(tagName('bold')).text())=='a'
Assert(s[0].find(tagName('bold')).text())=='a'
pass
def test17():
s=parse('<div>fred "jock"</div>')
Assert(Selection(s[0]).find(tagName('div')).text())=='fred "jock"'
pass
def test18():
s=parse('<body><div>a</div><div class="mid">b</div><div>c</div></body>')
Assert(s.find(hasClass('mid')).predecessors().text())=='a',s.find(hasClass('mid')).predecessors()
Assert(s.find(hasClass('mid')).successors().text())=='c',s.find(hasClass('mid')).predecessors()
pass
html19='''<html>
<body>
<script type="text/javascript">
var fred="fred"";
</script>
</body>
</html>'''
def test19():
s=parse(html19)
Assert(str(s))==html19
Assert(s.find(tagName('script')).text().strip())=='var fred="fred"";'
pass
html20='''<html>
<body>
<style type="text/css">
x > 20 {
bold;
}
</style>
</body>
</html>'''
def test20():
s=parse(html20)
Assert(str(s))==html20
Assert(s.find(tagName('style')).text().strip())=='''x > 20 {
bold;
}'''
pass
def raise_some_error(*args, **kwargs):
raise Exception('some error')
def test21():
with PatchAttr(HTMLParser,'feed',raise_some_error):
try:
s=parse('#3345')
except Exception as e:
Assert("failed to parse html at unknown").isIn(readable_repr(e))
Assert("some error").isIn(readable_repr(e))
else:
assert False
pass
pass
pass
def test22():
s=parse('fred')
Assert(s.predecessors().nodeList)==[]
Assert(s.successors().nodeList)==[]
pass
def test23():
s=parse('<html><p>fred</p></html>')
parse('<div>fred</div>').replace(s.find(tagName('p')))
Assert(str(s))=='<html><div>fred</div></html>'
pass
def test24():
s=parse('<html><p>fred</p></html>')
nodes=s.find(lambda n: True)
Assert([str(n) for n in nodes])==['<html><p>fred</p></html>', '<p>fred</p>', 'fred']
Assert([repr(n.nodeList[0]) for n in nodes])==['html at unknown:1:0', 'p at unknown:1:6', "data at unknown:1:9, 'fred'"]
pass
def test25():
s=parse('<p>fred</p>')
s.filter(tagName('p')).addClass('jock')
Assert(str(s))=='<p class="jock">fred</p>'
s.filter(tagName('p')).removeClass('jock')
Assert(str(s))=='<p>fred</p>'
s.filter(tagName('p')).attr('jock','ann')
Assert(str(s))=='<p jock="ann">fred</p>'
Assert(s.filter(attrEquals('jock','ann')).attr('jock'))=='ann'
Assert(s.filter(hasAttr('jock')).attr('jock'))=='ann'
s.filter(tagName('p')).removeAttr('jock')
Assert(str(s))=='<p>fred</p>'
pass
def test26():
x=parse('<html><p>fred</p></html>')
y=parse('<html><div>jock</div>')
y.find(tagName('div')).replace(x.find(tagName('p')))
Assert(str(x))=='<html><div>jock</div></html>'
pass
def test27():
x=parse('<html><p>fred<br><jock></p><ul><li>a<li>b</ul></html>')
Assert(x.filter(tagName('html')).text())=='fred\n<jock>\na\nb\n'
def test28():
x=parse('<html><p>&jock;</p>')
def test30():
x=parse('<html><p>jock<!-- comment --></p></html>')
Assert(x.filter(tagName('html')).text())=='jock\n'
Assert(str(x))=='<html><p>jock<!-- comment --></p></html>'
x=parse('<!-- comment -->').clone()
Assert(repr(x.clone().nodeList))=='[comment at unknown:1:0]'
pass
def test31():
x=parse('<!DOCTYPE FRED>')
Assert(str(x))=='<!DOCTYPE FRED>'
Assert(repr(x.clone().nodeList))=='[decl at unknown:1:0]'
Assert(x.text())==''
pass
def test32():
x=parse('<?fred>')
Assert(str(x))=='<?fred>'
Assert(repr(x.clone().nodeList))=='[processing instruction at unknown:1:0]'
Assert(x.text())==''
pass
def test33():
x=parse('<html><p>jock</p><p class="out">fred</p><p>ann</p></html>')
Assert(str(x.find(tagName('p')).unless(hasClass('out'))))=='<p>jock</p><p>ann</p>'
parse('<p>sal</p>').addAfter(x.find(hasClass('out')))
Assert(str(x))=='<html><p>jock</p><p class="out">fred</p><p>sal</p><p>ann</p></html>'
x.find(hasClass('out')).remove()
Assert(str(x))=='<html><p>jock</p><p>sal</p><p>ann</p></html>'
pass
def test34():
x=parse('<html><p>jock</p><p class="out">fred<br></p><p>ann</p></html>')
x.find(hasClass('out')).empty()
Assert(str(x))=='<html><p>jock</p><p class="out"></p><p>ann</p></html>'
Assert(x.find(tagName('p')).last().text())=='ann\n'
Assert(x.hasClass('out'))==False
pass
def test35():
x=parse('<html><p class="a">jock</p><p class="out">fred<br></p><p>ann</p></html>')
Assert(x.find(tagName('p')).attrs('class'))==['a','out','']
Assert(parse('<br>').join(x.find(tagName('p'))).text())=='jock\n\nfred\n\n\nann\n'
Assert(str(x.find(tagName('p'))[1:]))=='<p class="out">fred<br></p><p>ann</p>'
Assert(x.__add__(7))==NotImplemented
Assert(str(x.find(tagName('p')).attrs('class','in')))=='<p class="in">jock</p><p class="in">fred<br></p><p class="in">ann</p>'
try:
parseFile('/dev/non-existent')
except Exception as e:
Assert("No such file or directory: '/dev/non-existent'").isIn(str(e))
else:
assert False
pass
def test36():
with open('xxx.html','w') as f:
f.write('<html><p class="a">jock</p><p class="out">fred<br></p><p>ann</p></html>')
pass
Assert(str(loadFile('xxx.html').find(hasClass('out'))))=='<p class="out">fred<br></p>'
pass
def test37():
x=parse('<div><!-- fred --></div>')
y=x.clone()
Assert(str(y))=='<div><!-- fred --></div>'
pass
def test38():
x=parse('<div><?fred></div>')
y=x.clone()
Assert(str(y))=='<div><?fred></div>'
pass
if __name__=='__main__':
tests=[var for name,var in list(vars().items())
if name.startswith('test') and callable(var)]
for t in tests:
t()
pass
print('PASS - {0} steps'.format(len(tests)))
pass