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

Warn when parsing XML on strings with unescaped XML entities #358

Open
ZevEisenberg opened this issue Feb 25, 2019 · 3 comments
Open

Warn when parsing XML on strings with unescaped XML entities #358

ZevEisenberg opened this issue Feb 25, 2019 · 3 comments

Comments

@ZevEisenberg
Copy link
Collaborator

If a string looks like <b>Gilbert</b> & <b>Sullivan</b>, the unescaped & will break XML parsing. The string must be escaped like this: <b>Gilbert</b> &amp; <b>Sullivan</b>. Other characters must be escaped too, as described here. Their solution:

extension String {
    var xmlEscaped: String {
        return replacingOccurrences(of: "&", with: "&amp;")
            .replacingOccurrences(of: "\"", with: "&quot;")
            .replacingOccurrences(of: "'", with: "&#39;")
            .replacingOccurrences(of: ">", with: "&gt;")
            .replacingOccurrences(of: "<", with: "&lt;")
    }
}

This is fine, but also somewhat slow. It would be nice to:

  1. Make something a little nicer/faster
  2. See if we can detect unescaped entities and print some kind of warning.
@bonkowski
Copy link

The solution above will also escape the angle brackets.

@ZevEisenberg
Copy link
Collaborator Author

oof yes that is a glaring omission in my original post 😅

@bonkowski
Copy link

No worries, and thanks for the great library you have created 👍🏼

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