Skip to content

Truly independent web browser

3.3 KB
Raw Blame
<!DOCTYPE html>
<style>
    body {
        margin: 0;
    }
    .scroller {
        width: 150px;
        height: 100px;
        overflow-y: scroll;
        overflow-x: hidden;
    }
    iframe {
        display: block;
        border: 0;
    }
    #scroller-around-iframe iframe {
        width: 150px;
        height: 1000px;
    }
    #covered-scroller {
        position: absolute;
        left: 0;
        top: 150px;
    }
    #covering-iframe {
        position: absolute;
        left: 120px;
        top: 150px;
        width: 100px;
        height: 100px;
    }
    .content {
        height: 1000px;
    }
</style>
<div id="scroller-around-iframe" class="scroller"></div>
<div id="covered-scroller" class="scroller"><div class="content"></div></div>
<script src="../include.js"></script>
<script>
    function loadIframe(iframe, parent) {
        const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
        iframe.srcdoc = `<!DOCTYPE html><style>
            body { margin: 0; }
            #scroller { width: 30px; height: 100px; overflow-y: scroll; overflow-x: hidden; }
            #content { height: 1000px; }
        </style><div id="scroller"><div id="content"></div></div>`;
        parent.appendChild(iframe);
        return loaded;
    }

    // Drags down from near the top right corner of the element, where its vertical scrollbar's thumb is.
    async function dragFromTopRightCornerOf(element) {
        const rect = element.getBoundingClientRect();
        const x = rect.right - 3;
        await internals.mouseEventThroughUIProcess("mousemove", x, rect.top + 5);
        await internals.mouseEventThroughUIProcess("mousedown", x, rect.top + 5);
        await internals.mouseEventThroughUIProcess("mousemove", x, rect.top + 60, { buttons: 1 });
        await internals.mouseEventThroughUIProcess("mouseup", x, rect.top + 60);
        await animationFrame();
    }

    promiseTest(async () => {
        const iframeInScroller = document.createElement("iframe");
        await loadIframe(iframeInScroller, document.getElementById("scroller-around-iframe"));
        const coveringIframe = document.createElement("iframe");
        coveringIframe.id = "covering-iframe";
        await loadIframe(coveringIframe, document.body);
        await animationFrame();
        await animationFrame();

        // The scrollbar of a scroller is painted over the iframe in its content, so the scroller takes the drag.
        const scrollerAroundIframe = document.getElementById("scroller-around-iframe");
        await dragFromTopRightCornerOf(scrollerAroundIframe);
        println(`the scroller around the iframe scrolled: ${scrollerAroundIframe.scrollTop > 0}`);

        // An iframe painted over the scrollbar of a scroller takes the press, and the scrollbar of the iframe's own
        // scroller under the pointer takes the drag.
        const coveredScroller = document.getElementById("covered-scroller");
        const scrollerInCoveringIframe = coveringIframe.contentDocument.getElementById("scroller");
        await dragFromTopRightCornerOf(coveredScroller);
        println(`the scroller under the iframe did not scroll: ${coveredScroller.scrollTop === 0}`);
        println(`the scroller in the iframe scrolled: ${scrollerInCoveringIframe.scrollTop > 0}`);
    });
</script>