Debian Codegraph @

closure-library/closure/goog/html/utils_test.js

raw
1 // Copyright 2013 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Unit tests for goog.html.util.
17 */
18
19 goog.provide('goog.html.UtilsTest');
20
21 goog.require('goog.array');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.html.utils');
24 goog.require('goog.object');
25 goog.require('goog.testing.jsunit');
26
27 goog.setTestOnly('goog.html.UtilsTest');
28
29
30 var FAILURE_MESSAGE = 'Failed to strip all HTML.';
31 var STRIP = 'Hello world!';
32 var result;
33
34
35 function tearDown() {
36 result = null;
37 }
38
39
40 function testStripAllHtmlTagsSingle() {
41 goog.object.forEach(goog.dom.TagName, function(tag) {
42 result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP));
43 assertEquals(FAILURE_MESSAGE, STRIP, result);
44 });
45 }
46
47
48 function testStripAllHtmlTagsAttribute() {
49 goog.object.forEach(goog.dom.TagName, function(tag) {
50 result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP, 1, 0, 'a'));
51 assertEquals(FAILURE_MESSAGE, STRIP, result);
52 });
53 }
54
55
56 function testStripAllHtmlTagsDouble() {
57 var tag1 = goog.dom.TagName.B;
58 var tag2 = goog.dom.TagName.DIV;
59 result = goog.html.utils.stripHtmlTags(makeHtml_(tag1, STRIP, 2));
60 assertEquals(FAILURE_MESSAGE, STRIP + STRIP, result);
61 result = goog.html.utils.stripHtmlTags(makeHtml_(tag2, STRIP, 2));
62 assertEquals(FAILURE_MESSAGE, STRIP + ' ' + STRIP, result);
63 }
64
65
66 function testComplex() {
67 var html = '<h1 id=\"life\">Life at Google</h1>' +
68 '<p>Read and interact with the information below to learn about ' +
69 'life at <u>Google</u>.</p>' +
70 '<h2 id=\"food\">Food at Google</h2>' +
71 '<p>Google has <em>the best food in the world</em>.</p>' +
72 '<h2 id=\"transportation\">Transportation at Google</h2>' +
73 '<p>Google provides <i>free transportation</i>.</p>' +
74 // Some text with symbols to make sure that it does not get stripped
75 '<3i><x>\n-10<x<10 3cat < 3dog &amp;&lt;&gt;&quot;';
76 result = goog.html.utils.stripHtmlTags(html);
77 var expected = 'Life at Google ' +
78 'Read and interact with the information below to learn about ' +
79 'life at Google. ' +
80 'Food at Google ' +
81 'Google has the best food in the world. ' +
82 'Transportation at Google ' +
83 'Google provides free transportation. ' +
84 '-10<x<10 3cat < 3dog &<>\"';
85 assertEquals(FAILURE_MESSAGE, expected, result);
86 }
87
88
89 function testInteresting() {
90 result = goog.html.utils.stripHtmlTags(
91 '<img/src="bogus"onerror=alert(13) style="display:none">');
92 assertEquals(FAILURE_MESSAGE, '', result);
93 result = goog.html.utils.stripHtmlTags(
94 '<img o\'reilly blob src=bogus onerror=alert(1337)>');
95 assertEquals(FAILURE_MESSAGE, '', result);
96 }
97
98
99 /**
100 * Constructs the HTML of an element from the given tag and content.
101 * @param {!goog.dom.TagName} tag The HTML tagName for the element.
102 * @param {string} content The content.
103 * @param {number=} opt_copies Optional number of copies to make.
104 * @param {number=} opt_tabIndex Optional tabIndex to give the element.
105 * @param {string=} opt_id Optional id to give the element.
106 * @return {string} The HTML of an element from the given tag and content.
107 */
108 function makeHtml_(tag, content, opt_copies, opt_tabIndex, opt_id) {
109 var html = ['<' + tag, '>' + content + '</' + tag + '>'];
110 if (goog.isNumber(opt_tabIndex)) {
111 goog.array.insertAt(html, ' tabIndex=\"' + opt_tabIndex + '\"', 1);
112 }
113 if (goog.isString(opt_id)) {
114 goog.array.insertAt(html, ' id=\"' + opt_id + '\"', 1);
115 }
116 html = html.join('');
117 var array = [];
118 for (var i = 0, length = opt_copies || 1; i < length; i++) {
119 array[i] = html;
120 }
121 return array.join('');
122 }