Skip to content

Truly independent web browser

3.8 KB
Raw Blame
for (const flag of ["u", "v"]) {
    test(`/${flag} zero-width searches skip surrogate interiors`, () => {
        const match = new RegExp("(?!πŸ˜€)", flag).exec("πŸ˜€");
        expect(match[0]).toBe("");
        expect(match.index).toBe(2);
        expect(new RegExp("(?!\\W)$", flag).test("πŸ˜€")).toBeTrue();
        expect(new RegExp("(?!\\W)(?=.)", flag).test("πŸ˜€")).toBeFalse();
    });

    for (const [source, indices, replacement] of [
        ["(?<g1>.{0}\\k<g1>)", [0, 2, 4, 6], "#πŸ˜€#πŸ˜€#πŸ˜€#"],
        ["(?<a>)\\k<a>(?<!.)", [0], "#πŸ˜€πŸ˜€πŸ˜€"],
        ["(?!\\W)", [6], "πŸ˜€πŸ˜€πŸ˜€#"],
        ["(?<!.)", [0], "#πŸ˜€πŸ˜€πŸ˜€"],
        ["\\B", [0, 2, 4, 6], "#πŸ˜€#πŸ˜€#πŸ˜€#"],
    ]) {
        test(`/${source}/g${flag} matches only at code-point boundaries`, () => {
            const matcher = new RegExp(source, "g" + flag);
            expect(Array.from("πŸ˜€πŸ˜€πŸ˜€".matchAll(matcher), match => match.index)).toEqual(indices);
            expect("πŸ˜€πŸ˜€πŸ˜€".match(matcher)).toEqual(indices.map(() => ""));
            expect("πŸ˜€πŸ˜€πŸ˜€".replace(matcher, "#")).toBe(replacement);
            expect("πŸ˜€πŸ˜€πŸ˜€".replace(matcher, () => "#")).toBe(replacement);
        });
    }

    test(`/${flag} searches retain lone surrogates`, () => {
        const matcher = new RegExp("(?!πŸ˜€)", "g" + flag);
        const input = "\uD83Da\uDE00πŸ˜€";
        expect(Array.from(input.matchAll(matcher), match => match.index)).toEqual([0, 1, 2, 5]);
    });

    for (const source of ["\\B", "(?!πŸ˜€)"]) {
        test(`/${source}/g${flag} searches past a pair containing lastIndex`, () => {
            const matcher = new RegExp(source, "dg" + flag);
            matcher.lastIndex = 2;
            const match = matcher.exec("AπŸ˜€");
            expect(match[0]).toBe("");
            expect(match.index).toBe(3);
            expect(match.indices).toEqual([[3, 3]]);
            expect(matcher.lastIndex).toBe(3);
        });

        for (const flags of ["y" + flag, "gy" + flag]) {
            test(`/${source}/${flags} does not retry inside a surrogate pair`, () => {
                const matcher = new RegExp(source, flags);
                matcher.lastIndex = 2;
                expect(matcher.exec("AπŸ˜€")).toBeNull();
                expect(matcher.lastIndex).toBe(0);

                matcher.lastIndex = 2;
                expect(matcher.test("AπŸ˜€")).toBeFalse();
                expect(matcher.lastIndex).toBe(0);
            });
        }

        test(`/${source}/y${flag} replacement does not split a surrogate pair`, () => {
            const matcher = new RegExp(source, "y" + flag);
            matcher.lastIndex = 2;
            expect("AπŸ˜€".replace(matcher, "#")).toBe("AπŸ˜€");
            expect(matcher.lastIndex).toBe(0);

            matcher.lastIndex = 2;
            expect("AπŸ˜€".replace(matcher, () => "#")).toBe("AπŸ˜€");
            expect(matcher.lastIndex).toBe(0);
        });
    }

    for (const flags of ["g" + flag, "y" + flag]) {
        for (const [source, matched, end] of [
            ["(?:)", "", 1],
            [".", "πŸ˜€", 3],
        ]) {
            test(`/${source}/${flags} starts at the code point containing lastIndex`, () => {
                const matcher = new RegExp(source, flags);
                matcher.lastIndex = 2;
                const match = matcher.exec("AπŸ˜€");
                expect(match[0]).toBe(matched);
                expect(match.index).toBe(1);
                expect(matcher.lastIndex).toBe(end);
            });
        }
    }
}

test("non-Unicode zero-width patterns can match inside surrogate pairs", () => {
    expect(Array.from("πŸ˜€".matchAll(/\B/g), match => match.index)).toEqual([0, 1, 2]);
    const matcher = /\B/y;
    matcher.lastIndex = 2;
    expect(matcher.exec("AπŸ˜€").index).toBe(2);
    expect(matcher.lastIndex).toBe(2);
});