Debian Codegraph @

closure-library/closure/goog/dom/annotate_test.js

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