| 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 // TODO(rnystrom): Use "package:" URL (#4968). | 14 // TODO(rnystrom): Use "package:" URL (#4968). |
| 15 import '../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; | 15 import '../../sdk/lib/_internal/dartdoc/lib/dartdoc.dart'; |
| 16 import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; | 16 import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart'; |
| 17 import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dar
t'; | 17 import '../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util.dar
t'; |
| 18 | 18 |
| 19 // TODO(amouravski): There is currently magic that looks at dart:* libraries | 19 // TODO(amouravski): There is currently magic that looks at dart:* libraries |
| 20 // rather than the declared library names. This changed due to recent syntax | 20 // rather than the declared library names. This changed due to recent syntax |
| 21 // changes. We should only need to look at the library 'html'. | 21 // changes. We should only need to look at the library 'html'. |
| 22 const HTML_LIBRARY_NAME = 'dart:html'; | 22 const List<String> HTML_LIBRARY_NAMES = const ['dart:html', 'dart:svg']; |
| 23 const HTML_DECLARED_NAME = 'html'; | 23 const List<String> HTML_DECLARED_NAMES = const ['html', 'svg']; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * A class for computing a many-to-many mapping between the types and | 26 * A class for computing a many-to-many mapping between the types and |
| 27 * members in `dart:html` and the MDN DOM types. This mapping is | 27 * members in `dart:html` and the MDN DOM types. This mapping is |
| 28 * based on two indicators: | 28 * based on two indicators: |
| 29 * | 29 * |
| 30 * 1. Auto-detected wrappers. Most `dart:html` types correspond | 30 * 1. Auto-detected wrappers. Most `dart:html` types correspond |
| 31 * straightforwardly to a single `@domName` type, and | 31 * straightforwardly to a single `@domName` type, and |
| 32 * have the same name. In addition, most `dart:html` methods | 32 * have the same name. In addition, most `dart:html` methods |
| 33 * just call a single `@domName` method. This class | 33 * just call a single `@domName` method. This class |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 | 65 |
| 66 static Compilation _compilation; | 66 static Compilation _compilation; |
| 67 static MirrorSystem _mirrors; | 67 static MirrorSystem _mirrors; |
| 68 static LibraryMirror dom; | 68 static LibraryMirror dom; |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Perform static initialization of [world]. This should be run before | 71 * Perform static initialization of [world]. This should be run before |
| 72 * calling [HtmlDiff.run]. | 72 * calling [HtmlDiff.run]. |
| 73 */ | 73 */ |
| 74 static void initialize(Path libDir) { | 74 static void initialize(Path libDir) { |
| 75 _compilation = new Compilation.library(<Path>[new Path(HTML_LIBRARY_NAME)], | 75 var paths = <Path>[]; |
| 76 libDir); | 76 for (var libraryName in HTML_LIBRARY_NAMES) { |
| 77 paths.add(new Path(libraryName)); |
| 78 } |
| 79 _compilation = new Compilation.library(paths, libDir); |
| 77 _mirrors = _compilation.mirrors; | 80 _mirrors = _compilation.mirrors; |
| 78 } | 81 } |
| 79 | 82 |
| 80 HtmlDiff({bool printWarnings: false}) : | 83 HtmlDiff({bool printWarnings: false}) : |
| 81 _printWarnings = printWarnings, | 84 _printWarnings = printWarnings, |
| 82 htmlToDom = new Map<String, Set<String>>(), | 85 htmlToDom = new Map<String, Set<String>>(), |
| 83 htmlTypesToDom = new Map<String, Set<String>>(), | 86 htmlTypesToDom = new Map<String, Set<String>>(), |
| 84 comments = new CommentMap(); | 87 comments = new CommentMap(); |
| 85 | 88 |
| 86 void warn(String s) { | 89 void warn(String s) { |
| 87 if (_printWarnings) { | 90 if (_printWarnings) { |
| 88 print('Warning: $s'); | 91 print('Warning: $s'); |
| 89 } | 92 } |
| 90 } | 93 } |
| 91 | 94 |
| 92 /** | 95 /** |
| 93 * Computes the `@domName` to `dart:html` mapping, and | 96 * Computes the `@domName` to `dart:html` mapping, and |
| 94 * places it in [htmlToDom] and [htmlTypesToDom]. Before this is run, dart2js | 97 * places it in [htmlToDom] and [htmlTypesToDom]. Before this is run, dart2js |
| 95 * should be initialized (via [parseOptions] and [initializeWorld]) and | 98 * should be initialized (via [parseOptions] and [initializeWorld]) and |
| 96 * [HtmlDiff.initialize] should be called. | 99 * [HtmlDiff.initialize] should be called. |
| 97 */ | 100 */ |
| 98 void run() { | 101 void run() { |
| 99 LibraryMirror htmlLib = _mirrors.libraries[HTML_DECLARED_NAME]; | 102 for (var libraryName in HTML_DECLARED_NAMES) { |
| 100 if (htmlLib == null) { | 103 var library = _mirrors.libraries[libraryName]; |
| 101 warn('Could not find $HTML_LIBRARY_NAME'); | 104 if (library == null) { |
| 102 return; | 105 warn('Could not find $libraryName'); |
| 103 } | 106 return; |
| 104 for (ClassMirror htmlType in htmlLib.classes.values) { | 107 } |
| 105 final domTypes = htmlToDomTypes(htmlType); | 108 for (ClassMirror type in library.classes.values) { |
| 106 if (domTypes.isEmpty) continue; | 109 final domTypes = htmlToDomTypes(type); |
| 110 if (domTypes.isEmpty) continue; |
| 107 | 111 |
| 108 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName, | 112 htmlTypesToDom.putIfAbsent(type.qualifiedName, |
| 109 () => new Set()).addAll(domTypes); | 113 () => new Set()).addAll(domTypes); |
| 110 | 114 |
| 111 htmlType.members.forEach( | 115 type.members.forEach( |
| 112 (_, m) => _addMemberDiff(m, domTypes)); | 116 (_, m) => _addMemberDiff(m, domTypes, library.simpleName)); |
| 117 } |
| 113 } | 118 } |
| 114 } | 119 } |
| 115 | 120 |
| 116 /** | 121 /** |
| 117 * Records the `@domName` to `dart:html` mapping for | 122 * Records the `@domName` to `dart:html` mapping for |
| 118 * [htmlMember] (from `dart:html`). [domTypes] are the | 123 * [htmlMember] (from `dart:html`). [domTypes] are the |
| 119 * `@domName` type values that correspond to [htmlMember]'s | 124 * `@domName` type values that correspond to [htmlMember]'s |
| 120 * defining type. | 125 * defining type. |
| 121 */ | 126 */ |
| 122 void _addMemberDiff(MemberMirror htmlMember, List<String> domTypes) { | 127 void _addMemberDiff(MemberMirror htmlMember, List<String> domTypes, |
| 128 String libraryName) { |
| 123 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 129 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
| 124 if (htmlMember == null && !domMembers.isEmpty) { | 130 if (htmlMember == null && !domMembers.isEmpty) { |
| 125 warn('$HTML_LIBRARY_NAME member ' | 131 warn('$libraryName member ' |
| 126 '${htmlMember.owner.simpleName}.' | 132 '${htmlMember.owner.simpleName}.' |
| 127 '${htmlMember.simpleName} has no corresponding ' | 133 '${htmlMember.simpleName} has no corresponding ' |
| 128 '$HTML_LIBRARY_NAME member.'); | 134 '$libraryName member.'); |
| 129 } | 135 } |
| 130 | 136 |
| 131 if (htmlMember == null) return; | 137 if (htmlMember == null) return; |
| 132 if (!domMembers.isEmpty) { | 138 if (!domMembers.isEmpty) { |
| 133 htmlToDom[htmlMember.qualifiedName] = domMembers; | 139 htmlToDom[htmlMember.qualifiedName] = domMembers; |
| 134 } | 140 } |
| 135 } | 141 } |
| 136 | 142 |
| 137 /** | 143 /** |
| 138 * Returns the `@domName` type values that correspond to | 144 * Returns the `@domName` type values that correspond to |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 Map<String, String> _getTags(String comment) { | 235 Map<String, String> _getTags(String comment) { |
| 230 if (comment == null) return const <String, String>{}; | 236 if (comment == null) return const <String, String>{}; |
| 231 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); | 237 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); |
| 232 final tags = <String, String>{}; | 238 final tags = <String, String>{}; |
| 233 for (var m in re.allMatches(comment.trim())) { | 239 for (var m in re.allMatches(comment.trim())) { |
| 234 tags[m[1]] = m[2]; | 240 tags[m[1]] = m[2]; |
| 235 } | 241 } |
| 236 return tags; | 242 return tags; |
| 237 } | 243 } |
| 238 } | 244 } |
| OLD | NEW |