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