1<!doctype html>
2<!--
3@license
4Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
5This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8Code distributed by Google as part of the polymer project is also
9subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10-->
11<html>
12
13<head>
14  <meta charset="utf-8">
15  <script>
16    WCT = { waitFor(cb) { window.HTMLImports.whenReady(cb) } }
17  </script>
18  <script src="./test-flags.js"></script>
19  <script src="../node_modules/wct-browser-legacy/browser.js"></script>
20  <script src="../node_modules/@webcomponents/webcomponents-platform/webcomponents-platform.js"></script>
21  <script src="../node_modules/es6-promise/dist/es6-promise.auto.min.js"></script>
22  <script src="../node_modules/@webcomponents/template/template.js"></script>
23  <script src="../node_modules/@webcomponents/html-imports/html-imports.min.js"></script>
24  <script>
25    window.ShadyDOM = {force: true}
26  </script>
27  <script src="../node_modules/@webcomponents/shadydom/shadydom.min.js"></script>
28  <script>
29    // disable document watcher
30    window.ShadyDOM.handlesDynamicScoping = true;
31  </script>
32  <script src="../node_modules/@webcomponents/custom-elements/custom-elements.min.js"></script>
33  <script src="../scoping-shim.min.js"></script>
34  <script src="../apply-shim.min.js"></script>
35  <script src="../custom-style-interface.min.js"></script>
36  <script src="module/generated/make-element.js"></script>
37</head>
38<body>
39  <template id="sync-element">
40    <style>
41      div {
42        background: rgb(255, 0, 0);
43        border: 10px solid black;
44      }
45    </style>
46    <div id="inner">Test</div>
47  </template>
48  <div id="arena"></div>
49  <script>
50    function assertComputedStyle(node, expectedValue, property = 'border-top-width') {
51      const actualValue = getComputedStyle(node).getPropertyValue(property).trim();
52      assert.equal(actualValue, expectedValue, `${property} does not have the expected value`);
53    }
54    suite('Synchronous Scoping API', function() {
55      const arena = document.querySelector('#arena');
56      const csfn = (node) => {
57        return window.ShadyCSS.ScopingShim.currentScopeForNode(node);
58      };
59      const sfn = (node) => {
60        return window.ShadyCSS.ScopingShim.scopeForNode(node);
61      };
62      const scopeNode = (node, scope) => {
63        window.ShadyCSS.ScopingShim.scopeNode(node, scope);
64      }
65      const unscopeNode = (node, scope) => {
66        window.ShadyCSS.ScopingShim.unscopeNode(node, scope);
67      }
68      let el;
69
70      suiteSetup(function() {
71        makeElement('sync-element');
72      });
73
74      setup(function() {
75        el = document.createElement('sync-element');
76        arena.appendChild(el);
77      });
78
79      teardown(function() {
80        arena.innerHTML = '';
81      });
82
83      test('mutation observer is disabled', function(done) {
84        const inner = el.shadowRoot.querySelector('#inner');
85        arena.appendChild(inner);
86        setTimeout(() => {
87          assertComputedStyle(inner, 'rgb(255, 0, 0)', 'background-color');
88          done();
89        }, 100);
90      });
91
92      test('currentScopeForNode', function() {
93        assert.equal(csfn(el), '', 'sync-scoping should be document scope');
94        const inner = el.shadowRoot.querySelector('#inner');
95        assert.equal(csfn(inner), 'sync-element', 'inner div should have sync-element scope');
96        const disconnected = document.createElement('sync-element');
97        assert.equal(csfn(disconnected), '', 'disconnected element should have a blank scope')
98        const dynamic = document.createElement('div');
99        el.shadowRoot.appendChild(dynamic);
100        assert.equal(csfn(dynamic), '', 'dynamically appended node will not be scoped yet');
101      });
102
103      test('scopeForNode', function() {
104        assert.equal(sfn(el), '', 'sync-scoping should be document scope');
105        const inner = el.shadowRoot.querySelector('#inner');
106        assert.equal(sfn(inner), 'sync-element', 'inner div should have sync-element scope');
107        const disconnected = document.createElement('sync-element');
108        assert.equal(sfn(disconnected), '', 'disconnected element should have a blank scope');
109        const dynamic = document.createElement('div');
110        el.shadowRoot.appendChild(dynamic);
111        assert.equal(sfn(dynamic), 'sync-element', 'dynamically created node should have sync-element scope');
112      });
113
114      test('scopeNode', function() {
115        const div = document.createElement('div');
116        el.shadowRoot.appendChild(div);
117        scopeNode(div, sfn(div));
118        assertComputedStyle(div, '10px');
119        assertComputedStyle(div, 'rgb(255, 0, 0)', 'background-color');
120      });
121
122      test('unscopeNode', function() {
123        const inner = el.shadowRoot.querySelector('#inner');
124        arena.appendChild(inner);
125        unscopeNode(inner, csfn(inner));
126        assertComputedStyle(inner, '0px');
127      });
128    });
129  </script>
130</body>
131</html>