Skip to content

Truly independent web browser

2.2 KB
Raw Blame
<!DOCTYPE html>
<style>
    div {
        width: 100px;
        height: 100px;
    }
</style>
<script src="../include.js"></script>
<div id="target"></div>
<!-- The query point is beside the div, over nothing but the body. Without the empty-editable
     caret target, it resolves against this text's caret line instead of the div's, so the
     editable and non-editable answers differ. (A point over the div itself is at the div's start
     either way.) -->
<p id="text-below">some text below the target</p>
<script>
    test(() => {
        const { x, y, width, height } = target.getBoundingClientRect();
        const cx = x + width + 50;
        const cy = y + height / 2;
        const caretNodeBesideTarget = () => {
            const position = document.caretPositionFromPoint(cx, cy);
            return position ? `${position.offsetNode} @${position.offset}` : "null";
        };

        // Query once and then force a re-record, so the queries below run against
        // steady-state recordings rather than the initial one.
        caretNodeBesideTarget();
        document.body.style.outline = "1px solid transparent";

        // NB: Results are buffered and printed at the end: println() itself mutates the DOM,
        //     and any unrelated invalidation between a toggle and its query would rebuild the
        //     hit-test display list and mask a missed invalidation from the toggle itself.
        const results = [];
        results.push(`caret before toggle: ${caretNodeBesideTarget()}`);

        target.setAttribute("contenteditable", "");
        results.push(`caret with contenteditable: ${caretNodeBesideTarget()}`);

        target.removeAttribute("contenteditable");
        results.push(`caret after removal: ${caretNodeBesideTarget()}`);

        // designMode has no attribute-change path, so recomputing the editable flags is the
        // only invalidation these toggles get.
        document.designMode = "on";
        results.push(`caret with designMode: ${caretNodeBesideTarget()}`);

        document.designMode = "off";
        results.push(`caret after designMode off: ${caretNodeBesideTarget()}`);

        for (const result of results)
            println(result);
    });
</script>