forked from rennat/pynliner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
executable file
·428 lines (354 loc) · 20.4 KB
/
tests.py
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import pynliner
import StringIO
import logging
import cssutils
import mock
from pynliner import Pynliner
class Basic(unittest.TestCase):
def setUp(self):
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
self.p = Pynliner().from_string(self.html)
def test_fromString(self):
"""Test 'fromString' constructor"""
self.assertEqual(self.p.source_string, self.html)
def test_get_soup(self):
"""Test '_get_soup' method"""
self.p._get_soup()
self.assertEqual(unicode(self.p.soup), self.html)
def test_get_styles(self):
"""Test '_get_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.assertEqual(self.p.style_string, u'h1 { color:#ffcc00; }\n')
self.assertEqual(unicode(self.p.soup), u'<h1>Hello World!</h1>')
def test_apply_styles(self):
"""Test '_apply_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.p._apply_styles()
attr_dict = dict(self.p.soup.contents[0].attrs)
self.assertIn('style', attr_dict)
self.assertEqual(attr_dict['style'], u'color: #fc0')
def test_run(self):
"""Test 'run' method"""
output = self.p.run()
self.assertEqual(output, u'<h1 style="color: #fc0">Hello World!</h1>')
def test_with_cssString(self):
"""Test 'with_cssString' method"""
cssString = 'h1 {color: #f00;}'
self.p.with_cssString(cssString)
output = self.p.run()
self.assertEqual(output, u'<h1 style="color: #f00">Hello World!</h1>')
def test_fromString(self):
"""Test 'fromString' complete"""
output = pynliner.fromString(self.html)
desired = u'<h1 style="color: #fc0">Hello World!</h1>'
self.assertEqual(output, desired)
def test_fromURL(self):
"""Test 'fromURL' constructor"""
url = 'http://media.tannern.com/pynliner/test.html'
p = Pynliner()
with mock.patch.object(Pynliner, '_get_url') as mocked:
mocked.return_value = u"""<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
<link rel="stylesheet" type="text/css" href="test.css"/>
<style type="text/css">h1 {color: #fc0;}</style>
</head>
<body>
<h1>Hello World!</h1>
<p>:)</p>
</body>
</html>"""
p.from_url(url)
self.assertEqual(p.root_url, 'http://media.tannern.com')
self.assertEqual(p.relative_url, 'http://media.tannern.com/pynliner/')
p._get_soup()
with mock.patch.object(Pynliner, '_get_url') as mocked:
mocked.return_value = 'p {color: #999}'
p._get_external_styles()
self.assertEqual(p.style_string, "p {color: #999}")
p._get_internal_styles()
self.assertEqual(p.style_string, "p {color: #999}\nh1 {color: #fc0;}\n")
p._get_styles()
output = p.run()
desired = u"""<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
<h1 style="color: #fc0">Hello World!</h1>
<p style="color: #999">:)</p>
</body>
</html>"""
self.assertEqual(output, desired)
def test_overloaded_styles(self):
html = '<style>h1 { color: red; } #test { color: blue; }</style>' \
'<h1 id="test">Hello world!</h1>'
expected = '<h1 id="test" style="color: blue">Hello world!</h1>'
output = Pynliner().from_string(html).run()
self.assertEqual(expected, output)
def test_unicode_content(self):
html = u"""<h1>Hello World!</h1><p>\u2022 point</p>"""
css = """h1 { color: red; }"""
expected = u"""<h1 style="color: red">Hello World!</h1><p>\u2022 point</p>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_conditional_comments(self):
html = "<!-- <normal> --><!--[if condition]><p>special</p><![endif]-->"
expected = "<!-- <normal> --><!--[if condition]><p>special</p><![endif]-->"
output = Pynliner(allow_conditional_comments=True).from_string(html).run()
self.assertEqual(output, expected)
class CommaSelector(unittest.TestCase):
def setUp(self):
self.html = """<style>.b1,.b2 { font-weight:bold; } .c {color: red}</style><span class="b1">Bold</span><span class="b2 c">Bold Red</span>"""
self.p = Pynliner().from_string(self.html)
def test_fromString(self):
"""Test 'fromString' constructor"""
self.assertEqual(self.p.source_string, self.html)
def test_get_soup(self):
"""Test '_get_soup' method"""
self.p._get_soup()
self.assertEqual(unicode(self.p.soup), self.html)
def test_get_styles(self):
"""Test '_get_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.assertEqual(self.p.style_string, u'.b1,.b2 { font-weight:bold; } .c {color: red}\n')
self.assertEqual(unicode(self.p.soup), u'<span class="b1">Bold</span><span class="b2 c">Bold Red</span>')
def test_apply_styles(self):
"""Test '_apply_styles' method"""
self.p._get_soup()
self.p._get_styles()
self.p._apply_styles()
self.assertEqual(unicode(self.p.soup), u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>')
def test_run(self):
"""Test 'run' method"""
output = self.p.run()
self.assertEqual(output, u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>')
def test_with_cssString(self):
"""Test 'with_cssString' method"""
cssString = '.b1,.b2 {font-size: 2em;}'
self.p = Pynliner().from_string(self.html).with_cssString(cssString)
output = self.p.run()
self.assertEqual(output, u'<span class="b1" style="font-weight: bold; font-size: 2em">Bold</span><span class="b2 c" style="color: red; font-weight: bold; font-size: 2em">Bold Red</span>')
def test_fromString(self):
"""Test 'fromString' complete"""
output = pynliner.fromString(self.html)
desired = u'<span class="b1" style="font-weight: bold">Bold</span><span class="b2 c" style="color: red; font-weight: bold">Bold Red</span>'
self.assertEqual(output, desired)
def test_comma_whitespace(self):
"""Test excess whitespace in CSS"""
html = '<style>h1, h2 ,h3,\nh4{ color: #000} </style><h1>1</h1><h2>2</h2><h3>3</h3><h4>4</h4>'
desired_output = '<h1 style="color: #000">1</h1><h2 style="color: #000">2</h2><h3 style="color: #000">3</h3><h4 style="color: #000">4</h4>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
class Extended(unittest.TestCase):
def test_overwrite(self):
"""Test overwrite inline styles"""
html = '<style>h1 {color: #000;}</style><h1 style="color: #fff">Foo</h1>'
desired_output = '<h1 style="color: #000; color: #fff">Foo</h1>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
def test_overwrite_comma(self):
"""Test overwrite inline styles"""
html = '<style>h1,h2,h3 {color: #000;}</style><h1 style="color: #fff">Foo</h1><h3 style="color: #fff">Foo</h3>'
desired_output = '<h1 style="color: #000; color: #fff">Foo</h1><h3 style="color: #000; color: #fff">Foo</h3>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
class LogOptions(unittest.TestCase):
def setUp(self):
self.html = "<style>h1 { color:#ffcc00; }</style><h1>Hello World!</h1>"
def test_no_log(self):
self.p = Pynliner()
self.assertEqual(self.p.log, None)
self.assertEqual(cssutils.log.enabled, False)
def test_custom_log(self):
self.log = logging.getLogger('testlog')
self.log.setLevel(logging.DEBUG)
self.logstream = StringIO.StringIO()
handler = logging.StreamHandler(self.logstream)
log_format = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)
self.log.addHandler(handler)
self.p = Pynliner(self.log).from_string(self.html)
self.p.run()
log_contents = self.logstream.getvalue()
self.assertIn("DEBUG", log_contents)
class BeautifulSoupBugs(unittest.TestCase):
def test_double_doctype(self):
self.html = """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">"""
output = pynliner.fromString(self.html)
self.assertNotIn("<!<!", output)
def test_double_comment(self):
self.html = """<!-- comment -->"""
output = pynliner.fromString(self.html)
self.assertNotIn("<!--<!--", output)
class ComplexSelectors(unittest.TestCase):
def test_multiple_class_selector(self):
html = """<h1 class="a b">Hello World!</h1>"""
css = """h1.a.b { color: red; }"""
expected = u'<h1 class="a b" style="color: red">Hello World!</h1>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_combination_selector(self):
html = """<h1 id="a" class="b">Hello World!</h1>"""
css = """h1#a.b { color: red; }"""
expected = u'<h1 id="a" class="b" style="color: red">Hello World!</h1>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_descendant_selector(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """h1 span { color: red; }"""
expected = u'<h1><span style="color: red">Hello World!</span></h1>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_selector(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """h1 > span { color: red; }"""
expected = u'<h1><span style="color: red">Hello World!</span></h1>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_nested_child_selector(self):
html = """<div><h1><span>Hello World!</span></h1></div>"""
css = """div > h1 > span { color: red; }"""
expected = u"""<div><h1><span style="color: red">Hello World!</span></h1></div>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > span { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_all_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > * { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span><p style="color: red">foo</p><div class="barclass" style="color: red"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_adjacent_selector(self):
html = """<h1>Hello World!</h1><h2>How are you?</h2>"""
css = """h1 + h2 { color: red; }"""
expected = (u'<h1>Hello World!</h1>'
u'<h2 style="color: red">How are you?</h2>')
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_unknown_pseudo_selector(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > span:css4-selector { color: red; }"""
expected = u"""<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_follow_by_adjacent_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > span + p { color: red; }"""
expected = u"""<h1><span>Hello World!</span><p style="color: red">foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_follow_by_first_child_selector_with_white_spaces(self):
html = """<h1> <span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > :first-child { color: red; }"""
expected = u"""<h1> <span style="color: red">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_follow_by_first_child_selector_with_comments(self):
html = """<h1> <!-- enough said --><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > :first-child { color: red; }"""
expected = u"""<h1> <!-- enough said --><span style="color: red">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_follow_by_first_child_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > :first-child { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_last_child_selector(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """h1 > :last-child { color: red; }"""
expected = u"""<h1><span style="color: red">Hello World!</span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_follow_by_last_child_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > :last-child { color: red; }"""
expected = u"""<h1><span>Hello World!</span><p>foo</p><div class="barclass" style="color: red"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_with_first_child_override_selector_complex_dom(self):
html = """<div><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></div>"""
css = """div > * { color: green; } div > :first-child { color: red; }"""
expected = u"""<div><span style="color: red">Hello World!</span><p style="color: green">foo</p><div class="barclass" style="color: green"><span style="color: red">baz</span>bar</div></div>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_id_el_child_with_first_child_override_selector_complex_dom(self):
html = """<div id="abc"><span class="cde">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></div>"""
css = """#abc > * { color: green; } #abc > :first-child { color: red; }"""
expected = u"""<div id="abc"><span class="cde" style="color: red">Hello World!</span><p style="color: green">foo</p><div class="barclass" style="color: green"><span>baz</span>bar</div></div>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_with_first_and_last_child_override_selector(self):
html = """<p><span>Hello World!</span></p>"""
css = """p > * { color: green; } p > :first-child:last-child { color: red; }"""
expected = u"""<p><span style="color: red">Hello World!</span></p>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_nested_child_with_first_child_override_selector_complex_dom(self):
self.maxDiff = None
html = """<div><div><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></div></div>"""
css = """div > div > * { color: green; } div > div > :first-child { color: red; }"""
expected = u"""<div><div><span style="color: red">Hello World!</span><p style="color: green">foo</p><div class="barclass" style="color: green"><span style="color: red">baz</span>bar</div></div></div>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_with_first_child_and_class_selector_complex_dom(self):
html = """<h1><span class="hello">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > .hello:first-child { color: green; }"""
expected = u"""<h1><span class="hello" style="color: green">Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_child_with_first_child_and_unmatched_class_selector_complex_dom(self):
html = """<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 > .hello:first-child { color: green; }"""
expected = u"""<h1><span>Hello World!</span><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_first_child_descendant_selector(self):
html = """<h1><div><span>Hello World!</span></div></h1>"""
css = """h1 :first-child { color: red; }"""
expected = u"""<h1><div style="color: red"><span style="color: red">Hello World!</span></div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_last_child_descendant_selector(self):
html = """<h1><div><span>Hello World!</span></div></h1>"""
css = """h1 :last-child { color: red; }"""
expected = u"""<h1><div style="color: red"><span style="color: red">Hello World!</span></div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_first_child_descendant_selector_complex_dom(self):
html = """<h1><div><span>Hello World!</span></div><p>foo</p><div class="barclass"><span>baz</span>bar</div></h1>"""
css = """h1 :first-child { color: red; }"""
expected = u"""<h1><div style="color: red"><span style="color: red">Hello World!</span></div><p>foo</p><div class="barclass"><span style="color: red">baz</span>bar</div></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_attribute_selector_match(self):
html = """<h1 title="foo">Hello World!</h1>"""
css = """h1[title="foo"] { color: red; }"""
expected = u'<h1 title="foo" style="color: red">Hello World!</h1>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_attribute_selector_no_match(self):
html = """<h1 title="bar">Hello World!</h1>"""
css = """h1[title="foo"] { color: red; }"""
expected = u"""<h1 title="bar">Hello World!</h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
if __name__ == '__main__':
unittest.main()