Skip to content

Commit

Permalink
Convert adoptedStyleSheets to use ObservableArray
Browse files Browse the repository at this point in the history
This CL converts adoptedStyleSheets from a FrozenArray to the
new ObservableArray. This is per the new spec [1]. It should be
backwards compatible, and will allow mutation of adoptedStyleSheets
in place using regular array methods like .push():

  document.adoptedStyleSheets = [sheet1, sheet2];
  document.adoptedStyleSheets.push(sheet);
  ...etc...

Intent to ship:
https://groups.google.com/a/chromium.org/g/blink-dev/c/8p7QvGn3Ezo

[1] https://drafts.csswg.org/cssom/#extensions-to-the-document-or-shadow-root-interface

Bug: 1201744
Bug: 1236777
Change-Id: Iacbf66a38c0abd477b5628224c97bec9f8a317e4
Cq-Do-Not-Cancel-Tryjobs: true
  • Loading branch information
mfreed7 authored and chromium-wpt-export-bot committed Dec 1, 2021
1 parent 568c6e7 commit f579c78
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
12 changes: 12 additions & 0 deletions css/cssom/CSSStyleSheet-constructable-concat-ref.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Adoptedstylesheets.concat should work when starting empty</title>
<link rel="author" href="mailto:[email protected]">

<span>This should be green</span><br>
<span>This should be green</span>
<style>
span {
background-color:green;
}
</style>
28 changes: 28 additions & 0 deletions css/cssom/CSSStyleSheet-constructable-concat.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Adoptedstylesheets.concat should work when starting empty</title>
<link rel="author" href="mailto:[email protected]">
<link rel="help" href="https://drafts.csswg.org/cssom/#extensions-to-the-document-or-shadow-root-interface">
<link rel="match" href="CSSStyleSheet-constructable-concat-ref.html">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<span>This should be green</span>
<div id=host></div>

<script>
test(function() {
const sheet = new CSSStyleSheet();
sheet.replaceSync('span {background-color:green;}');
assert_equals(document.adoptedStyleSheets.length,0);
document.adoptedStyleSheets = document.adoptedStyleSheets.concat([sheet]);
assert_equals(document.adoptedStyleSheets.length,1);

const host = document.getElementById('host');
const shadow = host.attachShadow({mode: 'open'});
shadow.innerHTML = '<span>This should be green</span>';
assert_equals(shadow.adoptedStyleSheets.length,0);
shadow.adoptedStyleSheets = shadow.adoptedStyleSheets.concat([sheet]);
assert_equals(shadow.adoptedStyleSheets.length,1);
}, "adoptedStyleSheets should allow .concat on empty starting values");
</script>
80 changes: 80 additions & 0 deletions css/cssom/adoptedstylesheets-observablearray.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Adoptedstylesheets as ObservableArray</title>
<link rel="author" href="mailto:[email protected]">
<link rel="help" href="https://drafts.csswg.org/cssom/#extensions-to-the-document-or-shadow-root-interface">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<span id=target>Test Span</span>

<style>
#target {background-color: red;}
</style>

<script>
function assert_is(targetStyle, color) {
assert_equals(targetStyle.getPropertyValue('background-color'), color);
}

function testRoot(d, targetStyle) {
const red = 'rgb(255, 0, 0)';
const green = 'rgb(0, 255, 0)';
const blue = 'rgb(0, 0, 255)';

const sheet1 = new CSSStyleSheet();
sheet1.replaceSync('#target {background-color:lime !important;}');
const sheet2 = new CSSStyleSheet();
sheet2.replaceSync('#target {background-color:blue !important;}');
assert_equals(d.adoptedStyleSheets.length, 0);
assert_is(targetStyle, red);

d.adoptedStyleSheets = [sheet1];
assert_equals(d.adoptedStyleSheets.length, 1);
assert_is(targetStyle, green);

d.adoptedStyleSheets.push(sheet2);
assert_equals(d.adoptedStyleSheets.length, 2);
assert_is(targetStyle, blue);

d.adoptedStyleSheets.pop();
assert_equals(d.adoptedStyleSheets.length, 1);
assert_is(targetStyle, green);

d.adoptedStyleSheets.push(sheet2);
d.adoptedStyleSheets.reverse();
assert_equals(d.adoptedStyleSheets.length, 2);
assert_is(targetStyle, green);

d.adoptedStyleSheets.splice(1, 1);
assert_equals(d.adoptedStyleSheets.length, 1);
assert_is(targetStyle, blue);
d.adoptedStyleSheets.splice(0, 1, sheet1);
assert_equals(d.adoptedStyleSheets.length, 1);
assert_is(targetStyle, green);
}

test(function() {
const target = document.querySelector('#target');
const targetStyle = window.getComputedStyle(target);
testRoot(document, targetStyle);
}, "document.adoptedStyleSheets should allow mutation in-place");

test(function() {
const host = document.createElement('div');
document.body.appendChild(host);
const shadow = host.attachShadow({mode: 'open'});
shadow.innerHTML = '<span id=target>Test Shadow Span</span><style>#target{background-color: red;}</style>';
const target = shadow.querySelector('#target');
const targetStyle = window.getComputedStyle(target);
testRoot(shadow, targetStyle);
}, "shadowRoot.adoptedStyleSheets should allow mutation in-place");

test(function() {
assert_true(Array.isArray(document.adoptedStyleSheets));
const host = document.createElement('div');
document.body.appendChild(host);
const shadow = host.attachShadow({mode: 'open'});
assert_true(Array.isArray(shadow.adoptedStyleSheets));
}, "adoptedStyleSheets should return true for isArray()");
</script>

0 comments on commit f579c78

Please sign in to comment.