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 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 static Compilation _compilation; | 78 static Compilation _compilation; |
79 static MirrorSystem _mirrors; | 79 static MirrorSystem _mirrors; |
80 static LibraryMirror dom; | 80 static LibraryMirror dom; |
81 | 81 |
82 /** | 82 /** |
83 * Perform static initialization of [world]. This should be run before | 83 * Perform static initialization of [world]. This should be run before |
84 * calling [HtmlDiff.run]. | 84 * calling [HtmlDiff.run]. |
85 */ | 85 */ |
86 static void initialize(Path libDir) { | 86 static void initialize(Path libDir) { |
87 _compilation = new Compilation.library( | 87 _compilation = new Compilation.library( |
88 const <Path>[ | 88 const <Path>[const Path(HTML_LIBRARY_NAME)], libDir); |
89 const Path(DOM_LIBRARY_NAME), | |
90 const Path(HTML_LIBRARY_NAME) | |
91 ], libDir); | |
92 _mirrors = _compilation.mirrors; | 89 _mirrors = _compilation.mirrors; |
93 | 90 |
94 // Find 'dart:dom_deprecated' by its library tag 'dom'. | 91 // Find 'dart:dom_deprecated' by its library tag 'dom'. |
95 dom = findMirror(_mirrors.libraries, DOM_LIBRARY_NAME); | 92 dom = findMirror(_mirrors.libraries, DOM_LIBRARY_NAME); |
96 } | 93 } |
97 | 94 |
98 HtmlDiff([bool printWarnings = false]) : | 95 HtmlDiff([bool printWarnings = false]) : |
99 _printWarnings = printWarnings, | 96 _printWarnings = printWarnings, |
100 domToHtml = new Map<MemberMirror, Set<MemberMirror>>(), | 97 domToHtml = new Map<MemberMirror, Set<MemberMirror>>(), |
101 htmlToDom = new Map<String, Set<MemberMirror>>(), | 98 htmlToDom = new Map<String, Set<MemberMirror>>(), |
102 domTypesToHtml = new Map<String, Set<InterfaceMirror>>(), | 99 domTypesToHtml = new Map<String, Set<InterfaceMirror>>(), |
103 htmlTypesToDom = new Map<String, Set<InterfaceMirror>>(), | 100 htmlTypesToDom = new Map<String, Set<InterfaceMirror>>(), |
104 comments = new CommentMap(); | 101 comments = new CommentMap(); |
105 | 102 |
106 void warn(String s) { | 103 void warn(String s) { |
107 if (_printWarnings) { | 104 if (_printWarnings) { |
108 print('Warning: $s'); | 105 print('Warning: $s'); |
109 } | 106 } |
110 } | 107 } |
111 | 108 |
112 /** | 109 /** |
113 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and | 110 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and |
114 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and | 111 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and |
115 * [htmlTypesToDom]. Before this is run, Frog should be initialized | 112 * [htmlTypesToDom]. Before this is run, dart2js should be initialized |
116 * (via [parseOptions] and [initializeWorld]) and | 113 * (via [parseOptions] and [initializeWorld]) and |
117 * [HtmlDiff.initialize] should be called. | 114 * [HtmlDiff.initialize] should be called. |
118 */ | 115 */ |
119 void run() { | 116 void run() { |
120 LibraryMirror htmlLib = findMirror(_mirrors.libraries, HTML_LIBRARY_NAME); | 117 LibraryMirror htmlLib = findMirror(_mirrors.libraries, HTML_LIBRARY_NAME); |
121 if (htmlLib === null) { | 118 if (htmlLib === null) { |
122 warn('Could not find $HTML_LIBRARY_NAME'); | 119 warn('Could not find $HTML_LIBRARY_NAME'); |
123 return; | 120 return; |
124 } | 121 } |
125 for (InterfaceMirror htmlType in htmlLib.types.getValues()) { | 122 for (InterfaceMirror htmlType in htmlLib.types.getValues()) { |
126 final domTypes = htmlToDomTypes(htmlType); | 123 final domTypes = htmlToDomTypes(htmlType); |
127 if (domTypes.isEmpty()) continue; | 124 if (domTypes.isEmpty()) continue; |
128 | 125 |
129 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName, | 126 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName,// map of html->[its dom
types] |
130 () => new Set()).addAll(domTypes); | 127 () => new Set()).addAll(domTypes); |
131 domTypes.forEach((t) => | 128 domTypes.forEach((t) => // map of dom type -> [the html name]. |
132 domTypesToHtml.putIfAbsent(t.qualifiedName, | 129 domTypesToHtml.putIfAbsent(t.qualifiedName, |
133 () => new Set()).add(htmlType)); | 130 () => new Set()).add(htmlType)); |
134 | 131 |
135 htmlType.declaredMembers.forEach( | 132 htmlType.declaredMembers.forEach( |
136 (_, m) => _addMemberDiff(m, domTypes)); | 133 (_, m) => _addMemberDiff(m, domTypes)); // add those dom member types
to each |
| 134 // of the html member (name/type) we're looking at |
137 } | 135 } |
138 } | 136 } |
139 | 137 |
140 /** | 138 /** |
141 * Records the `dart:dom_deprecated` to `dart:html` mapping for | 139 * Records the `dart:dom_deprecated` to `dart:html` mapping for |
142 * [implMember] (from `dart:html`). [domTypes] are the | 140 * [implMember] (from `dart:html`). [domTypes] are the |
143 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s | 141 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s |
144 * defining [Type]. | 142 * defining [Type]. |
145 */ | 143 */ |
146 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { | 144 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { |
147 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 145 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
148 if (htmlMember == null && !domMembers.isEmpty()) { | 146 if (htmlMember == null && !domMembers.isEmpty()) { |
149 warn('$HTML_LIBRARY_NAME member ' | 147 warn('$HTML_LIBRARY_NAME member ' |
150 '${htmlMember.surroundingDeclaration.simpleName}.' | 148 '${htmlMember.surroundingDeclaration.simpleName}.' |
151 '${htmlMember.simpleName} has no corresponding ' | 149 '${htmlMember.simpleName} has no corresponding ' |
152 '$HTML_LIBRARY_NAME member.'); | 150 '$HTML_LIBRARY_NAME member.'); |
153 } | 151 } |
154 | 152 |
155 if (htmlMember == null) return; | 153 if (htmlMember == null) return; |
156 if (!domMembers.isEmpty()) { | 154 if (!domMembers.isEmpty()) { |
157 htmlToDom[htmlMember.qualifiedName] = domMembers; | 155 htmlToDom[htmlMember.qualifiedName] = domMembers; |
| 156 //htmlToDom[htmlmembername] -> list of corresponding dom members |
158 } | 157 } |
159 domMembers.forEach((m) => | 158 domMembers.forEach((m) => // add the html member name to the domToHtml |
160 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); | 159 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); |
161 } | 160 } |
162 | 161 |
163 /** | 162 /** |
164 * Returns the `dart:dom_deprecated` [Type]s that correspond to | 163 * Returns the `dart:dom_deprecated` [Type]s that correspond to |
165 * [htmlType] from `dart:html`. This can be the empty list if no | 164 * [htmlType] from `dart:html`. This can be the empty list if no |
166 * correspondence is found. | 165 * correspondence is found. |
167 */ | 166 */ |
168 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { | 167 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { |
169 if (htmlType.simpleName == null) return []; | 168 if (htmlType.simpleName == null) return []; |
170 final tags = _getTags(comments.find(htmlType.location)); | 169 final tags = _getTags(comments.find(htmlType.location)); |
171 if (tags.containsKey('domName')) { | 170 if (tags.containsKey('domName')) { // TODO(efortuna): instead just tag with |
| 171 // domTypes and domMembers instead of domName |
172 var domNames = <String>[]; | 172 var domNames = <String>[]; |
173 for (var s in tags['domName'].split(',')) { | 173 for (var s in tags['domName'].split(',')) { |
174 domNames.add(s.trim()); | 174 domNames.add(s.trim()); |
175 } | 175 } |
176 if (domNames.length == 1 && domNames[0] == 'none') return []; | 176 if (domNames.length == 1 && domNames[0] == 'none') return []; |
177 var domTypes = <InterfaceMirror>[]; | 177 var domTypes = <InterfaceMirror>[]; |
178 for (var domName in domNames) { | 178 for (var domName in domNames) { |
179 final domType = findMirror(dom.types, domName); | 179 final domType = findMirror(dom.types, domName); |
180 if (domType == null) { | 180 if (domType == null) { |
181 warn('no $DOM_LIBRARY_NAME type named $domName'); | 181 warn('no $DOM_LIBRARY_NAME type named $domName'); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 Map<String, String> _getTags(String comment) { | 281 Map<String, String> _getTags(String comment) { |
282 if (comment == null) return const <String, String>{}; | 282 if (comment == null) return const <String, String>{}; |
283 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); | 283 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); |
284 final tags = <String, String>{}; | 284 final tags = <String, String>{}; |
285 for (var m in re.allMatches(comment.trim())) { | 285 for (var m in re.allMatches(comment.trim())) { |
286 tags[m[1]] = m[2]; | 286 tags[m[1]] = m[2]; |
287 } | 287 } |
288 return tags; | 288 return tags; |
289 } | 289 } |
290 } | 290 } |
OLD | NEW |