Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 1 | gitbook.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 | |
| 21 | jQuery.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 | |
| 46 | jQuery.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 | |
| 57 | jQuery.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 | } |
| 64 | words = jQuery.grep(words, function(word, i){ |
| 65 | return word !== ''; |
| 66 | }); |
| 67 | words = jQuery.map(words, function(word, i) { |
| 68 | return word.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); |
| 69 | }); |
| 70 | if (words.length === 0) { return this; } |
| 71 | |
| 72 | var flag = settings.caseSensitive ? "" : "i"; |
| 73 | var pattern = "(" + words.join("|") + ")"; |
| 74 | if (settings.wordsOnly) { |
| 75 | pattern = "\\b" + pattern + "\\b"; |
| 76 | } |
| 77 | var re = new RegExp(pattern, flag); |
| 78 | |
| 79 | return this.each(function () { |
| 80 | jQuery.highlight(this, re, settings.element, settings.className); |
| 81 | }); |
| 82 | }; |
| 83 | |
| 84 | }); |