Created
October 20, 2010 11:13
-
-
Save gifnksm/636215 to your computer and use it in GitHub Desktop.
method toDOM() for XML and XMLList object
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
default xml namespace = "http://www.w3.org/1999/xhtml"; | |
(function() { | |
var parser = new DOMParser(), | |
range = document.createRange(); | |
function toDOM(xmlns) { | |
var pretty = XML.prettyPrinting; | |
// disable prettyPringing temporary to avoid extra space inclusion | |
XML.prettyPrinting = false; | |
var doc = parser.parseFromString( | |
'<root xmlns="' + xmlns + '">' + this.toXMLString() + "</root>", | |
"application/xml"); | |
XML.prettyPrinting = pretty; | |
range.selectNodeContents( | |
document.importNode(doc.documentElement, true)); | |
var fragment = range.extractContents(); | |
return fragment.childNodes.length > 1 ? fragment : fragment.firstChild; | |
}; | |
XML.prototype.function::toDOM = toDOM; | |
XMLList.prototype.function::toDOM = toDOM; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment