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 * Copyright (c) 2009 Bartek Szopka 7 * 8 * Licensed under MIT license. 9 * 10 */ 11jQuery.extend({ 12 highlight: function (node, re, nodeName, className, excludeParents) { 13 excludeParents = excludeParents == '' ? '.exhghttt' : excludeParents; 14 if (node.nodeType === 3) { 15 var normalized = node.data.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); 16 var match = normalized.match(re); 17 if (match) { 18 var highlight = document.createElement(nodeName || 'span'); 19 highlight.className = className || 'highlight'; 20 if ( /\.|,|\s/.test(match[0].charAt(0)) ) 21 var index = match.index + 1; 22 else 23 var index = match.index; 24 var wordNode = node.splitText(index); 25 wordNode.splitText(match[1].length); 26 var wordClone = wordNode.cloneNode(true); 27 highlight.appendChild(wordClone); 28 wordNode.parentNode.replaceChild(highlight, wordNode); 29 return 1; //skip added node in parent 30 } 31 } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children 32 !/(script|style)/i.test(node.tagName) && // ignore script and style nodes 33 !jQuery(node).closest(excludeParents).length > 0 && 34 !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted 35 for (var i = 0; i < node.childNodes.length; i++) { 36 i += jQuery.highlight(node.childNodes[i], re, nodeName, className, excludeParents); 37 } 38 } 39 return 0; 40 } 41}); 42 43jQuery.fn.unhighlight = function (options) { 44 var settings = { className: 'highlight', element: 'span' }; 45 jQuery.extend(settings, options); 46 47 return this.find(settings.element + "." + settings.className).each(function () { 48 var parent = this.parentNode; 49 parent.replaceChild(this.firstChild, this); 50 parent.normalize(); 51 }).end(); 52}; 53 54jQuery.fn.highlight = function (words, options) { 55 var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false, excludeParents: '' }; 56 jQuery.extend(settings, options); 57 58 if (words.constructor === String) { 59 words = [words]; 60 } 61 words = jQuery.grep(words, function(word, i){ 62 return word != ''; 63 }); 64 words = jQuery.map(words, function(word, i) { 65 return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&").normalize("NFD").replace(/[\u0300-\u036f]/g, ""); 66 }); 67 if (words.length == 0) { return this; }; 68 69 var flag = settings.caseSensitive ? "" : "i"; 70 var pattern = "(" + words.join("|") + ")"; 71 if (settings.wordsOnly) { 72 pattern = "(?:,|^|\\s)" + pattern + "(?:,|$|\\s)"; 73 } 74 var re = new RegExp(pattern, flag); 75 76 return this.each(function () { 77 jQuery.highlight(this, re, settings.element, settings.className, settings.excludeParents); 78 }); 79};
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.