Skip to content

Commit bc957db

Browse files
bwaldermanchromium-wpt-export-bot
authored andcommitted
[css-mixins] Give each effective MixinMap a unique identity
@apply is flattened into the RuleSet built for a stylesheet, so a RuleSet is only valid for the exact set of mixins it was flattened against. That set is per tree scope, but StyleSheetContents caches a single RuleSet and is shared between tree scopes (inline <style> text cache, adopted stylesheets) and between Documents (the parsed stylesheet cache on CSSStyleSheetResource). A RuleSet therefore records the identity of the mixin map it was built against; see RuleSet::DependingOnOutdatedMixins(). That identity used to be a counter owned by StyleSheetCollection, so two scopes could hand out the same value for different sets of mixins and reuse each other's flattened RuleSet, leaving the winning mixin dependent on which scope built its RuleSet first. It is now allocated by MixinMap::AllocateMapIdentifier(), unique across every scope and Document on the renderer main thread, and renamed from "generation" to "map_identifier" to reflect that it is an opaque identity. This also fixes a second bug: RuleSet recorded a dependency on the mixin map only when an @apply resolved to a mixin, so a RuleSet flattened where the mixin is undefined looked mixin-independent and leaked into scopes that do define it. The identity is now recorded for every @apply, and AddRulesFromSheet() no longer clears it, as it recurses for @import. Bug: 496807669 Test: external/wpt/css/css-mixins/mixins/mixin-shadow-dom.html Test: external/wpt/css/css-mixins/mixins/mixin-shared-across-documents.html Change-Id: Id4ad99b6bbd717a82a86ba3b98cdb27b6b2be4c3 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8252237 Reviewed-by: Rune Lillesveen <[email protected]> Commit-Queue: Brandon Walderman <[email protected]> Reviewed-by: Kurt Catti-Schmidt <[email protected]> Cr-Commit-Position: refs/heads/main@{#1684976}
1 parent a2dc05d commit bc957db

6 files changed

Lines changed: 335 additions & 0 deletions

‎css/css-mixins/mixins/mixin-shadow-dom.html‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,209 @@
111111
}, 'Style outside shadow DOM should _not_ have access to inside mixins');
112112
</script>
113113

