forked from cstigler/node-xhtml-purifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XHTMLPurifier.js
991 lines (935 loc) · 22.4 KB
/
XHTMLPurifier.js
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
/*
* XHTML Purifier By Mathias Biilmann Christensen
* and Rodrigo Alvarez
* Copyright Domestika 2008
*
*/
// disable JSHint complaints about switch statement fall-throughs
/* jshint -W086 */
var HTMLParser = require('./HTMLParser');
var XHTMLPurifier = (function() {
var allowHeaders = true;
var stack = [];
var active_elements = [];
var root;
var insertion_mode;
var noFormatting;
var scope_markers = {'td':true, 'th': true, 'caption':true};
var tags_with_implied_end = {'li':true, 'p':true};
var allowed_attributes = {
// WARNING: the original version didn't allow style attributes
// we allow it at Zaption, and it seems to be safe with all modern browsers
// BUT, proceed with caution.
'all_elements': ['class', 'style'],
'a': ['href', 'target', 'title', 'name', 'rel', 'rev', 'type'],
'blockquote': ['cite'],
'img': ['src', 'alt', 'title', 'longdesc'],
'td': ['colspan', 'class'],
'th': ['colspan', 'class'],
'tr': ['rowspan', 'class'],
'table': ['class'],
// Jialin: the following are for Froala
'span': ['mathquill-command-id', 'mathquill-block-id', 'data-latex', 'data-scale', 'contenteditable'],
'var': ['mathquill-command-id', 'mathquill-block-id', 'data-latex', 'data-scale'],
'sub': ['mathquill-command-id', 'mathquill-block-id', 'data-latex', 'data-scale'],
'sup': ['mathquill-command-id', 'mathquill-block-id', 'data-latex', 'data-scale'],
'big': ['mathquill-command-id', 'mathquill-block-id', 'data-latex', 'data-scale']
};
var allowed_attributes_as_hash;
var selfClosing = {
br: true,
hr: true,
img: true
};
var dontIndent = {
strong: true,
em: true,
pre: true
};
var indent = false;
var indent_string = " ";
var indentation = function(depth, switchOff) {
if (noFormatting) return "";
if (!indent) return "";
if (switchOff) indent = false;
var result = "\n";
for(var i=0; i<depth; i++) {
result += indent_string;
}
return result;
};
var TextNode = function(text) {
this.text = text.replace(/\s+/g, ' ');
};
TextNode.prototype = {
isEmpty: function() {
return !this.text;
},
textContent: function() {
return this.text;
},
toString: function() {
// WARNING: The second parameter passed to this.text.replace changed from the original
// ' ' to ' '
return this.isEmpty() ? '' : indentation(this.depth(), true) + this.text.replace(/( )+/, ' ');
},
depth: function() {
return this.parent.depth() + 1;
}
};
var Node = function(name) {
this.name = name;
this.children = [];
this.attributes = {};
};
Node.prototype = {
appendChild: function(child) {
this.children.push(child);
child.parent = this;
return child;
},
removeChild: function(child) {
for (var i=0, len = this.children.length; i<len; i++) {
if (this.children[i] === child) {
return this.children.splice(i,i);
}
}
return null;
},
lastChild: function() {
return this.children[this.children.length - 1];
},
clone: function() {
var clone = new Node(this.name);
for (var i in this.attributes) {
clone.attributes[i] = this.attributes[i];
}
return clone;
},
startTag: function() {
return "<" + this.name + this.attributeString() + ">";
},
endTag: function() {
return "</" + this.name + ">";
},
selfClosingTag: function() {
return "<" + this.name + this.attributeString() + "/>";
},
attributeString: function() {
var string = "";
var allowed_for_tag = allowed_attributes_as_hash[this.name] || {};
var allowed_for_all = allowed_attributes_as_hash['all_elements'] || {};
for (var i=0, len=(this.attributes || []).length; i<len; i++) {
var name = this.attributes[i].name;
var value = this.attributes[i].value;
if ((allowed_for_tag[name] || allowed_for_all[name]) && value) {
if (name === 'href') {
// don't allow links to anywhere other than http(s)
// because they could contain JavaScript (javascript:) or other bad things!
var permittedRegex = /^https?:\/\//i;
if (!permittedRegex.test(value)) {
// if not allowed, set the attribute to be empty
value = '';
}
}
string += " " + name + "=\"" + value + "\"";
}
}
return string;
},
innerHTML: function() {
var string = "";
for (var i=0, len=this.children.length; i<len; i++) {
string += this.children[i];
}
return string;
},
textContent: function() {
var text = "";
for (var i=0, len=this.children.length; i<len; i++) {
if (this.children[i] instanceof TextNode) {
text += this.children[i].text;
}
}
return text;
},
toString: function() {
if (this.isEmpty()) return '';
var string = "";
if (selfClosing[this.name]) {
string = indentation(this.depth(), true) + this.selfClosingTag();
} else {
indent = dontIndent[this.name] ? indent : true;
string = indentation(this.depth(), dontIndent[this.name]) + this.startTag() + this.innerHTML();
indent = dontIndent[this.name] ? indent : true;
string += indentation(this.depth()) + this.endTag();
}
return string;
},
depth: function() {
return this.parent ? this.parent.depth() + 1 : -1;
},
isEmpty: function() {
// Zaption mod: self-closing elements never count as empty
// otherwise <p><br/></p> gets removed entirely
if (selfClosing[this.name]) {
return false;
}
if (typeof(this._isEmpty) === "undefined") {
this._isEmpty = true;
for (var i=0, len=this.children.length; i<len; i++) {
if (!this.children[i].isEmpty()) {
this._isEmpty = false;
break;
}
}
}
return this._isEmpty;
}
};
function init() {
root = new Node('html');
stack = [root, root.appendChild(new Node('p'))];
active_elements = [];
allowed_attributes_as_hash = {};
var attr, i;
for(var key in allowed_attributes) {
allowed_attributes_as_hash[key] = {};
for(i in allowed_attributes['all_elements']) {
attr = allowed_attributes['all_elements'][i];
allowed_attributes_as_hash[key][attr] = true;
}
if(key === 'all_elements') {
continue;
}
for(i in allowed_attributes[key]) {
attr = allowed_attributes[key][i];
allowed_attributes_as_hash[key][attr] = true;
}
}
}
function last_el(list) {
var len = list.length;
if(len === 0) {
return null;
}
return list[len - 1];
}
function in_array(arr, elem) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === elem) return true;
}
return false;
}
function current_node() {
return last_el(stack);
}
function reconstruct_the_active_formatting_elements() {
if(active_elements.length === 0 || in_array(stack, last_el(active_elements))) {
return;
}
var entry;
for(var i = active_elements.length; i>0; i--) {
entry = active_elements[i-1];
if(in_array(stack, entry)) {
break;
}
}
do {
var clone = entry.clone();
current_node().appendChild(clone);
stack.push(clone);
active_elements[i] = clone;
i += 1;
} while(i !== active_elements.length);
}
function has_element_with(arr_of_elements, tagName) {
for(var i = arr_of_elements.length; i>0; i--) {
if(arr_of_elements[i-1].name === tagName) {
return true;
}
}
return false;
}
function in_scope(tagName) {
return has_element_with(stack, tagName);
}
function in_table_scope(tagName) {
for(var i = stack.length; i>0; i--) {
var nodeTag = stack[i-1].name;
if(nodeTag === tagName) {
return true;
} else if(nodeTag === 'table' || nodeTag === 'html') {
return false;
}
}
return false;
}
function insert_html_element_for(tagName, attrs) {
var node = new Node(tagName);
node.attributes = attrs;
current_node().appendChild(node);
stack.push(node);
return node;
}
function generate_implied_end_tags(exception) {
var tagName = current_node().name;
while(tags_with_implied_end[tagName] && tagName !== exception) {
end(tagName);
tagName = current_node().name;
}
}
function trim_to_1_space(str) {
return str.replace(/^\s+/, ' ').replace(/\s+$/, ' ');
}
function clear_stack_to_table_context() {
clear_stack_to_context_by_tags(['table', 'html']);
}
function clear_stack_to_table_body_context() {
clear_stack_to_context_by_tags(['tbody', 'tfoot', 'thead', 'html']);
}
function clear_stack_to_table_row_context() {
clear_stack_to_context_by_tags(['tr', 'html']);
}
function clear_stack_to_context_by_tags(tags) {
while(!in_array(tags, current_node().name)) {
stack.pop();
}
}
function clear_active_elements_to_last_marker() {
var entry;
do {
entry = active_elements.pop();
} while(!scope_markers[entry.name]);
}
function reset_insertion_mode() {
var last = false;
var node;
for (var i = stack.length - 1; i >= 0; i--){
node = stack[i];
if (node === stack[0]) {
last = true;
}
switch(node.name) {
case 'th':
case 'td':
if (!last) {
insertion_mode = InCell;
return;
}
case 'tr':
insertion_mode = InRow;
return;
case 'tbody':
case 'thead':
case 'tfoot':
insertion_mode = InTableBody;
return;
case 'caption':
insertion_mode = InCaption;
return;
case 'colgroup':
insertion_mode = InColumnGroup;
return;
case 'table':
insertion_mode = InTable;
return;
default:
if (last) {
insertion_mode = InBody;
return;
}
}
}
}
function close_the_cell() {
if (in_table_scope('td')) {
end('td');
} else {
end('th');
}
}
function start(tagName, attrs, unary) {
insertion_mode.insertion_mode_start(tagName, attrs, unary);
}
function end(tagName) {
insertion_mode.insertion_mode_end(tagName);
}
function chars(text) {
if(typeof(text) === 'undefined') {
return;
}
text = text.replace(/\n\s*\n\s*\n*/g,'\n\n').replace(/(^\n\n|\n\n$)/g,'');
var paragraphs = text.split('\n\n');
var trimmedText;
if(paragraphs.length > 1) {
for(var i in paragraphs) {
start('p');
reconstruct_the_active_formatting_elements();
trimmedText = trim_to_1_space(paragraphs[i]);
current_node().appendChild(new TextNode(trimmedText));
end('p');
}
} else {
if(text.match(/^\s*$/g) && current_node().children.length && current_node().lastChild().name === 'br') {
return;
}
reconstruct_the_active_formatting_elements();
trimmedText = trim_to_1_space(paragraphs[0]);
current_node().appendChild(new TextNode(trimmedText));
}
}
var InBody = {
insertion_mode_start: function (tagName, attrs) {
var node;
tagName = tagName.toLowerCase();
switch(tagName) {
case 'b':
start('strong');
return;
case 'i':
start('em');
return;
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'h7':
if(!allowHeaders) {
start('p');
start('strong');
return;
}
case 'blockquote':
case 'ol':
case 'p':
case 'ul':
case 'pre': // Techically PRE shouldn't be in this groups, since newlines should be ignored after a pre tag
if(in_scope('p')) {
end('p');
}
insert_html_element_for(tagName, attrs);
return;
case 'li':
if(in_scope('p')) {
end('p');
}
node = current_node();
while(node.name === 'li') {
stack.pop();
}
insert_html_element_for(tagName, attrs);
return;
case 'a':
for(var i=active_elements.length; i>0; i--) {
if(active_elements[i-1].name === 'a') {
end('a');
active_elements.splice(i-1,1);
}
}
reconstruct_the_active_formatting_elements();
node = insert_html_element_for(tagName, attrs);
active_elements.push(node);
return;
case 'strong':
case 'em':
case 'u':
case 'span':
case 'var':
case 'sup':
case 'sub':
case 'big':
reconstruct_the_active_formatting_elements();
node = insert_html_element_for(tagName, attrs);
active_elements.push(node);
return;
case 'table':
if (in_scope('p')) {
end('p');
}
insert_html_element_for(tagName, attrs);
insertion_mode = InTable;
return;
case 'br':
case 'img':
reconstruct_the_active_formatting_elements();
insert_html_element_for(tagName, attrs);
stack.pop();
return;
}
},
insertion_mode_end: function (tagName) {
if(typeof(tagName) === undefined) {
return;
}
var node;
tagName = tagName.toLowerCase();
switch(tagName) {
case 'b':
end('strong');
return;
case 'i':
end('em');
return;
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
case 'h7':
if(!allowHeaders) {
end('strong');
end('p');
return;
}
if(in_scope(tagName)) {
generate_implied_end_tags();
do {
node = stack.pop();
} while(node.name !== tagName);
}
return;
case 'blockquote':
case 'ol':
case 'ul':
case 'pre': // Techically PRE shouldn't be in this groups, since newlines should be ignored after a pre tag
if(in_scope(tagName)) {
generate_implied_end_tags();
}
if(in_scope(tagName)) {
do {
node = stack.pop();
} while(node.name !== tagName);
}
return;
case 'p':
if(in_scope(tagName)) {
generate_implied_end_tags(tagName);
}
var no_p_in_scope = true;
while(in_scope(tagName)) {
no_p_in_scope = false;
node = stack.pop();
}
if(no_p_in_scope) {
start('p',[],false);
end('p');
}
return;
case 'li':
if(in_scope(tagName)) {
generate_implied_end_tags(tagName);
}
if(in_scope(tagName)) {
do {
node = stack.pop();
} while(node.name !== tagName);
}
return;
case 'a':
case 'em':
case 'strong':
case 'u':
case 'span':
case 'var':
case 'sup':
case 'sub':
case 'big':
for(var i=active_elements.length; i>0; i--) {
if(active_elements[i-1].name === tagName) {
node = active_elements[i-1];
break;
}
}
if(typeof(node) === 'undefined' || !in_array(stack, node)) {
return;
}
// Step 2 from the algorithm in the HTML5 spec will never be necessary with the tags we allow
var popped_node;
do {
popped_node = stack.pop();
} while(popped_node !== node);
active_elements.splice(i-1, 1);
return;
default:
node = current_node();
if(node.name === tagName) {
generate_implied_end_tags();
while(stack.length > 0 && node !== current_node()) {
stack.pop();
}
}
}
}
};
var InTable = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case 'caption':
clear_stack_to_table_context();
active_elements.push(insert_html_element_for(tagName, attrs));
insertion_mode = InCaption;
return;
case 'colgroup':
clear_stack_to_table_context();
insert_html_element_for(tagName, attrs);
insertion_mode = InColumnGroup;
return;
case 'col':
start('colgroup');
start(tagName, attrs, unary);
return;
case 'tbody':
case 'tfoot':
case 'thead':
clear_stack_to_table_context();
insert_html_element_for(tagName, attrs);
insertion_mode = InTableBody;
return;
case 'td':
case 'th':
case 'tr':
start('tbody');
start(tagName, attrs, unary);
return;
}
},
insertion_mode_end: function (tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case 'table':
if(in_table_scope('table')) {
var node;
do {
node = stack.pop();
} while(node.name !== 'table');
}
reset_insertion_mode();
return;
}
}
};
var InCaption = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case 'caption':
case 'col':
case 'colgroup':
case 'tbody':
case 'td':
case 'tfoot':
case 'th':
case 'thead':
case 'tr':
end('caption');
start(tagName);
return;
default:
InBody.insertion_mode_start(tagName, attrs, unary);
return;
}
},
insertion_mode_end: function(tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case 'caption':
if (in_table_scope('caption')) {
generate_implied_end_tags();
if (current_node().name === 'caption') {
var node;
do {
node = stack.pop();
} while(node.name !== 'caption');
clear_active_elements_to_last_marker();
insertion_mode = InTable;
}
}
return;
case "body":
case "col":
case "colgroup":
case "html":
case "tbody":
case "td":
case "tfoot":
case "th":
case "thead":
case "tr":
return;
case 'table':
end('caption');
end('table');
return;
default:
InBody.insertion_mode_end(tagName);
return;
}
}
};
var InColumnGroup = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case 'html':
InBody.insertion_mode_start(tagName, attrs, unary);
return;
case 'col':
insert_html_element_for(tagName, attrs);
stack.pop();
return;
default:
end('colgroup');
start(tagName);
return;
}
},
insertion_mode_end: function(tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case 'colgroup':
if(current_node().name !== 'html') {
stack.pop();
insertion_mode = InTable;
}
return;
case 'col':
return;
default:
end('colgroup');
end(tagName);
return;
}
}
};
var InTableBody = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case 'tr':
clear_stack_to_table_body_context();
insert_html_element_for(tagName, attrs);
insertion_mode = InRow;
return;
case 'th':
case 'td':
start('tr');
start(tagName, attrs, unary);
return;
case "caption":
case "col":
case "colgroup":
case "tbody":
case "tfoot":
case "thead":
if (in_table_scope('tbody') || in_table_scope('thead') || in_table_scope('tfoot')) {
clear_stack_to_table_body_context();
end(current_node().name);
start(tagName, attrs, unary);
}
return;
}
},
insertion_mode_end: function(tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case 'tbody':
case 'tfoot':
case 'thead':
if (in_table_scope(tagName)) {
clear_stack_to_table_body_context();
stack.pop();
insertion_mode = InTable;
}
return;
case 'table':
if (in_table_scope('tbody') || in_table_scope('thead') || in_table_scope('tfoot')) {
clear_stack_to_table_body_context();
end(current_node().name);
end(tagName);
}
return;
case "body":
case "caption":
case "col":
case "colgroup":
case "html":
case "td":
case "th":
case "tr":
return;
default:
InTable.insertion_mode_end(tagName);
return;
}
}
};
var InRow = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case 'th':
case 'td':
clear_stack_to_table_row_context();
var node = insert_html_element_for(tagName, attrs);
insertion_mode = InCell;
active_elements.push(node);
return;
case "caption":
case "col":
case "colgroup":
case "tbody":
case "tfoot":
case "thead":
case "tr":
end('tr');
start(tagName, attrs, unary);
return;
default:
InTable.insertion_mode_start(tagName, attrs, unary);
return;
}
},
insertion_mode_end: function(tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case 'tr':
if (in_table_scope(tagName)) {
clear_stack_to_table_row_context();
stack.pop();
insertion_mode = InTableBody;
}
return;
case 'table':
end('tr');
// this line was in the original source but attrs/unary are not defined
// so not sure what to do with it. how was this working?
// start(tagName, attrs, unary);
return;
case "tbody":
case "tfoot":
case "thead":
if (in_table_scope(tagName)) {
end('tr');
end(tagName);
}
return;
case "body":
case "caption":
case "col":
case "colgroup":
case "html":
case "td":
case "th":
return;
default:
InTable.insertion_mode_end(tagName);
return;
}
}
};
var InCell = {
insertion_mode_start: function (tagName, attrs, unary) {
tagName = tagName.toLowerCase();
switch(tagName) {
case "caption":
case "col":
case "colgroup":
case "tbody":
case "td":
case "tfoot":
case "th":
case "thead":
case "tr":
if (in_table_scope('td') || in_table_scope('th')) {
close_the_cell();
start(tagName, attrs, unary);
}
return;
default:
InBody.insertion_mode_start(tagName, attrs, unary);
return;
}
},
insertion_mode_end: function(tagName) {
if(typeof(tagName) === undefined) {
return;
}
tagName = tagName.toLowerCase();
switch(tagName) {
case "td":
case "th":
if (in_table_scope(tagName)) {
generate_implied_end_tags();
if (current_node().name !== tagName) {
return;
}
var node;
do {
node = stack.pop();
} while(node.name !== tagName);
clear_active_elements_to_last_marker();
insertion_mode = InRow;
}
return;
case "body":
case "caption":
case "col":
case "colgroup":
case "html":
return;
case "table":
case "tbody":
case "tfoot":
case "thead":
case "tr":
if (in_table_scope(tagName)) {
close_the_cell();
end(tagName);
}
return;
default:
InBody.insertion_mode_end(tagName);
return;
}
}
};
return {
purify: function(text, dontFormat, catchErrors) {
init();
insertion_mode = InBody;
noFormatting = !!dontFormat;
// if we hit a parse error, just take whatever HTML we had
try {
HTMLParser(text, {
start: start,
end: end,
chars: chars
});
} catch (e) {
if (!catchErrors) throw e;
}
return root.innerHTML().replace(/^\s+/, '');
}
};
})();
exports = module.exports = XHTMLPurifier;