OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /** Dart APIs for interacting with the JavaScript Custom Elements polyfill. */ |
| 6 library web_components.polyfill; |
| 7 |
| 8 import 'dart:async'; |
| 9 import 'dart:html'; |
| 10 import 'dart:js' as js; |
| 11 |
| 12 /** |
| 13 * A future that completes once all custom elements in the initial HTML page |
| 14 * have been upgraded. |
| 15 * |
| 16 * This is needed because the native implementation can update the elements |
| 17 * while parsing the HTML document, but the custom element polyfill cannot, |
| 18 * so it completes this future once all elements are upgraded. |
| 19 */ |
| 20 // TODO(jmesserly): rename to webComponentsReady to match the event? |
| 21 Future customElementsReady = () { |
| 22 if (_isReady) return new Future.value(); |
| 23 |
| 24 // Not upgraded. Wait for the polyfill to fire the WebComponentsReady event. |
| 25 // Note: we listen on document (not on document.body) to allow this polyfill |
| 26 // to be loaded in the HEAD element. |
| 27 return document.on['WebComponentsReady'].first; |
| 28 }(); |
| 29 |
| 30 // Return true if we are using the polyfill and upgrade is complete, or if we |
| 31 // have native document.register and therefore the browser took care of it. |
| 32 // Otherwise return false, including the case where we can't find the polyfill. |
| 33 bool get _isReady { |
| 34 // If we don't have dart:js, assume things are ready |
| 35 if (js.context == null) return true; |
| 36 |
| 37 var customElements = js.context['CustomElements']; |
| 38 if (customElements == null) { |
| 39 // Return true if native document.register, otherwise false. |
| 40 // (Maybe the polyfill isn't loaded yet. Wait for it.) |
| 41 return document.supportsRegister; |
| 42 } |
| 43 |
| 44 return customElements['ready'] == true; |
| 45 } |
| 46 |
| 47 /** |
| 48 * *Note* this API is primarily intended for tests. In other code it is better |
| 49 * to write it in a style that works with or without the polyfill, rather than |
| 50 * using this method. |
| 51 * |
| 52 * Synchronously trigger evaluation of pending lifecycle events, which otherwise |
| 53 * need to wait for a [MutationObserver] to signal the changes in the polyfill. |
| 54 * This method can be used to resolve differences in timing between native and |
| 55 * polyfilled custom elements. |
| 56 */ |
| 57 void customElementsTakeRecords([Node node]) { |
| 58 var customElements = js.context['CustomElements']; |
| 59 if (customElements == null) return; |
| 60 customElements.callMethod('takeRecords', [node]); |
| 61 } |
OLD | NEW |