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

LibJS: Coerce named captures to an object before calling GetSubstitution

Per the spec, before invoking the GetSubstitution abstraction, the named
capture groups (if not undefined) should be coerced to an object via the
ToObject abstraction.
This commit is contained in:
Timothy Flynn 2021-07-06 09:06:49 -04:00 committed by Linus Groh
parent 424c7eaa40
commit 8fcdc57ae1
2 changed files with 24 additions and 1 deletions

View file

@ -139,3 +139,19 @@ test("replacement with substitution", () => {
expect("abc".replace(/(?<val1>a)b(?<val2>c)/, "$<val2>")).toBe("c");
expect("abc".replace(/(?<val1>a)b(?<val2>c)/, "$<val2>b$<val1>")).toBe("cba");
});
test("replacement with substitution and 'groups' coerced to an object", () => {
var r = /./;
var coercibleValue = {
length: 1,
0: "b",
index: 1,
groups: "123",
};
r.exec = function () {
return coercibleValue;
};
expect(r[Symbol.replace]("ab", "[$<length>]")).toBe("a[3]");
});