Implement DOMParser and XMLSerializer classes
See original GitHub issueAll modern browsers expose these two useful classes with both a very simple API:
https://developer.mozilla.org/en/docs/Web/API/DOMParser https://developer.mozilla.org/en/docs/XMLSerializer
Without knowing the details, I believe it shouldn’t be too hard to implement these as thin shim to what jsdom already has available.
I am currently using quickly hacked together shims in paper.js that look like the code below.
I am happy to work on a better version that can find its way into jsdom, if you think this would be useful and could give me some pointers as to where to start looking.
function XMLSerializer() {
}
XMLSerializer.prototype.serializeToString = function(node) {
var text = jsdom.serializeDocument(node);
// Fix a jsdom issue where all SVG tagNames are lowercased:
// https://github.com/tmpvar/jsdom/issues/620
var tagNames = ['linearGradient', 'radialGradient', 'clipPath', 'textPath'];
for (var i = 0, l = tagNames.length; i < l; i++) {
var tagName = tagNames[i];
text = text.replace(
new RegExp('(<|</)' + tagName.toLowerCase() + '\\b', 'g'),
function(all, start) {
return start + tagName;
});
}
return text;
};
function DOMParser() {
}
DOMParser.prototype.parseFromString = function(string, contenType) {
var div = document.createElement('div');
div.innerHTML = string;
return div.firstChild;
};
Issue Analytics
- State:
- Created 8 years ago
- Reactions:14
- Comments:43 (24 by maintainers)
Top Results From Across the Web
Parsing and serializing XML - Developer guides | MDN
Serializes DOM trees, converting them into strings containing XML. DOMParser. Constructs a DOM tree by parsing a string containing XML, ...
Read more >DOM Parsing and Serialization - W3C
Abstract. This specification defines various APIs for programmatic access to HTML and generic XML parsers by web applications for use in parsing ...
Read more >Java DOM Parser - Parse XML Document - Tutorialspoint
Import XML-related packages. Create a SAXBuilder. Create a Document from a file or stream; Extract the root element; Examine attributes; Examine sub-elements ...
Read more >DOMParser (Oracle XML Java API Reference)
This class implements an eXtensible Markup Language (XML) 1.0 parser according to the World Wide Web Consortium (W3C) recommendation. to parse a XML...
Read more >Java Read XML - Java DOM Parser Example - HowToDoInJava
These DOM objects are linked together in a tree like structure. Once the parser is done with parsing process, we get this tree-like...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@domenic, @tommedema, @ianks, @lehni. There is a complete javascript xml-serializer that i created and is avaliable as an npm package. It follows the w3c spec and even added some improvements. its serialization is neat and accurate with 99% test coverage.
So DOMParser is now implemented and will be in the next release.
I’ve looked at a bunch of XML serializers on npm and nobody seems to have put together one that matches the spec. In the meantime https://github.com/cburgmer/xmlserializer seems closest, so we could use that I guess. I opened https://github.com/cburgmer/xmlserializer/issues/8 to get more spec compliance.