PageSourceSearch

https://mdsr-book.github.io/mdsr2e/book_assets/gitbook-2.6.7/js/jquery.highlight.js

js mdsr-book.github.io collected 2026-10-03 09:36:16 UTC 3,201 bytes, 86 lines download raw bytes

1gitbook.require(["jQuery"], function(jQuery) {
2
3/*
4 * jQuery Highlight plugin
5 *
6 * Based on highlight v3 by Johann Burkard
7 * http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
8 *
9 * Code a little bit refactored and cleaned (in my humble opinion).
10 * Most important changes:
11 *  - has an option to highlight only entire words (wordsOnly - false by default),
12 *  - has an option to be case sensitive (caseSensitive - false by default)
13 *  - highlight element tag and class names can be specified in options
14 *
15 * Copyright (c) 2009 Bartek Szopka
16 *
17 * Licensed under MIT license.
18 *
19 */
20
21jQuery.extend({
22    highlight: function (node, re, nodeName, className) {
23        if (node.nodeType === 3) {
24            var match = node.data.match(re);
25            if (match) {
26                var highlight = document.createElement(nodeName || 'span');
27                highlight.className = className || 'highlight';
28                var wordNode = node.splitText(match.index);
29                wordNode.splitText(match[0].length);
30                var wordClone = wordNode.cloneNode(true);
31                highlight.appendChild(wordClone);
32                wordNode.parentNode.replaceChild(highlight, wordNode);
33                return 1; //skip added node in parent
34            }
35        } else if ((node.nodeType === 1 && node.childNodes) && // only element nodes that have children
36                !/(script|style)/i.test(node.tagName) && // ignore script and style nodes
37                !(node.tagName === nodeName.toUpperCase() && node.className === className)) { // skip if already highlighted
38            for (var i = 0; i < node.childNodes.length; i++) {
39                i += jQuery.highlight(node.childNodes[i], re, nodeName, className);
40            }
41        }
42        return 0;
43    }
44});
45
46jQuery.fn.unhighlight = function (options) {
47    var settings = { className: 'highlight', element: 'span' };
48    jQuery.extend(settings, options);
49
50    return this.find(settings.element + "." + settings.className).each(function () {
51        var parent = this.parentNode;
52        parent.replaceChild(this.firstChild, this);
53        parent.normalize();
54    }).end();
55};
56
57jQuery.fn.highlight = function (words, options) {
58    var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false };
59    jQuery.extend(settings, options);
60
61    if (words.constructor === String) {
62        words = [words];
63        // also match 'foo-bar' if search for 'foo bar'
64        if (/\s/.test(words[0])) words.push(words[0].replace(/\s+/, '-'));
65    }
66    words = jQuery.grep(words, function(word, i){
67      return word !== '';
68    });
69    words = jQuery.map(words, function(word, i) {
70      return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
71    });
72    if (words.length === 0) { return this; }
73
74    var flag = settings.caseSensitive ? "" : "i";
75    var pattern = "(" + words.join("|") + ")";
76    if (settings.wordsOnly) {
77        pattern = "\\b" + pattern + "\\b";
78    }
79    var re = new RegExp(pattern, flag);
80
81    return this.each(function () {
82        jQuery.highlight(this, re, settings.element, settings.className);
83    });
84};
85
86});

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.