Chromium Code Reviews| 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 ClassMirror cls = reflectClass(type); | |
| 47 if (_isBuiltinType(cls)) { | |
| 48 throw new UnsupportedError('Invalid custom element from ' | |
| 49 '${(cls.owner as LibraryMirror).uri}.'); | |
| 50 } | |
| 51 | |
| 52 var className = MirrorSystem.getName(cls.simpleName); | |
| 53 var createdConstructor = cls.declarations[new Symbol('$className.created')]; | |
| 54 if (createdConstructor == null || | |
| 55 createdConstructor is! MethodMirror || | |
| 56 !createdConstructor.isConstructor) { | |
| 57 throw new UnsupportedError( | |
| 58 'Class is missing constructor $className.created'); | |
| 59 } | |
| 60 | |
| 61 if (createdConstructor.parameters.length > 0) { | |
| 62 throw new UnsupportedError( | |
| 63 'Constructor $className.created must take zero arguments'); | |
| 64 } | |
| 65 | |
| 66 Symbol objectName = reflectClass(Object).qualifiedName; | |
|
Jennifer Messerly
2014/03/07 00:00:49
note: my comments below are before I realized this
| |
| 67 bool isRoot(ClassMirror cls) => | |
| 68 cls == null || cls.qualifiedName == objectName; | |
| 69 Symbol elementName = reflectClass(HtmlElement).qualifiedName; | |
|
Jennifer Messerly
2014/03/07 00:00:49
Hmmm. It might be more reliable to compare reflect
| |
| 70 bool isElement(ClassMirror cls) => | |
| 71 cls != null && cls.qualifiedName == elementName; | |
| 72 ClassMirror superClass = cls.superclass; | |
| 73 ClassMirror nativeClass = _isBuiltinType(superClass) ? superClass : null; | |
| 74 while(!isRoot(superClass) && !isElement(superClass)) { | |
|
Jennifer Messerly
2014/03/07 00:00:49
for isRoot, you could just walk up superClass == n
| |
| 75 superClass = superClass.superclass; | |
| 76 if (nativeClass == null && _isBuiltinType(superClass)) { | |
| 77 nativeClass = superClass; | |
| 78 } | |
| 79 } | |
| 80 return nativeClass; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 bool _isBuiltinType(ClassMirror cls) { | |
| 85 // TODO(vsm): Find a less hackish way to do this. | |
|
Jennifer Messerly
2014/03/07 00:00:49
ah, I see. I think instances compare correctly in
| |
| 86 LibraryMirror lib = cls.owner; | |
| 87 String libName = lib.uri.toString(); | |
| 88 return libName.startsWith('dart:'); | |
| 89 } | |
| OLD | NEW |