This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
/
test_content.py
1177 lines (1073 loc) · 46 KB
/
test_content.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from urlparse import urljoin
import bleach
from jinja2 import escape, Markup
from nose.tools import eq_, ok_
from nose.plugins.attrib import attr
from pyquery import PyQuery as pq
from kuma.core.tests import KumaTestCase
from kuma.users.tests import UserTestCase
import kuma.wiki.content
from ..constants import ALLOWED_TAGS, ALLOWED_ATTRIBUTES
from ..content import (CodeSyntaxFilter, SectionTOCFilter, SectionIDFilter,
H2TOCFilter, H3TOCFilter, SECTION_TAGS,
get_seo_description, get_content_sections,
extract_css_classnames, extract_html_attributes,
extract_kumascript_macro_names)
from ..helpers import bugize_text
from ..models import Document
from . import normalize_html, doc_rev, document
class ContentSectionToolTests(UserTestCase):
def test_section_pars_for_empty_docs(self):
doc = document(title='Doc', locale=u'fr', slug=u'doc', save=True,
html='<!-- -->')
res = get_content_sections(doc.html)
eq_(type(res).__name__, 'list')
def test_section_ids(self):
doc_src = """
<h1 class="header1">Header One</h1>
<p>test</p>
<section>
<h1 class="header2">Header Two</h1>
<p>test</p>
</section>
<h2 name="Constants" class="hasname">This title does not match the name</h2>
<p>test</p>
<h1 id="i-already-have-an-id" class="hasid">This text clobbers the ID</h1>
<h1 class="header3">Header Three</h1>
<p>test</p>
<section id="Quick_Links" class="Quick_Links">
<ol>
<li>Hey look, quick links</li>
</ol>
</section>
"""
result_src = (kuma.wiki.content
.parse(doc_src)
.injectSectionIDs()
.serialize())
result_doc = pq(result_src)
expected = (
('header1', 'Header_One'),
('header2', 'Header_Two'),
('hasname', 'Constants'),
('hasid', 'This_text_clobbers_the_ID'),
('Quick_Links', 'Quick_Links'),
)
for cls, id in expected:
eq_(id, result_doc.find('.%s' % cls).attr('id'))
# Then, ensure all elements in need of an ID now all have unique IDs.
ok_(len(SECTION_TAGS) > 0)
els = result_doc.find(', '.join(SECTION_TAGS))
seen_ids = set()
for i in range(0, len(els)):
id = els.eq(i).attr('id')
ok_(id is not None)
ok_(id not in seen_ids)
seen_ids.add(id)
def test_incremented_section_ids(self):
doc_src = """
<h1 class="header1">Header One</h1>
<h1>Header One</h1>
<h1>Header One</h1>
<h1>Header Two</h1>
<h1 name="someId">Header Two</h1>
"""
result_src = (kuma.wiki.content
.parse(doc_src)
.injectSectionIDs()
.serialize())
expected = """
<h1 id="Header_One" class="header1">Header One</h1>
<h1 id="Header_One_2">Header One</h1>
<h1 id="Header_One_3">Header One</h1>
<h1 id="Header_Two">Header Two</h1>
<h1 id="someId" name="someId">Header Two</h1>
"""
self.assertHTMLEqual(result_src, expected)
# Ensure 1, 2 doesn't turn into 3, 4
result_src = (kuma.wiki.content
.parse(expected)
.injectSectionIDs()
.serialize())
self.assertHTMLEqual(result_src, expected)
def test_simple_implicit_section_extract(self):
doc_src = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h1 id="s2">Head 2</h1>
<p>test</p>
<p>test</p>
"""
expected = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="s1")
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_contained_implicit_section_extract(self):
doc_src = """
<h1 id="s4-next">Head</h1>
<p>test</p>
<section id="parent-s5">
<h1 id="s5">Head 5</h1>
<p>test</p>
<p>test</p>
<section>
<h1>head subsection</h1>
</section>
<h2 id="s5-1">Head 5-1</h2>
<p>test</p>
<p>test</p>
<h1 id="s5-next">Head 5 next</h1>
<p>test</p>
<p>test</p>
</section>
<h1 id="s7">Head 7</h1>
<p>test</p>
<p>test</p>
"""
expected = """
<h1 id="s5">Head 5</h1>
<p>test</p>
<p>test</p>
<section>
<h1>head subsection</h1>
</section>
<h2 id="s5-1">Head 5-1</h2>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="s5")
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_explicit_section_extract(self):
doc_src = """
<h1 id="s4-next">Head</h1>
<p>test</p>
<section id="parent-s5">
<h1 id="s5">Head 5</h1>
<p>test</p>
<p>test</p>
<section>
<h1>head subsection</h1>
</section>
<h2 id="s5-1">Head 5-1</h2>
<p>test</p>
<p>test</p>
<h1 id="s5-next">Head 5 next</h1>
<p>test</p>
<p>test</p>
</section>
<h1 id="s7">Head 7</h1>
<p>test</p>
<p>test</p>
"""
expected = """
<h1 id="s5">Head 5</h1>
<p>test</p>
<p>test</p>
<section>
<h1>head subsection</h1>
</section>
<h2 id="s5-1">Head 5-1</h2>
<p>test</p>
<p>test</p>
<h1 id="s5-next">Head 5 next</h1>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="parent-s5")
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_multilevel_implicit_section_extract(self):
doc_src = """
<p>test</p>
<h1 id="s4">Head 4</h1>
<p>test</p>
<p>test</p>
<h2 id="s4-1">Head 4-1</h2>
<p>test</p>
<p>test</p>
<h3 id="s4-2">Head 4-1-1</h3>
<p>test</p>
<p>test</p>
<h1 id="s4-next">Head</h1>
<p>test</p>
"""
expected = """
<h1 id="s4">Head 4</h1>
<p>test</p>
<p>test</p>
<h2 id="s4-1">Head 4-1</h1>
<p>test</p>
<p>test</p>
<h3 id="s4-2">Head 4-1-1</h1>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="s4")
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_morelevels_implicit_section_extract(self):
doc_src = """
<h1 id="s7">Head 7</h1>
<p>test</p>
<p>test</p>
<h1 id="s8">Head</h1>
<p>test</p>
<h2 id="s8-1">Head</h1>
<p>test</p>
<h3 id="s8-1-1">Head</h3>
<p>test</p>
<h2 id="s8-2">Head</h1>
<p>test</p>
<h3 id="s8-2-1">Head</h3>
<p>test</p>
<h4 id="s8-2-1-1">Head</h4>
<p>test</p>
<h2 id="s8-3">Head</h1>
<p>test</p>
<h1 id="s9">Head</h1>
<p>test</p>
<p>test</p>
"""
expected = """
<h1 id="s8">Head</h1>
<p>test</p>
<h2 id="s8-1">Head</h1>
<p>test</p>
<h3 id="s8-1-1">Head</h3>
<p>test</p>
<h2 id="s8-2">Head</h1>
<p>test</p>
<h3 id="s8-2-1">Head</h3>
<p>test</p>
<h4 id="s8-2-1-1">Head</h4>
<p>test</p>
<h2 id="s8-3">Head</h1>
<p>test</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="s8")
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_basic_section_replace(self):
doc_src = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h1 id="s2">Head 2</h1>
<p>test</p>
<p>test</p>
<h1 id="s3">Head 3</h1>
<p>test</p>
<p>test</p>
"""
replace_src = """
<h1 id="s2">Head 2</h1>
<p>replacement worked</p>
"""
expected = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h1 id="s2">Head 2</h1>
<p>replacement worked</p>
<h1 id="s3">Head 3</h1>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content
.parse(doc_src)
.replaceSection(id="s2", replace_src=replace_src)
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_section_edit_links(self):
doc_src = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h2 id="s2">Head 2</h2>
<p>test</p>
<p>test</p>
<h3 id="s3">Head 3</h3>
<p>test</p>
<p>test</p>
"""
expected = """
<h1 id="s1"><a class="edit-section" data-section-id="s1" data-section-src-url="/en-US/docs/some-slug?raw=true&section=s1" href="/en-US/docs/some-slug$edit?section=s1&edit_links=true" title="Edit section">Edit</a>Head 1</h1>
<p>test</p>
<p>test</p>
<h2 id="s2"><a class="edit-section" data-section-id="s2" data-section-src-url="/en-US/docs/some-slug?raw=true&section=s2" href="/en-US/docs/some-slug$edit?section=s2&edit_links=true" title="Edit section">Edit</a>Head 2</h2>
<p>test</p>
<p>test</p>
<h3 id="s3"><a class="edit-section" data-section-id="s3" data-section-src-url="/en-US/docs/some-slug?raw=true&section=s3" href="/en-US/docs/some-slug$edit?section=s3&edit_links=true" title="Edit section">Edit</a>Head 3</h3>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content
.parse(doc_src)
.injectSectionEditingLinks('some-slug', 'en-US')
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_code_syntax_conversion(self):
doc_src = """
<h2>Some JavaScript</h2>:
<pre class="deki-transform" function="syntax.JavaScript">
function foo(){
alert("bar");
}
</pre>
<pre>Some CSS:</pre>
<pre class="dek-trans" function="syntax.CSS">
.dek-trans { color: red; }
</pre>
"""
expected = """
<h2>Some JavaScript</h2>:
<pre class="brush: js">
function foo(){
alert("bar");
}
</pre>
<pre>Some CSS:</pre>
<pre class="brush: css">
.dek-trans { color: red; }
</pre>
"""
result = (kuma.wiki.content
.parse(doc_src)
.filter(CodeSyntaxFilter).serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_non_ascii_section_headers(self):
headers = [
(u'Documentation à propos de HTML',
u'Documentation_à_propos_de_HTML'),
(u'Outils facilitant le développement HTML',
u'Outils_facilitant_le_développement_HTML'),
(u'字面值(literals)',
u'字面值(literals)'),
(u'Documentação',
u'Documentação'),
(u'Lektury uzupełniające',
u'Lektury_uzupełniające'),
(u'Атрибуты',
u'Атрибуты'),
(u'HTML5 엘리먼트',
u'HTML5_엘리먼트'),
(u'Non safe title "#$%&+,/:;=?@[\\]^`{|}~',
u'Non_safe_title'),
]
section_filter = SectionIDFilter('')
for original, slugified in headers:
ok_(slugified == section_filter.slugify(original))
@attr('toc')
def test_generate_toc(self):
doc_src = """
<h2 id="HTML">HTML</h2>
<h3 id="HTML5_canvas_element">HTML5 <code>canvas</code> element</h3>
<h2 id="JavaScript">JavaScript</h2>
JavaScript is awesome.
<h3 id="WebGL">WebGL</h3>
<h3 id="Audio">Audio</h3>
<h4 id="Audio-API">Audio API</h4>
<h2 id="CSS">CSS</h2>
<h4 id="CSS_transforms">CSS transforms</h4>
<h3 id="Gradients">Gradients</h3>
<h4 id="Scaling_backgrounds">Scaling backgrounds</h4>
"""
expected = """
<li><a rel="internal" href="#HTML">HTML</a>
<ol>
<li><a rel="internal" href="#HTML5_canvas_element">HTML5 <code>canvas</code> element</a></li>
</ol>
</li>
<li><a rel="internal" href="#JavaScript">JavaScript</a>
<ol>
<li><a rel="internal" href="#WebGL">WebGL</a>
<li><a rel="internal" href="#Audio">Audio</a>
<ol>
<li><a rel="internal" href="#Audio-API">Audio API</a></li>
</ol>
</li>
</ol>
</li>
<li><a rel="internal" href="#CSS">CSS</a>
<ol>
<li>
<ol>
<li><a rel="internal" href="#CSS_transforms">CSS transforms</a>
</ol>
</li>
<li><a rel="internal" href="#Gradients">Gradients</a>
<ol>
<li><a rel="internal" href="#Scaling_backgrounds">Scaling backgrounds</a>
</ol>
</ol>
</li>
"""
result = (kuma.wiki.content
.parse(doc_src)
.filter(SectionTOCFilter).serialize())
eq_(normalize_html(expected), normalize_html(result))
@attr('toc')
def test_generate_toc_h2(self):
doc_src = """
<h2 id="HTML">HTML</h2>
<h3 id="HTML5_canvas_element">HTML5 <code>canvas</code> element</h3>
<h2 id="JavaScript">JavaScript</h2>
JavaScript is awesome.
<h3 id="WebGL">WebGL</h3>
<h3 id="Audio">Audio</h3>
<h4 id="Audio-API">Audio API</h4>
<h2 id="CSS">CSS</h2>
<h4 id="CSS_transforms">CSS transforms</h4>
<h3 id="Gradients">Gradients</h3>
<h4 id="Scaling_backgrounds">Scaling backgrounds</h4>
"""
expected = """
<li><a rel="internal" href="#HTML">HTML</a>
</li>
<li><a rel="internal" href="#JavaScript">JavaScript</a>
</li>
<li><a rel="internal" href="#CSS">CSS</a>
</li>
"""
result = (kuma.wiki.content
.parse(doc_src)
.filter(H2TOCFilter).serialize())
eq_(normalize_html(expected), normalize_html(result))
@attr('toc')
def test_generate_toc_h3(self):
doc_src = """
<h2 id="HTML">HTML</h2>
<h3 id="HTML5_canvas_element">HTML5 <code>canvas</code> element</h3>
<h2 id="JavaScript">JavaScript</h2>
JavaScript is awesome.
<h3 id="WebGL">WebGL</h3>
<h3 id="Audio">Audio</h3>
<h4 id="Audio-API">Audio API</h4>
<h2 id="CSS">CSS</h2>
<h4 id="CSS_transforms">CSS transforms</h4>
<h3 id="Gradients">Gradients</h3>
<h4 id="Scaling_backgrounds">Scaling backgrounds</h4>
"""
expected = """
<li><a rel="internal" href="#HTML">HTML</a>
<ol>
<li><a rel="internal" href="#HTML5_canvas_element">HTML5 <code>canvas</code> element</a></li>
</ol>
</li>
<li><a rel="internal" href="#JavaScript">JavaScript</a>
<ol>
<li><a rel="internal" href="#WebGL">WebGL</a>
<li><a rel="internal" href="#Audio">Audio</a>
</li>
</ol>
</li>
<li><a rel="internal" href="#CSS">CSS</a>
<ol>
<li><a rel="internal" href="#Gradients">Gradients</a>
</ol>
</li>
"""
result = (kuma.wiki.content
.parse(doc_src)
.filter(H3TOCFilter).serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_bug_925043(self):
'''Bug 925043 - Redesign TOC has a bunch of empty <code> tags in markup'''
doc_src = """
<h2 id="Print">Mastering <code>print</code></h2>
<code>print 'Hello World!'</code>
"""
expected = """
<li>
<a href="#Print" rel="internal">Mastering<code>print</code></a>
</li>
"""
result = (kuma.wiki.content
.parse(doc_src)
.filter(SectionTOCFilter).serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_noinclude(self):
doc_src = u"""
<div class="noinclude">{{ XULRefAttr() }}</div>
<dl>
<dt>{{ XULAttr("maxlength") }}</dt>
<dd>Type: <em>integer</em></dd>
<dd>Przykłady 例 예제 示例</dd>
</dl>
<div class="noinclude">
<p>{{ languages( { "ja": "ja/XUL/Attribute/maxlength" } ) }}</p>
</div>
"""
expected = u"""
<dl>
<dt>{{ XULAttr("maxlength") }}</dt>
<dd>Type: <em>integer</em></dd>
<dd>Przykłady 例 예제 示例</dd>
</dl>
"""
result = (kuma.wiki.content.filter_out_noinclude(doc_src))
eq_(normalize_html(expected), normalize_html(result))
def test_noinclude_empty_content(self):
"""Bug 777475: The noinclude filter and pyquery seems to really dislike
empty string as input"""
doc_src = ''
try:
result = kuma.wiki.content.filter_out_noinclude(doc_src)
eq_('', result)
except:
self.fail("There should not have been an exception")
def test_sample_code_extraction(self):
sample_html = u"""
<div class="foo">
<p>Hello world!</p>
<p>Unicode fun: Przykłady 例 예제 示例</p>
</div>
"""
sample_css = u"""
.foo p { color: red; }
"""
sample_js = u"""
window.alert("Hi there!");
"""
doc_src = u"""
<p>This is a page. Deal with it.</p>
<h3 id="sample0">This is a section</h3>
<pre class="brush:html; highlight: [5, 15]; html-script: true">section html</pre>
<pre class="brush:css;">section css</pre>
<pre class="brush: js">section js</pre>
<h3>The following is a new section</h3>
<div id="sample1" class="code-sample">
<pre class="brush: html;">Ignore me</pre>
<pre class="brush:css;">Ignore me</pre>
<pre class="brush: js">Ignore me</pre>
</div>
<ul id="sample2" class="code-sample">
<li><span>HTML</span>
<pre class="brush: html">%s</pre>
</li>
<li><span>CSS</span>
<pre class="brush:css;random:crap;in:the;classname">%s</pre>
</li>
<li><span>JS</span>
<pre class="brush: js">%s</pre>
</li>
</ul>
<p>More content shows up here.</p>
<p id="not-a-sample">This isn't a sample, but it
shouldn't cause an error</p>
<h4 id="sample3">Another section</h4>
<pre class="brush: html">Ignore me</pre>
<pre class="brush: js">Ignore me</pre>
<h4>Yay a header</h4>
<p>Yadda yadda</p>
<div id="sample4" class="code-sample">
<pre class="brush: js">Ignore me</pre>
</div>
<p>Yadda yadda</p>
""" % (escape(sample_html), escape(sample_css), escape(sample_js))
# live sample using the section logic
result = kuma.wiki.content.extract_code_sample('sample0', doc_src)
eq_('section html', result['html'].strip())
eq_('section css', result['css'].strip())
eq_('section js', result['js'].strip())
# pull out a complete sample.
result = kuma.wiki.content.extract_code_sample('sample2', doc_src)
eq_(sample_html.strip(), result['html'].strip())
eq_(sample_css.strip(), result['css'].strip())
eq_(sample_js.strip(), result['js'].strip())
# a sample missing one part.
result = kuma.wiki.content.extract_code_sample('sample3', doc_src)
eq_('Ignore me', result['html'].strip())
eq_(None, result['css'])
eq_('Ignore me', result['js'].strip())
# a sample with only one part.
result = kuma.wiki.content.extract_code_sample('sample4', doc_src)
eq_(None, result['html'])
eq_(None, result['css'])
eq_('Ignore me', result['js'].strip())
# a "sample" with no code listings.
result = kuma.wiki.content.extract_code_sample('not-a-sample', doc_src)
eq_(None, result['html'])
eq_(None, result['css'])
eq_(None, result['js'])
def test_bug819999(self):
"""Non-breaking spaces are turned to normal spaces in code sample
extraction."""
doc_src = """
<h2 id="bug819999">Bug 819999</h2>
<pre class="brush: css">
.widget select,
.no-widget .select {
position : absolute;
left : -5000em;
height : 0;
overflow : hidden;
}
</pre>
"""
result = kuma.wiki.content.extract_code_sample('bug819999', doc_src)
ok_(result['css'].find(u'\xa0') == -1)
def test_bugize_text(self):
bad = 'Fixing bug #12345 again. <img src="http://davidwalsh.name" /> <a href="">javascript></a>'
good = 'Fixing <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=12345" target="_blank">bug 12345</a> again. <img src="http://davidwalsh.name" /> <a href="">javascript></a>'
eq_(bugize_text(bad), Markup(good))
bad_upper = 'Fixing Bug #12345 again.'
good_upper = 'Fixing <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=12345" target="_blank">Bug 12345</a> again.'
eq_(bugize_text(bad_upper), Markup(good_upper))
def test_iframe_host_filter(self):
slug = 'test-code-embed'
embed_url = 'https://sampleserver/en-US/docs/%s$samples/sample1' % slug
doc_src = """
<p>This is a page. Deal with it.</p>
<div id="sample1" class="code-sample">
<pre class="brush: html">Some HTML</pre>
<pre class="brush: css">.some-css { color: red; }</pre>
<pre class="brush: js">window.alert("HI THERE")</pre>
</div>
<iframe id="if1" src="%(embed_url)s"></iframe>
<iframe id="if2" src="http://testserver"></iframe>
<iframe id="if3" src="https://some.alien.site.com"></iframe>
<p>test</p>
""" % dict(embed_url=embed_url)
result_src = (kuma.wiki.content.parse(doc_src)
.filterIframeHosts('^https?\:\/\/sampleserver')
.serialize())
page = pq(result_src)
if1 = page.find('#if1')
eq_(if1.length, 1)
eq_(if1.attr('src'), embed_url)
if2 = page.find('#if2')
eq_(if2.length, 1)
eq_(if2.attr('src'), '')
if3 = page.find('#if3')
eq_(if3.length, 1)
eq_(if3.attr('src'), '')
def test_iframe_host_filter_invalid_host(self):
doc_src = """
<iframe id="if1" src="http://sampleserver"></iframe>
<iframe id="if2" src="http://testserver"></iframe>
<iframe id="if3" src="http://davidwalsh.name"></iframe>
<iframe id="if4" src="ftp://davidwalsh.name"></iframe>
<p>test</p>
"""
result_src = (kuma.wiki.content.parse(doc_src)
.filterIframeHosts('^https?\:\/\/(sample|test)server')
.serialize())
page = pq(result_src)
eq_(page.find('#if1').attr('src'), 'http://sampleserver')
eq_(page.find('#if2').attr('src'), 'http://testserver')
eq_(page.find('#if3').attr('src'), '')
eq_(page.find('#if4').attr('src'), '')
def test_iframe_host_filter_youtube(self):
tubes = (
'http://www.youtube.com/embed/iaNoBlae5Qw/?feature=player_detailpage',
'https://youtube.com/embed/iaNoBlae5Qw/?feature=player_detailpage',
'https://youtube.com/sembed/'
)
doc_src = """
<iframe id="if1" src="%s"></iframe>
<iframe id="if2" src="%s"></iframe>
<iframe id="if3" src="%s"></iframe>
<p>test</p>
""" % tubes
result_src = (kuma.wiki.content.parse(doc_src)
.filterIframeHosts('^https?\:\/\/(www.)?youtube.com\/embed\/(\.*)')
.serialize())
page = pq(result_src)
eq_(page.find('#if1').attr('src'), tubes[0])
eq_(page.find('#if2').attr('src'), tubes[1])
eq_(page.find('#if3').attr('src'), '')
def test_iframe_host_contents_filter(self):
"""Any contents inside an <iframe> should be removed"""
doc_src = """
<iframe>
<iframe src="javascript:alert(1);"></iframe>
</iframe>
"""
expected_src = """
<iframe>
</iframe>
"""
result_src = (kuma.wiki.content.parse(doc_src)
.filterIframeHosts('^https?\:\/\/sampleserver')
.serialize())
eq_(normalize_html(expected_src), normalize_html(result_src))
def test_link_annotation(self):
d, r = doc_rev("This document exists")
d.save()
r.save()
document(title=u'Héritée', locale=u'fr', slug=u'CSS/Héritage',
save=True)
document(title=u'DOM/StyleSheet', locale=u'en-US',
slug=u'DOM/StyleSheet', save=True)
base_url = u'https://testserver'
vars = dict(
base_url=base_url,
exist_url=d.get_absolute_url(),
exist_url_with_base=urljoin(base_url, d.get_absolute_url()),
uilocale_url=u'/en-US/docs/%s/%s' % (d.locale, d.slug),
noexist_url=u'/en-US/docs/no-such-doc',
noexist_url_with_base=urljoin(base_url,
u'/en-US/docs/no-such-doc'),
noexist_uilocale_url=u'/en-US/docs/en-US/blah-blah-blah',
nonen_slug='/fr/docs/CSS/H%c3%a9ritage',
tag_url='/en-US/docs/tag/foo',
feed_url='/en-US/docs/feeds/atom/all',
templates_url='/en-US/docs/templates',
)
doc_src = u"""
<li><a href="%(nonen_slug)s">Héritée</a></li>
<li><a href="%(exist_url)s">This doc should exist</a></li>
<li><a href="%(exist_url)s#withanchor">This doc should exist</a></li>
<li><a href="%(exist_url_with_base)s">This doc should exist</a></li>
<li><a href="%(exist_url_with_base)s#withanchor">This doc should exist</a></li>
<li><a href="%(uilocale_url)s">This doc should exist</a></li>
<li><a class="foobar" href="%(exist_url)s">This doc should exist, and its class should be left alone.</a></li>
<li><a href="%(noexist_url)s#withanchor">This doc should NOT exist</a></li>
<li><a href="%(noexist_url)s">This doc should NOT exist</a></li>
<li><a href="%(noexist_url_with_base)s">This doc should NOT exist</a></li>
<li><a href="%(noexist_url_with_base)s#withanchor">This doc should NOT exist</a></li>
<li><a href="%(noexist_uilocale_url)s">This doc should NOT exist</a></li>
<li><a class="foobar" href="%(noexist_url)s">This doc should NOT exist, and its class should be altered</a></li>
<li><a href="http://mozilla.org/">This is an external link</a></li>
<li><a class="foobar" name="quux">A lack of href should not cause a problem.</a></li>
<li><a>In fact, a "link" with no attributes should be no problem as well.</a></li>
<a href="%(tag_url)s">Tag link</a>
<a href="%(feed_url)s">Feed link</a>
<a href="%(templates_url)s">Templates link</a>
<a href="/en-US/docs/DOM/stylesheet">Case sensitive 1</a>
<a href="/en-US/docs/DOM/Stylesheet">Case sensitive 1</a>
<a href="/en-US/docs/DOM/StyleSheet">Case sensitive 1</a>
<a href="/en-us/docs/dom/StyleSheet">Case sensitive 1</a>
<a href="/en-US/docs/dom/Styles">For good measure</a>
""" % vars
expected = u"""
<li><a href="%(nonen_slug)s">Héritée</a></li>
<li><a href="%(exist_url)s">This doc should exist</a></li>
<li><a href="%(exist_url)s#withanchor">This doc should exist</a></li>
<li><a href="%(exist_url_with_base)s">This doc should exist</a></li>
<li><a href="%(exist_url_with_base)s#withanchor">This doc should exist</a></li>
<li><a href="%(uilocale_url)s">This doc should exist</a></li>
<li><a class="foobar" href="%(exist_url)s">This doc should exist, and its class should be left alone.</a></li>
<li><a class="new" href="%(noexist_url)s#withanchor">This doc should NOT exist</a></li>
<li><a class="new" href="%(noexist_url)s">This doc should NOT exist</a></li>
<li><a class="new" href="%(noexist_url_with_base)s">This doc should NOT exist</a></li>
<li><a class="new" href="%(noexist_url_with_base)s#withanchor">This doc should NOT exist</a></li>
<li><a class="new" href="%(noexist_uilocale_url)s">This doc should NOT exist</a></li>
<li><a class="foobar new" href="%(noexist_url)s">This doc should NOT exist, and its class should be altered</a></li>
<li><a class="external" href="http://mozilla.org/">This is an external link</a></li>
<li><a class="foobar" name="quux">A lack of href should not cause a problem.</a></li>
<li><a>In fact, a "link" with no attributes should be no problem as well.</a></li>
<a href="%(tag_url)s">Tag link</a>
<a href="%(feed_url)s">Feed link</a>
<a href="%(templates_url)s">Templates link</a>
<a href="/en-US/docs/DOM/stylesheet">Case sensitive 1</a>
<a href="/en-US/docs/DOM/Stylesheet">Case sensitive 1</a>
<a href="/en-US/docs/DOM/StyleSheet">Case sensitive 1</a>
<a href="/en-us/docs/dom/StyleSheet">Case sensitive 1</a>
<a class="new" href="/en-US/docs/dom/Styles">For good measure</a>
""" % vars
# Split the markup into lines, to better see failures
doc_lines = doc_src.strip().split("\n")
expected_lines = expected.strip().split("\n")
for idx in range(0, len(doc_lines)):
doc_line = doc_lines[idx]
expected_line = expected_lines[idx]
result_line = (kuma.wiki.content.parse(doc_line)
.annotateLinks(
base_url=vars['base_url'])
.serialize())
self.assertHTMLEqual(normalize_html(expected_line), normalize_html(result_line))
@attr('bug821986')
def test_editor_safety_filter(self):
"""Markup that's hazardous for editing should be stripped"""
doc_src = """
<svg><circle onload=confirm(3)>
<h1 class="header1">Header One</h1>
<p>test</p>
<section>
<h1 class="header2">Header Two</h1>
<p>test</p>
</section>
<h1 class="header3">Header Three</h1>
<p>test</p>
"""
expected_src = """
<svg><circle>
<h1 class="header1">Header One</h1>
<p>test</p>
<section>
<h1 class="header2">Header Two</h1>
<p>test</p>
</section>
<h1 class="header3">Header Three</h1>
<p>test</p>
"""
result_src = (kuma.wiki.content.parse(doc_src)
.filterEditorSafety()
.serialize())
eq_(normalize_html(expected_src), normalize_html(result_src))
def test_ignore_heading_section_extract(self):
doc_src = """
<p>test</p>
<h1 id="s4">Head 4</h1>
<p>test</p>
<h2 id="s4-1">Head 4-1</h2>
<p>test</p>
<h3 id="s4-2">Head 4-1-1</h3>
<p>test s4-2</p>
<h1 id="s4-next">Head</h1>
<p>test</p>
"""
expected = """
<p>test</p>
<h3 id="s4-2">Head 4-1-1</h3>
<p>test s4-2</p>
"""
result = (kuma.wiki.content.parse(doc_src)
.extractSection(id="s4-1",
ignore_heading=True)
.serialize())
eq_(normalize_html(expected), normalize_html(result))
def test_ignore_heading_section_replace(self):
doc_src = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h1 id="s2">Head 2</h1>
<p>test</p>
<p>test</p>
<h1 id="s3">Head 3</h1>
<p>test</p>
<p>test</p>
"""
replace_src = """
<p>replacement worked yay hooray</p>
"""
expected = """
<h1 id="s1">Head 1</h1>
<p>test</p>
<p>test</p>
<h1 id="s2">Head 2</h1>
<p>replacement worked yay hooray</p>
<h1 id="s3">Head 3</h1>
<p>test</p>
<p>test</p>
"""
result = (kuma.wiki.content
.parse(doc_src)
.replaceSection(id="s2",
replace_src=replace_src,
ignore_heading=True)
.serialize())
eq_(normalize_html(expected), normalize_html(result))
class AllowedHTMLTests(KumaTestCase):
simple_tags = (
'div', 'span', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'pre',
'code', 'dl', 'dt', 'dd', 'table',
'section', 'header', 'footer',
'nav', 'article', 'aside', 'figure', 'dialog', 'hgroup',
'mark', 'time', 'meter', 'output', 'progress',
'audio', 'details', 'datagrid', 'datalist', 'table',
'address'
)
unclose_tags = ('img', 'input', 'br', 'command')
special_tags = (
"<table><thead><tr><th>foo</th></tr></thead><tbody><tr><td>foo</td></tr></tbody></table>",
)
special_attributes = (
'<command id="foo">',
'<img align="left" alt="picture of foo" class="foo" dir="rtl" id="foo" src="foo" title="foo">',
'<a class="foo" href="foo" id="foo" title="foo">foo</a>',
'<div class="foo">foo</div>',
'<video class="movie" controls id="some-movie" lang="en-US" src="some-movie.mpg">Fallback</video>'
# TODO: Styles have to be cleaned on a case-by-case basis. We
# need to enumerate the styles we're going to allow, then feed
# them to bleach.
# '<span style="font-size: 24px"></span>',
)
def test_allowed_tags(self):
for tag in self.simple_tags:
html_str = '<%(tag)s></%(tag)s>' % {'tag': tag}
eq_(html_str, bleach.clean(html_str, attributes=ALLOWED_ATTRIBUTES,
tags=ALLOWED_TAGS))
for tag in self.unclose_tags:
html_str = '<%s>' % tag
eq_(html_str, bleach.clean(html_str, attributes=ALLOWED_ATTRIBUTES,
tags=ALLOWED_TAGS))
for html_str in self.special_tags:
eq_(html_str, bleach.clean(html_str, attributes=ALLOWED_ATTRIBUTES,