OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * A script to assist in documenting the difference between the dart:html API | 6 * A script to assist in documenting the difference between the dart:html API |
7 * and the old DOM API. | 7 * and the old DOM API. |
8 */ | 8 */ |
9 #library('html_diff'); | 9 #library('html_diff'); |
10 | 10 |
11 #import('dart:coreimpl'); | 11 #import('dart:coreimpl'); |
12 #import('dart:io'); | 12 #import('dart:io'); |
13 | 13 |
14 #import('../../lib/dartdoc/dartdoc.dart'); | 14 #import('../../lib/dartdoc/dartdoc.dart'); |
15 #import('../../lib/dartdoc/mirrors/mirrors.dart'); | 15 #import('../../lib/dartdoc/mirrors/mirrors.dart'); |
16 #import('../../lib/dartdoc/mirrors/mirrors_util.dart'); | 16 #import('../../lib/dartdoc/mirrors/mirrors_util.dart'); |
17 | 17 |
| 18 final HTML_LIBRARY_NAME = 'dart:html'; |
| 19 final DOM_LIBRARY_NAME = 'dart:dom_deprecated'; |
| 20 |
18 /** | 21 /** |
19 * A class for computing a many-to-many mapping between the types and | 22 * A class for computing a many-to-many mapping between the types and |
20 * members in `dart:dom_deprecated` and `dart:html`. This mapping is | 23 * members in `dart:dom_deprecated` and `dart:html`. This mapping is |
21 * based on two indicators: | 24 * based on two indicators: |
22 * | 25 * |
23 * 1. Auto-detected wrappers. Most `dart:html` types correspond | 26 * 1. Auto-detected wrappers. Most `dart:html` types correspond |
24 * straightforwardly to a single `dart:dom_deprecated` type, and | 27 * straightforwardly to a single `dart:dom_deprecated` type, and |
25 * have the same name. In addition, most `dart:html` methods | 28 * have the same name. In addition, most `dart:html` methods |
26 * just call a single `dart:dom_deprecated` method. This class | 29 * just call a single `dart:dom_deprecated` method. This class |
27 * detects these simple correspondences automatically. | 30 * detects these simple correspondences automatically. |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 static MirrorSystem _mirrors; | 79 static MirrorSystem _mirrors; |
77 static LibraryMirror dom; | 80 static LibraryMirror dom; |
78 | 81 |
79 /** | 82 /** |
80 * Perform static initialization of [world]. This should be run before | 83 * Perform static initialization of [world]. This should be run before |
81 * calling [HtmlDiff.run]. | 84 * calling [HtmlDiff.run]. |
82 */ | 85 */ |
83 static void initialize(Path libDir) { | 86 static void initialize(Path libDir) { |
84 _compilation = new Compilation.library( | 87 _compilation = new Compilation.library( |
85 const <Path>[ | 88 const <Path>[ |
86 const Path('dart:dom_deprecated'), | 89 const Path(DOM_LIBRARY_NAME), |
87 const Path('dart:html') | 90 const Path(HTML_LIBRARY_NAME) |
88 ], libDir); | 91 ], libDir); |
89 _mirrors = _compilation.mirrors(); | 92 _mirrors = _compilation.mirrors(); |
90 | 93 |
91 // Find 'dart:dom_deprecated' by its library tag 'dom'. | 94 // Find 'dart:dom_deprecated' by its library tag 'dom'. |
92 dom = findMirror(_mirrors.libraries(), 'dom'); | 95 dom = findMirror(_mirrors.libraries(), DOM_LIBRARY_NAME); |
93 } | 96 } |
94 | 97 |
95 HtmlDiff([bool printWarnings = false]) : | 98 HtmlDiff([bool printWarnings = false]) : |
96 _printWarnings = printWarnings, | 99 _printWarnings = printWarnings, |
97 domToHtml = new Map<MemberMirror, Set<MemberMirror>>(), | 100 domToHtml = new Map<MemberMirror, Set<MemberMirror>>(), |
98 htmlToDom = new Map<String, Set<MemberMirror>>(), | 101 htmlToDom = new Map<String, Set<MemberMirror>>(), |
99 domTypesToHtml = new Map<String, Set<InterfaceMirror>>(), | 102 domTypesToHtml = new Map<String, Set<InterfaceMirror>>(), |
100 htmlTypesToDom = new Map<String, Set<InterfaceMirror>>(), | 103 htmlTypesToDom = new Map<String, Set<InterfaceMirror>>(), |
101 comments = new CommentMap(); | 104 comments = new CommentMap(); |
102 | 105 |
103 void warn(String s) { | 106 void warn(String s) { |
104 if (_printWarnings) { | 107 if (_printWarnings) { |
105 print('Warning: $s'); | 108 print('Warning: $s'); |
106 } | 109 } |
107 } | 110 } |
108 | 111 |
109 /** | 112 /** |
110 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and | 113 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and |
111 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and | 114 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and |
112 * [htmlTypesToDom]. Before this is run, Frog should be initialized | 115 * [htmlTypesToDom]. Before this is run, Frog should be initialized |
113 * (via [parseOptions] and [initializeWorld]) and | 116 * (via [parseOptions] and [initializeWorld]) and |
114 * [HtmlDiff.initialize] should be called. | 117 * [HtmlDiff.initialize] should be called. |
115 */ | 118 */ |
116 void run() { | 119 void run() { |
117 LibraryMirror htmlLib = findMirror(_mirrors.libraries(), 'html'); | 120 LibraryMirror htmlLib = findMirror(_mirrors.libraries(), HTML_LIBRARY_NAME); |
118 if (htmlLib === null) { | 121 if (htmlLib === null) { |
119 warn('Could not find dart:html'); | 122 warn('Could not find $HTML_LIBRARY_NAME'); |
120 return; | 123 return; |
121 } | 124 } |
122 for (InterfaceMirror htmlType in htmlLib.types().getValues()) { | 125 for (InterfaceMirror htmlType in htmlLib.types().getValues()) { |
123 final domTypes = htmlToDomTypes(htmlType); | 126 final domTypes = htmlToDomTypes(htmlType); |
124 if (domTypes.isEmpty()) continue; | 127 if (domTypes.isEmpty()) continue; |
125 | 128 |
126 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName(), | 129 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName(), |
127 () => new Set()).addAll(domTypes); | 130 () => new Set()).addAll(domTypes); |
128 domTypes.forEach((t) => | 131 domTypes.forEach((t) => |
129 domTypesToHtml.putIfAbsent(t.qualifiedName(), | 132 domTypesToHtml.putIfAbsent(t.qualifiedName(), |
130 () => new Set()).add(htmlType)); | 133 () => new Set()).add(htmlType)); |
131 | 134 |
132 htmlType.declaredMembers().forEach( | 135 htmlType.declaredMembers().forEach( |
133 (_, m) => _addMemberDiff(m, domTypes)); | 136 (_, m) => _addMemberDiff(m, domTypes)); |
134 } | 137 } |
135 } | 138 } |
136 | 139 |
137 /** | 140 /** |
138 * Records the `dart:dom_deprecated` to `dart:html` mapping for | 141 * Records the `dart:dom_deprecated` to `dart:html` mapping for |
139 * [implMember] (from `dart:html`). [domTypes] are the | 142 * [implMember] (from `dart:html`). [domTypes] are the |
140 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s | 143 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s |
141 * defining [Type]. | 144 * defining [Type]. |
142 */ | 145 */ |
143 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { | 146 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { |
144 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 147 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
145 if (htmlMember == null && !domMembers.isEmpty()) { | 148 if (htmlMember == null && !domMembers.isEmpty()) { |
146 warn('dart:html member ' | 149 warn('$HTML_LIBRARY_NAME member ' |
147 '${htmlMember.surroundingDeclaration().simpleName()}.' | 150 '${htmlMember.surroundingDeclaration().simpleName()}.' |
148 '${htmlMember.simpleName()} has no corresponding dart:html member.'); | 151 '${htmlMember.simpleName()} has no corresponding ' |
| 152 '$HTML_LIBRARY_NAME member.'); |
149 } | 153 } |
150 | 154 |
151 if (htmlMember == null) return; | 155 if (htmlMember == null) return; |
152 if (!domMembers.isEmpty()) { | 156 if (!domMembers.isEmpty()) { |
153 htmlToDom[htmlMember.qualifiedName()] = domMembers; | 157 htmlToDom[htmlMember.qualifiedName()] = domMembers; |
154 } | 158 } |
155 domMembers.forEach((m) => | 159 domMembers.forEach((m) => |
156 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); | 160 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); |
157 } | 161 } |
158 | 162 |
159 /** | 163 /** |
160 * Returns the `dart:dom_deprecated` [Type]s that correspond to | 164 * Returns the `dart:dom_deprecated` [Type]s that correspond to |
161 * [htmlType] from `dart:html`. This can be the empty list if no | 165 * [htmlType] from `dart:html`. This can be the empty list if no |
162 * correspondence is found. | 166 * correspondence is found. |
163 */ | 167 */ |
164 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { | 168 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { |
165 if (htmlType.simpleName() == null) return []; | 169 if (htmlType.simpleName() == null) return []; |
166 final tags = _getTags(comments.find(htmlType.location())); | 170 final tags = _getTags(comments.find(htmlType.location())); |
167 if (tags.containsKey('domName')) { | 171 if (tags.containsKey('domName')) { |
168 var domNames = <String>[]; | 172 var domNames = <String>[]; |
169 for (var s in tags['domName'].split(',')) { | 173 for (var s in tags['domName'].split(',')) { |
170 domNames.add(s.trim()); | 174 domNames.add(s.trim()); |
171 } | 175 } |
172 if (domNames.length == 1 && domNames[0] == 'none') return []; | 176 if (domNames.length == 1 && domNames[0] == 'none') return []; |
173 var domTypes = <InterfaceMirror>[]; | 177 var domTypes = <InterfaceMirror>[]; |
174 for (var domName in domNames) { | 178 for (var domName in domNames) { |
175 final domType = findMirror(dom.types(), domName); | 179 final domType = findMirror(dom.types(), domName); |
176 if (domType == null) { | 180 if (domType == null) { |
177 warn('no dart:dom_deprecated type named $domName'); | 181 warn('no $DOM_LIBRARY_NAME type named $domName'); |
178 } else { | 182 } else { |
179 domTypes.add(domType); | 183 domTypes.add(domType); |
180 } | 184 } |
181 } | 185 } |
182 return domTypes; | 186 return domTypes; |
183 } | 187 } |
184 return <InterfaceMirror>[]; | 188 return <InterfaceMirror>[]; |
185 } | 189 } |
186 | 190 |
187 /** | 191 /** |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 Map<String, String> _getTags(String comment) { | 281 Map<String, String> _getTags(String comment) { |
278 if (comment == null) return const <String>{}; | 282 if (comment == null) return const <String>{}; |
279 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); | 283 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); |
280 final tags = <String>{}; | 284 final tags = <String>{}; |
281 for (var m in re.allMatches(comment.trim())) { | 285 for (var m in re.allMatches(comment.trim())) { |
282 tags[m[1]] = m[2]; | 286 tags[m[1]] = m[2]; |
283 } | 287 } |
284 return tags; | 288 return tags; |
285 } | 289 } |
286 } | 290 } |
OLD | NEW |