123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- goog.provide('goog.html.UtilsTest');
- goog.require('goog.array');
- goog.require('goog.dom.TagName');
- goog.require('goog.html.utils');
- goog.require('goog.object');
- goog.require('goog.testing.jsunit');
- goog.setTestOnly('goog.html.UtilsTest');
- var FAILURE_MESSAGE = 'Failed to strip all HTML.';
- var STRIP = 'Hello world!';
- var result;
- function tearDown() {
- result = null;
- }
- function testStripAllHtmlTagsSingle() {
- goog.object.forEach(goog.dom.TagName, function(tag) {
- result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP));
- assertEquals(FAILURE_MESSAGE, STRIP, result);
- });
- }
- function testStripAllHtmlTagsAttribute() {
- goog.object.forEach(goog.dom.TagName, function(tag) {
- result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP, 1, 0, 'a'));
- assertEquals(FAILURE_MESSAGE, STRIP, result);
- });
- }
- function testStripAllHtmlTagsDouble() {
- var tag1 = goog.dom.TagName.B;
- var tag2 = goog.dom.TagName.DIV;
- result = goog.html.utils.stripHtmlTags(makeHtml_(tag1, STRIP, 2));
- assertEquals(FAILURE_MESSAGE, STRIP + STRIP, result);
- result = goog.html.utils.stripHtmlTags(makeHtml_(tag2, STRIP, 2));
- assertEquals(FAILURE_MESSAGE, STRIP + ' ' + STRIP, result);
- }
- function testComplex() {
- var html = '<h1 id=\"life\">Life at Google</h1>' +
- '<p>Read and interact with the information below to learn about ' +
- 'life at <u>Google</u>.</p>' +
- '<h2 id=\"food\">Food at Google</h2>' +
- '<p>Google has <em>the best food in the world</em>.</p>' +
- '<h2 id=\"transportation\">Transportation at Google</h2>' +
- '<p>Google provides <i>free transportation</i>.</p>' +
-
- '<3i><x>\n-10<x<10 3cat < 3dog &<>"';
- result = goog.html.utils.stripHtmlTags(html);
- var expected = 'Life at Google ' +
- 'Read and interact with the information below to learn about ' +
- 'life at Google. ' +
- 'Food at Google ' +
- 'Google has the best food in the world. ' +
- 'Transportation at Google ' +
- 'Google provides free transportation. ' +
- '-10<x<10 3cat < 3dog &<>\"';
- assertEquals(FAILURE_MESSAGE, expected, result);
- }
- function testInteresting() {
- result = goog.html.utils.stripHtmlTags(
- '<img/src="bogus"onerror=alert(13) style="display:none">');
- assertEquals(FAILURE_MESSAGE, '', result);
- result = goog.html.utils.stripHtmlTags(
- '<img o\'reilly blob src=bogus onerror=alert(1337)>');
- assertEquals(FAILURE_MESSAGE, '', result);
- }
- function makeHtml_(tag, content, opt_copies, opt_tabIndex, opt_id) {
- var html = ['<' + tag, '>' + content + '</' + tag + '>'];
- if (goog.isNumber(opt_tabIndex)) {
- goog.array.insertAt(html, ' tabIndex=\"' + opt_tabIndex + '\"', 1);
- }
- if (goog.isString(opt_id)) {
- goog.array.insertAt(html, ' id=\"' + opt_id + '\"', 1);
- }
- html = html.join('');
- var array = [];
- for (var i = 0, length = opt_copies || 1; i < length; i++) {
- array[i] = html;
- }
- return array.join('');
- }
|