forked from YahooArchive/html-purify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml-purify.js
7376 lines (6699 loc) · 328 KB
/
html-purify.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
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Purifier = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/*
Copyright (c) 2015, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License.
See the accompanying LICENSE file for terms.
Authors: Nera Liu <neraliu@yahoo-inc.com>
Albert Yu <albertyu@yahoo-inc.com>
Adonis Fung <adon@yahoo-inc.com>
*/
/*jshint -W030 */
(function() {
"use strict";
var stateMachine = require('./html5-state-machine.js'),
htmlState = stateMachine.State,
reInputPreProcessing = /(?:\r\n?|[\x01-\x08\x0B\x0E-\x1F\x7F-\x9F\uFDD0-\uFDEF\uFFFE\uFFFF]|[\uD83F\uD87F\uD8BF\uD8FF\uD93F\uD97F\uD9BF\uD9FF\uDA3F\uDA3F\uDA7F\uDABF\uDAFF\uDB3F\uDB7F\uDBBF\uDBFF][\uDFFE\uDFFF])/g;
/**
* @class FastParser
* @constructor FastParser
*/
function FastParser(config) {
var self = this, k;
// deep copy config to this.config
self.config = {};
if (config) {
for (k in config) {
self.config[k] = config[k];
}
}
config = self.config;
// config enabled by default - no conversion needed
// config.enableInputPreProcessing = (config.enableInputPreProcessing !== false);
self.listeners = {};
self.reset();
}
/**
* @function FastParser#reset
*
* @description
* Reset all internal states, as if being created with the new operator
*/
FastParser.prototype.reset = function () {
var self = this;
self.state = stateMachine.State.STATE_DATA; /* Save the current status */
self.tags = ['', '']; /* Save the current tag name */
self.tagIdx = 0;
self.attrName = ''; /* Save the current attribute name */
self.attributeValue = null; /* Save the current attribute value */
self.input = '';
self.inputLen = 0;
return self;
};
/**
* @function FastParser#on
*
* @param {string} eventType - the event type
* @param {function} listener - the event listener
* @returns this
*
* @description
* <p>register the given event listener to the given eventType</p>
*
*/
FastParser.prototype.on = function (eventType, listener) {
var l = this.listeners[eventType];
if (listener) {
if (l) {
l.push(listener);
} else {
this.listeners[eventType] = [listener];
}
}
return this;
};
/**
* @function FastParser#once
*
* @param {string} eventType - the event type (e.g., preWalk, reWalk, postWalk, ...)
* @param {function} listener - the event listener
* @returns this
*
* @description
* <p>register the given event listener to the given eventType, for which it will be fired only once</p>
*
*/
FastParser.prototype.once = function(eventType, listener) {
var self = this, onceListener;
if (listener) {
onceListener = function () {
self.off(eventType, onceListener);
listener.apply(self, arguments);
};
return this.on(eventType, onceListener);
}
return this;
};
/**
* @function FastParser#off
*
* @param {string} eventType - the event type (e.g., preWalk, reWalk, postWalk, ...)
* @param {function} listener - the event listener
* @returns this
*
* @description
* <p>remove the listener from being fired when the eventType happen</p>
*
*/
FastParser.prototype.off = function (eventType, listener) {
if (listener) {
var i, len, listeners = this.listeners[eventType];
if (listeners) {
for (i = 0; listeners[i]; i++) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
break;
}
}
}
}
return this;
};
/**
* @function FastParser#emit
*
* @param {string} eventType - the event type (e.g., preWalk, reWalk, postWalk, ...)
* @returns this
*
* @description
* <p>fire those listeners correspoding to the given eventType</p>
*
*/
FastParser.prototype.emit = function (listeners, args) {
if (listeners) {
var i = -1, len;
if ((len = listeners.length)) {
while (++i < len) {
listeners[i].apply(this, args || []);
}
}
}
return this;
};
/*
* @function FastParser#walk
*
* @param {integer} i - the position of the current character in the input stream
* @param {string} input - the input stream
* @returns {integer} the new location of the current character.
*
*/
FastParser.prototype.walk = function(i, input, endsWithEOF) {
var ch = input[i],
symbol = this.lookupChar(ch),
extraLogic = stateMachine.lookupAltLogicFromSymbol[symbol][this.state],
reconsume = stateMachine.lookupReconsumeFromSymbol[symbol][this.state];
/* Set state based on the current head pointer symbol */
this.state = stateMachine.lookupStateFromSymbol[symbol][this.state];
/* See if there is any extra logic required for this state transition */
switch (extraLogic) {
case 1: this.createStartTag(ch); break;
case 2: this.createEndTag(ch); break;
case 3: this.appendTagName(ch); break;
case 4: this.resetEndTag(ch); break;
case 6: /* match end tag token with start tag token's tag name */
if(this.tags[0].toLowerCase() === this.tags[1].toLowerCase()) {
reconsume = 0; /* see 12.2.4.13 - switch state for the following case, otherwise, reconsume. */
this.matchEndTagWithStartTag(symbol);
}
break;
case 8: this.matchEscapedScriptTag(ch); break;
case 11: this.processTagName(ch); break;
case 12: this.createAttributeNameAndValueTag(ch); break;
case 13: this.appendAttributeNameTag(ch); break;
case 14: this.appendAttributeValueTag(ch); break;
}
if (reconsume) { /* reconsume the character */
this.listeners.reWalk && this.emit(this.listeners.reWalk, [this.state, i, endsWithEOF]);
return this.walk(i, input);
}
return i;
};
FastParser.prototype.createStartTag = function (ch) {
this.tagIdx = 0;
this.tags[0] = ch;
};
FastParser.prototype.createEndTag = function (ch) {
this.tagIdx = 1;
this.tags[1] = ch;
};
FastParser.prototype.appendTagName = function (ch) {
this.tags[this.tagIdx] += ch;
};
FastParser.prototype.resetEndTag = function (ch) {
this.tagIdx = 1;
this.tags[1] = '';
};
FastParser.prototype.matchEndTagWithStartTag = function (symbol) {
/* Extra Logic #6 :
WHITESPACE: If the current end tag token is an appropriate end tag token, then switch to the before attribute name state.
Otherwise, treat it as per the 'anything else' entry below.
SOLIDUS (/): If the current end tag token is an appropriate end tag token, then switch to the this.closing start tag state.
Otherwise, treat it as per the 'anything else' entry below.
GREATER-THAN SIGN (>): If the current end tag token is an appropriate end tag token, then switch to the data state and emit the current tag token.
Otherwise, treat it as per the 'anything else' entry below.
*/
this.tags[0] = '';
this.tags[1] = '';
switch (symbol) {
case stateMachine.Symbol.SPACE: /** Whitespaces */
this.state = stateMachine.State.STATE_BEFORE_ATTRIBUTE_NAME;
return ;
case stateMachine.Symbol.SOLIDUS: /** [/] */
this.state = stateMachine.State.STATE_SELF_CLOSING_START_TAG;
return ;
case stateMachine.Symbol.GREATER: /** [>] */
this.state = stateMachine.State.STATE_DATA;
return ;
}
};
FastParser.prototype.matchEscapedScriptTag = function (ch) {
/* switch to the script data double escaped state if we see <script> inside <script><!-- */
if ( this.tags[1].toLowerCase() === 'script') {
this.state = stateMachine.State.STATE_SCRIPT_DATA_DOUBLE_ESCAPED;
}
};
FastParser.prototype.processTagName = function (ch) {
/* context transition when seeing <sometag> and switch to Script / Rawtext / RCdata / ... */
switch (this.tags[0].toLowerCase()) {
// TODO - give exceptions when non-HTML namespace is used.
// case 'math':
// case 'svg':
// break;
case 'script':
this.state = stateMachine.State.STATE_SCRIPT_DATA;
break;
case 'noframes':
case 'style':
case 'xmp':
case 'iframe':
case 'noembed':
case 'noscript':
this.state = stateMachine.State.STATE_RAWTEXT;
break;
case 'textarea':
case 'title':
this.state = stateMachine.State.STATE_RCDATA;
break;
case 'plaintext':
this.state = stateMachine.State.STATE_PLAINTEXT;
break;
}
};
FastParser.prototype.createAttributeNameAndValueTag = function (ch) {
/* new attribute name and value token */
this.attributeValue = null;
this.attrName = ch;
};
FastParser.prototype.appendAttributeNameTag = function (ch) {
/* append to attribute name token */
this.attrName += ch;
};
FastParser.prototype.appendAttributeValueTag = function(ch) {
this.attributeValue = this.attributeValue === null ? ch : this.attributeValue + ch;
};
/**
* @function FastParser#lookupChar
*
* @param {char} ch - The character.
* @returns {integer} The integer to represent the type of input character.
*
* @description
* <p>Map the character to character type.
* e.g. [A-z] = type 17 (Letter [A-z])</p>
*
*/
FastParser.prototype.lookupChar = function(ch) {
var o = ch.charCodeAt(0);
if ( o > 122 ) { return 12; }
return stateMachine.lookupSymbolFromChar[o];
};
/**
* @function FastParser#contextualize
*
* @param {string} input - the input stream
*/
FastParser.prototype.contextualize = function(input, endsWithEOF) {
var self = this, listeners = self.listeners, i = -1, lastState;
// Perform input stream preprocessing
// Reference: https://html.spec.whatwg.org/multipage/syntax.html#preprocessing-the-input-stream
self.input = self.config.enableInputPreProcessing ? input.replace(reInputPreProcessing, function(m){return m[0] === '\r' ? '\n' : '\uFFFD';}) : input;
self.inputLen = self.input.length;
while (++i < self.inputLen) {
lastState = self.state;
// TODO: endsWithEOF handling
listeners.preWalk && this.emit(listeners.preWalk, [lastState, i, endsWithEOF]);
// these functions are not supposed to alter the input
self.beforeWalk(i, this.input);
self.walk(i, this.input, endsWithEOF);
self.afterWalk(i, this.input);
// TODO: endsWithEOF handling
listeners.postWalk && this.emit(listeners.postWalk, [lastState, self.state, i, endsWithEOF]);
}
};
/**
* @function FastParser#beforeWalk
*
* @param {integer} i - the location of the head pointer.
* @param {string} input - the input stream
*
* @description
* Interface function for subclass to implement logics before parsing the character.
*
*/
FastParser.prototype.beforeWalk = function (i, input) {};
/**
* @function FastParser#afterWalk
*
* @param {integer} i - the location of the head pointer.
* @param {string} input - the input stream
*
* @description
* Interface function for subclass to implement logics after parsing the character.
*
*/
FastParser.prototype.afterWalk = function (i, input) {};
/**
* @function FastParser#getStartTagName
* @depreciated Replace it by getCurrentTagIndex and getCurrentTag
*
* @returns {string} The current handling start tag name
*
*/
FastParser.prototype.getStartTagName = function() {
return this.tags[0] !== undefined? this.tags[0].toLowerCase() : undefined;
};
/**
* @function FastParser#getCurrentTagIndex
*
* @returns {integer} The current handling tag Idx
*
*/
FastParser.prototype.getCurrentTagIndex = function() {
return this.tagIdx;
};
/**
* @function FastParser#getCurrentTag
*
* @params {integer} The tag Idx
*
* @returns {string} The current tag name indexed by tag Idx
*
*/
FastParser.prototype.getCurrentTag = function(tagIdx) {
return tagIdx === 0 || tagIdx === 1? (this.tags[tagIdx] !== undefined? this.tags[tagIdx].toLowerCase():undefined) : undefined;
};
/**
* @function FastParser#getAttributeName
*
* @returns {string} The current handling attribute name.
*
* @description
* Get the current handling attribute name of HTML tag.
*
*/
FastParser.prototype.getAttributeName = function() {
return this.attrName.toLowerCase();
};
/**
* @function FastParser#getAttributeValue
*
* @returns {string} The current handling attribute name's value.
*
* @description
* Get the current handling attribute name's value of HTML tag.
*
*/
FastParser.prototype.getAttributeValue = function(htmlDecoded) {
// TODO: html decode the attribute value
return this.attributeValue;
};
/**
* @module Parser
*/
function Parser (config, listeners) {
var self = this, k;
config = config || {};
// config defaulted to false
config.enableCanonicalization = (config.enableCanonicalization === true);
config.enableVoidingIEConditionalComments = (config.enableVoidingIEConditionalComments === true);
// config defaulted to true
config.enableStateTracking = (config.enableStateTracking !== false);
// super constructor, reset() is called here
FastParser.call(self, config);
// deep copy the provided listeners, if any
if (typeof listeners === 'object') {
for (k in listeners) {
self.listeners[k] = listeners[k].slice();
}
return;
}
// ### DO NOT CHANGE THE ORDER OF THE FOLLOWING COMPONENTS ###
// fix parse errors before they're encountered in walk()
config.enableCanonicalization && self.on('preWalk', Canonicalize).on('reWalk', Canonicalize);
// enable IE conditional comments
config.enableVoidingIEConditionalComments && self.on('preWalk', DisableIEConditionalComments);
// TODO: rewrite IE <comment> tags
// TODO: When a start tag token is emitted with its self-closing flag set, if the flag is not acknowledged when it is processed by the tree construction stage, that is a parse error.
// TODO: When an end tag token is emitted with attributes, that is a parse error.
// TODO: When an end tag token is emitted with its self-closing flag set, that is a parse error.
// for bookkeeping the processed inputs and states
if (config.enableStateTracking) {
self.on('postWalk', function (lastState, state, i, endsWithEOF) {
this.buffer.push(this.input[i]);
this.states.push(state);
this.symbol.push(this._getSymbol(i));
}).on('reWalk', self.setCurrentState);
}
}
// as in https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype
Parser.prototype = Object.create(FastParser.prototype);
Parser.prototype.constructor = Parser;
/**
* @function Parser#reset
*
* @description
* Reset all internal states, as if being created with the new operator
*/
Parser.prototype.reset = function () {
var self = this;
FastParser.prototype.reset.call(self);
if (self.config.enableStateTracking) {
self.states = [this.state];
self.buffer = [];
self.symbol = [];
}
// delete any pending corrections (e.g., to close bogus comment)
delete self.listeners.preCanonicalize;
return self;
};
/**
* @function Parser._getSymbol
* @param {integer} i - the index of input stream
*
* @description
* Get the html symbol mapping for the character located in the given index of input stream
*/
Parser.prototype._getSymbol = function (i) {
return i < this.inputLen ? this.lookupChar(this.input[i]) : -1;
};
/**
* @function Parser._getNextState
* @param {integer} state - the current state
* @param {integer} i - the index of input stream
* @returns {integer} the potential state about to transition into, given the current state and an index of input stream
*
* @description
* Get the potential html state about to transition into
*/
Parser.prototype._getNextState = function (state, i, endsWithEOF) {
return i < this.inputLen ? stateMachine.lookupStateFromSymbol[this._getSymbol(i)][state] : -1;
};
/**
* @function Parser._convertString2Array
*
* @description
* Convert the immutable this.input to array type for Strict Context Parser processing (lazy conversion).
*
*/
Parser.prototype._convertString2Array = function () {
if (typeof this.input === "string") this.input = this.input.split('');
};
/**
* @function Parser.fork
* @returns {object} a new parser with all internal states inherited
*
* @description
* create a new parser with all internal states inherited
*/
Parser.prototype.fork = function() {
var parser = new this.constructor(this.config, this.listeners);
parser.state = this.state;
parser.tags = this.tags.slice();
parser.tagIdx = this.tagIdx;
parser.attrName = this.attrName;
parser.attributeValue = this.attributeValue;
if (this.config.enableStateTracking) {
parser.buffer = this.buffer.slice();
parser.states = this.states.slice();
parser.symbol = this.symbol.slice();
}
return parser;
};
/**
* @function Parser#contextualize
* @param {string} input - the input stream
*
* @description
* It is the same as the original contextualize() except that this method returns the internal input stream.
*/
Parser.prototype.contextualize = function (input, endsWithEOF) {
FastParser.prototype.contextualize.call(this, input, endsWithEOF);
return this.getModifiedInput();
};
/**
* @function Parser#getModifiedInput
*
* @description
* Get the modified input due to Strict Context Parser processing.
*
*/
Parser.prototype.getModifiedInput = function() {
// TODO: it is not defensive enough, should use Array.isArray, but need polyfill
return (typeof this.input === "string")? this.input:this.input.join('');
};
/**
* @function Parser#setCurrentState
*
* @param {integer} state - The state of HTML5 page.
*
* @description
* Set the current state of the HTML5 Context Parser.
*
*/
Parser.prototype.setCurrentState = function(state) {
this.states.pop();
this.states.push(this.state = state);
return this;
};
/**
* @function Parser#getCurrentState
*
* @returns {integer} The last state of the HTML5 Context Parser.
*
* @description
* Get the last state of HTML5 Context Parser.
*
*/
Parser.prototype.getCurrentState = function() {
return this.state;
};
/**
* @function Parser#getStates
*
* @returns {Array} An array of states.
*
* @description
* Get the states of the HTML5 page
*
*/
Parser.prototype.getStates = function() {
return this.states.slice();
};
/**
* @function Parser#setInitState
*
* @param {integer} state - The initial state of the HTML5 Context Parser.
*
* @description
* Set the init state of HTML5 Context Parser.
*
*/
Parser.prototype.setInitState = function(state) {
this.states = [state];
return this;
};
/**
* @function Parser#getInitState
*
* @returns {integer} The initial state of the HTML5 Context Parser.
*
* @description
* Get the init state of HTML5 Context Parser.
*
*/
Parser.prototype.getInitState = function() {
return this.states[0];
};
/**
* @function Parser#getLastState
*
* @returns {integer} The last state of the HTML5 Context Parser.
*
* @description
* Get the last state of HTML5 Context Parser.
*
*/
Parser.prototype.getLastState = function() {
// * undefined if length = 0
return this.states[ this.states.length - 1 ];
};
/**
* The implementation of Strict Context Parser functions
*
* - ConvertBogusCommentToComment
* - PreCanonicalizeConvertBogusCommentEndTag
* - Canonicalize
* - DisableIEConditionalComments
*
*/
function ConvertBogusCommentToComment(i) {
// for lazy conversion
this._convertString2Array();
// convert !--. i.e., from <* to <!--*
this.input.splice(i, 0, '!', '-', '-');
this.inputLen += 3;
// convert the next > to -->
this.on('preCanonicalize', PreCanonicalizeConvertBogusCommentEndTag);
}
function PreCanonicalizeConvertBogusCommentEndTag(state, i, endsWithEOF) {
if (this.input[i] === '>') {
// remove itself from the listener list
this.off('preCanonicalize', PreCanonicalizeConvertBogusCommentEndTag);
// for lazy conversion
this._convertString2Array();
// convert [>] to [-]->
this.input.splice(i, 0, '-', '-');
this.inputLen += 2;
this.emit(this.listeners.bogusCommentCoverted, [state, i, endsWithEOF]);
}
}
// those doctype states (52-67) are initially treated as bogus comment state, but they are further converted to comment state
// Canonicalize() will create no more bogus comment state except the fake (context-parser treats <!doctype as bogus) one hardcoded as <!doctype html> that has no NULL inside
var statesRequiringNullReplacement = [
// 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
/*0*/ 0, 0, 0, 1, 0, 1, 1, 1, 0, 0,
/*1*/ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*2*/ 0, 0, 1, 1, 1, 0, 0, 0, 0, 1,
/*3*/ 1, 1, 0, 0, 1, 1, 1, 1, 1, 1,
/*4*/ 1, 0, 0, 0, 1, 0, 1, 1, 1, 1,
/*5*/ 1, 1
];
// \uFFFD replacement is not required by the spec for DATA state
statesRequiringNullReplacement[htmlState.STATE_DATA] = 1;
function Canonicalize(state, i, endsWithEOF) {
this.emit(this.listeners.preCanonicalize, [state, i, endsWithEOF]);
var reCanonicalizeNeeded = true,
chr = this.input[i], nextChr = this.input[i+1],
potentialState = this._getNextState(state, i, endsWithEOF),
nextPotentialState = this._getNextState(potentialState, i + 1, endsWithEOF);
// console.log(i, state, potentialState, nextPotentialState, this.input.slice(i).join(''));
// batch replacement of NULL with \uFFFD would violate the spec
// - for example, NULL is untouched in CDATA section state
if (chr === '\x00' && statesRequiringNullReplacement[state]) {
// for lazy conversion
this._convertString2Array();
this.input[i] = '\uFFFD';
}
// encode < into < for [<]* (* is non-alpha) in STATE_DATA, [<]% and [<]! in STATE_RCDATA and STATE_RAWTEXT
else if ((potentialState === htmlState.STATE_TAG_OPEN && nextPotentialState === htmlState.STATE_DATA) || // [<]*, where * is non-alpha
((state === htmlState.STATE_RCDATA || state === htmlState.STATE_RAWTEXT) && // in STATE_RCDATA and STATE_RAWTEXT
chr === '<' && (nextChr === '%' || nextChr === '!'))) { // [<]% or [<]!
// for lazy conversion
this._convertString2Array();
// [<]*, [<]%, [<]!
this.input.splice(i, 1, '&', 'l', 't', ';');
this.inputLen += 3;
}
// enforce <!doctype html>
// + convert bogus comment or unknown doctype to the standard html comment
else if (potentialState === htmlState.STATE_MARKUP_DECLARATION_OPEN) { // <[!]***
reCanonicalizeNeeded = false;
// for lazy conversion
this._convertString2Array();
// context-parser treats the doctype and [CDATA[ as resulting into STATE_BOGUS_COMMENT
// so, we need our algorithm here to extract and check the next 7 characters
var commentKey = this.input.slice(i + 1, i + 8).join('');
// enforce <!doctype html>
if (commentKey.toLowerCase() === 'doctype') { // <![d]octype
// extract 6 chars immediately after <![d]octype and check if it's equal to ' html>'
if (this.input.slice(i + 8, i + 14).join('').toLowerCase() !== ' html>') {
// replace <[!]doctype xxxx> with <[!]--!doctype xxxx--><doctype html>
ConvertBogusCommentToComment.call(this, i);
this.once('bogusCommentCoverted', function (state, i) {
[].splice.apply(this.input, [i + 3, 0].concat('<!doctype html>'.split('')));
this.inputLen += 15;
});
reCanonicalizeNeeded = true;
}
}
// do not touch <![CDATA[ and <[!]--
else if (commentKey === '[CDATA[' ||
(nextChr === '-' && this.input[i+2] === '-')) {
// noop
}
// ends up in bogus comment
else {
// replace <[!]*** with <[!]--***
// will replace the next > to -->
ConvertBogusCommentToComment.call(this, i);
reCanonicalizeNeeded = true;
}
}
// convert bogus comment to the standard html comment
else if ((state === htmlState.STATE_TAG_OPEN &&
potentialState === htmlState.STATE_BOGUS_COMMENT) || // <[?] only from STATE_TAG_OPEN
(potentialState === htmlState.STATE_END_TAG_OPEN && // <[/]* or <[/]> from STATE_END_TAG_OPEN
nextPotentialState !== htmlState.STATE_TAG_NAME &&
nextPotentialState !== -1)) { // TODO: double check if there're any other cases requiring -1 check
// replace <? and </* respectively with <!--? and <!--/*
// will replace the next > to -->
ConvertBogusCommentToComment.call(this, i);
}
// remove the unnecessary SOLIDUS
else if (potentialState === htmlState.STATE_SELF_CLOSING_START_TAG && // <***[/]*
nextPotentialState === htmlState.STATE_BEFORE_ATTRIBUTE_NAME) { // this.input[i+1] is ANYTHING_ELSE (i.e., not EOF nor >)
// if ([htmlState.STATE_TAG_NAME, // <a[/]* replaced with <a[ ]*
// /* following is unknown to CP
// htmlState.STATE_RCDATA_END_TAG_NAME,
// htmlState.STATE_RAWTEXT_END_TAG_NAME,
// htmlState.STATE_SCRIPT_DATA_END_TAG_NAME,
// htmlState.STATE_SCRIPT_DATA_ESCAPED_END_TAG_NAME,
// */
// htmlState.STATE_BEFORE_ATTRIBUTE_NAME, // <a [/]* replaced with <a [ ]*
// htmlState.STATE_AFTER_ATTRIBUTE_VALUE_QUOTED].indexOf(state) !== -1) // <a abc=""[/]* replaced with <a abc=""[ ]*
// for lazy conversion
this._convertString2Array();
this.input[i] = ' ';
// given this.input[i] was '/', nextPotentialState was htmlState.STATE_BEFORE_ATTRIBUTE_NAME
// given this.input[i] is now ' ', nextPotentialState becomes STATE_BEFORE_ATTRIBUTE_NAME if current state is STATE_ATTRIBUTE_NAME or STATE_AFTER_ATTRIBUTE_NAME
// to preserve state, remove future EQUAL SIGNs (=)s to force STATE_AFTER_ATTRIBUTE_NAME behave as if it is STATE_BEFORE_ATTRIBUTE_NAME
// this is okay since EQUAL SIGNs (=)s will be stripped anyway in the STATE_BEFORE_ATTRIBUTE_NAME cleanup handling
if (state === htmlState.STATE_ATTRIBUTE_NAME || // <a abc[/]=abc replaced with <a abc[ ]*
state === htmlState.STATE_AFTER_ATTRIBUTE_NAME) { // <a abc [/]=abc replaced with <a abc [ ]*
for (var j = i + 1; j < this.inputLen && this.input[j] === '='; j++) {
this.input.splice(j, 1);
this.inputLen--;
}
}
}
// remove unnecessary equal signs, hence <input checked[=]> become <input checked[>], or <input checked [=]> become <input checked [>]
else if (potentialState === htmlState.STATE_BEFORE_ATTRIBUTE_VALUE && // only from STATE_ATTRIBUTE_NAME or STATE_AFTER_ATTRIBUTE_NAME
nextPotentialState === htmlState.STATE_DATA) { // <a abc[=]> or <a abc [=]>
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1);
this.inputLen--;
}
// insert a space for <a abc="***["]* or <a abc='***[']* after quoted attribute value (i.e., <a abc="***["] * or <a abc='***['] *)
else if (potentialState === htmlState.STATE_AFTER_ATTRIBUTE_VALUE_QUOTED && // <a abc=""[*] where * is not SPACE (\t,\n,\f,' ')
nextPotentialState === htmlState.STATE_BEFORE_ATTRIBUTE_NAME &&
this._getSymbol(i + 1) !== stateMachine.Symbol.SPACE) {
// for lazy conversion
this._convertString2Array();
this.input.splice(i + 1, 0, ' ');
this.inputLen++;
}
// else here means no special pattern was found requiring rewriting
else {
reCanonicalizeNeeded = false;
}
// remove " ' < = from being treated as part of attribute name (not as the spec recommends though)
switch (potentialState) {
case htmlState.STATE_BEFORE_ATTRIBUTE_NAME: // remove ambigious symbols in <a [*]href where * is ", ', <, or =
if (nextChr === "=") {
// for lazy conversion
this._convertString2Array();
this.input.splice(i + 1, 1);
this.inputLen--;
reCanonicalizeNeeded = true;
break;
}
/* falls through */
case htmlState.STATE_ATTRIBUTE_NAME: // remove ambigious symbols in <a href[*] where * is ", ', or <
case htmlState.STATE_AFTER_ATTRIBUTE_NAME: // remove ambigious symbols in <a href [*] where * is ", ', or <
if (nextChr === '"' || nextChr === "'" || nextChr === '<') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i + 1, 1);
this.inputLen--;
reCanonicalizeNeeded = true;
}
break;
}
if (reCanonicalizeNeeded) {
return Canonicalize.call(this, state, i, endsWithEOF);
}
switch (state) {
// escape " ' < = ` to avoid raising parse errors for unquoted value
case htmlState.STATE_ATTRIBUTE_VALUE_UNQUOTED:
if (chr === '"') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1, '&', 'q', 'u', 'o', 't', ';');
this.inputLen += 5;
break;
} else if (chr === "'") {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1, '&', '#', '3', '9', ';');
this.inputLen += 4;
break;
}
/* falls through */
case htmlState.STATE_BEFORE_ATTRIBUTE_VALUE: // treat < = ` as if they are in STATE_ATTRIBUTE_VALUE_UNQUOTED
if (chr === '<') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1, '&', 'l', 't', ';');
this.inputLen += 3;
} else if (chr === '=') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1, '&', '#', '6', '1', ';');
this.inputLen += 4;
} else if (chr === '`') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1, '&', '#', '9', '6', ';');
this.inputLen += 4;
}
break;
// add hyphens to complete <!----> to avoid raising parsing errors
// replace <!--[>] with <!--[-]->
case htmlState.STATE_COMMENT_START:
if (chr === '>') { // <!--[>]
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 0, '-', '-');
this.inputLen += 2;
// reCanonicalizeNeeded = true; // not need due to no where to treat its potential states
}
break;
// replace <!---[>] with <!---[-]>
case htmlState.STATE_COMMENT_START_DASH:
if (chr === '>') { // <!---[>]
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 0, '-');
this.inputLen++;
// reCanonicalizeNeeded = true; // not need due to no where to treat its potential states
}
break;
// replace --[!]> with --[>]
case htmlState.STATE_COMMENT_END:
if (chr === '!' && nextChr === '>') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i, 1);
this.inputLen--;
// reCanonicalizeNeeded = true; // not need due to no where to treat its potential states
}
// if (chr === '-'), ignored this parse error. TODO: consider stripping n-2 hyphens for ------>
break;
}
if (reCanonicalizeNeeded) {
return Canonicalize.call(this, state, i, endsWithEOF);
}
}
// remove IE conditional comments
function DisableIEConditionalComments(state, i){
if (state === htmlState.STATE_COMMENT && this.input[i] === ']' && this.input[i+1] === '>') {
// for lazy conversion
this._convertString2Array();
this.input.splice(i + 1, 0, ' ');
this.inputLen++;
}
}
module.exports = {
Parser: Parser,
FastParser: FastParser,
StateMachine: stateMachine
};
})();
},{"./html5-state-machine.js":2}],2:[function(require,module,exports){
/*
Copyright (c) 2015, Yahoo! Inc. All rights reserved.
Copyrights licensed under the New BSD License.
See the accompanying LICENSE file for terms.
Authors: Nera Liu <neraliu@yahoo-inc.com>
Albert Yu <albertyu@yahoo-inc.com>
Adonis Fung <adon@yahoo-inc.com>
*/
var StateMachine = {};
// /* Character ASCII map */
// StateMachine.Char = {};
// StateMachine.Char.TAB = 0x09;
// StateMachine.Char.LF = 0x0A;