| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 
 | <!DOCTYPE html>
<meta charset="utf-8">
<title>Adoptedstylesheets as ObservableArray</title>
<link rel="author" href="mailto:masonf@chromium.org">
<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>
<div id="divNonConstructed" class="nonConstructed"></div>
<style>
  #target {background-color: red;}
</style>
<script>
const shadowRootNonConstructed = divNonConstructed.attachShadow({mode:'open'})
nonConstructedStyle = document.createElement("style");
shadowRootNonConstructed.appendChild(nonConstructedStyle);
nonConstructedStyle.sheet.insertRule(".nonConstructed { color: red; }", 0);
const nonConstructedStyleSheet = nonConstructedStyle.sheet;
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);
  // Adding non-constructed stylesheet to AdoptedStyleSheets is not allowed.
  assert_throws_dom('NotAllowedError', () => { document.adoptedStyleSheets.push(nonConstructedStyleSheet); });
  assert_throws_js(TypeError, () => { document.adoptedStyleSheets.push("foo"); });
}
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>
 |