aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/dom/annotate_test.html
blob: 974b80bcf90ba8b89179fcfd00bae79dffa83d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.dom.annotate</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.annotate');
  goog.require('goog.testing.jsunit');
</script>
<style type="text/css">
  .c0 {background-color:#ff0}
  .c1 {background-color:#0ff}
</style>
</head>
<body>
<span id="p">Tom &amp; Jerry</span>
<table>
  <tr id="q">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>pig</i>gy</td>
  </tr>
  <tr id="r">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>pig</i>gy</td>
  </tr>
  <tr id="t">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>Pig</i>gy</td>
  </tr>
  <tr id="u">
    <td>This <b>little</b> piggy</td>
    <td class="s">That little <i>Pig</i>gy</td>
  </tr>
</table>

<div id="o">
<object classid="clsid:SAMPLE-UNRECOGNIZED-ID" width="100" height="50">
  <param name="BorderStyle" value="1" />
  <param name="MousePointer" value="0" />
  <param name="Enabled" value="1" />
  <param name="Min" value="0" />
  <param name="Max" value="10" />
  Your browser cannot display this object.
</object>
</div>

<script id="script">var variable;</script>
<style id="style" type="text/css">.orange{color:orange}</style>
<span id="comment"><!-- note --></span>

<script>
  var $ = goog.dom.getElement;

  var TEXT = 'This little piggy cried "Wee! Wee! Wee!" all the way home.';

  function doAnnotation(termIndex, termHtml) {
    return '<span class="c' + termIndex + '">' + termHtml + '</span>';
  }

  // goog.dom.annotate.annotateText tests

  function testAnnotateText() {
    var terms = [['pig', true]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals(null, html);

    terms = [['pig', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little <span class="c0">pig</span>gy cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);

    terms = [[' piggy ', true]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals(null, html);

    terms = [[' piggy ', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little<span class="c0"> piggy </span>cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);

    terms = [['goose', true], ['piggy', true]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This little <span class="c1">piggy</span> cried ' +
                 '&quot;Wee! Wee! Wee!&quot; all the way home.', html);
  }

  function testAnnotateTextHtmlEscaping() {
    var terms = [['a', false]];
    var html = goog.dom.annotate.annotateText('&a', terms, doAnnotation)
    assertEquals('&amp;<span class="c0">a</span>', html);

    terms = [['a', false]];
    html = goog.dom.annotate.annotateText('a&', terms, doAnnotation)
    assertEquals('<span class="c0">a</span>&amp;', html);

    terms = [['&', false]];
    html = goog.dom.annotate.annotateText('&', terms, doAnnotation)
    assertEquals('<span class="c0">&amp;</span>', html);
  }

  function testAnnotateTextIgnoreCase() {
    var terms = [['wEe', true]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
    assertEquals('This little piggy cried &quot;<span class="c0">Wee</span>! ' +
                 '<span class="c0">Wee</span>! <span class="c0">Wee</span>!' +
                 '&quot; all the way home.', html);

    terms = [['WEE!', true], ['HE', false]];
    html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
    assertEquals('This little piggy cried &quot;<span class="c0">Wee!</span> ' +
                 '<span class="c0">Wee!</span> <span class="c0">Wee!</span>' +
                 '&quot; all t<span class="c1">he</span> way home.', html);
  }

  function testAnnotateTextOverlappingTerms() {
    var terms = [['tt', false], ['little', false]];
    var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
    assertEquals('This <span class="c1">little</span> piggy cried &quot;Wee! ' +
                 'Wee! Wee!&quot; all the way home.', html);
  }

  // goog.dom.annotate.annotateTerms tests

  function testAnnotateTerms() {
    var terms = [['pig', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
    assertEquals('Tom &amp; Jerry', $('p').innerHTML);

    terms = [['Tom', true]];
    assertTrue(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('p'));
    assertEquals(1, spans.length);
    assertEquals('Tom', spans[0].innerHTML);
    assertEquals(' & Jerry', spans[0].nextSibling.nodeValue);
  }

  function testAnnotateTermsInTable() {
    var terms = [['pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms($('q'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('q'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('pig', spans[1].innerHTML);
    assertEquals('I', spans[1].parentNode.tagName);
  }

  function testAnnotateTermsWithClassExclusions() {
    var terms = [['pig', false]];
    var classesToIgnore = ['s'];
    assertTrue(goog.dom.annotate.annotateTerms($('r'), terms, doAnnotation,
                                               false, classesToIgnore));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('r'));
    assertEquals(1, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
  }

  function testAnnotateTermsIgnoreCase() {
    var terms1 = [['pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms(
        $('t'), terms1, doAnnotation, true));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('t'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('Pig', spans[1].innerHTML);

    var terms2 = [['Pig', false]];
    assertTrue(goog.dom.annotate.annotateTerms(
        $('u'), terms2, doAnnotation, true));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('u'));
    assertEquals(2, spans.length);
    assertEquals('pig', spans[0].innerHTML);
    assertEquals('gy', spans[0].nextSibling.nodeValue);
    assertEquals('Pig', spans[1].innerHTML);
  }

  function testAnnotateTermsInObject() {
    var terms = [['object', true]];
    assertTrue(goog.dom.annotate.annotateTerms($('o'), terms, doAnnotation));
    var spans = goog.dom.getElementsByTagNameAndClass('SPAN', 'c0', $('o'));
    assertEquals(1, spans.length);
    assertEquals('object', spans[0].innerHTML);
  }

  function testAnnotateTermsInScript() {
    var terms = [['variable', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('script'), terms,
                                                doAnnotation));
  }

  function testAnnotateTermsInStyle() {
    var terms = [['color', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('style'), terms,
                                                doAnnotation));
  }

  function testAnnotateTermsInHtmlComment() {
    var terms = [['note', true]];
    assertFalse(goog.dom.annotate.annotateTerms($('comment'), terms,
                                                doAnnotation));
  }

</script>
</body>
</html>