Skip to content

Commit 4bb4984

Browse files
Fix <link> stylesheet cascade and refetch with moveBefore
This fixes two bugs during state-preserving atomic moves on <link> elements, one where we weren't invaliding the stylesheet properly and another where we were incorrectly re-fetching the resource. The first bug is during RemovedFrom - we need to update the cascade order via RemoveStyleSheetCandidateNode. The refetch bug initiates in InsertedInto, where we need to early-exit in LinkStyle::Process to avoid re-fetching. This adds two WPT's that both pass in Gecko and fail in Blink without this change. stylesheet-cascade-order.html fails with: This is a testharness.js-based test. [FAIL] Moving loaded link stylesheets updates their cascade order assert_equals: removing the purple stylesheet makes blue win again expected "rgb(0, 0, 255)" but got "rgb(0, 0, 0)" stylesheet-no-refetch.html fails with: [FAIL] Moving a loaded link stylesheet preserves its resource assert_equals: moving the stylesheet does not fetch it again expected 1 but got 2 Harness: the test ran to completion. Change-Id: Ie94630259a09fd6d4ae84e4b3b1be152c8158222 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8236492 Reviewed-by: Noam Rosenthal <[email protected]> Commit-Queue: Kurt Catti-Schmidt <[email protected]> Cr-Commit-Position: refs/heads/main@{#1688119}
1 parent bc957db commit 4bb4984

6 files changed

Lines changed: 88 additions & 0 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.target {
2+
color: blue;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cache-Control: no-store
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.target {
2+
color: purple;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cache-Control: no-store
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<!DOCTYPE html>
2+
<title>moveBefore() updates stylesheet cascade order</title>
3+
<link rel="help" href="https://dom.spec.whatwg.org/#dom-parentnode-movebefore">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
7+
<div id="target" class="target">target</div>
8+
9+
<script>
10+
function createStylesheet(url) {
11+
const link = document.createElement("link");
12+
link.rel = "stylesheet";
13+
link.href = url;
14+
return link;
15+
}
16+
17+
promise_test(async t => {
18+
const purple =
19+
createStylesheet("resources/stylesheet-cascade-purple.css");
20+
const blue =
21+
createStylesheet("resources/stylesheet-cascade-blue.css");
22+
23+
t.add_cleanup(() => {
24+
purple.remove();
25+
blue.remove();
26+
});
27+
28+
const purpleEvents = new EventWatcher(t, purple, ["load", "error"]);
29+
const blueEvents = new EventWatcher(t, blue, ["load", "error"]);
30+
const loaded = Promise.all([
31+
purpleEvents.wait_for("load"),
32+
blueEvents.wait_for("load"),
33+
]);
34+
document.head.append(purple, blue);
35+
await loaded;
36+
37+
const target = document.getElementById("target");
38+
assert_equals(getComputedStyle(target).color, "rgb(0, 0, 255)",
39+
"the second stylesheet initially wins");
40+
41+
document.head.moveBefore(blue, purple);
42+
43+
assert_equals(getComputedStyle(target).color, "rgb(128, 0, 128)",
44+
"moving the blue stylesheet first makes purple win");
45+
46+
purple.remove();
47+
assert_equals(getComputedStyle(target).color, "rgb(0, 0, 255)",
48+
"removing the purple stylesheet makes blue win again");
49+
}, "Moving loaded link stylesheets updates their cascade order");
50+
</script>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<title>moveBefore() does not refetch a loaded stylesheet</title>
3+
<link rel="help" href="https://dom.spec.whatwg.org/#dom-parentnode-movebefore">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
7+
<script>
8+
promise_test(async t => {
9+
const link = document.createElement("link");
10+
link.rel = "stylesheet";
11+
link.href = "resources/stylesheet-cascade-blue.css";
12+
t.add_cleanup(() => link.remove());
13+
14+
const loaded = new Promise((resolve, reject) => {
15+
link.addEventListener("load", resolve, {once: true});
16+
link.addEventListener("error", reject, {once: true});
17+
});
18+
document.head.append(link);
19+
await loaded;
20+
21+
assert_equals(performance.getEntriesByName(link.href).length, 1,
22+
"the stylesheet is fetched once");
23+
24+
document.head.moveBefore(link, document.head.firstChild);
25+
await new Promise(resolve => t.step_timeout(resolve, 100));
26+
27+
assert_equals(performance.getEntriesByName(link.href).length, 1,
28+
"moving the stylesheet does not fetch it again");
29+
}, "Moving a loaded link stylesheet preserves its resource");
30+
</script>

0 commit comments

Comments
 (0)