Truly independent web browser
<!DOCTYPE html>
<meta charset="utf-8">
<script src="../../include.js"></script>
<script src="../../work-test.js"></script>
<script src="structural-matrix.js"></script>
<script>
test(() => {
// Verifies that updating the document URL doesn't trigger a full document style invalidation. Links whose
// :local-link match state actually flipped should be recomputed.
const NUM_LINKS = 50;
for (let i = 0; i < NUM_LINKS; i++) {
document.body.appendChild(makeElement("a", { attributes: { href: `https://example.com/page${i}` } }));
}
document.body.appendChild(makeElement("a", { id: "same-doc", attributes: { href: "#target" } }));
internals.updateStyle();
// Without any :local-link rule, a URL change should produce no invalidation work at all.
printPassWithStyleWork("no :local-link rule", styleWork(() => history.pushState({}, "", "#initial")));
const documentStyle = addStyle(document.head, `a:local-link { color: rgb(0, 128, 0); }`);
internals.updateStyle();
// With a :local-link rule, navigating to the targeted fragment must restyle only the matching anchor, the
// other 50 anchors and the rest of the document are untouched.
printPassWithStyleWork(":local-link rule, anchor flips", styleWork(() => history.pushState({}, "", "#target")));
println(`same-doc color: ${getComputedStyle(document.getElementById("same-doc")).color}`);
// A :local-link rule that lives only inside a shadow root must still trigger invalidation for shadow
// anchors when their match state flips on URL change.
//
// FIXME: This should style only the shadow anchor. The style engine also styles #same-doc, whose
// :local-link state flips back though no rule for it remains, so it is named.
documentStyle.remove();
const host = makeElement("div");
document.body.appendChild(host);
const shadowRoot = host.attachShadow({ mode: "open" });
addStyle(shadowRoot, `a:local-link { color: rgb(0, 0, 128); }`);
shadowRoot.appendChild(makeElement("a", { id: "shadow-anchor", attributes: { href: "#shadow-target" } }));
internals.updateStyle();
printPassWithStyleWork("shadow-only :local-link rule, shadow anchor flips",
styleWork(() => history.pushState({}, "", "#shadow-target")));
println(`shadow-anchor color: ${getComputedStyle(shadowRoot.getElementById("shadow-anchor")).color}`);
});
</script>