See http://www.w3.org/TR/REC-xml for reference
| 225 | |
| 226 | // See http://www.w3.org/TR/REC-xml for reference |
| 227 | wxString XMLWriter::XMLEsc(const wxString & s) |
| 228 | { |
| 229 | wxString result; |
| 230 | int len = s.length(); |
| 231 | |
| 232 | for(int i=0; i<len; i++) { |
| 233 | wxUChar c = s.GetChar(i); |
| 234 | |
| 235 | switch (c) { |
| 236 | case wxT('\''): |
| 237 | result += wxT("'"); |
| 238 | break; |
| 239 | |
| 240 | case wxT('"'): |
| 241 | result += wxT("""); |
| 242 | break; |
| 243 | |
| 244 | case wxT('&'): |
| 245 | result += wxT("&"); |
| 246 | break; |
| 247 | |
| 248 | case wxT('<'): |
| 249 | result += wxT("<"); |
| 250 | break; |
| 251 | |
| 252 | case wxT('>'): |
| 253 | result += wxT(">"); |
| 254 | break; |
| 255 | |
| 256 | default: |
| 257 | if (sizeof(c) == 2 && c >= MIN_HIGH_SURROGATE && c <= MAX_HIGH_SURROGATE && i < len - 1) { |
| 258 | // If wxUChar is 2 bytes, then supplementary characters (those greater than U+FFFF) are represented |
| 259 | // with a high surrogate (U+D800..U+DBFF) followed by a low surrogate (U+DC00..U+DFFF). |
| 260 | // Handle those here. |
| 261 | wxUChar c2 = s.GetChar(++i); |
| 262 | if (c2 >= MIN_LOW_SURROGATE && c2 <= MAX_LOW_SURROGATE) { |
| 263 | // Surrogate pair found; simply add it to the output string. |
| 264 | result += c; |
| 265 | result += c2; |
| 266 | } |
| 267 | else { |
| 268 | // That high surrogate isn't paired, so ignore it. |
| 269 | i--; |
| 270 | } |
| 271 | } |
| 272 | else if (!wxIsprint(c)) { |
| 273 | //ignore several characters such ase eot (0x04) and stx (0x02) because it makes expat parser bail |
| 274 | //see xmltok.c in expat checkCharRefNumber() to see how expat bails on these chars. |
| 275 | //also see wxWidgets-2.8.12/src/expat/lib/asciitab.h to see which characters are nonxml compatible |
| 276 | //post decode (we can still encode '&' and '<' with this table, but it prevents us from encoding eot) |
| 277 | //everything is compatible past ascii 0x20 except for surrogates and the noncharacters U+FFFE and U+FFFF, |
| 278 | //so we don't check the compatibility table higher than this. |
| 279 | if((c> 0x1F || charXMLCompatiblity[c]!=0) && |
| 280 | (c < MIN_HIGH_SURROGATE || c > MAX_LOW_SURROGATE) && |
| 281 | c != NONCHARACTER_FFFE && c != NONCHARACTER_FFFF) |
| 282 | result += wxString::Format(wxT("&#x%04x;"), c); |
| 283 | } |
| 284 | else { |