Skip to content

Truly independent web browser

2.0 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<style>
    ::selection { color: rgb(10, 20, 30); background-color: rgb(40, 50, 60); }
</style>
<div id="unrelated"></div>
<div id="editor" contenteditable>text</div>
<script>
    test(() => {
        for (let index = 0; index < 64; ++index) {
            const child = unrelated.appendChild(document.createElement("div"));
            child.textContent = "selectable text";
        }
        editor.focus();
        const selection = document.getSelection();
        selection.collapse(editor.firstChild, 4);
        internals.updateStyle();

        const restyledNames = work => `[${work.restyled().map(shortStyleName).join(", ")}]`;

        function type(label, text) {
            const work = styleWork(() => internals.sendText(editor, text));
            println(`${label} restyled: ${restyledNames(work)}`);
            println(`${label} leaves a caret: ${selection.isCollapsed}`);
        }

        type("typing", "a");
        type("repeated typing", "b");
        document.execCommand("bold");
        type("typing with a formatting override", "c");
        println(`inserted text: ${editor.textContent}`);
        println(`formatting preserved: ${getComputedStyle(selection.anchorNode.parentElement).fontWeight}`);
        println(`selection query: ${getComputedStyle(editor, "::selection").color}`);

        let inputHandlerSelection;
        editor.addEventListener("input", () => {
            selection.selectAllChildren(editor);
            internals.updateStyle();
            inputHandlerSelection = getComputedStyle(editor, "::selection").backgroundColor;
        }, { once: true });
        const selectionWork = styleWork(() => internals.sendText(editor, "d"), internals, { flush: false });
        println(`input handler selection: ${inputHandlerSelection}`);
        println(`input handler leaves a selection: ${!selection.isCollapsed}`);
        println(`selection styles activated: ${restyledNames(selectionWork)}`);
    });
</script>