114+
<script>
115+
function addMixinHost(name, localColor, sharedSheet) {
116+
const host = document.createElement('div');
117+
document.body.append(host);
118+
const root = host.attachShadow({mode: 'open'});
119+
if (localColor) {
120+
const localStyle = document.createElement('style');
121+
localStyle.textContent = `
122+
@mixin ${name}() {
123+
@result {
124+
color: ${localColor};
125+
}
126+
}
127+
`;
128+
root.append(localStyle);
129+
}
130+
const target = document.createElement('div');
131+
target.id = 'target';
132+
root.append(target);
133+
if (sharedSheet) {
134+
root.adoptedStyleSheets = [sharedSheet];
135+
} else {
136+
const applyStyle = document.createElement('style');
137+
applyStyle.textContent = `
138+
#target {
139+
color: red;
140+
@apply ${name};
141+
}
142+
`;
143+
root.prepend(applyStyle);
144+
}
145+
return target;
146+
}
147+
148+
function testSharedApplySheet(name, localFirst, constructed) {
149+
const documentStyle = document.createElement('style');
150+
documentStyle.textContent = `
151+
@mixin ${name}() {
152+
@result {
153+
color: green;
154+
}
155+
}
156+
`;
157+
document.head.append(documentStyle);
158+
159+
let sharedSheet = null;
160+
if (constructed) {
161+
sharedSheet = new CSSStyleSheet();
162+
sharedSheet.replaceSync(`
163+
#target {
164+
color: red;
165+
@apply ${name};
166+
}
167+
`);
168+
}
169+
170+
const first = localFirst
171+
? addMixinHost(name, 'blue', sharedSheet)
172+
: addMixinHost(name, null, sharedSheet);
173+
const second = localFirst
174+
? addMixinHost(name, null, sharedSheet)
175+
: addMixinHost(name, 'blue', sharedSheet);
176+
const documentTarget = localFirst ? second : first;
177+
const localTarget = localFirst ? first : second;
178+
179+
assert_equals(getComputedStyle(documentTarget).color,
180+
'rgb(0, 128, 0)');
181+
assert_equals(getComputedStyle(localTarget).color, 'rgb(0, 0, 255)');
182+
}
183+
184+
test(() => {
185+
testSharedApplySheet('--shared-inline-document-first', false, false);
186+
}, 'Shared inline @apply contents use the correct mixin in each scope');
187+
188+
test(() => {
189+
testSharedApplySheet('--shared-inline-local-first', true, false);
190+
}, 'Shared inline @apply contents do not depend on scope creation order');
191+
192+
test(() => {
193+
testSharedApplySheet('--shared-constructed-document-first', false,
194+
true);
195+
}, 'Shared constructed @apply sheet uses the correct mixin in each scope');
196+
197+
test(() => {
198+
testSharedApplySheet('--shared-constructed-local-first', true, true);
199+
}, 'Shared constructed @apply sheet does not depend on scope creation order');
200+
201+
// A scope where a mixin is undefined must not affect a scope that does
202+
// define it (and vice versa).
203+
function testUndefinedInOneScope(name, undefinedFirst) {
204+
const sheet = new CSSStyleSheet();
205+
sheet.replaceSync(`#target { color: red; @apply ${name}; }`);
206+
207+
function check(target, expected) {
208+
assert_equals(getComputedStyle(target).color, expected);
209+
}
210+
211+
if (undefinedFirst) {
212+
check(addMixinHost(name, null, sheet), 'rgb(255, 0, 0)');
213+
check(addMixinHost(name, 'blue', sheet), 'rgb(0, 0, 255)');
214+
} else {
215+
check(addMixinHost(name, 'blue', sheet), 'rgb(0, 0, 255)');
216+
check(addMixinHost(name, null, sheet), 'rgb(255, 0, 0)');
217+
}
218+
}
219+
220+
test(() => {
221+
testUndefinedInOneScope('--undefined-scope-first', true);
222+
}, 'An @apply of an undefined mixin does not affect a scope that defines it');
223+
224+
test(() => {
225+
testUndefinedInOneScope('--defined-scope-first', false);
226+
}, 'An @apply of a defined mixin does not leak into a scope without it');
227+
</script>
228+
229+
<style>
230+
@mixin --nearest-scope() {
231+
@result {
232+
color: green;
233+
}
234+
}
235+
</style>
236+
<div id="host5">
237+
<template shadowrootmode="open">
238+
<style>
239+
@mixin --nearest-scope() {
240+
@result {
241+
color: blue;
242+
}
243+
}
244+
#target {
245+
color: red;
246+
@apply --nearest-scope;
247+
}
248+
</style>
249+
<div id="target">This text should be blue.</div>
250+
</template>
251+
</div>
252+
<script>
253+
test(() => {
254+
const localTarget =
255+
document.getElementById('host5').shadowRoot.getElementById('target');
256+
assert_equals(getComputedStyle(localTarget).color, 'rgb(0, 0, 255)');
257+
}, 'A local mixin overrides a document mixin with the same name');
258+
</script>
259+
260+
<div id="host6">
261+
<template shadowrootmode="open">
262+
<style>
263+
@mixin --nearest-scope() {
264+
@result {
265+
color: yellow;
266+
}
267+
}
268+
</style>
269+
<div id="inner-host">
270+
<template shadowrootmode="open">
271+
<style>
272+
#target {
273+
color: red;
274+
@apply --nearest-scope;
275+
}
276+
</style>
277+
<div id="target">This text should be yellow.</div>
278+
</template>
279+
</div>
280+
</template>
281+
</div>
282+
<div id="host7">
283+
<template shadowrootmode="open">
284+
<style>
285+
@mixin --nearest-scope() {
286+
@result {
287+
color: aqua;
288+
}
289+
}
290+
</style>
291+
<div id="inner-host">
292+
<template shadowrootmode="open">
293+
<style>
294+
#target {
295+
color: red;
296+
@apply --nearest-scope;
297+
}
298+
</style>
299+
<div id="target">This text should be aqua.</div>
300+
</template>
301+
</div>
302+
</template>
303+
</div>
304+
<script>
305+
test(() => {
306+
const yellowTarget = document.getElementById('host6')
307+
.shadowRoot.getElementById('inner-host')
308+
.shadowRoot.getElementById('target');
309+
const aquaTarget = document.getElementById('host7')
310+
.shadowRoot.getElementById('inner-host')
311+
.shadowRoot.getElementById('target');
312+
assert_equals(getComputedStyle(yellowTarget).color,
313+
'rgb(255, 255, 0)');
314+
assert_equals(getComputedStyle(aquaTarget).color, 'rgb(0, 255, 255)');
315+
}, 'Nested shadows use the nearest mixin along each tree branch');
316+
</script>
317+
114318
</body>
115319
</html>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CSS Mixins: shared stylesheet across documents</title>
5+
<link rel="help" href="https://drafts.csswg.org/css-mixins-1/#using-mixins">
6+
<link rel="help" href="https://drafts.csswg.org/css-shadow-1/#css-tree-scoped-name">
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
</head>
10+
<body>
11+
<script>
12+
function loadFrame(color) {
13+
return new Promise(resolve => {
14+
const frame = document.createElement("iframe");
15+
frame.src =
16+
`support/mixin-shared-across-documents-frame.html?color=${color}`;
17+
frame.onload = () => resolve(frame);
18+
document.body.append(frame);
19+
});
20+
}
21+
22+
function targetColor(frame) {
23+
return getComputedStyle(
24+
frame.contentDocument.getElementById("target")).color;
25+
}
26+
27+
promise_test(async () => {
28+
const warmupFrame = await loadFrame("gray");
29+
const greenFrame = await loadFrame("green");
30+
const blueFrame = await loadFrame("blue");
31+
32+
assert_equals(targetColor(greenFrame), "rgb(0, 128, 0)",
33+
"First document should apply its own green mixin");
34+
assert_equals(targetColor(blueFrame), "rgb(0, 0, 255)",
35+
"Second document should apply its own blue mixin");
36+
}, "A shared stylesheet's @apply resolves to each document's own mixin");
37+
</script>
38+
</body>
39+
</html>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>CSS Mixins: sharing an @apply style sheet with a scope that has no mixins</title>
5+
<link rel="help" href="https://drafts.csswg.org/css-mixins-1/#apply-rule">
6+
<link rel="help" href="https://drafts.csswg.org/css-shadow-1/#css-tree-scoped-name">
7+
<script src="/resources/testharness.js"></script>
8+
<script src="/resources/testharnessreport.js"></script>
9+
</head>
10+
<body>
11+
<script>
12+
function addHost(name, localColor, sheet) {
13+
const host = document.createElement('div');
14+
document.body.append(host);
15+
const root = host.attachShadow({mode: 'open'});
16+
if (localColor) {
17+
const style = document.createElement('style');
18+
style.textContent = `
19+
@mixin ${name}() {
20+
@result {
21+
color: ${localColor};
22+
}
23+
}
24+
`;
25+
root.append(style);
26+
}
27+
const target = document.createElement('div');
28+
target.id = 'target';
29+
root.append(target);
30+
root.adoptedStyleSheets = [sheet];
31+
return target;
32+
}
33+
34+
function testScopeWithoutMixins(name, noMixinsFirst) {
35+
const sheet = new CSSStyleSheet();
36+
sheet.replaceSync(`#target { color: red; @apply ${name}; }`);
37+
document.adoptedStyleSheets = [sheet];
38+
39+
const check = (target, expected) =>
40+
assert_equals(getComputedStyle(target).color, expected);
41+
42+
if (noMixinsFirst) {
43+
check(addHost(name, null, sheet), 'rgb(255, 0, 0)');
44+
check(addHost(name, 'blue', sheet), 'rgb(0, 0, 255)');
45+
} else {
46+
check(addHost(name, 'blue', sheet), 'rgb(0, 0, 255)');
47+
check(addHost(name, null, sheet), 'rgb(255, 0, 0)');
48+
}
49+
document.adoptedStyleSheets = [];
50+
}
51+
52+
test(() => {
53+
testScopeWithoutMixins('--no-mixins-first', true);
54+
}, 'A scope with no mixins does not affect @apply in a scope that ' +
55+
'declares the mixin');
56+
57+
test(() => {
58+
testScopeWithoutMixins('--declared-first', false);
59+
}, 'An applied mixin does not affect @apply in a scope that has no ' +
60+
'mixins');
61+
</script>
62+
</body>
63+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>CSS Mixins: shared stylesheet frame</title>
5+
<script>
6+
const color = new URL(location.href).searchParams.get("color");
7+
const mixin = document.createElement("style");
8+
mixin.textContent = `
9+
@mixin --frame-mixin() {
10+
@result { color: ${color}; }
11+
}
12+
`;
13+
document.head.append(mixin);
14+
15+
const link = document.createElement("link");
16+
link.rel = "stylesheet";
17+
link.href = "mixin-shared-across-documents.css";
18+
document.head.append(link);
19+
</script>
20+
</head>
21+
<body>
22+
<div id="target">Target</div>
23+
</body>
24+
</html>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#target {
2+
color: red;
3+
@apply --frame-mixin();
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Cache-Control: max-age=3600

0 commit comments

Comments
 (0)