Last active
January 30, 2022 01:17
-
-
Save eligrey/195107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* e4x-dom.js | |
* Version 1.0.2 | |
* | |
* e4x-dom.js makes it easy to interact with the DOM with E4X XML objects. | |
* | |
* 2009-11-18 | |
* | |
* By Elijah Grey, http://eligrey.com | |
* | |
* Copyright (c) 2009 Elijah Grey | |
* | |
* 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. | |
* | |
*/ | |
/*global document, XML, DOMParser*/ | |
/*jslint white: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */ | |
"use strict"; | |
(function () { | |
var | |
doc = document, | |
xml = XML, // for minification | |
lengthProp = "length", | |
getElById = "getElementById", | |
selector = function (xmlObj, method, collectionSelector) { | |
var collection = doc.querySelectorAll(collectionSelector), | |
i = collection[lengthProp], | |
res = []; | |
while (i--) { | |
res.push(xmlObj[method](collection.item(i))); | |
} | |
return res; | |
}, | |
methods = [ // reverse order for the while loop | |
function (id) { | |
return this.insertAfter(doc[getElById](id)); | |
}, "insertAfterId", | |
function (el) { | |
if (typeof el === "string") { | |
return selector(this, "insertAfter", el); | |
} | |
return el.parentNode.insertBefore(this.node(), el.nextSibling); | |
}, "insertAfter", | |
function (id) { | |
return this.insertBefore(doc[getElById](id)); | |
}, "insertBeforeId", | |
function (el) { | |
if (typeof el === "string") { | |
return selector(this, "insertBefore", el); | |
} | |
return el.parentNode.insertBefore(this.node(), el); | |
}, "insertBefore", | |
function (id) { | |
return this.appendTo(doc[getElById](id)); | |
}, "appendToId", | |
function (el) { | |
if (typeof el === "string") { | |
return selector(this, "appendTo", el); | |
} | |
return el.appendChild(this.node()); | |
}, "appendTo", | |
function (id) { | |
return this.fill(doc[getElById](id)); | |
}, "fillId", | |
function (el) { | |
if (typeof el === "string") { | |
return selector(this, "over", el); | |
} | |
var i = el.childNodes[lengthProp]; | |
while (i--) { | |
el.removeChild(el.childNodes.item(i)); | |
} | |
return el.appendChild(this.node()); | |
}, "fill", | |
function (id) { | |
this.@id = id; | |
return this.over(doc[getElById](id)); | |
}, "overId", | |
function (el) { | |
if (typeof el === "string") { | |
return selector(this, "over", el); | |
} | |
var node = this.node(), | |
i = el.childNodes[lengthProp]; | |
while (i--) { | |
el.removeChild(el.childNodes.item(i)); | |
} | |
el.parentNode.insertBefore(node, el); | |
el.parentNode.removeChild(el); | |
return node; | |
}, "over", | |
function (method, args) { | |
var node = this.node(); | |
return node[method].apply(node, args); | |
}, "__noSuchMethod__", | |
function () { | |
if (this.nodeKind() === "text") { | |
return doc.createTextNode(this.toString()); | |
} | |
var prettyPrinting = XML.prettyPrinting, | |
i = this.function::[lengthProp](), | |
elem; | |
while (i--) { | |
if (this[i].name().uri === "") { // no namespace | |
this[i].@xmlns = "http://www.w3.org/1999/xhtml"; | |
} | |
} | |
xml.prettyPrinting = false; | |
elem = doc.adoptNode((new DOMParser()).parseFromString( | |
this.toXMLString(), "application/xml" | |
).documentElement); | |
xml.prettyPrinting = prettyPrinting; | |
return elem; | |
}, "node" | |
], | |
i = methods[lengthProp]; | |
while (i--) { | |
xml.prototype.function::[methods[i--]] = methods[i]; | |
} | |
xml.ignoreWhitespace = false; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment