Skip to content

Truly independent web browser

42.4 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    // This fixture uses the styled runs, empty blocks, and images from the SCEditor front-page demo. The expected DOM
    // and selection endpoints were recorded by driving Chromium through WebDriver with the same selections and keys.
    test(() => {
        const selection = getSelection();
        const failures = [];
        let assertionCount = 0;
        let host = null;

        const originalHTML = '<div id="first">Give it a try! <img id="smile" alt=":)"><br></div><div id="empty"><br></div><div id="colors"><font id="red" color="#ff00">Red text and </font><font id="blue" color="#3399ff">blue text!</font><br></div><div id="last">Just type <strong>:</strong>) and it will be converted into <img id="lastimg" alt=":)"> as you type.</div>';

        function newHost(html = originalHTML) {
            if (host)
                host.remove();
            host = document.createElement("div");
            host.contentEditable = "true";
            host.innerHTML = html;
            document.body.appendChild(host);
            host.focus();
        }

        function path(node) {
            if (!node)
                return "null";
            const segments = [];
            while (node !== host) {
                segments.unshift(Array.prototype.indexOf.call(node.parentNode.childNodes, node));
                node = node.parentNode;
            }
            return `host${segments.length ? "/" + segments.join("/") : ""}`;
        }

        function selectionString() {
            return `${path(selection.anchorNode)}@${selection.anchorOffset}->${path(selection.focusNode)}@${selection.focusOffset}`;
        }

        function expect(actual, expected, label) {
            ++assertionCount;
            if (actual !== expected)
                failures.push(`${label}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
        }

        function expectState(label, html, selectionState) {
            expect(host.innerHTML, html, `${label} DOM`);
            expect(selectionString(), selectionState, `${label} selection`);
        }

        const styledReplacement = '<div id="first">Give it a try! <img id="smile" alt=":)"><br></div><div id="empty"><br></div><div id="colors"><font id="red" color="#ff00">Red X</font><font id="blue" color="#3399ff">&nbsp;text!</font><br></div><div id="last">Just type <strong>:</strong>) and it will be converted into <img id="lastimg" alt=":)"> as you type.</div>';
        newHost();
        selection.setBaseAndExtent(host.querySelector("#red").firstChild, 4, host.querySelector("#blue").firstChild, 4);
        internals.sendText(host, "X");
        expectState("replace across styled runs", styledReplacement, "host/2/0/0@5->host/2/0/0@5");
        expect(document.execCommand("undo"), true, "undo styled replacement");
        expectState("undo styled replacement", originalHTML, "host/2/0/0@4->host/2/1/0@4");
        expect(document.execCommand("redo"), true, "redo styled replacement");
        expectState("redo styled replacement", styledReplacement, "host/2/0/0@5->host/2/0/0@5");

        const imageReplacement = '<div id="first">Give it a try! <img id="smile" alt=":)"><br></div><div id="empty"><br></div><div id="colors"><font id="red" color="#ff00">Red text and </font><font id="blue" color="#3399ff">blue text!</font><br></div><div id="last">Just type <strong>:</strong>) and it will be convertX&nbsp;you type.</div>';
        newHost();
        const image = host.querySelector("#lastimg");
        const lastLine = host.querySelector("#last");
        selection.setBaseAndExtent(lastLine.childNodes[2], 24, lastLine.childNodes[4], 3);
        internals.sendText(host, "X");
        expectState("replace across an image", imageReplacement, "host/3/2@25->host/3/2@25");
        expect(document.execCommand("undo"), true, "undo image replacement");
        expectState("undo image replacement", originalHTML, "host/3/2@24->host/3/4@3");
        expect(host.querySelector("#lastimg"), image, "undo restores the same image node");
        expect(document.execCommand("redo"), true, "redo image replacement");
        expectState("redo image replacement", imageReplacement, "host/3/2@25->host/3/2@25");

        const emptyBlockReplacement = '<div id="first">Give it a try! <img id="smile" alt=":)"><br></div><div id="empty"><font id="red" color="#ff00">Xtext and </font><font id="blue" color="#3399ff">blue text!</font></div><div id="last">Just type <strong>:</strong>) and it will be converted into <img id="lastimg" alt=":)"> as you type.</div>';
        newHost();
        selection.setBaseAndExtent(host.querySelector("#empty"), 0, host.querySelector("#red").firstChild, 4);
        internals.sendText(host, "X");
        expectState("replace from an empty block into styled text", emptyBlockReplacement, "host/1/0/0@1->host/1/0/0@1");
        expect(document.execCommand("undo"), true, "undo empty-block replacement");
        expectState("undo empty-block replacement", originalHTML, "host/1@0->host/2/0/0@4");
        expect(document.execCommand("redo"), true, "redo empty-block replacement");
        expectState("redo empty-block replacement", emptyBlockReplacement, "host/1/0/0@1->host/1/0/0@1");

        // Replacing text inside a link retains that link for every code unit in a single insertText command.
        // These DOM and selection results were checked in Chromium, including reverse selections and Unicode.
        for (const [markup, textPath] of [
            ["Cloud", "host/0/0/0"],
            ["<b>Cloud</b>", "host/0/0/0/0"],
            ['<img width="45" height="60">Cloud', "host/0/0/1"],
        ]) {
            for (const [start, end] of [[0, 2], [1, 4], [3, 5]]) {
                for (const value of ["X", "XYZ", "😀", "e\u0301"]) {
                    for (const backwards of [false, true]) {
                        const html = `<p><a href="https://example.com">${markup}</a><br></p>`;
                        newHost(html);
                        const anchor = host.querySelector("a");
                        const text = anchor.lastChild.nodeType === Node.TEXT_NODE ? anchor.lastChild : anchor.lastChild.firstChild;
                        const from = backwards ? end : start;
                        const to = backwards ? start : end;
                        selection.setBaseAndExtent(text, from, text, to);
                        const label = `replace ${markup}[${start},${end}] with ${JSON.stringify(value)} backwards=${backwards}`;
                        document.execCommand("insertText", false, value);
                        const replacement = "Cloud".slice(0, start) + value + "Cloud".slice(end);
                        const result = html.replace("Cloud", replacement);
                        const caret = start + value.length;
                        expectState(label, result, `${textPath}@${caret}->${textPath}@${caret}`);
                        expect(document.execCommand("undo"), true, `undo ${label}`);
                        expectState(`undo ${label}`, html, `${textPath}@${from}->${textPath}@${to}`);
                        expect(document.execCommand("redo"), true, `redo ${label}`);
                        expectState(`redo ${label}`, result, `${textPath}@${caret}->${textPath}@${caret}`);
                        if (end === 5) {
                            // The replacement context belongs only to that command. Later typing at its end exits the link.
                            document.execCommand("insertText", false, "Q");
                            expect(host.querySelector("a").textContent, replacement, `typing after ${label} leaves link`);
                            expect(host.textContent, replacement + "Q", `typing after ${label} inserts outside`);
                        }
                    }
                }
            }
        }

        // Invisible siblings do not create another editable line beyond the first or last paragraph.
        for (const invisible of ["  ", "<!--empty-->", '<span style="display:none">hidden</span>']) {
            for (const forward of [false, true]) {
                const html = `${invisible}<p>Cloud</p>${invisible}`;
                newHost(html);
                const text = host.querySelector("p").firstChild;
                selection.collapse(text, forward ? text.length : 0);
                const before = selectionString();
                document.execCommand(forward ? "forwardDelete" : "delete");
                expectState(`delete past invisible sibling ${invisible} forward=${forward}`, html, before);
            }
        }

        // This fixture comes from Fragrantica's perfume insertion in SCEditor. Keep the editor's leading space,
        // linked image/name, and trailing NBSP, space, and placeholder. The image has fixed local dimensions so the
        // cases need no network resources. DOM and selection expectations come from headless Chrome keyboard input.
        const perfumeExpectedStates = {
            "before link insertText": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"><img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "before link delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0@0->host/0@0",
            ],
            "before link forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "link start insertText": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"><img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "link start delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@0->host/0/0@0",
            ],
            "link start forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "before image insertText": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"><img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "before image delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "before image forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "after image insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60">X Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@1->host/0/0/2@1",
            ],
            "after image delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "after image forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60">Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@2",
            ],
            "name start insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60">X Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@1->host/0/0/2@1",
            ],
            "name start delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "name start forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60">Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@2",
            ],
            "name middle insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink XAriana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@13->host/0/0/2@13",
            ],
            "name middle delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud PinkAriana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@11->host/0/0/2@11",
            ],
            "name middle forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink riana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@12->host/0/0/2@12",
            ],
            "name end insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>X&nbsp; <br></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "name end delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grand</a>&nbsp;&nbsp;<br></p>',
                "host/0/0/2@24->host/0/0/2@24",
            ],
            "name end forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a><br></p>',
                "host/0/0/2@25->host/0/0/2@25",
            ],
            "link end insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>X&nbsp; <br></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "link end delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grand</a>&nbsp;&nbsp;<br></p>',
                "host/0/0/2@24->host/0/0/2@24",
            ],
            "link end forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a><br></p>',
                "host/0/0/2@25->host/0/0/2@25",
            ],
            "after link insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>X&nbsp; <br></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "after link delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grand</a>&nbsp;&nbsp;<br></p>',
                "host/0/0/2@24->host/0/0/2@24",
            ],
            "after link forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a><br></p>',
                "host/0/0/2@25->host/0/0/2@25",
            ],
            "after space insertText": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp;X<br></p>',
                "host/0/1@2->host/0/1@2",
            ],
            "after space delete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a><br></p>',
                "host/0/0/2@25->host/0/0/2@25",
            ],
            "after space forwardDelete": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0@2->host/0@2",
            ],
            "shorten perfume name": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Gr</a><br></p>',
                "host/0/0/2@21->host/0/0/2@21",
            ],
            "type after shortening": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Gr</a>XYZ<br></p>',
                "host/0/1@3->host/0/1@3",
            ],
            "undo typing": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@25->host/0/0/2@21",
            ],
            "undo shortening": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@25->host/0/0/2@21",
            ],
            "redo shortening": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Gr</a>XYZ<br></p>',
                "host/0/1@3->host/0/1@3",
            ],
            "redo typing": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Gr</a>XYZ<br></p>',
                "host/0/1@3->host/0/1@3",
            ],
            "backspace through perfume": ["<p><br></p>", "host/0@0->host/0@0"],
            "type after deleting perfume": ["<p>X</p>", "host/0/0@1->host/0/0@1"],
            "selected image delete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "undo selected image delete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@2",
            ],
            "redo selected image delete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "selected image delete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "undo selected image delete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@1",
            ],
            "redo selected image delete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "selected image forwardDelete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "undo selected image forwardDelete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@2",
            ],
            "redo selected image forwardDelete backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "selected image forwardDelete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "undo selected image forwardDelete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@1",
            ],
            "redo selected image forwardDelete backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "selected image insertText backwards=false": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "undo selected image insertText backwards=false": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@2",
            ],
            "redo selected image insertText backwards=false": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "selected image insertText backwards=true": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "undo selected image insertText backwards=true": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@1",
            ],
            "redo selected image insertText backwards=true": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">&nbsp;Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "replace name suffix": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink X</a>&nbsp; <br></p>',
                "host/0/0/2@13->host/0/0/2@13",
            ],
            "replace image and partial name": [
                '<p>X<a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html">Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "bold link end": ['<p><a href="https://example.com"><b>Cloud</b></a>X<br></p>', "host/0/1@1->host/0/1@1"],
            "bold link middle": [
                '<p><a href="https://example.com"><b>CloXud</b></a><br></p>',
                "host/0/0/0/0@4->host/0/0/0/0@4",
            ],
            "block link end": [
                '<a style="display:block" href="https://example.com">CloudX</a>',
                "host/0/0@6->host/0/0@6",
            ],
            "link before internal break": [
                '<p><a href="https://example.com">CloudX<br>Pink</a></p>',
                "host/0/0/0@6->host/0/0/0@6",
            ],
            "link before trailing break": [
                '<p><a href="https://example.com">CloudX<br></a></p>',
                "host/0/0/0@6->host/0/0/0@6",
            ],
            "adjacent links": [
                '<p><a href="https://example.com/1">Cloud</a>X<a href="https://example.com/2">Pink</a></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "unlinked anchor": ['<p><a id="bookmark">CloudX</a><br></p>', "host/0/0/0@6->host/0/0/0@6"],
            "nested image delete": ['<p><a href="https://example.com">Cloud</a><br></p>', "host/0/0/0@0->host/0/0/0@0"],
            "undo nested image delete": [
                '<p><a href="https://example.com"><b><img width="45" height="60"></b>Cloud</a><br></p>',
                "host/0/0/0@1->host/0/0/0@0",
            ],
            "redo nested image delete": [
                '<p><a href="https://example.com">Cloud</a><br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "nested image forwardDelete": [
                '<p><a href="https://example.com">Cloud</a><br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "undo nested image forwardDelete": [
                '<p><a href="https://example.com"><b><img width="45" height="60"></b>Cloud</a><br></p>',
                "host/0/0/0@0->host/0/0/0@1",
            ],
            "redo nested image forwardDelete": [
                '<p><a href="https://example.com">Cloud</a><br></p>',
                "host/0/0/0@0->host/0/0/0@0",
            ],
            "delete supplementary perfume character": [
                '<p><a href="https://example.com">Cloud</a><br></p>',
                "host/0/0/0@5->host/0/0/0@5",
            ],
            "type after supplementary deletion": [
                '<p><a href="https://example.com">Cloud</a>X<br></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "replace complete link contents": ["<p>X&nbsp;&nbsp;<br></p>", "host/0/0@1->host/0/0@1"],
            "delete entire editor selection": ["<p><br></p>", "host/0@0->host/0@0"],
            "type after clearing editor": ["<p>X</p>", "host/0/0@1->host/0/0@1"],
            "move left across image": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@1->host/0/0@1",
            ],
            "move right across image": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@2",
            ],
            "move into name": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@1->host/0/0/2@1",
            ],
            "type after moving into name": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60">&nbsp;XCloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@2->host/0/0/2@2",
            ],
            "move left inside link end": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0/2@24->host/0/0/2@24",
            ],
            "type inside link after moving left": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana GrandXe</a>&nbsp; <br></p>',
                "host/0/0/2@25->host/0/0/2@25",
            ],
            "move right out of link": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/1@1->host/0/1@1",
            ],
            "type after moving out of link": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp;X<br></p>',
                "host/0/1@2->host/0/1@2",
            ],
            "cancel image backspace": [
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>',
                "host/0/0@2->host/0/0@2",
            ],
        };

        function perfumeScenarios() {
            const originalHTML =
                '<p><a href="https://www.fragrantica.com/perfume/Ariana-Grande/Cloud-Pink-83995.html"> <img width="45" height="60"> Cloud Pink Ariana Grande</a>&nbsp; <br></p>';
            const reset = (html = originalHTML) => newHost(html);
            function state(label) {
                const expected = perfumeExpectedStates[label];
                expectState(label, expected[0], expected[1]);
            }
            function command(name, value = "") {
                if (name === "insertText") internals.sendText(host, value);
                else if (name === "delete" || name === "forwardDelete")
                    internals.sendKey(host, name === "delete" ? "Backspace" : "Delete");
                else if (name === "ArrowLeft" || name === "ArrowRight")
                    internals.sendKey(host, name === "ArrowLeft" ? "Left" : "Right");
                else if (name === "Home" || name === "End")
                    internals.sendKey(host, name);
                else document.execCommand(name);
            }
            const link = () => host.querySelector("a");
            const name = () => link().lastChild;
            const points = {
                "before link": () => [host.firstChild, 0],
                "link start": () => [link(), 0],
                "before image": () => [link(), 1],
                "after image": () => [link(), 2],
                "name start": () => [name(), 0],
                "name middle": () => [name(), 12],
                "name end": () => [name(), name().length],
                "link end": () => [link(), link().childNodes.length],
                "after link": () => [host.firstChild, 1],
                "after space": () => [host.firstChild, 2],
            };
            for (const [label, point] of Object.entries(points)) {
                for (const action of ["insertText", "delete", "forwardDelete"]) {
                    reset();
                    selection.collapse(...point());
                    command(action, action === "insertText" ? "X" : "");
                    state(label + " " + action);
                }
            }
            reset();
            selection.collapse(host.firstChild, 2);
            for (let i = 0; i < 5; ++i) command("delete");
            state("shorten perfume name");
            command("insertText", "XYZ");
            state("type after shortening");
            command("undo");
            state("undo typing");
            command("undo");
            state("undo shortening");
            command("redo");
            state("redo shortening");
            command("redo");
            state("redo typing");
            reset();
            selection.collapse(host.firstChild, 2);
            for (let i = 0; i < 27; ++i) command("delete");
            state("backspace through perfume");
            command("insertText", "X");
            state("type after deleting perfume");
            for (const action of ["delete", "forwardDelete", "insertText"]) {
                for (const backwards of [false, true]) {
                    reset();
                    selection.setBaseAndExtent(link(), backwards ? 2 : 1, link(), backwards ? 1 : 2);
                    command(action, action === "insertText" ? "X" : "");
                    state("selected image " + action + " backwards=" + backwards);
                    command("undo");
                    state("undo selected image " + action + " backwards=" + backwards);
                    command("redo");
                    state("redo selected image " + action + " backwards=" + backwards);
                }
            }
            reset();
            selection.setBaseAndExtent(name(), 12, name(), name().length);
            command("insertText", "X");
            state("replace name suffix");
            reset();
            selection.setBaseAndExtent(link(), 1, name(), 12);
            command("insertText", "X");
            state("replace image and partial name");
            // Formatting, real line breaks, and editing boundaries must survive link-edge editing.
            for (const [label, html, selector, offset] of [
                ["bold link end", '<p><a href="https://example.com"><b>Cloud</b></a><br></p>', "b", 5],
                ["bold link middle", '<p><a href="https://example.com"><b>Cloud</b></a><br></p>', "b", 3],
                ["block link end", '<a style="display:block" href="https://example.com">Cloud</a>', "a", 5],
                ["link before internal break", '<p><a href="https://example.com">Cloud<br>Pink</a></p>', "a", 5],
                ["link before trailing break", '<p><a href="https://example.com">Cloud<br></a></p>', "a", 5],
                [
                    "adjacent links",
                    '<p><a href="https://example.com/1">Cloud</a><a href="https://example.com/2">Pink</a></p>',
                    "a",
                    5,
                ],
                ["unlinked anchor", '<p><a id="bookmark">Cloud</a><br></p>', "a", 5],
            ]) {
                reset(html);
                selection.collapse(host.querySelector(selector).firstChild, offset);
                command("insertText", "X");
                state(label);
            }
            for (const action of ["delete", "forwardDelete"]) {
                reset('<p><a href="https://example.com"><b><img width="45" height="60"></b>Cloud</a><br></p>');
                selection.collapse(host.querySelector("b"), action === "delete" ? 1 : 0);
                command(action);
                state("nested image " + action);
                command("undo");
                state("undo nested image " + action);
                command("redo");
                state("redo nested image " + action);
            }
            reset('<p><a href="https://example.com">Cloud🌸</a><br></p>');
            selection.collapse(link().firstChild, 7);
            command("delete");
            state("delete supplementary perfume character");
            command("insertText", "X");
            state("type after supplementary deletion");
            reset();
            selection.selectAllChildren(link());
            command("insertText", "X");
            state("replace complete link contents");
            reset();
            selection.selectAllChildren(host);
            command("delete");
            state("delete entire editor selection");
            command("insertText", "X");
            state("type after clearing editor");
            reset();
            selection.collapse(name(), 0);
            command("ArrowLeft");
            state("move left across image");
            command("ArrowRight");
            state("move right across image");
            command("ArrowRight");
            state("move into name");
            command("insertText", "X");
            state("type after moving into name");
            reset();
            selection.collapse(name(), name().length);
            command("ArrowLeft");
            state("move left inside link end");
            command("insertText", "X");
            state("type inside link after moving left");
            reset();
            selection.collapse(name(), name().length);
            command("ArrowRight");
            state("move right out of link");
            command("insertText", "X");
            state("type after moving out of link");
            reset();
            selection.collapse(link(), 2);
            host.addEventListener("beforeinput", event => event.preventDefault(), { once: true });
            command("delete");
            state("cancel image backspace");
            // Home must leave a painted caret before the linked image, even though its parent is a nonempty inline.
            // Deleting the character inserted there must not strand the caret in an exhausted whitespace text node.
            // Check display-list commands instead of waiting for blink phases, so this is stable on slow machines.
            function expectPaintedCaret(label) {
                const commands = internals.dumpDisplayList().split("\n").filter(line => line.includes("PaintCaret"));
                const rect = internals.currentCaretRect();
                expect(commands.length, 1, `${label} paints one blinking caret`);
                expect(rect !== null && rect.width > 0 && rect.height > 0, true, `${label} has caret geometry`);
                return rect;
            }
            reset();
            selection.collapse(host.firstChild, 2);
            command("Home");
            expectState("Home before inserted perfume", originalHTML, "host/0/0@1->host/0/0@1");
            const homeCaret = expectPaintedCaret("Home before perfume");
            const perfumeImageRect = host.querySelector("img").getBoundingClientRect();
            expect(homeCaret !== null && homeCaret.x === perfumeImageRect.left, true, "Home caret is before image");
            expect(homeCaret !== null && homeCaret.y <= perfumeImageRect.top && homeCaret.bottom >= perfumeImageRect.bottom,
                true, "Home caret spans the image line");
            command("insertText", "X");
            expectState("type after Home", originalHTML.replace('<p><a', '<p>X<a').replace('> <img', '><img'), "host/0/0@1->host/0/0@1");
            expectPaintedCaret("typing after Home");
            command("delete");
            expectState("backspace after Home typing", originalHTML.replace('> <img', '><img'), "host/0/0@0->host/0/0@0");
            const deletedCaret = expectPaintedCaret("backspace after Home typing");
            expect(deletedCaret !== null && deletedCaret.x === homeCaret?.x && deletedCaret.y === homeCaret?.y,
                true, "backspace returns to the same caret position");
            command("End");
            expectState("End after Home typing and backspace", originalHTML.replace('> <img', '><img'), "host/0/1@1->host/0/1@1");
            const endCaret = expectPaintedCaret("End after backspace");
            expect(endCaret !== null && endCaret.x > deletedCaret?.x, true, "End moves the painted caret past the perfume");

            for (const html of ["abc   <br>", "abc   ", "<span>abc </span><br>", "<span>abc </span><span> </span><br>"]) {
                reset(html);
                selection.collapse(host, 0);
                command("End");
                const text = host.firstChild.nodeType === Node.TEXT_NODE ? host.firstChild : host.firstChild.firstChild;
                expect(selection.anchorNode === text && selection.anchorOffset === 3, true,
                    `End stops before collapsed whitespace in ${html}`);
                expectPaintedCaret(`End in ${html}`);
            }

            // Both sides of an image need caret geometry in inline wrappers, including self-painting wrappers and
            // non-horizontal writing modes. Their caret must use the same display-list path as text carets.
            for (const [label, style, beforeEdge, afterEdge, coordinate] of [
                ["LTR", "", "left", "right", "x"],
                ["RTL", "direction:rtl", "right", "left", "x"],
                ["vertical", "writing-mode:vertical-rl", "top", "bottom", "y"],
            ]) {
                for (const positioned of [false, true]) {
                    reset(`<p style="${style}"><span style="${positioned ? "position:relative" : ""}"><img width="45" height="60"></span></p>`);
                    const wrapper = host.querySelector("span");
                    const imageRect = host.querySelector("img").getBoundingClientRect();
                    if (positioned) {
                        // Derive the expected position before moving the wrapper, independently of descendant rect reporting.
                        wrapper.style.left = "13px";
                        wrapper.style.top = "7px";
                        imageRect.x += 13;
                        imageRect.y += 7;
                        const movedImageRect = host.querySelector("img").getBoundingClientRect();
                        expect(movedImageRect.x === imageRect.x && movedImageRect.y === imageRect.y, true,
                            `${label} image follows positioned inline wrapper`);
                    }
                    for (const [offset, edge] of [[0, beforeEdge], [1, afterEdge]]) {
                        selection.collapse(wrapper, offset);
                        const labelWithSide = `${label} positioned=${positioned} image offset=${offset}`;
                        const rect = expectPaintedCaret(labelWithSide);
                        expect(rect !== null && rect[coordinate] === imageRect[edge], true, `${labelWithSide} is at image edge`);
                        expect(rect !== null && (coordinate === "x"
                            ? rect.y <= imageRect.top && rect.bottom >= imageRect.bottom
                            : rect.x <= imageRect.left && rect.right >= imageRect.right), true, `${labelWithSide} spans the image line`);
                    }
                }
            }
            host.remove();
        }
        perfumeScenarios();

        if (failures.length)
            println(
                `FAIL: ${failures.length}/${assertionCount} SCEditor rich-editing assertions failed\n${failures.join("\n")}`
            );
        else println(`PASS: ${assertionCount} SCEditor rich-editing assertions match Chromium`);
    });
</script>