Skip to content

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

Open

Description

@ZevEisenberg

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.

Activity

  1. bonkowski commented on Jan 26, 2022

    @bonkowski

    The solution above will also escape the angle brackets.

  2. ZevEisenberg commented on Jan 26, 2022

    @ZevEisenberg
    CollaboratorAuthor

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

  3. bonkowski commented on Jan 27, 2022

    @bonkowski

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions