Truly independent web browser
<!DOCTYPE html>
<script src="../../include.js"></script>
<style>
.item { --chosen: attr(data-color type(<color>)); color: rgb(1, 2, 3); }
.attribute .item { color: var(--chosen); }
.item > span { color: var(--chosen); }
#fallback { --length: attr(data-length type(<length>), 1px); width: var(--length); }
</style>
<div id="parent"><div class="item" id="first" data-color="green"><span id="child"></span></div><div class="item" id="second" data-color="purple"></div></div>
<div id="fallback-parent" style="--length: 1px"></div>
<script>
// A custom property declared with attr() reads the declaring element's attributes: two
// elements alike in everything else resolve their own values, a child inherits the resolved
// value, and an attribute change reaches the element the engine computes.
test(() => {
internals.updateStyle();
document.getElementById("parent").classList.add("attribute");
internals.updateStyle();
println(`colors: ${getComputedStyle(first).color} ${getComputedStyle(second).color} ${getComputedStyle(child).color}`);
first.dataset.color = "red";
internals.updateStyle();
println(`colors: ${getComputedStyle(first).color} ${getComputedStyle(second).color} ${getComputedStyle(child).color}`);
// A declaration whose attr() takes the fallback the parent holds resolves to the parent's
// environment, and still reads the attribute.
const fallback = document.createElement("div");
fallback.id = "fallback";
document.getElementById("fallback-parent").appendChild(fallback);
internals.updateStyle();
println(`fallback width: ${getComputedStyle(fallback).width}`);
fallback.dataset.length = "5px";
internals.updateStyle();
println(`attribute width: ${getComputedStyle(fallback).width}`);
});
</script>