Skip to content

Truly independent web browser

2.9 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
    promiseTest(async () => {
        const platformControl = internals.MOD_CTRL | internals.MOD_SUPER;
        const platformControlShift = platformControl | internals.MOD_SHIFT;
        const selection = getSelection();
        const originalHTML = `<ul id="source"><li>A<br></li><li>With two items<br></li></ul><div id="target">x</div>`;
        const pastedHTML = `<ul id="source"><li>A<br></li><li>With two items<br></li></ul><div id="target">x</div><ul id="source"><li>A<br></li><li>With two items</li></ul>`;

        const host = document.createElement("div");
        host.contentEditable = "true";
        host.innerHTML = originalHTML;
        document.body.appendChild(host);
        host.focus();

        function selectionString() {
            const segments = [];
            let node = selection.anchorNode;
            while (node !== host) {
                segments.unshift(Array.prototype.indexOf.call(node.parentNode.childNodes, node));
                node = node.parentNode;
            }
            return `host/${segments.join("/")}@${selection.anchorOffset}`;
        }

        const source = host.querySelector("#source");
        selection.setBaseAndExtent(source.firstElementChild.firstChild, 0, source.lastElementChild, 2);
        internals.sendKey(host, "C", platformControl);
        await readClipboardFromPaste(host);

        selection.collapse(host.querySelector("#target").firstChild, 1);
        let clipboardHTML;
        host.addEventListener("paste", event => {
            clipboardHTML = event.clipboardData.getData("text/html");
        }, { once: true });
        const pasteInput = new Promise(resolve => host.addEventListener("input", event => {
            if (event.inputType === "insertFromPaste")
                resolve();
        }, { once: true }));
        internals.sendKey(host, "V", platformControl);
        await pasteInput;
        println(clipboardHTML.endsWith(`<li>With two items</li></ul>`) ? "PASS: clipboard omits terminal placeholder" : `FAIL: clipboard markup: ${clipboardHTML}`);
        println(host.innerHTML === pastedHTML ? "PASS: paste DOM" : `FAIL: paste DOM: ${host.innerHTML}`);
        println(selectionString() === "host/2/1/0@14" ? "PASS: paste selection" : `FAIL: paste selection: ${selectionString()}`);

        internals.sendKey(host, "Z", platformControl);
        println(host.innerHTML === originalHTML ? "PASS: undo DOM" : `FAIL: undo DOM: ${host.innerHTML}`);
        println(selectionString() === "host/1/0@1" ? "PASS: undo selection" : `FAIL: undo selection: ${selectionString()}`);

        internals.sendKey(host, "Z", platformControlShift);
        println(host.innerHTML === pastedHTML ? "PASS: redo DOM" : `FAIL: redo DOM: ${host.innerHTML}`);
        println(selectionString() === "host/2/1/0@14" ? "PASS: redo selection" : `FAIL: redo selection: ${selectionString()}`);
    });
</script>