Skip to content

Commit 44eadaf

Browse files
andruudchromium-wpt-export-bot
authored andcommitted
[@scope] Support @scope(:host)
In CalculateActivations, we'd previously just call Element.parentElement up the ancestor chain, therefore never reach the host. Additionally, we did not propagate the original SelectorCheckerContext.scope which is needed for :host to match. This CL uses ParentOrShadowHostElement, and adds the concept of "activation ceiling" to deal with the fact that we need to look at one element *above* the current activation root when that root is a ShadowRoot. Note that :scope is supposed to be able to match "virtual" roots [1] (like a ShadowRoot), but Blink does not support this in general. This CL does not address this issue. [1] w3c/csswg-drafts#7261 Bug: 1280240 Change-Id: I2d0be103f4cd02ec576711b959e075a5118e694a
1 parent 2bb6046 commit 44eadaf

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

css/css-cascade/scope-shadow.html

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<!DOCTYPE html>
2+
<title>@scope - ShadowDOM</title>
3+
<link rel="help" href="https://drafts.csswg.org/css-cascade-6/#scope-atrule">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/resources/declarative-shadow-dom-polyfill.js"></script>
7+
<style>
8+
#outer {
9+
container-type: size;
10+
width: 200px;
11+
height: 200px;
12+
}
13+
#direct {
14+
container-type: inline-size;
15+
width: 50cqw;
16+
height: 50cqh;
17+
}
18+
#nondirect {
19+
width: 10cqw;
20+
height: 10cqh;
21+
background: green;
22+
}
23+
</style>
24+
<div id=host_plain>
25+
<template shadowroot=open>
26+
<style>
27+
@scope (:host) {
28+
.a {
29+
z-index: 1;
30+
}
31+
}
32+
</style>
33+
<div class=a>
34+
</div>
35+
</template>
36+
</div>
37+
<script>
38+
setup(() => {
39+
polyfill_declarative_shadow_dom(document);
40+
});
41+
42+
test(() => {
43+
let a = host_plain.shadowRoot.querySelector('.a');
44+
assert_equals(getComputedStyle(a).zIndex, '1');
45+
}, '@scope can match :host');
46+
</script>
47+
48+
<div id=host_functional>
49+
<template shadowroot=open>
50+
<style>
51+
@scope (:host(div)) {
52+
.a {
53+
z-index: 1;
54+
}
55+
}
56+
/* Should not match: */
57+
@scope (:host(span)) {
58+
.a {
59+
z-index: 42;
60+
}
61+
}
62+
</style>
63+
<div class=a>
64+
</div>
65+
</template>
66+
</div>
67+
<script>
68+
setup(() => {
69+
polyfill_declarative_shadow_dom(document);
70+
});
71+
72+
test(() => {
73+
let a = host_functional.shadowRoot.querySelector('.a');
74+
assert_equals(getComputedStyle(a).zIndex, '1');
75+
}, '@scope can match :host(...)');
76+
</script>
77+
78+
<div id=host_scope>
79+
<template shadowroot=open>
80+
<style>
81+
:host {
82+
z-index: 1;
83+
}
84+
@scope (:host) {
85+
/*
86+
The :scope pseudo should not match: Only :host can match the host
87+
within the shadow tree.
88+
89+
https://drafts.csswg.org/css-scoping-1/#host-element-in-tree
90+
*/
91+
:scope, .a {
92+
z-index: 42;
93+
}
94+
}
95+
</style>
96+
<div class=a>
97+
</div>
98+
</template>
99+
</div>
100+
<script>
101+
setup(() => {
102+
polyfill_declarative_shadow_dom(document);
103+
});
104+
105+
test(() => {
106+
assert_equals(getComputedStyle(host_scope).zIndex, '1');
107+
let a = host_scope.shadowRoot.querySelector('.a');
108+
assert_equals(getComputedStyle(a).zIndex, '42');
109+
}, 'Selecting :host with :scope');
110+
</script>

0 commit comments

Comments
 (0)