1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 23:37:36 +00:00

LibJS: Evaluate replacement value before searching source string

The String.prototype.replace spec requires evaluating the replacement
value (if it is not a function) before searching the source string.

Fixes 4 test262 tests.
This commit is contained in:
Timothy Flynn 2021-07-06 10:49:36 -04:00 committed by Linus Groh
parent 65003241e4
commit 81fec49ac3
4 changed files with 67 additions and 0 deletions

View file

@ -158,3 +158,21 @@ test("replacement with substitution and 'groups' coerced to an object", () => {
expect(r[Symbol.replace]("ab", "[$<length>]")).toBe("a[3]");
});
test("replacement value is evaluated before searching the source string", () => {
var calls = 0;
var replaceValue = {
toString: function () {
calls += 1;
return "b";
},
};
var newString = "".replace("a", replaceValue);
expect(newString).toBe("");
expect(calls).toBe(1);
newString = "".replace(/a/g, replaceValue);
expect(newString).toBe("");
expect(calls).toBe(2);
});

View file

@ -104,3 +104,21 @@ test("functional regex replacement", () => {
})
).toBe("xd");
});
test("replacement value is evaluated before searching the source string", () => {
var calls = 0;
var replaceValue = {
toString: function () {
calls += 1;
return "b";
},
};
var newString = "".replaceAll("a", replaceValue);
expect(newString).toBe("");
expect(calls).toBe(1);
newString = "".replaceAll(/a/g, replaceValue);
expect(newString).toBe("");
expect(calls).toBe(2);
});