Index: pkg/custom_element/lib/polyfill.dart |
diff --git a/pkg/custom_element/lib/polyfill.dart b/pkg/custom_element/lib/polyfill.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8d5b7460a68c33c75cb69535758a58b959fbfddd |
--- /dev/null |
+++ b/pkg/custom_element/lib/polyfill.dart |
@@ -0,0 +1,42 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+/** Dart APIs for interacting with the JavaScript Custom Elements polyfill. */ |
+library custom_element.polyfill; |
+ |
+import 'dart:async'; |
+import 'dart:html'; |
+import 'dart:js' as js; |
+ |
+/** |
+ * A future that completes once all custom elements in the initial HTML page |
+ * have been upgraded. |
+ * |
+ * This is needed because the native implementation can update the elements |
+ * while parsing the HTML document, but the custom element polyfill cannot, |
+ * so it completes this future once all elements are upgraded. |
+ */ |
+Future customElementsReady = () { |
+ if (_isReady) return new Future.value(); |
+ |
+ // Not upgraded. Wait for the polyfill to fire the WebComponentsReady event. |
+ return document.body.on['WebComponentsReady'].first; |
+}(); |
+ |
+// Return true if we are using the polyfill and upgrade is complete, or if we |
+// have native document.register and therefore the browser took care of it. |
+// Otherwise return false, including the case where we can't find the polyfill. |
+bool get _isReady { |
+ // If we don't have dart:js, assume things are ready |
+ if (js.context == null) return true; |
+ |
+ var customElements = js.context['CustomElements']; |
+ if (customElements == null) { |
+ // Return true if native document.register, otherwise false. |
+ // (Maybe the polyfill isn't loaded yet) |
+ return new js.JsObject.fromBrowserObject(document).hasProperty('register'); |
blois
2013/10/21 21:08:12
document.supportsRegister?
Jennifer Messerly
2013/10/21 21:42:56
Done.
|
+ } |
+ |
+ return customElements['ready'] == true; |
+} |