BonMot
BonMot copied to clipboard
Warn when parsing XML on strings with unescaped XML entities
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> & <b>Sullivan</b>
. Other characters must be escaped too, as described here. Their solution:
extension String {
var xmlEscaped: String {
return replacingOccurrences(of: "&", with: "&")
.replacingOccurrences(of: "\"", with: """)
.replacingOccurrences(of: "'", with: "'")
.replacingOccurrences(of: ">", with: ">")
.replacingOccurrences(of: "<", with: "<")
}
}
This is fine, but also somewhat slow. It would be nice to:
- Make something a little nicer/faster
- See if we can detect unescaped entities and print some kind of warning.
The solution above will also escape the angle brackets.
oof yes that is a glaring omission in my original post 😅
No worries, and thanks for the great library you have created 👍🏼