Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to w3c-xmlserializer #2282

Merged
merged 5 commits into from
Oct 29, 2018
Merged

Move to w3c-xmlserializer #2282

merged 5 commits into from
Oct 29, 2018

Conversation

Sebmaster
Copy link
Member

This finally gives us proper serialization for XML documents and the
XMLSerializer interface.

@domenic
Copy link
Member

domenic commented Jul 26, 2018

@Sebmaster mind taking a look at the test failures? Probably all just bad tests...

@Sebmaster
Copy link
Member Author

I think some of these were because serializing a sub-element always included an xmlns attribute and I wasn't sure if that was a spec bug and how browsers behave in that regard. Will test that out on the weekend.

adomasven added a commit to zotero/translation-server that referenced this pull request Jul 31, 2018
This is pending as a PR and hopefully we can remove the extra npm
dependency once it is in:
jsdom/jsdom#2282
@Sebmaster Sebmaster force-pushed the feature/xmlserializer branch 4 times, most recently from 884b9a1 to 5371882 Compare August 7, 2018 02:11
@Sebmaster Sebmaster force-pushed the feature/xmlserializer branch 7 times, most recently from d412cc0 to 4641424 Compare August 18, 2018 20:46
Copy link
Member

@Zirro Zirro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work. Is the dependency ready for a v1.0.0 release?

function htmlSerialization(node) {
if (
node.nodeType === NODE_TYPE.ELEMENT_NODE &&
node.namespaceURI === "http://www.w3.org/1999/xhtml" &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lib/api.js Outdated
@@ -61,7 +61,7 @@ class JSDOM {
}

serialize() {
return domToHtml([idlUtils.implForWrapper(this[window]._document)]);
return fragmentSerialization(idlUtils.implForWrapper(this[window]._document), false);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use an options object instead? https://ariya.io/2011/08/hall-of-api-shame-boolean-trap

This finally gives us proper serialization for XML documents and the
XMLSerializer interface.
@Sebmaster
Copy link
Member Author

Fixed feedback and rebased on master. Greeeen 🌈

@papandreou
Copy link
Contributor

Really looking forward to this, great work!

Is it intentional that jsdom.serialize(document) does not preserve the XML declaration?

const JSDOM = require('jsdom').JSDOM;
const text = `<?xml version="1.0" encoding="utf-8"?><foo></foo>`;
const jsdom = new JSDOM(text, {
  contentType: 'application/xhtml+xml'
});
const document = jsdom.window.document;
console.log(jsdom.serialize(document)); // <foo/>

I can easily work around that, but just wanted to check ;)

@domenic
Copy link
Member

domenic commented Oct 10, 2018

Hmm, there is no spec for serialize(), so we get to make up the rules. I tend to think it should be included?

I'll try to do that on my next jsdom weekend (and then merge this), unless anyone has counterobjections.

@papandreou
Copy link
Contributor

Thanks for the reply! For my case the most handy thing would be if the XML declaration were included if and only if it was in the string that was originally parsed, similar to how <!DOCTYPE ...> behaves for HTML:

const JSDOM = require('jsdom').JSDOM;
function roundTrip(text, contentType = 'text/html') {
  const jsdom = new JSDOM(text, { contentType });
  console.log(jsdom.serialize(jsdom.window.document));
}

roundTrip(`<html><head></head><body></body></html>`);
//         <html><head></head><body></body></html>

roundTrip(`<!DOCTYPE html><html><head></head><body></body></html>`);
//         <!DOCTYPE html><html><head></head><body></body></html>

roundTrip('<foo/>', 'application/xml');
//         <foo/>

roundTrip('<?xml version="1.0"?><foo/>', 'application/xml');
//         <foo/>

Would be great if the last one logged <?xml version="1.0"?><foo/>.

@domenic
Copy link
Member

domenic commented Oct 29, 2018

After some investigation it seems that the XML declaration is properly treated as non-existent in the DOM; it's basically a sort of invisible comment/header that identifies the file, and not part of the document object model. Thus, any serializations of the DOM must not include it.

As such, this is ready to merge as-is.

@domenic domenic merged commit 4b42f06 into master Oct 29, 2018
@domenic domenic deleted the feature/xmlserializer branch October 29, 2018 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants