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

XMLStringifier does not handle HTML character entities #97

Closed
feedler opened this issue Nov 23, 2015 · 6 comments
Closed

XMLStringifier does not handle HTML character entities #97

feedler opened this issue Nov 23, 2015 · 6 comments
Labels

Comments

@feedler
Copy link

feedler commented Nov 23, 2015

The stringifier replaces all occurences of & with & but does not consider that the string might contain HTML character entities itself (like for example ü or ü for the umlaut ü). The following change in the (javascript-) source code should fix this:

// Current source:
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');
// Fixed source:
return str.replace(/(?!&(\D+|#\d+);)&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');
@feedler
Copy link
Author

feedler commented Nov 25, 2015

Had a mistake in the regex myself. This version should work now:

return str.replace(/(?!&#?\S+;)&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\r/g, '&#xD;');

@oozcitak
Copy link
Owner

Are you aiming to avoid double encoding of &? Also isn't /(?!&\S+;)&/g sufficient in your regex?

@feedler
Copy link
Author

feedler commented Dec 10, 2015

Yes - that's what I'm aiming at. And you're right: your regex should be sufficient. :)

@oozcitak
Copy link
Owner

I am going to add a doubleEncode flag for this. It will default to true so that current default behavior will remain unchanged. When explicitly set to false it will use your regex, Would that work for you?

@feedler
Copy link
Author

feedler commented Dec 15, 2015

Yes, that will work - thank you very much!

@oozcitak
Copy link
Owner

You can now create the builder with noDoubleEncoding: true to preserve HTML entities.
See: https://github.com/oozcitak/xmlbuilder-js/blob/master/test/issues/97.coffee#L9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants