This repository was archived by the owner on Mar 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHTMLParser.js
More file actions
652 lines (585 loc) · 26.3 KB
/
HTMLParser.js
File metadata and controls
652 lines (585 loc) · 26.3 KB
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
// ### HTML Parsing
//
// The HTML token stream parser object has references to the stream,
// as well as a DOM builder that is used to construct the DOM while we
// run through the token stream.
module.exports = (function(){
"use strict";
var ParseError = require("./ParseError");
var CSSParser = require("./CSSParser");
var voidHtmlElements = require("./voidHtmlElements");
// ### Character Entity Parsing
//
// We currently only parse the most common named character entities.
var CHARACTER_ENTITY_REFS = {
lt: "<",
gt: ">",
apos: "'",
quot: '"',
amp: "&"
};
// HTML attribute parsing rules are based on
// http://www.w3.org/TR/2011/WD-html5-20110525/elements.html#attr-data
// -> ref http://www.w3.org/TR/2011/WD-html5-20110525/infrastructure.html#xml-compatible
// -> ref http://www.w3.org/TR/REC-xml/#NT-NameChar
// note: this lacks the final \\u10000-\\uEFFFF in the startchar set, because JavaScript
// cannot cope with unciode characters with points over 0xFFFF.
var attributeNameStartChar = "A-Za-z_\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD";
var nameStartChar = new RegExp("[" + attributeNameStartChar + "]");
var attributeNameChar = attributeNameStartChar + "0-9\\-\\.\\u00B7\\u0300-\\u036F\\u203F-\\u2040:";
var nameChar = new RegExp("[" + attributeNameChar + "]");
//Define a property checker for https page
var checkMixedContent = require("./checkMixedContent").mixedContent;
//Define activeContent with tag-attribute pairs
function isActiveContent (tagName, attrName) {
if (attrName === "href") {
return ["link"].indexOf(tagName) > -1;
}
if (attrName === "src") {
return ["script", "iframe"].indexOf(tagName) > -1;
}
if (attrName === "data") {
return ["object"].indexOf(tagName) > -1;
}
return false;
}
// the current active omittable html Element
var activeTagNode = false;
// the parent html Element for optional closing tag tags
var parentTagNode = false;
// 'foresee' if there is no more content in the parent element, and the
// parent element is not an a element in the case of activeTag is a p element.
function isNextTagParent(stream, parentTagName) {
return stream.findNext(/<\/([\w\-]+)\s*>/, 1) === parentTagName;
}
// 'foresee' if the next tag is a close tag
function isNextCloseTag(stream) {
return stream.findNext(/<\/([\w\-]+)\s*>/, 1);
}
// Check exception for Tag omission rules: for p tag, if there is no more
// content in the parent element and the parent element is not an a element.
function allowsOmmitedEndTag(parentTagName, tagName) {
if (tagName === "p") {
return ["a"].indexOf(parentTagName) > -1;
}
return false;
}
function HTMLParser(stream, domBuilder) {
this.warnings = [];
this.stream = stream;
this.domBuilder = domBuilder;
this.cssParser = new CSSParser(stream, domBuilder, this.warnings);
}
// `replaceEntityRefs()` will replace named character entity references
// (e.g. `<`) in the given text string and return the result. If an
// entity name is unrecognized, don't replace it at all. Writing HTML
// would be surprisingly painful without this forgiving behavior.
//
// This function does not currently replace numeric character entity
// references (e.g., ` `).
function replaceEntityRefs(text) {
return text.replace(/&([A-Za-z]+);/g, function(ref, name) {
name = name.toLowerCase();
if (name in CHARACTER_ENTITY_REFS)
return CHARACTER_ENTITY_REFS[name];
return ref;
});
};
HTMLParser.prototype = {
// since SVG requires a slightly different code path,
// we need to track whether we're in HTML or SVG mode.
parsingSVG: false,
// For SVG DOM elements, we need to know the SVG namespace.
svgNameSpace: "http://www.w3.org/2000/svg",
// HTML5 documents have a special doctype that we must use
html5Doctype: "<!DOCTYPE html>",
// Void HTML elements are the ones that don't need to have a closing tag.
voidHtmlElements: voidHtmlElements,
// Tag Omission Rules, based on the rules on optional tags as outlined in
// http://www.w3.org/TR/html5/syntax.html#optional-tags
// HTML elements that with omittable close tag
omittableCloseTagHtmlElements: ["p", "li", "thead", "tbody", "tfoot", "tr", "td", "th",
"dt", "dd", "rb", "rt", "rtc", "rp", "option", "optgroup" ],
// HTML elements that paired with omittable close tag list
omittableCloseTags: {
"p": ["address", "article", "aside", "blockquote", "dir", "div", "dl",
"fieldset", "footer", "form", "h1", "h2", "h3", "h4", "h5", "h6",
"header", "hgroup", "hr", "main", "nav", "ol", "p", "pre",
"section", "table", "ul"],
"th": ["th", "td"],
"td": ["th", "td"],
"tr": ["tr"],
"li": ["li"],
"dt": ["dt", "dd"],
"dd": ["dt", "dd"],
"rb": ["rb","rt","rtc","rp"],
"rt": ["rb","rt","rtc","rp"],
"rtc": ["rb","rtc","rp"],
"rp": ["rb","rt","rtc","rp"],
"optgroup": ["optgroup"],
"option": ["option", "optgroup"],
"thead": ["tbody", "tfoot"],
"tbody": ["tbody", "tfoot"],
"tfoot": ["tbody"]
},
// We keep a list of all valid HTML5 elements.
htmlElements: ["a", "abbr", "address", "area", "article", "aside",
"audio", "b", "base", "bdi", "bdo", "bgsound", "blink",
"blockquote", "body", "br", "button", "canvas", "caption",
"cite", "code", "col", "colgroup", "command", "datalist",
"dd", "del", "details", "dfn", "div", "dl", "dt", "em",
"embed", "fieldset", "figcaption", "figure", "footer",
"form", "frame", "frameset", "h1", "h2", "h3", "h4", "h5",
"h6", "head", "header", "hgroup", "hr", "html", "i",
"iframe", "img", "input", "ins", "kbd", "keygen", "label",
"legend", "li", "link", "main", "map", "mark", "marquee", "menu",
"meta", "meter", "nav", "nobr", "noscript", "object", "ol",
"optgroup", "option", "output", "p", "param", "pre",
"progress", "q", "rp", "rt", "ruby", "samp", "script",
"section", "select", "small", "source", "spacer", "span",
"strong", "style", "sub", "summary", "sup", "svg", "table",
"tbody", "td", "textarea", "tfoot", "th", "thead", "time",
"title", "tr", "track", "u", "ul", "var", "video", "wbr"],
// HTML5 allows SVG elements
svgElements: ["a", "altglyph", "altglyphdef", "altglyphitem", "animate",
"animatecolor", "animatemotion", "animatetransform", "circle",
"clippath", "color-profile", "cursor", "defs", "desc",
"ellipse", "feblend", "fecolormatrix", "fecomponenttransfer",
"fecomposite", "feconvolvematrix", "fediffuselighting",
"fedisplacementmap", "fedistantlight", "feflood", "fefunca",
"fefuncb", "fefuncg", "fefuncr", "fegaussianblur", "feimage",
"femerge", "femergenode", "femorphology", "feoffset",
"fepointlight", "fespecularlighting", "fespotlight",
"fetile", "feturbulence", "filter", "font", "font-face",
"font-face-format", "font-face-name", "font-face-src",
"font-face-uri", "foreignobject", "g", "glyph", "glyphref",
"hkern", "image", "line", "lineargradient", "marker", "mask",
"metadata", "missing-glyph", "mpath", "path", "pattern",
"polygon", "polyline", "radialgradient", "rect", "script",
"set", "stop", "style", "svg", "switch", "symbol", "text",
"textpath", "title", "tref", "tspan", "use", "view", "vkern"],
// HTML5 doesn't use namespaces, but actually it does. These are supported:
attributeNamespaces: ["xlink", "xml"],
// We also keep a list of HTML elements that are now obsolete, but
// may still be encountered in the wild on popular sites.
obsoleteHtmlElements: ["acronym", "applet", "basefont", "big", "center",
"dir", "font", "isindex", "listing", "noframes",
"plaintext", "s", "strike", "tt", "xmp"],
webComponentElements: ["template", "shadow", "content"],
// This is a helper function to determine whether a given string
// is a custom HTML element as per Custom Elements spec
// (see http://www.w3.org/TR/2013/WD-custom-elements-20130514/#terminology).
_isCustomElement: function(tagName) {
return tagName.search(/^[\w\d]+-[\w\d]+$/) > -1;
},
// This is a helper function to determine whether a given string
// is a legal HTML5 element tag.
_knownHTMLElement: function(tagName) {
return this.voidHtmlElements.indexOf(tagName) > -1 ||
this.htmlElements.indexOf(tagName) > -1 ||
this.obsoleteHtmlElements.indexOf(tagName) > -1 ||
this.webComponentElements.indexOf(tagName) > -1;
},
// This is a helper function to determine whether a given string
// is a legal SVG element tag.
_knownSVGElement: function(tagName) {
return this.svgElements.indexOf(tagName) > -1;
},
// This is a helper function to determine whether a given string
// is a void HTML element tag.
_knownVoidHTMLElement: function(tagName) {
return this.voidHtmlElements.indexOf(tagName) > -1;
},
// This is a helper function to determine whether a given string
// is a HTML element tag which can optional omit its close tag.
_knownOmittableCloseTagHtmlElement: function(tagName) {
return this.omittableCloseTagHtmlElements.indexOf(tagName) > -1;
},
// This is a helper function to determine whether a given string
// is in the list of ommittableCloseTags which enable an active tag omit its close tag.
_knownOmittableCloseTags: function(activeTagName, foundTagName) {
return this.omittableCloseTags[activeTagName].indexOf(foundTagName) > -1;
},
// This is a helper function to determine whether an attribute namespace
// is supposed in the HTML spec. Currently these are "xlink" and "xml".
_supportedAttributeNameSpace: function(ns) {
return this.attributeNamespaces.indexOf(ns) !== -1;
},
_isNextTagEnder: function (stream) {
var closeTag=isNextCloseTag(stream);
var currNode = this.domBuilder.currentNode ;
var n=1;
if (!closeTag) {return false;}
closeTag=closeTag.toLowerCase();
while (currNode && currNode.nodeName) {
var tagName = currNode.nodeName.toLowerCase();
if (closeTag == tagName) { return {'n': +n}; }
if (!this._knownOmittableCloseTagHtmlElement(tagName)) { return false;}
currNode=currNode.parentNode;
n++;
}
return false;
},
_popOmited: function(tag) {
var currNode = this.domBuilder.currentNode ;
var n=1;
tag=tag.toLowerCase();
while (currNode && currNode.nodeName) {
var tagName = currNode.nodeName.toLowerCase();
if (!this._knownOmittableCloseTagHtmlElement(tagName)) { return false;}
n++;
if (this._knownOmittableCloseTags(tagName,tag)) break;
//if (tag == tagName) { return {'n': +n}; }
currNode=currNode.parentNode;
}
this.domBuilder.popElements(n-1);
},
// #### The HTML Master Parse Function
//
// The HTML master parse function works the same as the CSS
// parser: it takes the token stream and will try to parse
// the content as a sequence of HTML elements.
//
// Any parse errors along the way will result in the code
// throwing a `ParseError`.
parse: function() {
// First we check to see if the beginning of our stream is
// an HTML5 doctype tag. We're currently quite strict and don't
// parse XHTML or other doctypes.
if (this.stream.match(this.html5Doctype, true, true))
this.domBuilder.fragment.node.parseInfo = {
doctype: {
start: 0,
end: this.stream.pos
}
};
// Next, we parse "tag soup", creating text nodes and diving into
// tags as we find them.
while (!this.stream.end()) {
if (this.stream.peek() == '<') {
this._buildTextNode();
this._parseStartTag();
} else
this.stream.next();
}
this._buildTextNode();
// At the end, it's possible we're left with an open tag, so
// we test for that.
if (this.domBuilder.currentNode != this.domBuilder.fragment.node)
throw new ParseError("UNCLOSED_TAG", this);
return {
warnings: (this.warnings.length > 0 ? this.warnings : false)
};
},
// This is a helper to build a DOM text node.
_buildTextNode: function() {
var token = this.stream.makeToken();
if (token) {
this.domBuilder.text(replaceEntityRefs(token.value), token.interval);
}
},
// #### HTML Tag Parsing
//
// This is the entry point for parsing the beginning of an HTML tag.
// It assumes the stream is on a `<` character.
_parseStartTag: function() {
if (this.stream.next() != '<')
throw new Error('assertion failed, expected to be on "<"');
if (this.stream.match('!--', true)) {
this.domBuilder.pushContext("text", this.stream.pos);
this._parseComment();
this.domBuilder.pushContext("html", this.stream.pos);
return;
}
this.stream.eat(/\//);
this.stream.eatWhile(/[\w\d-]/);
var token = this.stream.makeToken();
var tagName = token.value.slice(1).toLowerCase();
if (tagName === "svg")
this.parsingSVG = true;
// If the character after the `<` is a `/`, we're on a closing tag.
// We want to report useful errors about whether the tag is unexpected
// or doesn't match with the most recent opening tag.
if (tagName[0] == '/') {
activeTagNode = false;
var closeTagName = tagName.slice(1).toLowerCase();
if (closeTagName === "svg")
this.parsingSVG = false;
if (this._knownVoidHTMLElement(closeTagName))
throw new ParseError("CLOSE_TAG_FOR_VOID_ELEMENT", this,
closeTagName, token);
if (!this.domBuilder.currentNode.parseInfo)
throw new ParseError("UNEXPECTED_CLOSE_TAG", this, closeTagName,
token);
this.domBuilder.currentNode.parseInfo.closeTag = {
start: token.interval.start
};
var openTagName = this.domBuilder.currentNode.nodeName.toLowerCase();
if (closeTagName != openTagName)
throw new ParseError("MISMATCHED_CLOSE_TAG", this, openTagName,
closeTagName, token);
this._parseEndCloseTag();
}
else {
if (tagName) {
var badSVG = this.parsingSVG && !this._knownSVGElement(tagName);
var badHTML = !this.parsingSVG && !this._knownHTMLElement(tagName) && !this._isCustomElement(tagName);
if (badSVG || badHTML) {
throw new ParseError("INVALID_TAG_NAME", tagName, token);
}
}
else {
throw new ParseError("INVALID_TAG_NAME", tagName, token);
}
var parseInfo = { openTag: { start: token.interval.start }};
var nameSpace = (this.parsingSVG ? this.svgNameSpace : undefined);
// If the preceding tag and the active tag is omittableCloseTag pairs,
// we tell our DOM builder that we're done.
// FIXME TODO get rid of activeTagNode; add loop that can pop nested omitable FIXME TODO
this._popOmited(tagName);
/*
if (activeTagNode && parentTagNode != this.domBuilder.fragment.node){
var activeTagName = activeTagNode.nodeName.toLowerCase();
if(this._knownOmittableCloseTags(activeTagName, tagName)) {
this.domBuilder.popElement();
}
}
*/
// Store currentNode as the parentTagNode
parentTagNode = this.domBuilder.currentNode;
this.domBuilder.pushElement(tagName, parseInfo, nameSpace);
if (!this.stream.end())
this._parseEndOpenTag(tagName);
}
},
// This helper parses HTML comments. It assumes the stream has just
// passed the beginning `<!--` of an HTML comment.
_parseComment: function() {
var token;
while (!this.stream.end()) {
if (this.stream.match('-->', true)) {
token = this.stream.makeToken();
this.domBuilder.comment(token.value.slice(4, -3), token.interval);
return;
}
this.stream.next();
}
token = this.stream.makeToken();
throw new ParseError("UNTERMINATED_COMMENT", token);
},
// This helper parses CDATA content, which should be treated as raw text,
// rather than being parsed for markup. It assumes the stream has just
// passed the beginning `<tagname` of an HTML element.
_parseCDATA: function(tagname) {
var token,
matchString = '</'+tagname+'>',
text,
textInterval = { start: 0, end: 0 },
openTagEnd = this.domBuilder.currentNode.parseInfo.openTag.end,
closeTagInterval;
this.stream.makeToken();
while (!this.stream.end()) {
if (this.stream.match(matchString, true)) {
token = this.stream.makeToken();
text = token.value.slice(0, -matchString.length);
closeTagInterval = {
start: openTagEnd + text.length,
end: token.interval.end
};
this.domBuilder.currentNode.parseInfo.closeTag = closeTagInterval;
textInterval.start = token.interval.start;
textInterval.end = token.interval.end - (closeTagInterval.end - closeTagInterval.start);
this.domBuilder.text(text, textInterval);
this.domBuilder.popElement();
return;
}
this.stream.next();
}
throw new ParseError("UNCLOSED_TAG", this);
},
// This helper function checks if the current tag contains an attribute
containsAttribute: function (stream) {
return stream.eat(nameStartChar);
},
// This helper function parses the end of a closing tag. It expects
// the stream to be right after the end of the closing tag's tag
// name.
_parseEndCloseTag: function() {
this.stream.eatSpace();
if (this.stream.next() != '>') {
if(this.containsAttribute(this.stream)) {
throw new ParseError("ATTRIBUTE_IN_CLOSING_TAG", this);
} else {
throw new ParseError("UNTERMINATED_CLOSE_TAG", this);
}
}
var end = this.stream.makeToken().interval.end;
this.domBuilder.currentNode.parseInfo.closeTag.end = end;
this.domBuilder.popElement();
},
// This helper function parses the rest of an opening tag after
// its tag name, looking for `attribute="value"` data until a
// `>` is encountered.
_parseEndOpenTag: function(tagName) {
var tagMark = this.stream.pos,
startMark = this.stream.pos;
while (!this.stream.end()) {
if (this.containsAttribute(this.stream)) {
if (this.stream.peek !== "=") {
this.stream.eatWhile(nameChar);
}
this._parseAttribute(tagName);
}
else if (this.stream.eatSpace()) {
this.stream.makeToken();
startMark = this.stream.pos;
}
else if (this.stream.peek() == '>' || this.stream.match("/>")) {
var selfClosing = this.stream.match("/>", true);
if (selfClosing) {
if (!this.parsingSVG && !this._knownVoidHTMLElement(tagName))
throw new ParseError("SELF_CLOSING_NON_VOID_ELEMENT", this,
tagName);
} else
this.stream.next();
var end = this.stream.makeToken().interval.end;
this.domBuilder.currentNode.parseInfo.openTag.end = end;
// If the opening tag represents a void element, there will not be
// a closing element, so we tell our DOM builder that we're done.
if (tagName && ((selfClosing && this._knownSVGElement(tagName)) || this._knownVoidHTMLElement(tagName)))
this.domBuilder.popElement();
// If the open tag represents a optional-omit-close-tag element, there may be
// an optional closing element, so we save the currentNode into activeTag for next step check.
// FIXME TODO kill activeTagNode
activeTagNode = false;
if (tagName && this._knownOmittableCloseTagHtmlElement(tagName)){
activeTagNode = this.domBuilder.currentNode;
}
// If the opening tag represents a `<style>` element, we hand
// off parsing to our CSS parser.
if (!this.stream.end() && tagName === "style") {
this.domBuilder.pushContext("css", this.stream.pos);
var cssBlock = this.cssParser.parse();
this.domBuilder.pushContext("html", this.stream.pos);
this.domBuilder.text(cssBlock.value, cssBlock.parseInfo);
}
// If the opening tag represents a `<textarea>` element, we need
// to parse all its contents as CDATA (unparsed character data)
if (tagName && tagName === "script") {
this.domBuilder.pushContext("javascript", this.stream.pos);
this._parseCDATA("script");
this.domBuilder.pushContext("html", this.stream.pos);
}
// If the opening tag represents a `<textarea>` element, we need
// to parse all its contents as CDATA (unparsed character data)
if (tagName && tagName === "textarea") {
this.domBuilder.pushContext("text", this.stream.pos);
this._parseCDATA("textarea");
this.domBuilder.pushContext("html", this.stream.pos);
}
// FIXME TODO isNextParent is replaced by _isNextTagEnder(stream)
// also it can pop more than one element FIXME TODO
// if there is no more content in the parent element, we tell DOM builder that we're done.
// this. _isNextTagEnder(this.stream);
var tagEnds=this. _isNextTagEnder(this.stream);
if (tagEnds && tagEnds.n > 1) {
this.domBuilder.popElements(tagEnds.n-1);
}
/* if(parentTagNode && parentTagNode != this.domBuilder.fragment.node) {
var parentTagName = parentTagNode.nodeName.toLowerCase(),
nextIsParent = isNextTagParent(this.stream, parentTagName),
needsEndTag = !allowsOmmitedEndTag(parentTagName, tagName),
optionalEndTag = this._knownOmittableCloseTagHtmlElement(parentTagName),
nextTagCloses = isNextCloseTag(this.stream);
if(nextIsParent && (needsEndTag || (optionalEndTag && nextTagCloses))) {
if(this._knownOmittableCloseTagHtmlElement(tagName)) {
this.domBuilder.popElement();
}
}
}
*/
return;
}
// error cases: bad attribute name, or unclosed tag
else {
this.stream.eatWhile(/[^'"\s=<>]/);
var attrToken = this.stream.makeToken();
if (!attrToken) {
this.stream.tokenStart = tagMark;
attrToken = this.stream.makeToken();
var peek = this.stream.peek();
if(peek === "'" || peek === '"') {
this.stream.next();
this.stream.eatWhile(new RegExp("[^"+peek+"]"));
this.stream.next();
var token = this.stream.makeToken();
throw new ParseError("UNBOUND_ATTRIBUTE_VALUE", this, token);
}
throw new ParseError("UNTERMINATED_OPEN_TAG", this);
}
attrToken.interval.start = startMark;
throw new ParseError("INVALID_ATTR_NAME", this, attrToken);
}
}
},
// This helper function parses an HTML tag attribute. It expects
// the stream to be right after the end of an attribute name.
_parseAttribute: function(tagName) {
var nameTok = this.stream.makeToken();
nameTok.value = nameTok.value.toLowerCase();
this.stream.eatSpace();
// If the character after the attribute name is a `=`, then we
// look for an attribute value; otherwise, this is a boolean
// attribute.
if (this.stream.peek() == '=') {
this.stream.next();
if(nameTok.value.indexOf(":") !== -1) {
var parts = nameTok.value.split(":");
if(parts.length > 2) {
throw new ParseError("MULTIPLE_ATTR_NAMESPACES", this, nameTok);
}
var nameSpace = parts[0],
attributeName = parts[1];
if(!this._supportedAttributeNameSpace(nameSpace)) {
throw new ParseError("UNSUPPORTED_ATTR_NAMESPACE", this, nameTok);
}
}
// Currently, we only support quoted attribute values, even
// though the HTML5 standard allows them to sometimes go unquoted.
this.stream.eatSpace();
this.stream.makeToken();
var quoteType = this.stream.next();
if (quoteType !== '"' && quoteType !== "'") {
throw new ParseError("UNQUOTED_ATTR_VALUE", this);
}
if (quoteType === '"') {
this.stream.eatWhile(/[^"]/);
} else {
this.stream.eatWhile(/[^']/);
}
if (this.stream.next() !== quoteType) {
throw new ParseError("UNTERMINATED_ATTR_VALUE", this, nameTok);
}
var valueTok = this.stream.makeToken();
//Add a new validator to check if there is a http link in a https page
if (checkMixedContent && valueTok.value.match(/http:/) && isActiveContent(tagName, nameTok.value)) {
this.warnings.push(
new ParseError("HTTP_LINK_FROM_HTTPS_PAGE", this, nameTok, valueTok)
);
}
var unquotedValue = replaceEntityRefs(valueTok.value.slice(1, -1));
this.domBuilder.attribute(nameTok.value, unquotedValue, {
name: nameTok.interval,
value: valueTok.interval
});
} else {
this.stream.makeToken();
this.domBuilder.attribute(nameTok.value, '', {
name: nameTok.interval
});
}
}
};
HTMLParser.replaceEntityRefs = replaceEntityRefs;
return HTMLParser;
}());