| 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>[const Path(HTML_LIBRARY_NAME)], libDir); | 88 const <Path>[ |
| 89 const Path(DOM_LIBRARY_NAME), |
| 90 const Path(HTML_LIBRARY_NAME) |
| 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_LIBRARY_NAME); | 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, dart2js 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_LIBRARY_NAME); | 120 LibraryMirror htmlLib = findMirror(_mirrors.libraries, HTML_LIBRARY_NAME); |
| 118 if (htmlLib === null) { | 121 if (htmlLib === null) { |
| 119 warn('Could not find $HTML_LIBRARY_NAME'); | 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,// map of html->[its dom
types] | 129 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName, |
| 127 () => new Set()).addAll(domTypes); | 130 () => new Set()).addAll(domTypes); |
| 128 domTypes.forEach((t) => // map of dom type -> [the html name]. | 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)); // add those dom member types
to each | 136 (_, m) => _addMemberDiff(m, domTypes)); |
| 134 // of the html member (name/type) we're looking at | |
| 135 } | 137 } |
| 136 } | 138 } |
| 137 | 139 |
| 138 /** | 140 /** |
| 139 * Records the `dart:dom_deprecated` to `dart:html` mapping for | 141 * Records the `dart:dom_deprecated` to `dart:html` mapping for |
| 140 * [implMember] (from `dart:html`). [domTypes] are the | 142 * [implMember] (from `dart:html`). [domTypes] are the |
| 141 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s | 143 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s |
| 142 * defining [Type]. | 144 * defining [Type]. |
| 143 */ | 145 */ |
| 144 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { | 146 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { |
| 145 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 147 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
| 146 if (htmlMember == null && !domMembers.isEmpty()) { | 148 if (htmlMember == null && !domMembers.isEmpty()) { |
| 147 warn('$HTML_LIBRARY_NAME member ' | 149 warn('$HTML_LIBRARY_NAME member ' |
| 148 '${htmlMember.surroundingDeclaration.simpleName}.' | 150 '${htmlMember.surroundingDeclaration.simpleName}.' |
| 149 '${htmlMember.simpleName} has no corresponding ' | 151 '${htmlMember.simpleName} has no corresponding ' |
| 150 '$HTML_LIBRARY_NAME member.'); | 152 '$HTML_LIBRARY_NAME member.'); |
| 151 } | 153 } |
| 152 | 154 |
| 153 if (htmlMember == null) return; | 155 if (htmlMember == null) return; |
| 154 if (!domMembers.isEmpty()) { | 156 if (!domMembers.isEmpty()) { |
| 155 htmlToDom[htmlMember.qualifiedName] = domMembers; | 157 htmlToDom[htmlMember.qualifiedName] = domMembers; |
| 156 //htmlToDom[htmlmembername] -> list of corresponding dom members | |
| 157 } | 158 } |
| 158 domMembers.forEach((m) => // add the html member name to the domToHtml | 159 domMembers.forEach((m) => |
| 159 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); | 160 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); |
| 160 } | 161 } |
| 161 | 162 |
| 162 /** | 163 /** |
| 163 * Returns the `dart:dom_deprecated` [Type]s that correspond to | 164 * Returns the `dart:dom_deprecated` [Type]s that correspond to |
| 164 * [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 |
| 165 * correspondence is found. | 166 * correspondence is found. |
| 166 */ | 167 */ |
| 167 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { | 168 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { |
| 168 if (htmlType.simpleName == null) return []; | 169 if (htmlType.simpleName == null) return []; |
| 169 final tags = _getTags(comments.find(htmlType.location)); | 170 final tags = _getTags(comments.find(htmlType.location)); |
| 170 if (tags.containsKey('domName')) { // TODO(efortuna): instead just tag with | 171 if (tags.containsKey('domName')) { |
| 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 |