summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/css/cssom/CSSStyleSheet-constructable-duplicate.html
blob: 579f5d0fcedc44b38ad98a94610e01850a3fef5d (plain)
1
2
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
<!doctype html>
<title>Cascade order of a stylesheet for duplicate sheets.</title>
<link rel="author" title="Emilio Cobos Álvarez" href="mailto:emilio@crisal.io">
<link rel="help" href="https://wicg.github.io/construct-stylesheets/">
<script src = '/resources/testharness.js'></script>
<script src = '/resources/testharnessreport.js'></script>
<div id="target"></div>
<script>

function attachShadowDiv(host) {
  const shadowRoot = host.attachShadow({mode: 'open'});
  const shadowDiv = document.createElement("div");
  shadowRoot.appendChild(shadowDiv);
  return shadowDiv;
}

function blueSheetsWithIncreasingZIndex(n) {
  let sheets = [];
  for (let i = 0; i < n; ++i) {
    sheets.push(new CSSStyleSheet());
    sheets[i].replaceSync("div { z-index: " + i + "; color: blue; }");
  }
  return sheets;
}

let sheets = [];

test(function() {
  sheets = blueSheetsWithIncreasingZIndex(2);

  document.adoptedStyleSheets = [sheets[1], sheets[0], sheets[1]];
  assert_equals(getComputedStyle(document.querySelector("div")).zIndex, "1", "duplicate stylesheet should take right position in the cascade");

  document.adoptedStyleSheets = [];
}, "Duplicate stylesheets have the right cascade position in the Document");

test(function() {
  sheets = blueSheetsWithIncreasingZIndex(2);

  const sheet = new CSSStyleSheet();
  sheet.replaceSync("div { color: red; }");

  document.adoptedStyleSheets = [sheets[1], sheets[0]];
  assert_equals(getComputedStyle(document.querySelector("div")).zIndex, "0", "backmost stylesheet should take precedence");
  assert_equals(getComputedStyle(document.querySelector("div")).color, "rgb(0, 0, 255)", "backmost stylesheet should take precedence");

  document.adoptedStyleSheets = [sheets[1], sheets[0], sheets[1], sheet];
  assert_equals(getComputedStyle(document.querySelector("div")).zIndex, "1", "duplicate stylesheet should take the right position in the cascade");
  assert_equals(getComputedStyle(document.querySelector("div")).color, "rgb(255, 0, 0)", "backmost stylesheet should take precedence");

  document.adoptedStyleSheets = [];
}, "Appending duplicate stylesheets yields the correct cascade position in the Document");

test(function() {
  sheets = blueSheetsWithIncreasingZIndex(2);

  const span = document.createElement("span");
  target.appendChild(span);
  attachShadowDiv(span);

  span.shadowRoot.adoptedStyleSheets = [sheets[1], sheets[0], sheets[1]];
  assert_equals(getComputedStyle(span.shadowRoot.querySelector("div")).zIndex, "1", "duplicate stylesheet should take right position in the cascade");
}, "Duplicate stylesheets have the right cascade position in the ShadowRoot");

test(function() {
  sheets = blueSheetsWithIncreasingZIndex(2);

  const sheet = new CSSStyleSheet();
  sheet.replaceSync("div { color: red; }");

  const span = document.createElement("span");
  target.appendChild(span);
  attachShadowDiv(span);

  span.shadowRoot.adoptedStyleSheets = [sheets[1], sheets[0]];
  assert_equals(getComputedStyle(span.shadowRoot.querySelector("div")).zIndex, "0", "backmost stylesheet should take precedence");
  assert_equals(getComputedStyle(span.shadowRoot.querySelector("div")).color, "rgb(0, 0, 255)", "backmost stylesheet should take precedence");

  span.shadowRoot.adoptedStyleSheets = [sheets[1], sheets[0], sheets[1], sheet];
  assert_equals(getComputedStyle(span.shadowRoot.querySelector("div")).zIndex, "1", "duplicate stylesheet should take right position in the cascade");
  assert_equals(getComputedStyle(span.shadowRoot.querySelector("div")).color, "rgb(255, 0, 0)", "backmost stylesheet should take precedence");
}, "Appending duplicate stylesheets yields the correct cascade position in the ShadowRoot");

</script>