Skip to content

Truly independent web browser

4.0 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script src="frame-in-flight.js"></script>
<style>
    .item { height: 10px; }
    .tall { height: 25px; }
    [data-size="big"] { height: 40px; }
    #container { width: 300px; }
    #container.narrow { width: 200px; }
    #container.narrow > .item:last-child { color: rgb(0, 128, 0); }
    .item:first-child + .item { color: rgb(0, 0, 255); }
    [data-late="on"] { color: rgb(128, 0, 128); }
</style>
<div id="container"><div id="first" class="item">first</div><div id="second" class="item" data-size="small">second</div></div>
<script>
    // DOM mutations and attribute and class changes beside the frame whose style pass styles them: they change the tree
    // beside the frame, and the next style update sees every change, whichever elements the pass in flight styles.
    function describe(element) {
        const style = getComputedStyle(element);
        return `${element.id}: ${element.offsetWidth}x${element.offsetHeight} ${style.color}`;
    }

    asyncTest(async done => {
        const inserted = await whileRecordingInFlight(() => {
            first.classList.add("tall");
        }, state => {
            container.classList.add("narrow");
            second.setAttribute("data-size", "big");
            const afterAttributes = internals.frameSchedulerState();
            // Names no element has carried yet, one a rule matches and one nothing does.
            first.setAttribute("data-late", "on");
            first.setAttribute("data-never-matched", "yes");
            const afterFirstSeenNames = internals.frameSchedulerState();
            const third = document.createElement("div");
            third.id = "third";
            third.className = "item";
            third.textContent = "third";
            container.appendChild(third);
            return { state, afterAttributes, afterFirstSeenNames, afterInsertion: internals.frameSchedulerState() };
        });
        println(`insertion: state in the next task: ${inserted.state}`);
        println(`insertion: state after the attribute changes: ${inserted.afterAttributes}`);
        println(`insertion: state after first-seen attribute names: ${inserted.afterFirstSeenNames}`);
        println(`insertion: state after the insertion: ${inserted.afterInsertion}`);
        await twoFrames();
        println(`insertion: container height: ${container.offsetHeight}`);
        for (const element of container.children)
            println(`insertion: ${describe(element)}`);

        // The element the pass styles leaves the tree, a sibling combinator's subject changes, and a class goes away.
        const secondElement = second;
        const removed = await whileRecordingInFlight(() => {
            secondElement.classList.add("tall");
        }, state => {
            secondElement.remove();
            first.classList.remove("tall");
            first.firstChild.data = "first, changed";
            third.removeAttribute("class");
            return state;
        });
        println(`removal: state in the next task: ${removed}`);
        await twoFrames();
        println(`removal: container height: ${container.offsetHeight}`);
        println(`removal: second connected: ${secondElement.isConnected}`);
        for (const element of container.children)
            println(`removal: ${describe(element)}`);

        // The whole subtree is replaced beside the frame.
        const replaced = await whileRecordingInFlight(() => {
            container.classList.remove("narrow");
        }, state => {
            container.innerHTML = `<div id="fourth" class="item">fourth</div><div id="fifth" class="item" data-size="big">fifth</div>`;
            container.classList.add("narrow");
            return state;
        });
        println(`replacement: state in the next task: ${replaced}`);
        await twoFrames();
        println(`replacement: children: ${container.children.length}`);
        for (const element of container.children)
            println(`replacement: ${describe(element)}`);
        done();
    });
</script>