annotate.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. * @fileoverview Methods for annotating occurrences of query terms in text or
  16. * in a DOM tree. Adapted from Gmail code.
  17. *
  18. */
  19. goog.provide('goog.dom.annotate');
  20. goog.provide('goog.dom.annotate.AnnotateFn');
  21. goog.require('goog.array');
  22. goog.require('goog.asserts');
  23. goog.require('goog.dom');
  24. goog.require('goog.dom.NodeType');
  25. goog.require('goog.dom.TagName');
  26. goog.require('goog.dom.safe');
  27. goog.require('goog.html.SafeHtml');
  28. goog.require('goog.object');
  29. /**
  30. * A function that takes:
  31. * (1) the number of the term that is "hit",
  32. * (2) the HTML (search term) to be annotated,
  33. * and returns the annotated term as an HTML.
  34. * @typedef {function(number, !goog.html.SafeHtml): !goog.html.SafeHtml}
  35. */
  36. goog.dom.annotate.AnnotateFn;
  37. /**
  38. * Calls {@code annotateFn} for each occurrence of a search term in text nodes
  39. * under {@code node}. Returns the number of hits.
  40. *
  41. * @param {Node} node A DOM node.
  42. * @param {Array<!Array<string|boolean>>} terms
  43. * An array of [searchTerm, matchWholeWordOnly] tuples.
  44. * The matchWholeWordOnly value is a per-term attribute because some terms
  45. * may be CJK, while others are not. (For correctness, matchWholeWordOnly
  46. * should always be false for CJK terms.).
  47. * @param {goog.dom.annotate.AnnotateFn} annotateFn
  48. * @param {*=} opt_ignoreCase Whether to ignore the case of the query
  49. * terms when looking for matches.
  50. * @param {Array<string>=} opt_classesToSkip Nodes with one of these CSS class
  51. * names (and its descendants) will be skipped.
  52. * @param {number=} opt_maxMs Number of milliseconds after which this function,
  53. * if still annotating, should stop and return.
  54. *
  55. * @return {boolean} Whether any terms were annotated.
  56. */
  57. goog.dom.annotate.annotateTerms = function(
  58. node, terms, annotateFn, opt_ignoreCase, opt_classesToSkip, opt_maxMs) {
  59. if (opt_ignoreCase) {
  60. terms = goog.dom.annotate.lowercaseTerms_(terms);
  61. }
  62. var stopTime = opt_maxMs > 0 ? goog.now() + opt_maxMs : 0;
  63. return goog.dom.annotate.annotateTermsInNode_(
  64. node, terms, annotateFn, opt_ignoreCase, opt_classesToSkip || [],
  65. stopTime, 0);
  66. };
  67. /**
  68. * The maximum recursion depth allowed. Any DOM nodes deeper than this are
  69. * ignored.
  70. * @type {number}
  71. * @private
  72. */
  73. goog.dom.annotate.MAX_RECURSION_ = 200;
  74. /**
  75. * The node types whose descendants should not be affected by annotation.
  76. * @private {!Object<string, boolean>}
  77. */
  78. goog.dom.annotate.NODES_TO_SKIP_ = goog.object.createSet(
  79. goog.dom.TagName.SCRIPT, goog.dom.TagName.STYLE, goog.dom.TagName.TEXTAREA);
  80. /**
  81. * Recursive helper function.
  82. *
  83. * @param {Node} node A DOM node.
  84. * @param {Array<!Array<string|boolean>>} terms
  85. * An array of [searchTerm, matchWholeWordOnly] tuples.
  86. * The matchWholeWordOnly value is a per-term attribute because some terms
  87. * may be CJK, while others are not. (For correctness, matchWholeWordOnly
  88. * should always be false for CJK terms.).
  89. * @param {goog.dom.annotate.AnnotateFn} annotateFn
  90. * @param {*} ignoreCase Whether to ignore the case of the query terms
  91. * when looking for matches.
  92. * @param {Array<string>} classesToSkip Nodes with one of these CSS class
  93. * names will be skipped (as will their descendants).
  94. * @param {number} stopTime Deadline for annotation operation (ignored if 0).
  95. * @param {number} recursionLevel How deep this recursive call is; pass the
  96. * value 0 in the initial call.
  97. * @return {boolean} Whether any terms were annotated.
  98. * @private
  99. */
  100. goog.dom.annotate.annotateTermsInNode_ = function(
  101. node, terms, annotateFn, ignoreCase, classesToSkip, stopTime,
  102. recursionLevel) {
  103. if ((stopTime > 0 && goog.now() >= stopTime) ||
  104. recursionLevel > goog.dom.annotate.MAX_RECURSION_) {
  105. return false;
  106. }
  107. var annotated = false;
  108. if (node.nodeType == goog.dom.NodeType.TEXT) {
  109. var html = goog.dom.annotate.helpAnnotateText_(
  110. node.nodeValue, terms, annotateFn, ignoreCase);
  111. if (html != null) {
  112. // Replace the text with the annotated html. First we put the html into
  113. // a temporary node, to get its DOM structure. To avoid adding a wrapper
  114. // element as a side effect, we'll only actually use the temporary node's
  115. // children.
  116. var tempNode =
  117. goog.dom.getDomHelper(node).createElement(goog.dom.TagName.SPAN);
  118. goog.dom.safe.setInnerHtml(tempNode, html);
  119. var parentNode = node.parentNode;
  120. var nodeToInsert;
  121. while ((nodeToInsert = tempNode.firstChild) != null) {
  122. // Each parentNode.insertBefore call removes the inserted node from
  123. // tempNode's list of children.
  124. parentNode.insertBefore(nodeToInsert, node);
  125. }
  126. parentNode.removeChild(node);
  127. annotated = true;
  128. }
  129. } else if (
  130. node.hasChildNodes() &&
  131. !goog.dom.annotate
  132. .NODES_TO_SKIP_[/** @type {!Element} */ (node).tagName]) {
  133. var classes = /** @type {!Element} */ (node).className.split(/\s+/);
  134. var skip = goog.array.some(classes, function(className) {
  135. return goog.array.contains(classesToSkip, className);
  136. });
  137. if (!skip) {
  138. ++recursionLevel;
  139. var curNode = node.firstChild;
  140. while (curNode) {
  141. var nextNode = curNode.nextSibling;
  142. var curNodeAnnotated = goog.dom.annotate.annotateTermsInNode_(
  143. curNode, terms, annotateFn, ignoreCase, classesToSkip, stopTime,
  144. recursionLevel);
  145. annotated = annotated || curNodeAnnotated;
  146. curNode = nextNode;
  147. }
  148. }
  149. }
  150. return annotated;
  151. };
  152. /**
  153. * Regular expression that matches non-word characters.
  154. *
  155. * Performance note: Testing a one-character string using this regex is as fast
  156. * as the equivalent string test ("a-zA-Z0-9_".indexOf(c) < 0), give or take a
  157. * few percent. (The regex is about 5% faster in IE 6 and about 4% slower in
  158. * Firefox 1.5.) If performance becomes critical, it may be better to convert
  159. * the character to a numerical char code and check whether it falls in the
  160. * word character ranges. A quick test suggests that could be 33% faster.
  161. *
  162. * @type {RegExp}
  163. * @private
  164. */
  165. goog.dom.annotate.NONWORD_RE_ = /\W/;
  166. /**
  167. * Annotates occurrences of query terms in plain text. This process consists of
  168. * identifying all occurrences of all query terms, calling a provided function
  169. * to get the appropriate replacement HTML for each occurrence, and
  170. * HTML-escaping all the text.
  171. *
  172. * @param {string} text The plain text to be searched.
  173. * @param {Array<Array<?>>} terms An array of
  174. * [{string} searchTerm, {boolean} matchWholeWordOnly] tuples.
  175. * The matchWholeWordOnly value is a per-term attribute because some terms
  176. * may be CJK, while others are not. (For correctness, matchWholeWordOnly
  177. * should always be false for CJK terms.).
  178. * @param {goog.dom.annotate.AnnotateFn} annotateFn
  179. * @param {*=} opt_ignoreCase Whether to ignore the case of the query
  180. * terms when looking for matches.
  181. * @return {goog.html.SafeHtml} The HTML equivalent of {@code text} with terms
  182. * annotated, or null if the text did not contain any of the terms.
  183. */
  184. goog.dom.annotate.annotateText = function(
  185. text, terms, annotateFn, opt_ignoreCase) {
  186. if (opt_ignoreCase) {
  187. terms = goog.dom.annotate.lowercaseTerms_(terms);
  188. }
  189. return goog.dom.annotate.helpAnnotateText_(
  190. text, terms, annotateFn, opt_ignoreCase);
  191. };
  192. /**
  193. * Annotates occurrences of query terms in plain text. This process consists of
  194. * identifying all occurrences of all query terms, calling a provided function
  195. * to get the appropriate replacement HTML for each occurrence, and
  196. * HTML-escaping all the text.
  197. *
  198. * @param {string} text The plain text to be searched.
  199. * @param {Array<Array<?>>} terms An array of
  200. * [{string} searchTerm, {boolean} matchWholeWordOnly] tuples.
  201. * If {@code ignoreCase} is true, each search term must already be lowercase.
  202. * The matchWholeWordOnly value is a per-term attribute because some terms
  203. * may be CJK, while others are not. (For correctness, matchWholeWordOnly
  204. * should always be false for CJK terms.).
  205. * @param {goog.dom.annotate.AnnotateFn} annotateFn
  206. * @param {*} ignoreCase Whether to ignore the case of the query terms
  207. * when looking for matches.
  208. * @return {goog.html.SafeHtml} The HTML equivalent of {@code text} with terms
  209. * annotated, or null if the text did not contain any of the terms.
  210. * @private
  211. */
  212. goog.dom.annotate.helpAnnotateText_ = function(
  213. text, terms, annotateFn, ignoreCase) {
  214. var hit = false;
  215. var textToSearch = ignoreCase ? text.toLowerCase() : text;
  216. var textLen = textToSearch.length;
  217. var numTerms = terms.length;
  218. // Each element will be an array of hit positions for the term.
  219. var termHits = new Array(numTerms);
  220. // First collect all the hits into allHits.
  221. for (var i = 0; i < numTerms; i++) {
  222. var term = terms[i];
  223. var hits = [];
  224. var termText = term[0];
  225. if (termText != '') {
  226. var matchWholeWordOnly = term[1];
  227. var termLen = termText.length;
  228. var pos = 0;
  229. // Find each hit for term t and append to termHits.
  230. while (pos < textLen) {
  231. var hitPos = textToSearch.indexOf(termText, pos);
  232. if (hitPos == -1) {
  233. break;
  234. } else {
  235. var prevCharPos = hitPos - 1;
  236. var nextCharPos = hitPos + termLen;
  237. if (!matchWholeWordOnly ||
  238. ((prevCharPos < 0 ||
  239. goog.dom.annotate.NONWORD_RE_.test(
  240. textToSearch.charAt(prevCharPos))) &&
  241. (nextCharPos >= textLen ||
  242. goog.dom.annotate.NONWORD_RE_.test(
  243. textToSearch.charAt(nextCharPos))))) {
  244. hits.push(hitPos);
  245. hit = true;
  246. }
  247. pos = hitPos + termLen;
  248. }
  249. }
  250. }
  251. termHits[i] = hits;
  252. }
  253. if (hit) {
  254. var html = [];
  255. var pos = 0;
  256. while (true) {
  257. // First determine which of the n terms is the next hit.
  258. var termIndexOfNextHit;
  259. var posOfNextHit = -1;
  260. for (var i = 0; i < numTerms; i++) {
  261. var hits = termHits[i];
  262. // pull off the position of the next hit of term t
  263. // (it's always the first in the array because we're shifting
  264. // hits off the front of the array as we process them)
  265. // this is the next candidate to consider for the next overall hit
  266. if (!goog.array.isEmpty(hits)) {
  267. var hitPos = hits[0];
  268. // Discard any hits embedded in the previous hit.
  269. while (hitPos >= 0 && hitPos < pos) {
  270. hits.shift();
  271. hitPos = goog.array.isEmpty(hits) ? -1 : hits[0];
  272. }
  273. if (hitPos >= 0 && (posOfNextHit < 0 || hitPos < posOfNextHit)) {
  274. termIndexOfNextHit = i;
  275. posOfNextHit = hitPos;
  276. }
  277. }
  278. }
  279. // Quit if there are no more hits.
  280. if (posOfNextHit < 0) break;
  281. goog.asserts.assertNumber(termIndexOfNextHit);
  282. // Remove the next hit from our hit list.
  283. termHits[termIndexOfNextHit].shift();
  284. // Append everything from the end of the last hit up to this one.
  285. html.push(text.substr(pos, posOfNextHit - pos));
  286. // Append the annotated term.
  287. var termLen = terms[termIndexOfNextHit][0].length;
  288. var termHtml =
  289. goog.html.SafeHtml.htmlEscape(text.substr(posOfNextHit, termLen));
  290. html.push(
  291. annotateFn(goog.asserts.assertNumber(termIndexOfNextHit), termHtml));
  292. pos = posOfNextHit + termLen;
  293. }
  294. // Append everything after the last hit.
  295. html.push(text.substr(pos));
  296. return goog.html.SafeHtml.concat(html);
  297. } else {
  298. return null;
  299. }
  300. };
  301. /**
  302. * Converts terms to lowercase.
  303. *
  304. * @param {Array<Array<?>>} terms An array of
  305. * [{string} searchTerm, {boolean} matchWholeWordOnly] tuples.
  306. * @return {!Array<Array<?>>} An array of
  307. * [{string} searchTerm, {boolean} matchWholeWordOnly] tuples.
  308. * @private
  309. */
  310. goog.dom.annotate.lowercaseTerms_ = function(terms) {
  311. var lowercaseTerms = [];
  312. for (var i = 0; i < terms.length; ++i) {
  313. var term = terms[i];
  314. lowercaseTerms[i] = [term[0].toLowerCase(), term[1]];
  315. }
  316. return lowercaseTerms;
  317. };