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
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.dom.annotateTest');
goog.setTestOnly('goog.dom.annotateTest');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.annotate');
goog.require('goog.html.SafeHtml');
goog.require('goog.testing.jsunit');
var $ = goog.dom.getElement;
var TEXT = 'This little piggy cried "Wee! Wee! Wee!" all the way home.';
function doAnnotation(termIndex, termHtml) {
return goog.html.SafeHtml.create(
'span', {'class': 'c' + termIndex}, termHtml);
}
// 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);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This little <span class="c0">pig</span>gy cried ' +
'"Wee! Wee! Wee!" 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);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This little<span class="c0"> piggy </span>cried ' +
'"Wee! Wee! Wee!" all the way home.',
html);
terms = [['goose', true], ['piggy', true]];
html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This little <span class="c1">piggy</span> cried ' +
'"Wee! Wee! Wee!" all the way home.',
html);
}
function testAnnotateTextHtmlEscaping() {
var terms = [['a', false]];
var html = goog.dom.annotate.annotateText('&a', terms, doAnnotation);
html = goog.html.SafeHtml.unwrap(html);
assertEquals('&<span class="c0">a</span>', html);
terms = [['a', false]];
html = goog.dom.annotate.annotateText('a&', terms, doAnnotation);
html = goog.html.SafeHtml.unwrap(html);
assertEquals('<span class="c0">a</span>&', html);
terms = [['&', false]];
html = goog.dom.annotate.annotateText('&', terms, doAnnotation);
html = goog.html.SafeHtml.unwrap(html);
assertEquals('<span class="c0">&</span>', html);
}
function testAnnotateTextIgnoreCase() {
var terms = [['wEe', true]];
var html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This little piggy cried "<span class="c0">Wee</span>! ' +
'<span class="c0">Wee</span>! <span class="c0">Wee</span>!' +
'" all the way home.',
html);
terms = [['WEE!', true], ['HE', false]];
html = goog.dom.annotate.annotateText(TEXT, terms, doAnnotation, true);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This little piggy cried "<span class="c0">Wee!</span> ' +
'<span class="c0">Wee!</span> <span class="c0">Wee!</span>' +
'" 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);
html = goog.html.SafeHtml.unwrap(html);
assertEquals(
'This <span class="c1">little</span> piggy cried "Wee! ' +
'Wee! Wee!" 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 & Jerry', $('p').innerHTML);
terms = [['Tom', true]];
assertTrue(goog.dom.annotate.annotateTerms($('p'), terms, doAnnotation));
var spans = goog.dom.getElementsByTagNameAndClass(
goog.dom.TagName.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(
goog.dom.TagName.SPAN, 'c0', $('q'));
assertEquals(2, spans.length);
assertEquals('pig', spans[0].innerHTML);
assertEquals('gy', spans[0].nextSibling.nodeValue);
assertEquals('pig', spans[1].innerHTML);
assertEquals(String(goog.dom.TagName.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(
goog.dom.TagName.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(
goog.dom.TagName.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(
goog.dom.TagName.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(
goog.dom.TagName.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));
}
|