mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 13:57:35 +00:00
LibJS: Fix JSON.stringify with stale surrogate codepoints
This fix this test262 test: built-ins/JSON/stringify/value-string-escape-unicode.js
This commit is contained in:
parent
5aa803f7f2
commit
dc42ca37bd
2 changed files with 10 additions and 2 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonParser.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <AK/Utf16View.h>
|
||||
#include <AK/Utf8View.h>
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Array.h>
|
||||
|
@ -354,7 +355,6 @@ ThrowCompletionOr<String> JSONObject::serialize_json_array(GlobalObject& global_
|
|||
// 25.5.2.2 QuoteJSONString ( value ), https://tc39.es/ecma262/#sec-quotejsonstring
|
||||
String JSONObject::quote_json_string(String string)
|
||||
{
|
||||
// FIXME: Handle UTF16
|
||||
StringBuilder builder;
|
||||
builder.append('"');
|
||||
auto utf_view = Utf8View(string);
|
||||
|
@ -382,7 +382,7 @@ String JSONObject::quote_json_string(String string)
|
|||
builder.append("\\\\");
|
||||
break;
|
||||
default:
|
||||
if (code_point < 0x20) {
|
||||
if (code_point < 0x20 || Utf16View::is_high_surrogate(code_point) || Utf16View::is_low_surrogate(code_point)) {
|
||||
builder.appendff("\\u{:04x}", code_point);
|
||||
} else {
|
||||
builder.append_code_point(code_point);
|
||||
|
|
|
@ -63,6 +63,14 @@ describe("correct behavior", () => {
|
|||
o[sym] = "qux";
|
||||
expect(JSON.stringify(o)).toBe('{"foo":"bar"}');
|
||||
});
|
||||
|
||||
test("escape surrogate codepoints in strings", () => {
|
||||
expect(JSON.stringify("\ud83d\ude04")).toBe('"😄"');
|
||||
expect(JSON.stringify("\ud83d")).toBe('"\\ud83d"');
|
||||
expect(JSON.stringify("\ude04")).toBe('"\\ude04"');
|
||||
expect(JSON.stringify("\ud83d\ud83d\ude04\ud83d\ude04\ude04")).toBe('"\\ud83d😄😄\\ude04"');
|
||||
expect(JSON.stringify("\ude04\ud83d\ude04\ud83d\ude04\ud83d")).toBe('"\\ude04😄😄\\ud83d"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("errors", () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue