utils_test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * @fileoverview Unit tests for goog.html.util.
  16. */
  17. goog.provide('goog.html.UtilsTest');
  18. goog.require('goog.array');
  19. goog.require('goog.dom.TagName');
  20. goog.require('goog.html.utils');
  21. goog.require('goog.object');
  22. goog.require('goog.testing.jsunit');
  23. goog.setTestOnly('goog.html.UtilsTest');
  24. var FAILURE_MESSAGE = 'Failed to strip all HTML.';
  25. var STRIP = 'Hello world!';
  26. var result;
  27. function tearDown() {
  28. result = null;
  29. }
  30. function testStripAllHtmlTagsSingle() {
  31. goog.object.forEach(goog.dom.TagName, function(tag) {
  32. result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP));
  33. assertEquals(FAILURE_MESSAGE, STRIP, result);
  34. });
  35. }
  36. function testStripAllHtmlTagsAttribute() {
  37. goog.object.forEach(goog.dom.TagName, function(tag) {
  38. result = goog.html.utils.stripHtmlTags(makeHtml_(tag, STRIP, 1, 0, 'a'));
  39. assertEquals(FAILURE_MESSAGE, STRIP, result);
  40. });
  41. }
  42. function testStripAllHtmlTagsDouble() {
  43. var tag1 = goog.dom.TagName.B;
  44. var tag2 = goog.dom.TagName.DIV;
  45. result = goog.html.utils.stripHtmlTags(makeHtml_(tag1, STRIP, 2));
  46. assertEquals(FAILURE_MESSAGE, STRIP + STRIP, result);
  47. result = goog.html.utils.stripHtmlTags(makeHtml_(tag2, STRIP, 2));
  48. assertEquals(FAILURE_MESSAGE, STRIP + ' ' + STRIP, result);
  49. }
  50. function testComplex() {
  51. var html = '<h1 id=\"life\">Life at Google</h1>' +
  52. '<p>Read and interact with the information below to learn about ' +
  53. 'life at <u>Google</u>.</p>' +
  54. '<h2 id=\"food\">Food at Google</h2>' +
  55. '<p>Google has <em>the best food in the world</em>.</p>' +
  56. '<h2 id=\"transportation\">Transportation at Google</h2>' +
  57. '<p>Google provides <i>free transportation</i>.</p>' +
  58. // Some text with symbols to make sure that it does not get stripped
  59. '<3i><x>\n-10<x<10 3cat < 3dog &amp;&lt;&gt;&quot;';
  60. result = goog.html.utils.stripHtmlTags(html);
  61. var expected = 'Life at Google ' +
  62. 'Read and interact with the information below to learn about ' +
  63. 'life at Google. ' +
  64. 'Food at Google ' +
  65. 'Google has the best food in the world. ' +
  66. 'Transportation at Google ' +
  67. 'Google provides free transportation. ' +
  68. '-10<x<10 3cat < 3dog &<>\"';
  69. assertEquals(FAILURE_MESSAGE, expected, result);
  70. }
  71. function testInteresting() {
  72. result = goog.html.utils.stripHtmlTags(
  73. '<img/src="bogus"onerror=alert(13) style="display:none">');
  74. assertEquals(FAILURE_MESSAGE, '', result);
  75. result = goog.html.utils.stripHtmlTags(
  76. '<img o\'reilly blob src=bogus onerror=alert(1337)>');
  77. assertEquals(FAILURE_MESSAGE, '', result);
  78. }
  79. /**
  80. * Constructs the HTML of an element from the given tag and content.
  81. * @param {!goog.dom.TagName} tag The HTML tagName for the element.
  82. * @param {string} content The content.
  83. * @param {number=} opt_copies Optional number of copies to make.
  84. * @param {number=} opt_tabIndex Optional tabIndex to give the element.
  85. * @param {string=} opt_id Optional id to give the element.
  86. * @return {string} The HTML of an element from the given tag and content.
  87. */
  88. function makeHtml_(tag, content, opt_copies, opt_tabIndex, opt_id) {
  89. var html = ['<' + tag, '>' + content + '</' + tag + '>'];
  90. if (goog.isNumber(opt_tabIndex)) {
  91. goog.array.insertAt(html, ' tabIndex=\"' + opt_tabIndex + '\"', 1);
  92. }
  93. if (goog.isString(opt_id)) {
  94. goog.array.insertAt(html, ' id=\"' + opt_id + '\"', 1);
  95. }
  96. html = html.join('');
  97. var array = [];
  98. for (var i = 0, length = opt_copies || 1; i < length; i++) {
  99. array[i] = html;
  100. }
  101. return array.join('');
  102. }