annotate_test.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2006 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. goog.provide('goog.dom.annotateTest');
  15. goog.setTestOnly('goog.dom.annotateTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.dom.annotate');
  19. goog.require('goog.html.SafeHtml');
  20. goog.require('goog.testing.jsunit');
  21. var $ = goog.dom.getElement;
  22. var TEXT = 'This little piggy cried "Wee! Wee! Wee!" all the way home.';
  23. function doAnnotation(termIndex, termHtml) {
  24. return goog.html.SafeHtml.create(
  25. 'span', {'class': 'c' + termIndex}, termHtml);
  26. }
  27. // goog.dom.annotate.annotateText tests
  28. function testAnnotateText() {
  29. var terms = [['pig', true]];
  30. var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  31. assertEquals(null, html);
  32. terms = [['pig', false]];
  33. html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  34. html = goog.html.SafeHtml.unwrap(html);
  35. assertEquals(
  36. 'This little <span class="c0">pig</span>gy cried ' +
  37. '&quot;Wee! Wee! Wee!&quot; all the way home.',
  38. html);
  39. terms = [[' piggy ', true]];
  40. html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  41. assertEquals(null, html);
  42. terms = [[' piggy ', false]];
  43. html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  44. html = goog.html.SafeHtml.unwrap(html);
  45. assertEquals(
  46. 'This little<span class="c0"> piggy </span>cried ' +
  47. '&quot;Wee! Wee! Wee!&quot; all the way home.',
  48. html);
  49. terms = [['goose', true], ['piggy', true]];
  50. html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  51. html = goog.html.SafeHtml.unwrap(html);
  52. assertEquals(
  53. 'This little <span class="c1">piggy</span> cried ' +
  54. '&quot;Wee! Wee! Wee!&quot; all the way home.',
  55. html);
  56. }
  57. function testAnnotateTextHtmlEscaping() {
  58. var terms = [['a', false]];
  59. var html = goog.dom.annotate.annotateText('&a', terms, doAnnotation);
  60. html = goog.html.SafeHtml.unwrap(html);
  61. assertEquals('&amp;<span class="c0">a</span>', html);
  62. terms = [['a', false]];
  63. html = goog.dom.annotate.annotateText('a&', terms, doAnnotation);
  64. html = goog.html.SafeHtml.unwrap(html);
  65. assertEquals('<span class="c0">a</span>&amp;', html);
  66. terms = [['&', false]];
  67. html = goog.dom.annotate.annotateText('&', terms, doAnnotation);
  68. html = goog.html.SafeHtml.unwrap(html);
  69. assertEquals('<span class="c0">&amp;</span>', html);
  70. }
  71. function testAnnotateTextIgnoreCase() {
  72. var terms = [['wEe', true]];
  73. var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
  74. html = goog.html.SafeHtml.unwrap(html);
  75. assertEquals(
  76. 'This little piggy cried &quot;<span class="c0">Wee</span>! ' +
  77. '<span class="c0">Wee</span>! <span class="c0">Wee</span>!' +
  78. '&quot; all the way home.',
  79. html);
  80. terms = [['WEE!', true], ['HE', false]];
  81. html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
  82. html = goog.html.SafeHtml.unwrap(html);
  83. assertEquals(
  84. 'This little piggy cried &quot;<span class="c0">Wee!</span> ' +
  85. '<span class="c0">Wee!</span> <span class="c0">Wee!</span>' +
  86. '&quot; all t<span class="c1">he</span> way home.',
  87. html);
  88. }
  89. function testAnnotateTextOverlappingTerms() {
  90. var terms = [['tt', false], ['little', false]];
  91. var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
  92. html = goog.html.SafeHtml.unwrap(html);
  93. assertEquals(
  94. 'This <span class="c1">little</span> piggy cried &quot;Wee! ' +
  95. 'Wee! Wee!&quot; all the way home.',
  96. html);
  97. }
  98. // goog.dom.annotate.annotateTerms tests
  99. function testAnnotateTerms() {
  100. var terms = [['pig', true]];
  101. assertFalse(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
  102. assertEquals('Tom &amp; Jerry', $('p').innerHTML);
  103. terms = [['Tom', true]];
  104. assertTrue(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
  105. var spans = goog.dom.getElementsByTagNameAndClass(
  106. goog.dom.TagName.SPAN, 'c0', $('p'));
  107. assertEquals(1, spans.length);
  108. assertEquals('Tom', spans[0].innerHTML);
  109. assertEquals(' & Jerry', spans[0].nextSibling.nodeValue);
  110. }
  111. function testAnnotateTermsInTable() {
  112. var terms = [['pig', false]];
  113. assertTrue(goog.dom.annotate.annotateTerms($('q'), terms, doAnnotation));
  114. var spans = goog.dom.getElementsByTagNameAndClass(
  115. goog.dom.TagName.SPAN, 'c0', $('q'));
  116. assertEquals(2, spans.length);
  117. assertEquals('pig', spans[0].innerHTML);
  118. assertEquals('gy', spans[0].nextSibling.nodeValue);
  119. assertEquals('pig', spans[1].innerHTML);
  120. assertEquals(String(goog.dom.TagName.I), spans[1].parentNode.tagName);
  121. }
  122. function testAnnotateTermsWithClassExclusions() {
  123. var terms = [['pig', false]];
  124. var classesToIgnore = ['s'];
  125. assertTrue(
  126. goog.dom.annotate.annotateTerms(
  127. $('r'), terms, doAnnotation, false, classesToIgnore));
  128. var spans = goog.dom.getElementsByTagNameAndClass(
  129. goog.dom.TagName.SPAN, 'c0', $('r'));
  130. assertEquals(1, spans.length);
  131. assertEquals('pig', spans[0].innerHTML);
  132. assertEquals('gy', spans[0].nextSibling.nodeValue);
  133. }
  134. function testAnnotateTermsIgnoreCase() {
  135. var terms1 = [['pig', false]];
  136. assertTrue(
  137. goog.dom.annotate.annotateTerms($('t'), terms1, doAnnotation, true));
  138. var spans = goog.dom.getElementsByTagNameAndClass(
  139. goog.dom.TagName.SPAN, 'c0', $('t'));
  140. assertEquals(2, spans.length);
  141. assertEquals('pig', spans[0].innerHTML);
  142. assertEquals('gy', spans[0].nextSibling.nodeValue);
  143. assertEquals('Pig', spans[1].innerHTML);
  144. var terms2 = [['Pig', false]];
  145. assertTrue(
  146. goog.dom.annotate.annotateTerms($('u'), terms2, doAnnotation, true));
  147. var spans = goog.dom.getElementsByTagNameAndClass(
  148. goog.dom.TagName.SPAN, 'c0', $('u'));
  149. assertEquals(2, spans.length);
  150. assertEquals('pig', spans[0].innerHTML);
  151. assertEquals('gy', spans[0].nextSibling.nodeValue);
  152. assertEquals('Pig', spans[1].innerHTML);
  153. }
  154. function testAnnotateTermsInObject() {
  155. var terms = [['object', true]];
  156. assertTrue(goog.dom.annotate.annotateTerms($('o'), terms, doAnnotation));
  157. var spans = goog.dom.getElementsByTagNameAndClass(
  158. goog.dom.TagName.SPAN, 'c0', $('o'));
  159. assertEquals(1, spans.length);
  160. assertEquals('object', spans[0].innerHTML);
  161. }
  162. function testAnnotateTermsInScript() {
  163. var terms = [['variable', true]];
  164. assertFalse(
  165. goog.dom.annotate.annotateTerms($('script'), terms, doAnnotation));
  166. }
  167. function testAnnotateTermsInStyle() {
  168. var terms = [['color', true]];
  169. assertFalse(goog.dom.annotate.annotateTerms($('style'), terms, doAnnotation));
  170. }
  171. function testAnnotateTermsInHtmlComment() {
  172. var terms = [['note', true]];
  173. assertFalse(
  174. goog.dom.annotate.annotateTerms($('comment'), terms, doAnnotation));
  175. }