1/* 2 * jQuery Highlight plugin 3 * 4 * Based on highlight v3 by Johann Burkard 5 * http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html 6 * 7 * Code a little bit refactored and cleaned (in my humble opinion). 8 * Most important changes: 9 * - has an option to highlight only entire words (wordsOnly - false by default), 10 * - has an option to be case sensitive (caseSensitive - false by default) 11 * - highlight element tag and class names can be specified in options 12 * 13 * Usage: 14 * // wrap every occurrance of text 'lorem' in content 15 * // with <span class='highlight'> (default options) 16 * $('#content').highlight('lorem'); 17 * 18 * // search for and highlight more terms at once 19 * // so you can save some time on traversing DOM 20 * $('#content').highlight(['lorem', 'ipsum']); 21 * $('#content').highlight('lorem ipsum'); 22 * 23 * // search only for entire word 'lorem' 24 * $('#content').highlight('lorem', { wordsOnly: true }); 25 * 26 * // don't ignore case during search of term 'lorem' 27 * $('#content').highlight('lorem', { caseSensitive: true }); 28 * 29 * // wrap every occurrance of term 'ipsum' in content 30 * // with <em class='important'> 31 * $('#content').highlight('ipsum', { element: 'em', className: 'important' }); 32 * 33 * // remove default highlight 34 * $('#content').unhighlight(); 35 * 36 * // remove custom highlight 37 * $('#content').unhighlight({ element: 'em', className: 'important' }); 38 * 39 * 40 * Copyright (c) 2009 Bartek Szopka 41 * 42 * Licensed under MIT license. 43 * 44 */ 45 46var nrItemsHighlighted = 0; 47jQuery.extend({ 48 highlight: function (node, re, nodeName, className, ie11, highlightStatus) { 49 var exit = false; 50 if (!exit && !highlightStatus) { 51 exit = (nrItemsHighlighted > 0); 52 } 53 if (node.nodeType === 3 && !exit) { 54 var match = node.data.match(re); 55 if (match) { 56 var highlight = document.createElement(nodeName || 'span'); 57 highlight.className = className || 'highlight-doc'; 58 var wordNode = node.splitText(match.index); 59 wordNode.splitText(match[0].length); 60 var wordClone = wordNode.cloneNode(true); 61 highlight.appendChild(wordClone); 62 wordNode.parentNode.replaceChild(highlight, wordNode); 63 nrItemsHighlighted++; 64 return 1; //skip added node in parent 65 } 66 } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children 67 !/(script|style)/i.test(node.tagName) && // ignore script and style nodes 68 !(node.tagName === nodeName.toUpperCase() && node.className === className) && !exit) { // skip if already highlighted 69 for (var i = 0; i < node.childNodes.length; i++) { 70 i += jQuery.highlight(node.childNodes[i], re, nodeName, className, ie11, highlightStatus); 71 } 72 } 73 return 0; 74 } 75}); 76 77jQuery.fn.unhighlight = function (options) { 78 var settings = { className: 'highlight-doc', element: 'span' }; 79 jQuery.extend(settings, options); 80 81 return this.find(settings.element + "." + settings.className).each(function () { 82 var parent = this.parentNode; 83 parent.replaceChild(this.firstChild, this); 84 parent.normalize(); 85 }).end(); 86}; 87 88jQuery.fn.highlight = function (highlightStatus, words, options) { 89 var settings = { className: 'highlight-doc', element: 'span', caseSensitive: false, wordsOnly: false }; 90 jQuery.extend(settings, options); 91 92 if (words.constructor === String) { 93 words = [words]; 94 } 95 words = jQuery.grep(words, function(word, i){ 96 return word != ''; 97 }); 98 words = jQuery.map(words, function(word, i) { 99 return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 100 }); 101 if (words.length == 0) { return this; }; 102 103 var flag = settings.caseSensitive ? "" : "i"; 104 var pattern = "(" + words.join("|") + ")"; 105 if (settings.wordsOnly) { 106 pattern = "\\b" + pattern + "\\b"; 107 } 108 var re = new RegExp(pattern, flag); 109 110 var ie11 = (navigator.userAgent.toLowerCase().indexOf("like gecko") > -1 && navigator.userAgent.toLowerCase().indexOf("11.") > -1); 111 nrItemsHighlighted = 0; 112 return this.each(function () { 113 jQuery.highlight(this, re, settings.element, settings.className, ie11, highlightStatus
vendor: 17 bytes, lines 113-115
113); 114 }); 115};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.