mirror of https://github.com/zulip/zulip.git
78 lines
2.5 KiB
JavaScript
78 lines
2.5 KiB
JavaScript
/** @preserve
|
|
Software from "highlight v4", a jQuery plugin for JavaScript text higlighting,
|
|
is Copyright (c) Johann Burkard and is provided under the following license:
|
|
--
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
--
|
|
*/
|
|
|
|
/*
|
|
|
|
highlight v4
|
|
|
|
Highlights arbitrary terms.
|
|
|
|
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
|
|
|
|
MIT license.
|
|
|
|
Johann Burkard
|
|
<http://johannburkard.de>
|
|
<mailto:jb@eaio.com>
|
|
|
|
*/
|
|
|
|
jQuery.fn.highlight = function(pat) {
|
|
function innerHighlight(node, pat) {
|
|
var skip = 0;
|
|
if (node.nodeType == 3) {
|
|
var pos = node.data.toUpperCase().indexOf(pat);
|
|
if (pos >= 0) {
|
|
var spannode = document.createElement('span');
|
|
spannode.className = 'highlight';
|
|
var middlebit = node.splitText(pos);
|
|
var endbit = middlebit.splitText(pat.length);
|
|
var middleclone = middlebit.cloneNode(true);
|
|
spannode.appendChild(middleclone);
|
|
middlebit.parentNode.replaceChild(spannode, middlebit);
|
|
skip = 1;
|
|
}
|
|
}
|
|
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
|
|
for (var i = 0; i < node.childNodes.length; ++i) {
|
|
i += innerHighlight(node.childNodes[i], pat);
|
|
}
|
|
}
|
|
return skip;
|
|
}
|
|
return this.length && pat && pat.length ? this.each(function() {
|
|
innerHighlight(this, pat.toUpperCase());
|
|
}) : this;
|
|
};
|
|
|
|
jQuery.fn.removeHighlight = function() {
|
|
return this.find("span.highlight").each(function() {
|
|
this.parentNode.firstChild.nodeName;
|
|
with (this.parentNode) {
|
|
replaceChild(this.firstChild, this);
|
|
normalize();
|
|
}
|
|
}).end();
|
|
};
|