BonMot icon indicating copy to clipboard operation
BonMot copied to clipboard

Warn when parsing XML on strings with unescaped XML entities

Open ZevEisenberg opened this issue 5 years ago • 3 comments

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.

ZevEisenberg avatar Feb 25 '19 19:02 ZevEisenberg

The solution above will also escape the angle brackets.

bonkowski avatar Jan 26 '22 07:01 bonkowski

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

ZevEisenberg avatar Jan 26 '22 15:01 ZevEisenberg

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

bonkowski avatar Jan 27 '22 10:01 bonkowski