mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-03-14 05:35:49 +01:00
Cleanup the search highlighting code a bit.
This commit is contained in:
123
content/find.js
123
content/find.js
@@ -141,93 +141,38 @@ liberator.Search = function () //{{{
|
|||||||
|
|
||||||
var range;
|
var range;
|
||||||
while ((range = finder.Find(aWord,
|
while ((range = finder.Find(aWord,
|
||||||
this._searchRange,
|
this.searchRange,
|
||||||
this._startPt,
|
this.startPt,
|
||||||
this._endPt)))
|
this.endPt)))
|
||||||
yield range;
|
yield range;
|
||||||
},
|
},
|
||||||
|
|
||||||
highlightDoc: function highlightDoc(win, aWord)
|
highlightDoc: function highlightDoc(win, aWord)
|
||||||
{
|
{
|
||||||
var textFound = false;
|
Array.forEach(win.frames, function (frame) highlightDoc(aWord, frame));
|
||||||
Array.forEach(win.frames, function (frame)
|
|
||||||
{
|
|
||||||
if (highlightDoc(aWord, frame))
|
|
||||||
textFound = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
var doc = win.document;
|
var doc = win.document;
|
||||||
if (!doc || !(doc instanceof HTMLDocument))
|
if (!doc || !(doc instanceof HTMLDocument))
|
||||||
return textFound;
|
return;
|
||||||
|
|
||||||
var body = doc.body;
|
|
||||||
|
|
||||||
var count = body.childNodes.length;
|
|
||||||
this._searchRange = doc.createRange();
|
|
||||||
this._startPt = doc.createRange();
|
|
||||||
this._endPt = doc.createRange();
|
|
||||||
|
|
||||||
this._searchRange.setStart(body, 0);
|
|
||||||
this._searchRange.setEnd(body, count);
|
|
||||||
|
|
||||||
this._startPt.setStart(body, 0);
|
|
||||||
this._startPt.setEnd(body, 0);
|
|
||||||
this._endPt.setStart(body, count);
|
|
||||||
this._endPt.setEnd(body, count);
|
|
||||||
|
|
||||||
if (!aWord) {
|
if (!aWord) {
|
||||||
// Remove highlighting. We use the find API again rather than
|
let elems = doc.getElementsByClassName("__liberator-search");
|
||||||
// searching for our span elements so that we gain access to the
|
for (let i=elems.length; --i >= 0;)
|
||||||
// anonymous content that nsIFind searches.
|
|
||||||
|
|
||||||
if (!this._lastHighlightString)
|
|
||||||
return textFound;
|
|
||||||
|
|
||||||
var retRange = null;
|
|
||||||
var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"]
|
|
||||||
.createInstance(Components.interfaces.nsIFind);
|
|
||||||
|
|
||||||
for (let retRange in this.search(this._lastHighlightString))
|
|
||||||
{
|
{
|
||||||
var startContainer = retRange.startContainer;
|
let elem = elems[i];
|
||||||
var elem = null;
|
let docfrag = doc.createDocumentFragment();
|
||||||
try
|
let next = elem.nextSibling;
|
||||||
{
|
let parent = elem.parentNode;
|
||||||
elem = startContainer.parentNode;
|
|
||||||
}
|
|
||||||
catch (ex) {}
|
|
||||||
|
|
||||||
if (elem && elem.className == "__liberator-search")
|
let child;
|
||||||
{
|
while (child = elem.firstChild)
|
||||||
var child = null;
|
docfrag.appendChild(child);
|
||||||
var docfrag = doc.createDocumentFragment();
|
|
||||||
var next = elem.nextSibling;
|
|
||||||
var parent = elem.parentNode;
|
|
||||||
|
|
||||||
while ((child = elem.firstChild)) {
|
parent.removeChild(elem);
|
||||||
docfrag.appendChild(child);
|
parent.insertBefore(docfrag, next);
|
||||||
}
|
parent.normalize();
|
||||||
|
|
||||||
this._startPt = doc.createRange();
|
|
||||||
this._startPt.setStartAfter(elem);
|
|
||||||
|
|
||||||
parent.removeChild(elem);
|
|
||||||
parent.insertBefore(docfrag, next);
|
|
||||||
parent.normalize();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Somehow we didn't highlight this instance; just skip it.
|
|
||||||
this._startPt = doc.createRange();
|
|
||||||
this._startPt.setStart(retRange.endContainer,
|
|
||||||
retRange.endOffset);
|
|
||||||
}
|
|
||||||
|
|
||||||
this._startPt.collapse(true);
|
|
||||||
|
|
||||||
textFound = true;
|
|
||||||
}
|
}
|
||||||
return textFound;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var baseNode = doc.createElementNS("http://www.w3.org/1999/xhtml", "span");
|
var baseNode = doc.createElementNS("http://www.w3.org/1999/xhtml", "span");
|
||||||
@@ -237,28 +182,28 @@ liberator.Search = function () //{{{
|
|||||||
baseNode.style.padding = "0";
|
baseNode.style.padding = "0";
|
||||||
baseNode.className = "__liberator-search";
|
baseNode.className = "__liberator-search";
|
||||||
|
|
||||||
return this.highlightText(aWord, baseNode) || textFound;
|
var body = doc.body;
|
||||||
},
|
var count = body.childNodes.length;
|
||||||
|
this.searchRange = doc.createRange();
|
||||||
|
this.startPt = doc.createRange();
|
||||||
|
this.endPt = doc.createRange();
|
||||||
|
|
||||||
highlightText: function highlightText(aWord, aBaseNode)
|
this.searchRange.setStart(body, 0);
|
||||||
{
|
this.searchRange.setEnd(body, count);
|
||||||
var retRange = null;
|
|
||||||
|
this.startPt.setStart(body, 0);
|
||||||
|
this.startPt.setEnd(body, 0);
|
||||||
|
this.endPt.setStart(body, count);
|
||||||
|
this.endPt.setEnd(body, count);
|
||||||
|
|
||||||
for(let retRange in this.search(aWord, caseSensitive)) {
|
for(let retRange in this.search(aWord, caseSensitive)) {
|
||||||
var textFound = false;
|
|
||||||
// Highlight
|
// Highlight
|
||||||
var nodeSurround = aBaseNode.cloneNode(true);
|
var nodeSurround = baseNode.cloneNode(true);
|
||||||
var node = this.highlight(retRange, nodeSurround);
|
var node = this.highlight(retRange, nodeSurround);
|
||||||
this._startPt = node.ownerDocument.createRange();
|
this.startPt = node.ownerDocument.createRange();
|
||||||
this._startPt.setStart(node, node.childNodes.length);
|
this.startPt.setStart(node, node.childNodes.length);
|
||||||
this._startPt.setEnd(node, node.childNodes.length);
|
this.startPt.setEnd(node, node.childNodes.length);
|
||||||
|
|
||||||
textFound = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this._lastHighlightString = aWord;
|
|
||||||
|
|
||||||
return textFound;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
highlight: function highlight(aRange, aNode)
|
highlight: function highlight(aRange, aNode)
|
||||||
|
|||||||
Reference in New Issue
Block a user