OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 part of dart.dom.html; |
| 6 |
| 7 /// Dartium ElementUpgrader implementation. |
| 8 class _VMElementUpgrader implements ElementUpgrader { |
| 9 final Type _type; |
| 10 final Type _nativeType; |
| 11 |
| 12 _VMElementUpgrader(Document document, Type type, String extendsTag) : |
| 13 _type = type, |
| 14 _nativeType = _validateCustomType(type).reflectedType { |
| 15 |
| 16 if (extendsTag == null) { |
| 17 if (_nativeType != HtmlElement) { |
| 18 throw new UnsupportedError('Class must provide extendsTag if base ' |
| 19 'native class is not HtmlElement'); |
| 20 } |
| 21 } else { |
| 22 if (document.createElement(extendsTag).runtimeType != _nativeType) { |
| 23 throw new UnsupportedError( |
| 24 'extendsTag does not match base native class'); |
| 25 } |
| 26 } |
| 27 } |
| 28 |
| 29 Element upgrade(Element element) { |
| 30 if (element.runtimeType != _nativeType) { |
| 31 throw new UnsupportedError('Element is incorrect type'); |
| 32 } |
| 33 return _Utils.changeElementWrapper(element, _type); |
| 34 return null; |
| 35 } |
| 36 } |
| 37 |
| 38 /// Validates that the custom type is properly formed- |
| 39 /// |
| 40 /// * Is a user-defined class. |
| 41 /// * Has a created constructor with zero args. |
| 42 /// * Derives from an Element subclass. |
| 43 /// |
| 44 /// Then returns the native base class. |
| 45 ClassMirror _validateCustomType(Type type) { |
| 46 // TODO(vsm): Move these checks into native code. |
| 47 ClassMirror cls = reflectClass(type); |
| 48 if (_isBuiltinType(cls)) { |
| 49 throw new UnsupportedError("Invalid custom element from ${(cls.owner as Libr
aryMirror).uri}."); |
| 50 } |
| 51 var className = MirrorSystem.getName(cls.simpleName); |
| 52 var createdConstructor = cls.declarations[new Symbol('$className.created')]; |
| 53 if (createdConstructor == null || |
| 54 createdConstructor is! MethodMirror || |
| 55 !createdConstructor.isConstructor) { |
| 56 throw new UnsupportedError( |
| 57 'Class is missing constructor $className.created'); |
| 58 } |
| 59 |
| 60 if (createdConstructor.parameters.length > 0) { |
| 61 throw new UnsupportedError( |
| 62 'Constructor $className.created must take zero arguments'); |
| 63 } |
| 64 |
| 65 Symbol objectName = reflectClass(Object).qualifiedName; |
| 66 bool isRoot(ClassMirror cls) => |
| 67 cls == null || cls.qualifiedName == objectName; |
| 68 Symbol elementName = reflectClass(HtmlElement).qualifiedName; |
| 69 bool isElement(ClassMirror cls) => |
| 70 cls != null && cls.qualifiedName == elementName; |
| 71 ClassMirror superClass = cls.superclass; |
| 72 ClassMirror nativeClass = _isBuiltinType(superClass) ? superClass : null; |
| 73 while(!isRoot(superClass) && !isElement(superClass)) { |
| 74 superClass = superClass.superclass; |
| 75 if (nativeClass == null && _isBuiltinType(superClass)) { |
| 76 nativeClass = superClass; |
| 77 } |
| 78 } |
| 79 return nativeClass; |
| 80 } |
| 81 |
| 82 |
| 83 bool _isBuiltinType(ClassMirror cls) { |
| 84 // TODO(vsm): Find a less hackish way to do this. |
| 85 LibraryMirror lib = cls.owner; |
| 86 String libName = lib.uri.toString(); |
| 87 return libName.startsWith('dart:'); |
| 88 } |
OLD | NEW |