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

Required escaping missing in version 2 #43

Closed
Allain55 opened this issue Oct 27, 2020 · 3 comments
Closed

Required escaping missing in version 2 #43

Allain55 opened this issue Oct 27, 2020 · 3 comments

Comments

@Allain55
Copy link

Hi, v2 doesn't escape the '&' sign everywhere, but '&' needs to be always escaped

For example:
"capacité"

Result in v1 (as it should be):
"capacité"

Result in v2 is unchanged, not escaped

In v1 I use these filters:

'<' : '&lt;',
'>' : '&gt;',
'"' : '&quot;',
'\'': '&apos;',
'&' : '&amp;'

In v2 using the same settings doesn't work

For reference see:
https://www.freeformatter.com/xml-escape.html

Related to #41

I added some checks to make sure things won't be double-encoded.

Maybe this caused the problem?

@davidcalhoun
Copy link
Owner

davidcalhoun commented Oct 27, 2020

Hi, thank you for the heads up! That is indeed what's happening - jstoxml sees that &eacute; (é) is already entity encoded, and it's leaving it as-is. The same thing happens when it runs across &amp; (&) - because it wouldn't make sense to output &amp;amp; (running it through again would unfortunately end up triple-encoding it to &amp;amp;amp;, and so on).

I am not sure what's going on in your use case, but I could see that it may be useful to force turning off the double-encoding check. Will look into what it would take to add that.

@Allain55
Copy link
Author

How about disallow double-encoding only for these values:

&amp; (&)
&lt; (<)
&gt; (>)
&quot; (")
&apos; (\)

@davidcalhoun
Copy link
Owner

Sorry for the delay! You can now achieve these sort of results by passing in a custom contentMap() function which runs after the the replacements have been made. In your case you should be able to do something like this:

toXML(obj, {
  contentMap: (val) => val.replaceAll('&', '&amp;')
});

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