-
Notifications
You must be signed in to change notification settings - Fork 93
/
tests.py
executable file
·646 lines (530 loc) · 30 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import logging
from io import StringIO
import cssutils
import mock
import pynliner
from pynliner import Pynliner
unicode = getattr(__builtins__, 'unicode', str)
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.assertTrue('style' in 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_complete(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)
class ExternalStyles(unittest.TestCase):
def setUp(self):
self.html_template = """<link rel="stylesheet" href="{href}"></link><span class="b1">Bold</span><span class="b2 c">Bold Red</span>"""
self.root_url = 'http://server.com'
self.relative_url = 'http://server.com/parent/child/'
def _test_external_url(self, url, expected_url):
with mock.patch.object(Pynliner, '_get_url') as mocked:
def check_url(url):
self.assertEqual(url, expected_url)
return ".b1,.b2 { font-weight:bold; } .c {color: red}"
mocked.side_effect = check_url
p = Pynliner()
p.root_url = self.root_url
p.relative_url = self.relative_url
p.from_string(self.html_template.format(href=url))
p._get_soup()
p._get_styles()
def test_simple_url(self):
self._test_external_url('test.css', 'http://server.com/parent/child/test.css')
def test_relative_url(self):
self._test_external_url('../test.css', 'http://server.com/parent/test.css')
def test_absolute_url(self):
self._test_external_url('/something/test.css', 'http://server.com/something/test.css')
def test_external_url(self):
self._test_external_url('http://other.com/something/test.css', 'http://other.com/something/test.css')
def test_external_ssl_url(self):
self._test_external_url('https://other.com/something/test.css', 'https://other.com/something/test.css')
def test_external_schemeless_url(self):
self._test_external_url('//other.com/something/test.css', 'http://other.com/something/test.css')
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="font-weight: bold; color: red">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="font-weight: bold; color: red">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="font-weight: bold; color: red; font-size: 2em">Bold Red</span>')
def test_fromString_complete(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="font-weight: bold; color: red">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)
def test_comma_separated_nested_styles(self):
html = """<style>.orange-wrapper p, .super-orange-wrapper p { color:orange; }</style><div class="orange-wrapper"><p>Orange</p></div><div><p>Black</p></div>"""
desired_output = """<div class="orange-wrapper"><p style="color: orange">Orange</p></div><div><p>Black</p></div>"""
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 MediaQuery(unittest.TestCase):
def setUp(self):
self.mq = '@media (min-width: 640px) { .infobox { float: right } }'
def test_leave_alone(self):
"""Test media queries are 'left alone'"""
html = '<style>' + self.mq + '</style>'\
'<h1>Foo</h1><div class="infobox">Blah</div>'
desired_output = '<style>' + self.mq + '</style>'\
'<h1>Foo</h1><div class="infobox">Blah</div>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
def test_mixed_styles(self):
"""Test media queries do not affect regular operation"""
html = '<style>' + self.mq + ' h1 {color:#ffcc00;}</style>'\
'<h1>Foo</h1><div class="infobox">Blah</div>'
desired_output = '<style>' + self.mq + '</style>'\
'<h1 style="color: #fc0">Foo</h1><div class="infobox">Blah</div>'
output = Pynliner().from_string(html).run()
self.assertEqual(output, desired_output)
def test_real_html(self):
"""Test re-inserted styles are placed in the body for HTML"""
html = '<html><head><style>' + self.mq + ' h1 {color:#ffcc00;}</style></head>'\
'<body><h1>Foo</h1><div class="infobox">Blah</div>'
desired_output = '<html><head></head><body><style>' + self.mq + '</style>'\
'<h1 style="color: #fc0">Foo</h1><div class="infobox">Blah</div>'\
'</body></html>'
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()
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.assertTrue("DEBUG" in 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.assertTrue("<!<!" not in output)
def test_double_comment(self):
self.html = """<!-- comment -->"""
output = pynliner.fromString(self.html)
self.assertTrue("<!--<!--" not in output)
class Entities(unittest.TestCase):
def test_html_entities_preserved_by_default(self):
html = u'<p> </p>'
output = pynliner.fromString(html)
expected = html
self.assertEqual(expected, output)
def test_html_entities_preserved_explicitly(self):
html = u'<p> </p>'
output = pynliner.fromString(html, preserve_entities=True)
expected = html
self.assertEqual(expected, output)
def test_html_entities_unpreserved_explicitly(self):
html = u'<p> </p>'
output = pynliner.fromString(html, preserve_entities=False)
expected = u'<p>\xa0</p>'
self.assertEqual(expected, output)
class ComplexSelectors(unittest.TestCase):
def test_comma_specificity(self):
html = '<i>howdy</i>'
css = 'i, i { color: red; } i { color: blue; }'
expected = '<i style="color: blue">howdy</i>'
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_missing_link_descendant_selector(self):
html = '<div id="a"><i>x</i></div>'
css = '#a b i { color: red }'
expected = html
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
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_conflicting_multiple_class_selector(self):
html = """<h1 class="a b">Hello World!</h1><h1 class="a">I should not be changed</h1>"""
css = """h1.a.b { color: red; }"""
expected = u'<h1 class="a b" style="color: red">Hello World!</h1><h1 class="a">I should not be changed</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 class="b" id="a" 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_multiple_pseudo_selectors(self):
html = """<h1><span>Hello World!</span></h1>"""
css = """span:first-child: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)
html = """<h1><span>Hello World!</span><span>again!</span></h1>"""
css = """span:first-child:last-child { color: red; }"""
expected = u"""<h1><span>Hello World!</span><span>again!</span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
def test_parent_pseudo_selector(self):
html = """<h1><span><span>Hello World!</span></span></h1>"""
css = """span:last-child span { color: red; }"""
expected = u"""<h1><span><span style="color: red">Hello World!</span></span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
html = """<h1><span><span>Hello World!</span></span></h1>"""
css = """span:last-child > span { color: red; }"""
expected = u"""<h1><span><span style="color: red">Hello World!</span></span></h1>"""
output = Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(output, expected)
html = """<h1><span><span>Hello World!</span></span><span>nope</span></h1>"""
css = """span:last-child > span { color: red; }"""
expected = u"""<h1><span><span>Hello World!</span></span><span>nope</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_specificity(self):
html = """<div class="foo"></div>"""
css1 = """div,a,b,c,d,e,f,g,h,i,j { color: red; }"""
css2 = """.foo { color: blue; }"""
expected = u"""<div class="foo" style="color: blue"></div>"""
output = pynliner.Pynliner().from_string(html).with_cssString(css1).with_cssString(css2).run()
self.assertEqual(output, expected)
class AttributeSelectorTestCase(unittest.TestCase):
def assert_pynlined(self, html, css, expected):
actual = pynliner.Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(actual, expected)
def test_exists(self):
html = '<span data-type="thing">1</span>'
css = '[data-type] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
def test_equals(self):
html = '<span data-type="thing">1</span>'
css = '[data-type="thing"] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
css = '[data-type = "thing"] {color: red;}'
self.assert_pynlined(html, css, expected)
def test_one_of(self):
html = '<span data-type="thing1 thing2">1</span>'
css = '[data-type~="thing1"] {color: red;}'
expected = u'<span data-type="thing1 thing2" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
css = '[data-type~="thing2"] {color: red;}'
expected = u'<span data-type="thing1 thing2" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
def test_starts_with(self):
html = '<span data-type="thing">1</span>'
css = '[data-type^="th"] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
def test_ends_with(self):
html = '<span data-type="thing">1</span>'
css = '[data-type$="ng"] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
def test_contains(self):
html = '<span data-type="thing">1</span>'
css = '[data-type*="i"] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
def test_is_or_prefixed_by(self):
html = '<span data-type="thing">1</span>'
css = '[data-type|="thing"] {color: red;}'
expected = u'<span data-type="thing" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
html = '<span data-type="thing-1">1</span>'
expected = u'<span data-type="thing-1" style="color: red">1</span>'
self.assert_pynlined(html, css, expected)
class IdenticalElementStringTest(unittest.TestCase):
def test_identical_element(self):
css = """
.text-right {
text-align: right;
}
.box {
width:200px;
border: 1px solid #000;
}
"""
html = """<div class="box"><p>Hello World</p><p class="text-right">Hello World on right</p><p class="text-right">Hello World on right</p></div>"""
expected = """<div class="box" style="width: 200px; border: 1px solid #000"><p>Hello World</p><p class="text-right" style="text-align: right">Hello World on right</p><p class="text-right" style="text-align: right">Hello World on right</p></div>"""
output = pynliner.Pynliner().from_string(html).with_cssString(css).run()
self.assertEqual(expected, output)
if __name__ == '__main__':
unittest.main()