Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert adoptedStyleSheets to use ObservableArray #31737

Merged
merged 1 commit into from
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>