UNPKG

6.42 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
4 * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 * Code distributed by Google as part of the polymer project is also
8 * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9 */
10
11(function () {
12 'use strict';
13
14 /**
15 * Basic flow of the loader process
16 *
17 * There are 4 flows the loader can take when booting up
18 *
19 * - Synchronous script, no polyfills needed
20 * - wait for `DOMContentLoaded`
21 * - fire WCR event, as there could not be any callbacks passed to `waitFor`
22 *
23 * - Synchronous script, polyfills needed
24 * - document.write the polyfill bundle
25 * - wait on the `load` event of the bundle to batch Custom Element upgrades
26 * - wait for `DOMContentLoaded`
27 * - run callbacks passed to `waitFor`
28 * - fire WCR event
29 *
30 * - Asynchronous script, no polyfills needed
31 * - wait for `DOMContentLoaded`
32 * - run callbacks passed to `waitFor`
33 * - fire WCR event
34 *
35 * - Asynchronous script, polyfills needed
36 * - Append the polyfill bundle script
37 * - wait for `load` event of the bundle
38 * - batch Custom Element Upgrades
39 * - run callbacks pass to `waitFor`
40 * - fire WCR event
41 */
42
43 var polyfillsLoaded = false;
44 var whenLoadedFns = [];
45 var allowUpgrades = false;
46 var flushFn;
47
48 function fireEvent() {
49 window.WebComponents.ready = true;
50 document.dispatchEvent(
51 new CustomEvent('WebComponentsReady', {bubbles: true})
52 );
53 }
54
55 function batchCustomElements() {
56 if (window.customElements && customElements.polyfillWrapFlushCallback) {
57 customElements.polyfillWrapFlushCallback(function (flushCallback) {
58 flushFn = flushCallback;
59 if (allowUpgrades) {
60 flushFn();
61 }
62 });
63 }
64 }
65
66 function asyncReady() {
67 batchCustomElements();
68 ready();
69 }
70
71 function ready() {
72 // bootstrap <template> elements before custom elements
73 if (window.HTMLTemplateElement && HTMLTemplateElement.bootstrap) {
74 HTMLTemplateElement.bootstrap(window.document);
75 }
76 polyfillsLoaded = true;
77 runWhenLoadedFns().then(fireEvent);
78 }
79
80 function runWhenLoadedFns() {
81 allowUpgrades = false;
82 var fnsMap = whenLoadedFns.map(function (fn) {
83 return fn instanceof Function ? fn() : fn;
84 });
85 whenLoadedFns = [];
86 return Promise.all(fnsMap)
87 .then(function () {
88 allowUpgrades = true;
89 flushFn && flushFn();
90 })
91 .catch(function (err) {
92 console.error(err);
93 });
94 }
95
96 window.WebComponents = window.WebComponents || {};
97 window.WebComponents.ready = window.WebComponents.ready || false;
98 window.WebComponents.waitFor =
99 window.WebComponents.waitFor ||
100 function (waitFn) {
101 if (!waitFn) {
102 return;
103 }
104 whenLoadedFns.push(waitFn);
105 if (polyfillsLoaded) {
106 runWhenLoadedFns();
107 }
108 };
109 window.WebComponents._batchCustomElements = batchCustomElements;
110
111 var name = 'webcomponents-loader.js';
112 // Feature detect which polyfill needs to be imported.
113 var polyfills = [];
114 if (
115 !(
116 'attachShadow' in Element.prototype && 'getRootNode' in Element.prototype
117 ) ||
118 (window.ShadyDOM && window.ShadyDOM.force)
119 ) {
120 polyfills.push('sd');
121 }
122 if (!window.customElements || window.customElements.forcePolyfill) {
123 polyfills.push('ce');
124 }
125
126 var needsTemplate = (function () {
127 // no real <template> because no `content` property (IE and older browsers)
128 var t = document.createElement('template');
129 if (!('content' in t)) {
130 return true;
131 }
132 // broken doc fragment (older Edge)
133 if (!(t.content.cloneNode() instanceof DocumentFragment)) {
134 return true;
135 }
136 // broken <template> cloning (Edge up to at least version 17)
137 var t2 = document.createElement('template');
138 t2.content.appendChild(document.createElement('div'));
139 t.content.appendChild(t2);
140 var clone = t.cloneNode(true);
141 return (
142 clone.content.childNodes.length === 0 ||
143 clone.content.firstChild.content.childNodes.length === 0
144 );
145 })();
146
147 // NOTE: any browser that does not have template or ES6 features
148 // must load the full suite of polyfills.
149 if (
150 !window.Promise ||
151 !Array.from ||
152 !window.URL ||
153 !window.Symbol ||
154 needsTemplate
155 ) {
156 polyfills = ['sd-ce-pf'];
157 }
158
159 if (polyfills.length) {
160 var url;
161 var polyfillFile = 'bundles/webcomponents-' + polyfills.join('-') + '.js';
162
163 // Load it from the right place.
164 if (window.WebComponents.root) {
165 url = window.WebComponents.root + polyfillFile;
166 } else {
167 var script = document.querySelector('script[src*="' + name + '"]');
168 // Load it from the right place.
169 url = script.src.replace(name, polyfillFile);
170 }
171
172 var newScript = document.createElement('script');
173 newScript.src = url;
174 // if readyState is 'loading', this script is synchronous
175 if (document.readyState === 'loading') {
176 // make sure custom elements are batched whenever parser gets to the injected script
177 newScript.setAttribute(
178 'onload',
179 'window.WebComponents._batchCustomElements()'
180 );
181 document.write(newScript.outerHTML);
182 document.addEventListener('DOMContentLoaded', ready);
183 } else {
184 newScript.addEventListener('load', function () {
185 asyncReady();
186 });
187 newScript.addEventListener('error', function () {
188 throw new Error('Could not load polyfill bundle' + url);
189 });
190 document.head.appendChild(newScript);
191 }
192 } else {
193 // if readyState is 'complete', script is loaded imperatively on a spec-compliant browser, so just fire WCR
194 if (document.readyState === 'complete') {
195 polyfillsLoaded = true;
196 fireEvent();
197 } else {
198 // this script may come between DCL and load, so listen for both, and cancel load listener if DCL fires
199 window.addEventListener('load', ready);
200 window.addEventListener('DOMContentLoaded', function () {
201 window.removeEventListener('load', ready);
202 ready();
203 });
204 }
205 }
206})();