extension PureXML.Emitting { /// The shared XML output escaping rules, used by both the tree serializer and /// the incremental writer so their output is identical. Element content /// escapes the markup characters; attribute values additionally escape the /// quote and the whitespace characters as numeric references (matching /// libxml2), so attributes survive attribute-value normalization. enum Escaping { static func text(_ value: String) -> String { var result = "" for character in value { switch character { case "&": result += "&" case "<": result += "<" case ">": result += ">" default: result.append(character) } } return result } static func attribute(_ value: String, quote: Character = "\"") -> String { var result = "" for character in value { if character == quote { result += quote == "'" ? "'" : """ continue } switch character { case "&": result += "&" case "<": result += "<" case ">": result += ">" case "\t": result += " " case "\n": result += " " case "\r": result += " " default: result.append(character) } } return result } } }