Returns an XML-escaped copy of the input string str. If is_attribute is true, the text is meant to appear as an attribute value, and normalizable whitespace is preserved by replacing it with character references. Invalid XML characters in str, if any, are stripped from the output. It is expected that most, if not all, of the text processed by this module will consist of ordinary English text. If
| 4425 | // TODO(wan): It might be nice to have a minimally invasive, human-readable |
| 4426 | // escaping scheme for invalid characters, rather than dropping them. |
| 4427 | String XmlUnitTestResultPrinter::EscapeXml(const char* str, bool is_attribute) { |
| 4428 | Message m; |
| 4429 | |
| 4430 | if (str != NULL) { |
| 4431 | for (const char* src = str; *src; ++src) { |
| 4432 | switch (*src) { |
| 4433 | case '<': |
| 4434 | m << "<"; |
| 4435 | break; |
| 4436 | case '>': |
| 4437 | m << ">"; |
| 4438 | break; |
| 4439 | case '&': |
| 4440 | m << "&"; |
| 4441 | break; |
| 4442 | case '\'': |
| 4443 | if (is_attribute) |
| 4444 | m << "'"; |
| 4445 | else |
| 4446 | m << '\''; |
| 4447 | break; |
| 4448 | case '"': |
| 4449 | if (is_attribute) |
| 4450 | m << """; |
| 4451 | else |
| 4452 | m << '"'; |
| 4453 | break; |
| 4454 | default: |
| 4455 | if (IsValidXmlCharacter(*src)) { |
| 4456 | if (is_attribute && IsNormalizableWhitespace(*src)) |
| 4457 | m << String::Format("&#x%02X;", unsigned(*src)); |
| 4458 | else |
| 4459 | m << *src; |
| 4460 | } |
| 4461 | break; |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | return m.GetString(); |
| 4467 | } |
| 4468 | |
| 4469 | // Returns the given string with all characters invalid in XML removed. |
| 4470 | // Currently invalid characters are dropped from the string. An |