OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 } | 59 } |
60 | 60 |
61 void writeIndent(int depth, StringBuilder* output) | 61 void writeIndent(int depth, StringBuilder* output) |
62 { | 62 { |
63 for (int i = 0; i < depth; ++i) | 63 for (int i = 0; i < depth; ++i) |
64 output->appendLiteral(" "); | 64 output->appendLiteral(" "); |
65 } | 65 } |
66 | 66 |
67 } // anonymous namespace | 67 } // anonymous namespace |
68 | 68 |
69 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) | 69 void escapeStringForJSON(const String& str, StringBuilder* dst) |
70 { | 70 { |
71 dst->append('"'); | |
72 for (unsigned i = 0; i < str.length(); ++i) { | 71 for (unsigned i = 0; i < str.length(); ++i) { |
73 UChar c = str[i]; | 72 UChar c = str[i]; |
74 if (!escapeChar(c, dst)) { | 73 if (!escapeChar(c, dst)) { |
75 if (c < 32 || c > 126 || c == '<' || c == '>') { | 74 if (c < 32 || c > 126 || c == '<' || c == '>') { |
76 // 1. Escaping <, > to prevent script execution. | 75 // 1. Escaping <, > to prevent script execution. |
77 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this | 76 // 2. Technically, we could also pass through c > 126 as UTF8, b
ut this |
78 // is also optional. It would also be a pain to implement her
e. | 77 // is also optional. It would also be a pain to implement her
e. |
79 unsigned symbol = static_cast<unsigned>(c); | 78 unsigned symbol = static_cast<unsigned>(c); |
80 String symbolCode = String::format("\\u%04X", symbol); | 79 String symbolCode = String::format("\\u%04X", symbol); |
81 dst->append(symbolCode); | 80 dst->append(symbolCode); |
82 } else { | 81 } else { |
83 dst->append(c); | 82 dst->append(c); |
84 } | 83 } |
85 } | 84 } |
86 } | 85 } |
| 86 } |
| 87 |
| 88 void doubleQuoteStringForJSON(const String& str, StringBuilder* dst) |
| 89 { |
| 90 dst->append('"'); |
| 91 escapeStringForJSON(str, dst); |
87 dst->append('"'); | 92 dst->append('"'); |
88 } | 93 } |
89 | 94 |
90 String JSONValue::quoteString(const String& input) | 95 String JSONValue::quoteString(const String& input) |
91 { | 96 { |
92 StringBuilder builder; | 97 StringBuilder builder; |
93 doubleQuoteStringForJSON(input, &builder); | 98 doubleQuoteStringForJSON(input, &builder); |
94 return builder.toString(); | 99 return builder.toString(); |
95 } | 100 } |
96 | 101 |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 m_data.append(value); | 525 m_data.append(value); |
521 } | 526 } |
522 | 527 |
523 PassRefPtr<JSONValue> JSONArrayBase::get(size_t index) | 528 PassRefPtr<JSONValue> JSONArrayBase::get(size_t index) |
524 { | 529 { |
525 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); | 530 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); |
526 return m_data[index]; | 531 return m_data[index]; |
527 } | 532 } |
528 | 533 |
529 } // namespace blink | 534 } // namespace blink |
OLD | NEW |