Debian Codegraph @

closure-library/third_party/closure/goog/caja/string/html/htmlparser.js

raw
1 // Copyright 2006-2008, The Google Caja project.
2 // Modifications Copyright 2009 The Closure Library Authors.
3 // All Rights Reserved
4
5 /**
6 * @license Portions of this code are from the google-caja project, received by
7 * Google under the Apache license (http://code.google.com/p/google-caja/).
8 * All other code is Copyright 2009 Google, Inc. All Rights Reserved.
9
10 // Copyright (C) 2006 Google Inc.
11 //
12 // Licensed under the Apache License, Version 2.0 (the "License");
13 // you may not use this file except in compliance with the License.
14 // You may obtain a copy of the License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
23
24 */
25
26 /**
27 * @fileoverview A Html SAX parser.
28 *
29 * Examples of usage of the {@code goog.string.html.HtmlParser}:
30 * <pre>
31 * var handler = new MyCustomHtmlVisitorHandlerThatExtendsHtmlSaxHandler();
32 * var parser = new goog.string.html.HtmlParser();
33 * parser.parse(handler, '<html><a href="google.com">link found!</a></html>');
34 * </pre>
35 *
36 * TODO(user, msamuel): validate sanitizer regex against the HTML5 grammar at
37 * http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html
38 * http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html
39 * http://www.whatwg.org/specs/web-apps/current-work/multipage/tokenization.html
40 * http://www.whatwg.org/specs/web-apps/current-work/multipage/tree-construction.html
41 *
42 * @author [email protected] (Mike Samuel)
43 * @supported IE6+, FF1.5+, Chrome 3.0+, Safari and Opera 10.
44 */
45
46 goog.provide('goog.string.html');
47 goog.provide('goog.string.html.HtmlParser');
48 goog.provide('goog.string.html.HtmlParser.EFlags');
49 goog.provide('goog.string.html.HtmlParser.Elements');
50 goog.provide('goog.string.html.HtmlParser.Entities');
51 goog.provide('goog.string.html.HtmlSaxHandler');
52
53
54
55 /**
56 * An Html parser: {@code parse} takes a string and calls methods on
57 * {@code goog.string.html.HtmlSaxHandler} while it is visiting it.
58 *
59 * @constructor
60 */
61 goog.string.html.HtmlParser = function() {
62 };
63
64
65 /**
66 * HTML entities that are encoded/decoded.
67 * TODO(user): use {@code goog.string.htmlEncode} instead.
68 * @type {!Object<string, string>}
69 */
70 goog.string.html.HtmlParser.Entities = {
71 'lt': '<',
72 'gt': '>',
73 'amp': '&',
74 'nbsp': '\u00a0',
75 'quot': '"',
76 'apos': '\''
77 };
78
79
80 /**
81 * The html eflags, used internally on the parser.
82 * @enum {number}
83 */
84 goog.string.html.HtmlParser.EFlags = {
85 OPTIONAL_ENDTAG: 1,
86 EMPTY: 2,
87 CDATA: 4,
88 RCDATA: 8,
89 UNSAFE: 16,
90 FOLDABLE: 32
91 };
92
93
94 /**
95 * A map of element to a bitmap of flags it has, used internally on the parser.
96 * @type {Object<string,number>}
97 */
98 goog.string.html.HtmlParser.Elements = {
99 'a': 0,
100 'abbr': 0,
101 'acronym': 0,
102 'address': 0,
103 'applet': goog.string.html.HtmlParser.EFlags.UNSAFE,
104 'area': goog.string.html.HtmlParser.EFlags.EMPTY,
105 'b': 0,
106 'base': goog.string.html.HtmlParser.EFlags.EMPTY |
107 goog.string.html.HtmlParser.EFlags.UNSAFE,
108 'basefont': goog.string.html.HtmlParser.EFlags.EMPTY |
109 goog.string.html.HtmlParser.EFlags.UNSAFE,
110 'bdo': 0,
111 'big': 0,
112 'blockquote': 0,
113 'body': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG |
114 goog.string.html.HtmlParser.EFlags.UNSAFE |
115 goog.string.html.HtmlParser.EFlags.FOLDABLE,
116 'br': goog.string.html.HtmlParser.EFlags.EMPTY,
117 'button': 0,
118 'canvas': 0,
119 'caption': 0,
120 'center': 0,
121 'cite': 0,
122 'code': 0,
123 'col': goog.string.html.HtmlParser.EFlags.EMPTY,
124 'colgroup': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
125 'dd': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
126 'del': 0,
127 'dfn': 0,
128 'dir': 0,
129 'div': 0,
130 'dl': 0,
131 'dt': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
132 'em': 0,
133 'fieldset': 0,
134 'font': 0,
135 'form': 0,
136 'frame': goog.string.html.HtmlParser.EFlags.EMPTY |
137 goog.string.html.HtmlParser.EFlags.UNSAFE,
138 'frameset': goog.string.html.HtmlParser.EFlags.UNSAFE,
139 'h1': 0,
140 'h2': 0,
141 'h3': 0,
142 'h4': 0,
143 'h5': 0,
144 'h6': 0,
145 'head': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG |
146 goog.string.html.HtmlParser.EFlags.UNSAFE |
147 goog.string.html.HtmlParser.EFlags.FOLDABLE,
148 'hr': goog.string.html.HtmlParser.EFlags.EMPTY,
149 'html': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG |
150 goog.string.html.HtmlParser.EFlags.UNSAFE |
151 goog.string.html.HtmlParser.EFlags.FOLDABLE,
152 'i': 0,
153 'iframe': goog.string.html.HtmlParser.EFlags.UNSAFE |
154 goog.string.html.HtmlParser.EFlags.CDATA,
155 'img': goog.string.html.HtmlParser.EFlags.EMPTY,
156 'input': goog.string.html.HtmlParser.EFlags.EMPTY,
157 'ins': 0,
158 'isindex': goog.string.html.HtmlParser.EFlags.EMPTY |
159 goog.string.html.HtmlParser.EFlags.UNSAFE,
160 'kbd': 0,
161 'label': 0,
162 'legend': 0,
163 'li': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
164 'link': goog.string.html.HtmlParser.EFlags.EMPTY |
165 goog.string.html.HtmlParser.EFlags.UNSAFE,
166 'map': 0,
167 'menu': 0,
168 'meta': goog.string.html.HtmlParser.EFlags.EMPTY |
169 goog.string.html.HtmlParser.EFlags.UNSAFE,
170 'noframes': goog.string.html.HtmlParser.EFlags.UNSAFE |
171 goog.string.html.HtmlParser.EFlags.CDATA,
172 'noscript': goog.string.html.HtmlParser.EFlags.UNSAFE |
173 goog.string.html.HtmlParser.EFlags.CDATA,
174 'object': goog.string.html.HtmlParser.EFlags.UNSAFE,
175 'ol': 0,
176 'optgroup': 0,
177 'option': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
178 'p': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
179 'param': goog.string.html.HtmlParser.EFlags.EMPTY |
180 goog.string.html.HtmlParser.EFlags.UNSAFE,
181 'pre': 0,
182 'q': 0,
183 's': 0,
184 'samp': 0,
185 'script': goog.string.html.HtmlParser.EFlags.UNSAFE |
186 goog.string.html.HtmlParser.EFlags.CDATA,
187 'select': 0,
188 'small': 0,
189 'span': 0,
190 'strike': 0,
191 'strong': 0,
192 'style': goog.string.html.HtmlParser.EFlags.UNSAFE |
193 goog.string.html.HtmlParser.EFlags.CDATA,
194 'sub': 0,
195 'sup': 0,
196 'table': 0,
197 'tbody': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
198 'td': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
199 'textarea': goog.string.html.HtmlParser.EFlags.RCDATA,
200 'tfoot': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
201 'th': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
202 'thead': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
203 'title': goog.string.html.HtmlParser.EFlags.RCDATA |
204 goog.string.html.HtmlParser.EFlags.UNSAFE,
205 'tr': goog.string.html.HtmlParser.EFlags.OPTIONAL_ENDTAG,
206 'tt': 0,
207 'u': 0,
208 'ul': 0,
209 'var': 0
210 };
211
212
213 /**
214 * Regular expression that matches &s.
215 * @type {RegExp}
216 * @package
217 */
218 goog.string.html.HtmlParser.AMP_RE = /&/g;
219
220
221 /**
222 * Regular expression that matches loose &s.
223 * @type {RegExp}
224 * @private
225 */
226 goog.string.html.HtmlParser.LOOSE_AMP_RE_ =
227 /&([^a-z#]|#(?:[^0-9x]|x(?:[^0-9a-f]|$)|$)|$)/gi;
228
229
230 /**
231 * Regular expression that matches <.
232 * @type {RegExp}
233 * @package
234 */
235 goog.string.html.HtmlParser.LT_RE = /</g;
236
237
238 /**
239 * Regular expression that matches >.
240 * @type {RegExp}
241 * @package
242 */
243 goog.string.html.HtmlParser.GT_RE = />/g;
244
245
246 /**
247 * Regular expression that matches ".
248 * @type {RegExp}
249 * @package
250 */
251 goog.string.html.HtmlParser.QUOTE_RE = /\"/g;
252
253
254 /**
255 * Regular expression that matches =.
256 * @type {RegExp}
257 * @package
258 */
259 goog.string.html.HtmlParser.EQUALS_RE = /=/g;
260
261
262 /**
263 * Regular expression that matches null characters.
264 * @type {RegExp}
265 * @private
266 */
267 goog.string.html.HtmlParser.NULL_RE_ = /\0/g;
268
269
270 /**
271 * Regular expression that matches entities.
272 * @type {RegExp}
273 * @private
274 */
275 goog.string.html.HtmlParser.ENTITY_RE_ = /&(#\d+|#x[0-9A-Fa-f]+|\w+);/g;
276
277
278 /**
279 * Regular expression that matches decimal numbers.
280 * @type {RegExp}
281 * @private
282 */
283 goog.string.html.HtmlParser.DECIMAL_ESCAPE_RE_ = /^#(\d+)$/;
284
285
286 /**
287 * Regular expression that matches hexadecimal numbers.
288 * @type {RegExp}
289 * @private
290 */
291 goog.string.html.HtmlParser.HEX_ESCAPE_RE_ = /^#x([0-9A-Fa-f]+)$/;
292
293
294 /**
295 * Regular expression that matches the next token to be processed.
296 * @type {RegExp}
297 * @private
298 */
299 goog.string.html.HtmlParser.INSIDE_TAG_TOKEN_ = new RegExp(
300 // Don't capture space.
301 '^\\s*(?:' +
302 // Capture an attribute name in group 1, and value in group 3.
303 // We capture the fact that there was an attribute in group 2, since
304 // interpreters are inconsistent in whether a group that matches nothing
305 // is null, undefined, or the empty string.
306 ('(?:' +
307 '([a-z][a-z-]*)' + // attribute name
308 ('(' + // optionally followed
309 '\\s*=\\s*' +
310 ('(' +
311 // A double quoted string.
312 '\"[^\"]*\"' +
313 // A single quoted string.
314 '|\'[^\']*\'' +
315 // The positive lookahead is used to make sure that in
316 // <foo bar= baz=boo>, the value for bar is blank, not "baz=boo".
317 '|(?=[a-z][a-z-]*\\s*=)' +
318 // An unquoted value that is not an attribute name.
319 // We know it is not an attribute name because the previous
320 // zero-width match would've eliminated that possibility.
321 '|[^>\"\'\\s]*' +
322 ')'
323 ) +
324 ')'
325 ) + '?' +
326 ')'
327 ) +
328 // End of tag captured in group 3.
329 '|(/?>)' +
330 // Don't capture cruft
331 '|[^a-z\\s>]+)',
332 'i');
333
334
335 /**
336 * Regular expression that matches the next token to be processed when we are
337 * outside a tag.
338 * @type {RegExp}
339 * @private
340 */
341 goog.string.html.HtmlParser.OUTSIDE_TAG_TOKEN_ = new RegExp(
342 '^(?:' +
343 // Entity captured in group 1.
344 '&(\\#[0-9]+|\\#[x][0-9a-f]+|\\w+);' +
345 // Comment, doctypes, and processing instructions not captured.
346 '|<[!]--[\\s\\S]*?-->|<!\\w[^>]*>|<\\?[^>*]*>' +
347 // '/' captured in group 2 for close tags, and name captured in group 3.
348 '|<(/)?([a-z][a-z0-9]*)' +
349 // Text captured in group 4.
350 '|([^<&>]+)' +
351 // Cruft captured in group 5.
352 '|([<&>]))',
353 'i');
354
355
356 /**
357 * Given a SAX-like {@code goog.string.html.HtmlSaxHandler} parses a
358 * {@code htmlText} and lets the {@code handler} know the structure while
359 * visiting the nodes.
360 *
361 * @param {goog.string.html.HtmlSaxHandler} handler The HtmlSaxHandler that will
362 * receive the events.
363 * @param {string} htmlText The html text.
364 */
365 goog.string.html.HtmlParser.prototype.parse = function(handler, htmlText) {
366 var htmlLower = null;
367 var inTag = false; // True iff we're currently processing a tag.
368 var attribs = []; // Accumulates attribute names and values.
369 var tagName; // The name of the tag currently being processed.
370 var eflags; // The element flags for the current tag.
371 var openTag; // True if the current tag is an open tag.
372
373 // Lets the handler know that we are starting to parse the document.
374 handler.startDoc();
375
376 // Consumes tokens from the htmlText and stops once all tokens are processed.
377 while (htmlText) {
378 var regex = inTag ?
379 goog.string.html.HtmlParser.INSIDE_TAG_TOKEN_ :
380 goog.string.html.HtmlParser.OUTSIDE_TAG_TOKEN_;
381 // Gets the next token
382 var m = htmlText.match(regex);
383 // And removes it from the string
384 htmlText = htmlText.substring(m[0].length);
385
386 // TODO(goto): cleanup this code breaking it into separate methods.
387 if (inTag) {
388 if (m[1]) { // Attribute.
389 // SetAttribute with uppercase names doesn't work on IE6.
390 var attribName = goog.string.html.toLowerCase(m[1]);
391 var decodedValue;
392 if (m[2]) {
393 var encodedValue = m[3];
394 switch (encodedValue.charCodeAt(0)) { // Strip quotes.
395 case 34: case 39:
396 encodedValue = encodedValue.substring(
397 1, encodedValue.length - 1);
398 break;
399 }
400 decodedValue = this.unescapeEntities_(this.stripNULs_(encodedValue));
401 } else {
402 // Use name as value for valueless attribs, so
403 // <input type=checkbox checked>
404 // gets attributes ['type', 'checkbox', 'checked', 'checked']
405 decodedValue = attribName;
406 }
407 attribs.push(attribName, decodedValue);
408 } else if (m[4]) {
409 if (eflags !== void 0) { // False if not in whitelist.
410 if (openTag) {
411 if (handler.startTag) {
412 handler.startTag(/** @type {string} */ (tagName), attribs);
413 }
414 } else {
415 if (handler.endTag) {
416 handler.endTag(/** @type {string} */ (tagName));
417 }
418 }
419 }
420
421 if (openTag && (eflags &
422 (goog.string.html.HtmlParser.EFlags.CDATA |
423 goog.string.html.HtmlParser.EFlags.RCDATA))) {
424 if (htmlLower === null) {
425 htmlLower = goog.string.html.toLowerCase(htmlText);
426 } else {
427 htmlLower = htmlLower.substring(
428 htmlLower.length - htmlText.length);
429 }
430 var dataEnd = htmlLower.indexOf('</' + tagName);
431 if (dataEnd < 0) {
432 dataEnd = htmlText.length;
433 }
434 if (eflags & goog.string.html.HtmlParser.EFlags.CDATA) {
435 if (handler.cdata) {
436 handler.cdata(htmlText.substring(0, dataEnd));
437 }
438 } else if (handler.rcdata) {
439 handler.rcdata(
440 this.normalizeRCData_(htmlText.substring(0, dataEnd)));
441 }
442 htmlText = htmlText.substring(dataEnd);
443 }
444
445 tagName = eflags = openTag = void 0;
446 attribs.length = 0;
447 inTag = false;
448 }
449 } else {
450 if (m[1]) { // Entity.
451 handler.pcdata(m[0]);
452 } else if (m[3]) { // Tag.
453 openTag = !m[2];
454 inTag = true;
455 tagName = goog.string.html.toLowerCase(m[3]);
456 eflags = goog.string.html.HtmlParser.Elements.hasOwnProperty(tagName) ?
457 goog.string.html.HtmlParser.Elements[tagName] : void 0;
458 } else if (m[4]) { // Text.
459 handler.pcdata(m[4]);
460 } else if (m[5]) { // Cruft.
461 switch (m[5]) {
462 case '<': handler.pcdata('&lt;'); break;
463 case '>': handler.pcdata('&gt;'); break;
464 default: handler.pcdata('&amp;'); break;
465 }
466 }
467 }
468 }
469
470 // Lets the handler know that we are done parsing the document.
471 handler.endDoc();
472 };
473
474
475 /**
476 * Decodes an HTML entity.
477 *
478 * @param {string} name The content between the '&' and the ';'.
479 * @return {string} A single unicode code-point as a string.
480 * @private
481 */
482 goog.string.html.HtmlParser.prototype.lookupEntity_ = function(name) {
483 // TODO(goto): use {goog.string.htmlDecode} instead ?
484 // TODO(goto): &pi; is different from &Pi;
485 name = goog.string.html.toLowerCase(name);
486 if (goog.string.html.HtmlParser.Entities.hasOwnProperty(name)) {
487 return goog.string.html.HtmlParser.Entities[name];
488 }
489 var m = name.match(goog.string.html.HtmlParser.DECIMAL_ESCAPE_RE_);
490 if (m) {
491 return String.fromCharCode(parseInt(m[1], 10));
492 } else if (m = name.match(goog.string.html.HtmlParser.HEX_ESCAPE_RE_)) {
493 return String.fromCharCode(parseInt(m[1], 16));
494 }
495 return '';
496 };
497
498
499 /**
500 * Removes null characters on the string.
501 * @param {string} s The string to have the null characters removed.
502 * @return {string} A string without null characters.
503 * @private
504 */
505 goog.string.html.HtmlParser.prototype.stripNULs_ = function(s) {
506 return s.replace(goog.string.html.HtmlParser.NULL_RE_, '');
507 };
508
509
510 /**
511 * The plain text of a chunk of HTML CDATA which possibly containing.
512 *
513 * TODO(goto): use {@code goog.string.unescapeEntities} instead ?
514 * @param {string} s A chunk of HTML CDATA. It must not start or end inside
515 * an HTML entity.
516 * @return {string} The unescaped entities.
517 * @private
518 */
519 goog.string.html.HtmlParser.prototype.unescapeEntities_ = function(s) {
520 return s.replace(
521 goog.string.html.HtmlParser.ENTITY_RE_, goog.bind(
522 function(fullEntity, name) {
523 return this.lookupEntity_(name);
524 }, this));
525 };
526
527
528 /**
529 * Escape entities in RCDATA that can be escaped without changing the meaning.
530 * @param {string} rcdata The RCDATA string we want to normalize.
531 * @return {string} A normalized version of RCDATA.
532 * @private
533 */
534 goog.string.html.HtmlParser.prototype.normalizeRCData_ = function(rcdata) {
535 return rcdata.
536 replace(goog.string.html.HtmlParser.LOOSE_AMP_RE_, '&amp;$1').
537 replace(goog.string.html.HtmlParser.LT_RE, '&lt;').
538 replace(goog.string.html.HtmlParser.GT_RE, '&gt;');
539 };
540
541
542 /**
543 * TODO(goto): why isn't this in the string package ? does this solves any
544 * real problem ? move it to the goog.string package if it does.
545 *
546 * @param {string} str The string to lower case.
547 * @return {string} The str in lower case format.
548 */
549 goog.string.html.toLowerCase = function(str) {
550 // The below may not be true on browsers in the Turkish locale.
551 if ('script' === 'SCRIPT'.toLowerCase()) {
552 return str.toLowerCase();
553 } else {
554 return str.replace(/[A-Z]/g, function(ch) {
555 return String.fromCharCode(ch.charCodeAt(0) | 32);
556 });
557 }
558 };
559
560
561
562 /**
563 * An interface to the {@code goog.string.html.HtmlParser} visitor, that gets
564 * called while the HTML is being parsed.
565 *
566 * @interface
567 */
568 goog.string.html.HtmlSaxHandler = function() {
569 };
570
571
572 /**
573 * Handler called when the parser found a new tag.
574 * @param {string} name The name of the tag that is starting.
575 * @param {Array<string>} attributes The attributes of the tag.
576 */
577 goog.string.html.HtmlSaxHandler.prototype.startTag = goog.abstractMethod;
578
579
580 /**
581 * Handler called when the parser found a closing tag.
582 * @param {string} name The name of the tag that is ending.
583 */
584 goog.string.html.HtmlSaxHandler.prototype.endTag = goog.abstractMethod;
585
586
587 /**
588 * Handler called when PCDATA is found.
589 * @param {string} text The PCDATA text found.
590 */
591 goog.string.html.HtmlSaxHandler.prototype.pcdata = goog.abstractMethod;
592
593
594 /**
595 * Handler called when RCDATA is found.
596 * @param {string} text The RCDATA text found.
597 */
598 goog.string.html.HtmlSaxHandler.prototype.rcdata = goog.abstractMethod;
599
600
601 /**
602 * Handler called when CDATA is found.
603 * @param {string} text The CDATA text found.
604 */
605 goog.string.html.HtmlSaxHandler.prototype.cdata = goog.abstractMethod;
606
607
608 /**
609 * Handler called when the parser is starting to parse the document.
610 */
611 goog.string.html.HtmlSaxHandler.prototype.startDoc = goog.abstractMethod;
612
613
614 /**
615 * Handler called when the parsing is done.
616 */
617 goog.string.html.HtmlSaxHandler.prototype.endDoc = goog.abstractMethod;