windex

jQuery-like that's safe to use in a Firefox Extension
git clone https://wehaveforgeathome.hates.computer/windex.git
Log | Files | Refs | LICENSE

commit 94e1ebef10bc9bf71df845ba2172f861e7cb5c4f
parent cb60b03b2c664de6fd66c8bd7cc5220416858e1e
Author: Ryan Wolf <rwolf@borderstylo.com>
Date:   Thu, 24 Feb 2011 15:01:15 -0800

implemented after

Diffstat:
Mwindex.js | 56++++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 48 insertions(+), 8 deletions(-)

diff --git a/windex.js b/windex.js @@ -376,14 +376,10 @@ WindexNodes.prototype.before = function (arg) { // See: https://developer.mozilla.org/en/Code_snippets/HTML_to_DOM WindexNodes.prototype._beforeHTML = function (html) { this.forEach(function (node) { - if (node.ownerDocument instanceof Components.interfaces.nsIImageDocument) { - var fragment = Components.classes["@mozilla.org/feed-unescapehtml;1"]. - getService(Components.interfaces.nsIScriptableUnescapeHTML). - parseFragment(html, false, null, node); - node.parentNode.insertBefore(fragment, node); - } else { - node.innerHTML = node.innerHTML + html; - } + var fragment = Components.classes["@mozilla.org/feed-unescapehtml;1"]. + getService(Components.interfaces.nsIScriptableUnescapeHTML). + parseFragment(html, false, null, node); + node.parentNode.insertBefore(fragment, node); }); return this; }; @@ -406,6 +402,50 @@ WindexNodes.prototype._beforeWindexNodes = function (windexNodes) { return this; }; +// See: http://api.jquery.com/after/ +WindexNodes.prototype.after = function (arg) { + if (typeof arg == "string" || typeof arg == "number") { + return this._afterHTML(arg); + } + if (isNode(arg)) { return this._afterNode(arg); } + if (arg instanceof WindexNodes) { return this._afterWindexNodes(arg); } + throw new Error("'" + arg + "' is not a String, Node, or WindexNodes"); +}; + +// See: http://snipplr.com/view/2107/insertafter-function-for-the-dom/ +var after = function (node, toAdd) { + if (!node.nextSibling) { return node.parentNode.append(toAdd); } + node.parentNode.insertBefore(toAdd, node.nextSibling); +}; + +// See: https://bugzilla.mozilla.org/show_bug.cgi?id=550612 +// See: https://developer.mozilla.org/en/Code_snippets/HTML_to_DOM +WindexNodes.prototype._afterHTML = function (html) { + this.forEach(function (node) { + var fragment = Components.classes["@mozilla.org/feed-unescapehtml;1"]. + getService(Components.interfaces.nsIScriptableUnescapeHTML). + parseFragment(html, false, null, node); + after(node, fragment); + }); + return this; +}; + +// See: https://developer.mozilla.org/En/DOM/Node.cloneNode +WindexNodes.prototype._afterNode = function (nodeToAppend) { + this.forEach(function (node) { after(node, nodeToAppend.cloneNode(true)); }); + return this; +}; + +// See: https://developer.mozilla.org/En/DOM/Node.cloneNode +WindexNodes.prototype._afterWindexNodes = function (windexNodes) { + this.forEach(function (node) { + windexNodes.forEach(function (nodeToAppend) { + after(node, nodeToAppend.cloneNode(true)); + }); + }); + return this; +}; + // See: http://api.jquery.com/html/ WindexNodes.prototype.html = function (replacement) { if (!replacement) { return this._htmlGet(); };