|
111 | 111 | }, 'Style outside shadow DOM should _not_ have access to inside mixins'); |
112 | 112 | </script> |
113 | 113 |
|
| 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 | + |
114 | 318 | </body> |
115 | 319 | </html> |
0 commit comments