PageSourceSearch

https://dwayzer.netlify.app/site_libs/dt-plugin-searchhighlight-1.10.20/jquery.highlight.js

js dwayzer.netlify.app collected 2026-10-03 10:40:44 UTC 5,400 bytes, 140 lines download raw bytes

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 || match.length == 0) {
51              return 0;
52            }
53
54            // If there is a match, return the first non-empty string result.  This guards against regex that
55            // are incredibly broad (e.g., "^", ".") which will match everything.  If we don't protect against
56            // that, very broad regex cause the system to hang as it tries to resolve every node.
57            var firstMatch = null;
58            for (var index = 0; index < match.length; index++) {
59              var item = match[index];
60              if (item != null && item != '') {
61                firstMatch = item;
62                break;
63              }
64            }
65            if (firstMatch) {
66                var highlight = document.createElement(nodeName || 'span');
67                highlight.className = className || 'highlight';
68                var firstMatchIndex = node.data.indexOf(firstMatch);
69                var wordNode = node.splitText(firstMatchIndex);
70                wordNode.splitText(firstMatch.length);
71                var wordClone = wordNode.cloneNode(true);
72                highlight.appendChild(wordClone);
73                wordNode.parentNode.replaceChild(highlight, wordNode);
74                return 1; //skip added node in parent
75            }
76        } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
77                !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
78                !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
79            for (var i = 0; i < node.childNodes.length; i++) {
80                i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
81            }
82        }
83        return 0;
84    }
85});
86
87jQuery.fn.unhighlight = function (options) {
88    var settings = { className: 'highlight', element: 'span' };
89    jQuery.extend(settings, options);
90
91    return this.find(settings.element + "." + settings.className).each(function () {
92        var parent = this.parentNode;
93        parent.replaceChild(this.firstChild, this);
94        parent.normalize();
95    }).end();
96};
97
98jQuery.fn.highlight = function (words, options) {
99    var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
100    jQuery.extend(settings, options);
101
102    if (words.constructor === String) {
103        words = [words];
104    }
105    words = jQuery.grep(words, function(word, i){
106      return word != '';
107    });
108
109    // If we are not configured for regex matching, escape regex characters
110    if (jQuery.fn.highlight.options.escapeRegex) {
111      words = jQuery.map(words, function(word, i) {
112        return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
113      });
114    }
115    if (words.length == 0) { return this; };
116
117    var flag = settings.caseSensitive ? "" : "i";
118    var pattern = "(" + words.join("|") + ")";
119    if (settings.wordsOnly) {
120        pattern = "\\b" + pattern + "\\b";
121    }
122
123    try {
124      var re = new RegExp(pattern, flag);
125      return this.each(function () {
126        jQuery.highlight(this, re, settings.element, settings.className);
127      });
128    }
129    catch(e) {
130      // If the regex is invalid, we are going to leave the highlighting
131      // status unchanged.  This is likely caused when a user is putting
132      // in a regex that is not valid (or is incomplete).
133    }
134};
135
136// Define a global option for the plugin on whether or not we should escape regex when
137// performing the search.  By default we want to escape regex so that searching "^."
138// actually becomes a search for the literal "\^\."
139jQuery.fn.highlight.options = { escapeRegex: true };
140

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.