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

Spaces getting lost after xml tags #136

Open
pokhiii opened this issue May 6, 2022 · 2 comments
Open

Spaces getting lost after xml tags #136

pokhiii opened this issue May 6, 2022 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@pokhiii
Copy link

pokhiii commented May 6, 2022

Describe the bug

const { fragment } = require('xmlbuilder2');

const mainTag = 'permissions';
const root = fragment({ encoding: 'UTF-8' }).ele(mainTag);

let content = '<copyright-statement>this copyright <bold>statement</bold> <italic>here</italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link></copyright-statement><license  xmlns:xlink="http://www.w3.org/1999/xlink" ><license-p>some license statement here <bold>statement</bold>  <italic>here  </italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link>h stuff </license-p></license>';

console.log(content)
content = root.ele(content).end()
console.log(content)

image

To Reproduce
The above code should suffice to reproduce the issue.

Expected behavior
Space should be preserved after tags.

Version:

  • node.js: [v17.6.0]
  • xmlbuilder2 [3.0.2]

Additional context
Add any other context about the problem here.

@pokhiii pokhiii added the bug Something isn't working label May 6, 2022
@pokhiii
Copy link
Author

pokhiii commented May 6, 2022

tried to come up with workaround using custom parser but not able to make it work completely. The content is getting XML encoded

const { fragment } = require('xmlbuilder2');

const mainTag = 'permissions';
let root = fragment({
	encoding: 'UTF-8',
	parser: { parse: (parent, contents) => {
		return parent.txt(contents)
	} },
}).ele(mainTag)
let content = '<copyright-statement>this copyright <bold>statement</bold> <italic>here</italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link></copyright-statement><license  xmlns:xlink="http://www.w3.org/1999/xlink" ><license-p>some license statement here <bold>statement</bold>  <italic>here  </italic> <ext-link xmlns:xlink="http://www.w3.org/1999/xlink" ext-link-type="uri" xlink:href="google.com">wit</ext-link>h stuff </license-p></license>';

console.log(content)
content  = root.ele(content).end()
console.log(content)

image

@jwasnoggin
Copy link

jwasnoggin commented Jan 18, 2024

I've got what I believe is a similar issue, but in this case elements that contain only a space are having the space removed after being converted to an object and back. To replicate:

import { convert } from 'xmlbuilder2';

const xml = `<t> </t>`;
const obj = convert(xml, { format: 'object' });
const newXml = convert({ encoding: 'UTF-8' }, obj);
console.log(newXml);

This results in <?xml version="1.0" encoding="UTF-8"?><t/>

After examining the code, I discovered that I change the initial convert line to solve the problem:

const obj = convert({skipWhitespaceOnlyText: false}, xml, { format: 'object' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants