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 46jQuery.extend({ 47 highlight: function (node, re, nodeName, className) { 48 if (node.nodeType === 3) { 49 var match = node.data.match(re); 50 if (match) { 51 if(jQuery(node).closest('.sv-defaultlist').length !== 1 && jQuery(node).closest('.vdHandelse').length !== 1) { //TILLÃGG FÃR ATT UNDANTA VISSA KLASSER 52 var highlight = document.createElement(nodeName || 'span'); 53 highlight.className = className || 'highlight'; 54 var wordNode = node.splitText(match.index); 55 wordNode.splitText(match[0].length); 56 var wordClone = wordNode.cloneNode(true); 57 highlight.appendChild(wordClone); 58 wordNode.parentNode.replaceChild(highlight, wordNode); 59 } 60 return 1; //skip added node in parent 61 } 62 } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children 63 !/(script|style)/i.test(node.tagName) && // ignore script and style nodes 64 !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted 65 for (var i = 0; i < node.childNodes.length; i++) { 66 i += jQuery.highlight(node.childNodes[i], re, nodeName, className); 67 } 68 } 69 return 0; 70 } 71}); 72 73jQuery.fn.unhighlight = function (options) { 74 var settings = { className: 'highlight', element: 'span' }; 75 jQuery.extend(settings, options); 76 77 return this.find(settings.element + "." + settings.className).each(function () { 78 var parent = this.parentNode; 79 parent.replaceChild(this.firstChild, this); 80 parent.normalize(); 81 }).end(); 82}; 83 84jQuery.fn.highlight = function (words, options) { 85 var settings = { className: 'highlight', element: 'span', caseSensitive: false }; 86 jQuery.extend(settings, options); 87 88 if (words.constructor === String) { 89 words = [words]; 90 } 91 92 words = jQuery.grep(words, function(word, i){ 93 return word != ''; 94 }); 95 96 words = jQuery.map(words, function(word, i) { 97 98 word = word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 99 100 if (word.indexOf("\\*") != 0) 101 { 102 word = "\\b" + word; 103 } 104 else 105 { 106 word = word.substring(2); 107 } 108 109 if (word.indexOf("\\*", word.length - 2) !== -1) 110 { 111 word = word.substring(0, word.length - 2); 112 } 113 else 114 { 115 word = word + "\\b"; 116 } 117 118 return word; 119 });
120 121 words = jQuery.map(words, function(word, i) { 122 return word; 123 }); 124 125 if (words.length == 0) { return this; }; 126 127 var flag = settings.caseSensitive ? "" : "i"; 128 var pattern = "(" + words.join("|") + ")"; 129 var re = new RegExp(pattern, flag); 130 131 return this.each(function () { 132 jQuery.highlight(this, re, settings.element, settings.className); 133 }); 134}; 135
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.