Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Unified Diff: tools/dom/src/dart2js_CustomElementSupport.dart

Issue 177113014: Adding a mechanism to 'upgrade' the Dart type of elements (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: tools/dom/src/dart2js_CustomElementSupport.dart
diff --git a/tools/dom/src/dart2js_CustomElementSupport.dart b/tools/dom/src/dart2js_CustomElementSupport.dart
index aa1c0372869484cefaff498db97b696d000ed05d..009f85767202a6d77e6f0b9f56a796483fecd84f 100644
--- a/tools/dom/src/dart2js_CustomElementSupport.dart
+++ b/tools/dom/src/dart2js_CustomElementSupport.dart
@@ -87,7 +87,7 @@ void _registerCustomElement(context, document, String tag, Type type,
if (extendsTagName == null) {
if (baseClassName != 'HTMLElement') {
throw new UnsupportedError('Class must provide extendsTag if base '
- 'native class is not HTMLElement');
+ 'native class is not HtmlElement');
}
} else {
if (!JS('bool', '(#.createElement(#) instanceof window[#])',
@@ -128,3 +128,60 @@ void _registerCustomElement(context, document, String tag, Type type,
void _initializeCustomElement(Element e) {
// TODO(blois): Add validation that this is only in response to an upgrade.
}
+
+/// Dart2JS implementation of ElementUpgrader
+class _JSElementUpgrader implements ElementUpgrader {
+ var _interceptor;
+ var _constructor;
+ var _nativeType;
+
+ _JSElementUpgrader(Document document, Type type, String extendsTag) {
+ var interceptorClass = findInterceptorConstructorForType(type);
+ if (interceptorClass == null) {
+ throw new ArgumentError(type);
+ }
+
+ _constructor = findConstructorForNativeSubclassType(type, 'created');
+ if (_constructor == null) {
+ throw new ArgumentError("$type has no constructor called 'created'");
+ }
+
+ // Workaround for 13190- use an article element to ensure that HTMLElement's
+ // interceptor is resolved correctly.
+ getNativeInterceptor(new Element.tag('article'));
+
+ var baseClassName = findDispatchTagForInterceptorClass(interceptorClass);
+ if (baseClassName == null) {
+ throw new ArgumentError(type);
+ }
+
+ if (extendsTag == null) {
+ if (baseClassName != 'HTMLElement') {
+ throw new UnsupportedError('Class must provide extendsTag if base '
+ 'native class is not HtmlElement');
+ }
+ _nativeType = HtmlElement;
+ } else {
+ var element = document.createElement(extendsTag);
+ if (!JS('bool', '(# instanceof window[#])',
+ element, baseClassName)) {
+ throw new UnsupportedError(
+ 'extendsTag does not match base native class');
+ }
+ _nativeType = element.runtimeType;
+ }
+
+ _interceptor = JS('=Object', '#.prototype', interceptorClass);
+ }
+
+ Element upgrade(Element element) {
+ // Only exact type matches are supported- cannot be a subclass.
+ if (element.runtimeType != _nativeType) {
+ throw new ArgumentError('element is not subclass of $_nativeType');
+ }
+
+ setNativeSubclassDispatchRecord(element, _interceptor);
+ JS('', '#(#)', _constructor, element);
+ return element;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698