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

\r encoding in element txt #24

Closed
neilcampbell opened this issue Jun 17, 2020 · 3 comments
Closed

\r encoding in element txt #24

neilcampbell opened this issue Jun 17, 2020 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@neilcampbell
Copy link

Describe the bug
I am building some xml which sometimes has \r\n characters in the text, which results in what feels like some inconsistent encoding behaviour. It appears to be due to https://github.com/oozcitak/xmlbuilder2/blob/master/src/writers/BaseWriter.ts#L937

I have tried removing the above line and no tests break, so I'm trying to work out why it exists? Happy to submit a PR if you think it can be removed.

To Reproduce

import { create } from 'xmlbuilder2';

const root = create().ele('parent', {
  text: 'Hello \r\n'
});
root
  .ele('child')
  .txt('Line 1\r\nLine 2\r\nLine 3');

console.log(root.end({
  prettyPrint: true,
  headless: true,
  newline: '\r\n',
  noDoubleEncoding: true,
}));

Actual Output

<parent text="Hello &#xD;&#xA;">
  <child>Line 1&#xD;
Line 2&#xD;
Line 3</child>
</parent>

Expected behavior
When I set the line endings to \r\n, I'd expect both the \r and \n to not be encoded within the element text. Currently only the \n is not being encoded.

Expected Output

<parent text="Hello &#xD;&#xA;">
  <child>Line 1
Line 2
Line 3</child>
</parent>

Version:

  • node.js: [12.6.0]
  • xmlbuilder2 [2.1.4]
@neilcampbell neilcampbell added the bug Something isn't working label Jun 17, 2020
@neilcampbell neilcampbell changed the title \r escaping in element txt \r encoding in element txt Jun 17, 2020
@oozcitak
Copy link
Owner

oozcitak commented Jun 17, 2020

I have tried removing the above line and no tests break, so I'm trying to work out why it exists? Happy to submit a PR if you think it can be removed.

They should probably be removed (there is also a similar check for attribute values). But let me check the spec and get back to you.

@neilcampbell
Copy link
Author

@oozcitak Awesome, thanks heaps!

oozcitak added a commit that referenced this issue Jun 18, 2020
@neilcampbell
Copy link
Author

neilcampbell commented Jun 19, 2020

@oozcitak Just looking at 87adb72. Is it valid to have an attribute value containing \r or \n chars without them being encoded? I would have thought <parent text="Hello \r\n"> should be <parent text="Hello &#xD;&#xA;">

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

2 participants