| 11 | // ---------------------------------------------------------------------- |
| 12 | |
| 13 | function stringToXmlElement(str) { |
| 14 | let needsBase64Encoding = false |
| 15 | for (let i=0; i<str.length; i++) { |
| 16 | const c = str.charCodeAt(i) |
| 17 | if (c >= 0 && c <= 0x1F && c !== 0x09 && c !== 0x0A && c !== 0x0D) { |
| 18 | needsBase64Encoding = true |
| 19 | break |
| 20 | } |
| 21 | } |
| 22 | if (needsBase64Encoding === true) { |
| 23 | const encoder = new TextEncoder() |
| 24 | if (!/\p{Surrogate}/u.test(str) === false) { throw new Error(`Invalid string value`); } |
| 25 | const bytes = encoder.encode(str) |
| 26 | const binaryString = String.fromCodePoint(...bytes) |
| 27 | return "<B>"+btoa(binaryString)+"</B>" |
| 28 | } else { |
| 29 | return "<V>"+str.replace("&", "&").replace("<", "<")+"</V>" |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | export function rsvToXml(rows) { |
| 34 | return `<?xml version="1.0"?>\n<Rows>`+(rows.length > 0 ? "\n" : "") + |