-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert adoptedStyleSheets to use ObservableArray
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
1 parent
568c6e7
commit f579c78
Showing
3 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |