1<!doctype html>
2<!--
3@license
4Copyright (c) 2017 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<script>WCT = { waitFor(cb) { addEventListener('DOMContentLoaded', cb) } };</script>
12<script src="test-flags.js"></script>
13<script src="../node_modules/wct-browser-legacy/browser.js"></script>
14<script src="../node_modules/@webcomponents/webcomponents-platform/webcomponents-platform.js"></script>
15<script src="../node_modules/es6-promise/dist/es6-promise.auto.min.js"></script>
16<script src="../node_modules/@webcomponents/template/template.js"></script>
17<script src="../node_modules/@webcomponents/html-imports/html-imports.min.js"></script>
18<script src="../node_modules/@webcomponents/shadydom/shadydom.min.js"></script>
19<script src="../node_modules/@webcomponents/custom-elements/custom-elements.min.js"></script>
20<script src="../scoping-shim.min.js"></script>
21<script src="../apply-shim.min.js"></script>
22<script src="../custom-style-interface.min.js"></script>
23<template id="eager-host">
24  <style>
25    :host {
26      display: block;
27      height: 100px;
28      background-color: blue;
29    }
30
31    :host > late-client {
32      --foo: rgb(255, 0, 0);
33    }
34  </style>
35  <late-client></late-client>
36</template>
37<template id="late-client">
38  <style>
39    :host {
40      display: block;
41      color: var(--foo);
42    }
43
44    div {
45      border: 2px solid rgb(0, 255, 0);
46      border-color: var(--foo);
47    }
48  </style>
49  <div>Hello!</div>
50</template>
51
52<template id="x-parent">
53  <style>
54    :host {
55      --property: 10px solid black;
56    }
57  </style>
58  <x-child></x-child>
59</template>
60<template id="x-child">
61  <style>
62    div {
63      border: var(--property);
64    }
65  </style>
66  <div></div>
67</template>
68
69<script>
70  class LateClient extends HTMLElement {
71    constructor() {
72      super();
73      this.initialized = false;
74      this.attachShadow({mode: 'open'});
75    }
76    init() {
77      if (this.initialized) {
78        return;
79      }
80      this.initialized = true;
81      const template = document.querySelector(`template#${this.localName}`);
82      if (!template.initialized) {
83        template.initialized = true;
84        window.ShadyCSS.prepareTemplate(template, this.localName);
85      }
86      this.shadowRoot.appendChild(template.content.cloneNode(true));
87      window.ShadyCSS.styleElement(this);
88    }
89    connectedCallback() {
90      if (this.initialized) {
91        window.ShadyCSS.styleElement(this);
92      }
93    }
94  }
95
96  class EagerHost extends HTMLElement {
97    constructor() {
98      super();
99      this.template = document.querySelector(`template#${this.localName}`);
100      if (!this.template.initialized) {
101        this.template.initialized = true;
102        window.ShadyCSS.prepareTemplate(this.template, this.localName);
103      }
104    }
105    connectedCallback() {
106      window.ShadyCSS.styleElement(this);
107      if (this.template && !this.shadowRoot) {
108        this.attachShadow({mode: 'open'});
109        this.shadowRoot.appendChild(this.template.content.cloneNode(true));
110      }
111    }
112  }
113
114  class StampBeforeStyle extends HTMLElement {
115    constructor() {
116      super();
117      this.template = document.querySelector(`template#${this.localName}`);
118      if (!this.template.initialized) {
119        this.template.initialized = true;
120        window.ShadyCSS.prepareTemplate(this.template, this.localName);
121      }
122    }
123    connectedCallback() {
124      if (this.template && !this.shadowRoot) {
125        this.attachShadow({ mode: 'open' });
126        this.shadowRoot.appendChild(this.template.content.cloneNode(true));
127      }
128      window.ShadyCSS.styleElement(this);
129    }
130  }
131
132  suite('Lazy Initialization', function() {
133    test('Late child element is eventually correct', function() {
134      customElements.define('late-client', class extends LateClient{});
135      customElements.define('eager-host', class extends EagerHost{});
136      const host = document.createElement('eager-host');
137      document.body.appendChild(host);
138      window.ShadyCSS.styleDocument();
139      const inner = host.shadowRoot.querySelector('late-client');
140      if (inner.init) {
141        inner.init();
142      }
143      const div = inner.shadowRoot.querySelector('div');
144      assert.equal(getComputedStyle(div).getPropertyValue('border-color').trim(), 'rgb(255, 0, 0)');
145    });
146
147    test('Custom Property Shim can force unprepared parent to evaluate', function() {
148      customElements.define('x-child', class extends StampBeforeStyle {});
149      customElements.define('x-parent', class extends StampBeforeStyle {});
150      const host = document.createElement('x-parent');
151      document.body.appendChild(host);
152      const inner = host.shadowRoot.querySelector('x-child');
153      const div = inner.shadowRoot.querySelector('div');
154      assert.equal(getComputedStyle(div).getPropertyValue('border-top-width').trim(), '10px');
155    });
156  });
157</script>