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

Add a new preserveXmlDeclaration parser option #31

Merged
merged 1 commit into from Jan 29, 2023
Merged

Add a new preserveXmlDeclaration parser option #31

merged 1 commit into from Jan 29, 2023

Conversation

rgrove
Copy link
Owner

@rgrove rgrove commented Jan 29, 2023

When true, an XmlDeclaration node representing the XML declaration (if there is one) will be included in the parsed document. When false, the XML declaration will be discarded. The default is false, which matches the behavior of previous versions.

This option is useful if you want to preserve the XML declaration when later serializing a document back to XML. Previously, the XML declaration was always discarded, which meant that if you parsed a document with an XML declaration and then serialized it, the original XML declaration would be lost.

const { parseXml } = require('@rgrove/parse-xml');

let xml = '<?xml version="1.0" encoding="UTF-8"?><root />';
let doc = parseXml(xml, { preserveXmlDeclaration: true });

console.log(doc.children[0].toJSON());
// => { type: 'xmldecl', version: '1.0', encoding: 'UTF-8' }

This is the first half of #30.

/cc @wooorm

@wooorm
Copy link

wooorm commented Jan 29, 2023

Very cool!!

@rgrove rgrove merged commit 038cbd6 into next Jan 29, 2023
@rgrove rgrove deleted the xml-decl branch January 29, 2023 21:56
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

2 participants