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

Serialize XML declaration #13

Open
xmedeko opened this issue Sep 11, 2019 · 2 comments
Open

Serialize XML declaration #13

xmedeko opened this issue Sep 11, 2019 · 2 comments

Comments

@xmedeko
Copy link

xmedeko commented Sep 11, 2019

The XML declaration is not serialized, try the code:

let doc = new window.DOMParser().parseFromString('<?xml version="1.0" encoding="utf-8" standalone="yes"?><a>Ahoy</a>', 'application/xml');
console.log(new window.XMLSerializer().serializeToString(doc));

Chrome output: <?xml version="1.0" encoding="utf-8" standalone="yes"?><a>Ahoy</a>
jsdom 15.1.1, w3c-xmlserializer 1.1.2 output: <a>Ahoy</a>

@yingted
Copy link

yingted commented Dec 12, 2020

You can work around it like this:

let doc = new window.DOMParser().parseFromString('<a>Ahoy</a>', 'application/xml');
doc.insertBefore(doc.createProcessingInstruction('xml', 'version="1.0" encoding="utf-8" standalone="yes"'), doc.firstChild);
console.log(new window.XMLSerializer().serializeToString(doc));

@xmedeko
Copy link
Author

xmedeko commented Dec 13, 2020

@yingted I just do a simple workaround:

let str = new window.XMLSerializer().serializeToString(doc);
if (!str.startsWith("<?xml"))
    str = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>' + str;

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

No branches or pull requests

2 participants