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 | 12 |
13 #import('../../frog/lang.dart'); | 13 #import('../../frog/lang.dart'); |
14 #import('../../frog/file_system_vm.dart'); | 14 #import('../../frog/file_system_vm.dart'); |
15 #import('../../frog/file_system.dart'); | 15 #import('../../frog/file_system.dart'); |
16 #import('../../lib/dartdoc/dartdoc.dart'); | 16 #import('../../lib/dartdoc/dartdoc.dart'); |
17 | 17 |
18 /** | 18 /** |
19 * A class for computing a many-to-many mapping between the types and members in | 19 * A class for computing a many-to-many mapping between the types and |
20 * `dart:dom` and `dart:html`. This mapping is based on two indicators: | 20 * members in `dart:dom_deprecated` and `dart:html`. This mapping is |
| 21 * based on two indicators: |
21 * | 22 * |
22 * 1. Auto-detected wrappers. Most `dart:html` types correspond | 23 * 1. Auto-detected wrappers. Most `dart:html` types correspond |
23 * straightforwardly to a single `dart:dom` type, and have the same name. | 24 * straightforwardly to a single `dart:dom_deprecated` type, and |
24 * In addition, most `dart:html` methods just call a single `dart:dom` | 25 * have the same name. In addition, most `dart:html` methods |
25 * method. This class detects these simple correspondences automatically. | 26 * just call a single `dart:dom_deprecated` method. This class |
| 27 * detects these simple correspondences automatically. |
26 * | 28 * |
27 * 2. Manual annotations. When it's not clear which `dart:dom` items a given | 29 * 2. Manual annotations. When it's not clear which |
28 * `dart:html` item corresponds to, the `dart:html` item can be | 30 * `dart:dom_deprecated` items a given `dart:html` item |
29 * annotated in the documentation comments using the `@domName` annotation. | 31 * corresponds to, the `dart:html` item can be annotated in the |
| 32 * documentation comments using the `@domName` annotation. |
30 * | 33 * |
31 * The `@domName` annotations for types and members are of the form `@domName | 34 * The `@domName` annotations for types and members are of the form |
32 * NAME(, NAME)*`, where the `NAME`s refer to the `dart:dom` types/members that | 35 * `@domName NAME(, NAME)*`, where the `NAME`s refer to the |
33 * correspond to the annotated `dart:html` type/member. `NAME`s on member | 36 * `dart:dom_deprecated` types/members that correspond to the |
34 * annotations can refer to either fully-qualified member names (e.g. | 37 * annotated `dart:html` type/member. `NAME`s on member annotations |
35 * `Document.createElement`) or unqualified member names (e.g. `createElement`). | 38 * can refer to either fully-qualified member names (e.g. |
36 * Unqualified member names are assumed to refer to members of one of the | 39 * `Document.createElement`) or unqualified member names |
37 * corresponding `dart:dom` types. | 40 * (e.g. `createElement`). Unqualified member names are assumed to |
| 41 * refer to members of one of the corresponding `dart:dom_deprecated` |
| 42 * types. |
38 */ | 43 */ |
39 class HtmlDiff { | 44 class HtmlDiff { |
40 /** A map from `dart:dom` members to corresponding `dart:html` members. */ | 45 /** A map from `dart:dom_deprecated` members to corresponding |
| 46 * `dart:html` members. */ |
41 final Map<Member, Set<Member>> domToHtml; | 47 final Map<Member, Set<Member>> domToHtml; |
42 | 48 |
43 /** A map from `dart:html` members to corresponding `dart:dom` members. */ | 49 /** A map from `dart:html` members to corresponding |
| 50 * `dart:dom_deprecated` members. */ |
44 final Map<Member, Set<Member>> htmlToDom; | 51 final Map<Member, Set<Member>> htmlToDom; |
45 | 52 |
46 /** A map from `dart:dom` types to corresponding `dart:html` types. */ | 53 /** A map from `dart:dom_deprecated` types to corresponding |
| 54 * `dart:html` types. */ |
47 final Map<Type, Set<Type>> domTypesToHtml; | 55 final Map<Type, Set<Type>> domTypesToHtml; |
48 | 56 |
49 /** A map from `dart:html` types to corresponding `dart:dom` types. */ | 57 /** A map from `dart:html` types to corresponding |
| 58 * `dart:dom_deprecated` types. */ |
50 final Map<Type, Set<Type>> htmlTypesToDom; | 59 final Map<Type, Set<Type>> htmlTypesToDom; |
51 | 60 |
52 final CommentMap comments; | 61 final CommentMap comments; |
53 | 62 |
54 /** If true, then print warning messages. */ | 63 /** If true, then print warning messages. */ |
55 final bool _printWarnings; | 64 final bool _printWarnings; |
56 | 65 |
57 static Library dom; | 66 static Library dom; |
58 | 67 |
59 /** | 68 /** |
60 * Perform static initialization of [world]. This should be run before | 69 * Perform static initialization of [world]. This should be run before |
61 * calling [HtmlDiff.run]. | 70 * calling [HtmlDiff.run]. |
62 */ | 71 */ |
63 static void initialize() { | 72 static void initialize() { |
64 world.getOrAddLibrary('dart:dom'); | 73 world.getOrAddLibrary('dart:dom_deprecated'); |
65 world.getOrAddLibrary('dart:html'); | 74 world.getOrAddLibrary('dart:html'); |
66 world.process(); | 75 world.process(); |
67 | 76 |
68 dom = world.libraries['dart:dom']; | 77 dom = world.libraries['dart:dom_deprecated']; |
69 } | 78 } |
70 | 79 |
71 HtmlDiff([bool printWarnings = false]) : | 80 HtmlDiff([bool printWarnings = false]) : |
72 _printWarnings = printWarnings, | 81 _printWarnings = printWarnings, |
73 domToHtml = new Map<Member, Set<Member>>(), | 82 domToHtml = new Map<Member, Set<Member>>(), |
74 htmlToDom = new Map<Member, Set<Member>>(), | 83 htmlToDom = new Map<Member, Set<Member>>(), |
75 domTypesToHtml = new Map<Type, Set<Type>>(), | 84 domTypesToHtml = new Map<Type, Set<Type>>(), |
76 htmlTypesToDom = new Map<Type, Set<Type>>(), | 85 htmlTypesToDom = new Map<Type, Set<Type>>(), |
77 comments = new CommentMap(); | 86 comments = new CommentMap(); |
78 | 87 |
79 void warn(String s) { | 88 void warn(String s) { |
80 if (_printWarnings) { | 89 if (_printWarnings) { |
81 print('Warning: ' + s); | 90 print('Warning: ' + s); |
82 } | 91 } |
83 } | 92 } |
84 | 93 |
85 /** | 94 /** |
86 * Computes the `dart:dom` to `dart:html` mapping, and places it in | 95 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and |
87 * [domToHtml], [htmlToDom], [domTypesToHtml], and [htmlTypesToDom]. Before | 96 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and |
88 * this is run, Frog should be initialized (via [parseOptions] and | 97 * [htmlTypesToDom]. Before this is run, Frog should be initialized |
89 * [initializeWorld]) and [HtmlDiff.initialize] should be called. | 98 * (via [parseOptions] and [initializeWorld]) and |
| 99 * [HtmlDiff.initialize] should be called. |
90 */ | 100 */ |
91 void run() { | 101 void run() { |
92 final htmlLib = world.libraries['dart:html']; | 102 final htmlLib = world.libraries['dart:html']; |
93 for (Type htmlType in htmlLib.types.getValues()) { | 103 for (Type htmlType in htmlLib.types.getValues()) { |
94 final domTypes = htmlToDomTypes(htmlType); | 104 final domTypes = htmlToDomTypes(htmlType); |
95 if (domTypes.isEmpty()) continue; | 105 if (domTypes.isEmpty()) continue; |
96 | 106 |
97 htmlTypesToDom.putIfAbsent(htmlType, | 107 htmlTypesToDom.putIfAbsent(htmlType, |
98 () => new Set()).addAll(domTypes); | 108 () => new Set()).addAll(domTypes); |
99 domTypes.forEach((t) => | 109 domTypes.forEach((t) => |
100 domTypesToHtml.putIfAbsent(t, () => new Set()).add(htmlType)); | 110 domTypesToHtml.putIfAbsent(t, () => new Set()).add(htmlType)); |
101 | 111 |
102 final members = new List.from(htmlType.members.getValues()); | 112 final members = new List.from(htmlType.members.getValues()); |
103 members.addAll(htmlType.constructors.getValues()); | 113 members.addAll(htmlType.constructors.getValues()); |
104 htmlType.factories.forEach((f) => members.add(f)); | 114 htmlType.factories.forEach((f) => members.add(f)); |
105 members.forEach((m) => _addMemberDiff(m, domTypes)); | 115 members.forEach((m) => _addMemberDiff(m, domTypes)); |
106 } | 116 } |
107 } | 117 } |
108 | 118 |
109 /** | 119 /** |
110 * Records the `dart:dom` to `dart:html` mapping for [implMember] (from | 120 * Records the `dart:dom_deprecated` to `dart:html` mapping for |
111 * `dart:html`). [domTypes] are the `dart:dom` [Type]s that correspond to | 121 * [implMember] (from `dart:html`). [domTypes] are the |
112 * [implMember]'s defining [Type]. | 122 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s |
| 123 * defining [Type]. |
113 */ | 124 */ |
114 void _addMemberDiff(Member htmlMember, List<Type> domTypes) { | 125 void _addMemberDiff(Member htmlMember, List<Type> domTypes) { |
115 if (htmlMember.isProperty) { | 126 if (htmlMember.isProperty) { |
116 if (htmlMember.canGet) _addMemberDiff(htmlMember.getter, domTypes); | 127 if (htmlMember.canGet) _addMemberDiff(htmlMember.getter, domTypes); |
117 if (htmlMember.canSet) _addMemberDiff(htmlMember.setter, domTypes); | 128 if (htmlMember.canSet) _addMemberDiff(htmlMember.setter, domTypes); |
118 } | 129 } |
119 | 130 |
120 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 131 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
121 if (htmlMember == null && !domMembers.isEmpty()) { | 132 if (htmlMember == null && !domMembers.isEmpty()) { |
122 warn('dart:html member ${htmlMember.declaringType.name}.' + | 133 warn('dart:html member ${htmlMember.declaringType.name}.' + |
123 '${htmlMember.name} has no corresponding dart:html member.'); | 134 '${htmlMember.name} has no corresponding dart:html member.'); |
124 } | 135 } |
125 | 136 |
126 if (htmlMember == null) return; | 137 if (htmlMember == null) return; |
127 if (!domMembers.isEmpty()) htmlToDom[htmlMember] = domMembers; | 138 if (!domMembers.isEmpty()) htmlToDom[htmlMember] = domMembers; |
128 domMembers.forEach((m) => | 139 domMembers.forEach((m) => |
129 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); | 140 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); |
130 } | 141 } |
131 | 142 |
132 /** | 143 /** |
133 * Returns the `dart:dom` [Type]s that correspond to [htmlType] from | 144 * Returns the `dart:dom_deprecated` [Type]s that correspond to |
134 * `dart:html`. This can be the empty list if no correspondence is found. | 145 * [htmlType] from `dart:html`. This can be the empty list if no |
| 146 * correspondence is found. |
135 */ | 147 */ |
136 List<Type> htmlToDomTypes(Type htmlType) { | 148 List<Type> htmlToDomTypes(Type htmlType) { |
137 if (htmlType.name == null) return []; | 149 if (htmlType.name == null) return []; |
138 final tags = _getTags(comments.find(htmlType.span)); | 150 final tags = _getTags(comments.find(htmlType.span)); |
139 | 151 |
140 if (tags.containsKey('domName')) { | 152 if (tags.containsKey('domName')) { |
141 var domNames = map(tags['domName'].split(','), (s) => s.trim()); | 153 var domNames = map(tags['domName'].split(','), (s) => s.trim()); |
142 if (domNames.length == 1 && domNames[0] == 'none') return []; | 154 if (domNames.length == 1 && domNames[0] == 'none') return []; |
143 return map(domNames, (domName) { | 155 return map(domNames, (domName) { |
144 final domType = dom.types[domName]; | 156 final domType = dom.types[domName]; |
145 if (domType == null) warn('no dart:dom type named $domName'); | 157 if (domType == null) warn('no dart:dom_deprecated type named $domName'); |
146 return domType; | 158 return domType; |
147 }); | 159 }); |
148 } | 160 } |
149 return <Type>[]; | 161 return <Type>[]; |
150 } | 162 } |
151 | 163 |
152 /** | 164 /** |
153 * Returns the `dart:dom` [Member]s that correspond to [htmlMember] from | 165 * Returns the `dart:dom_deprecated` [Member]s that correspond to |
154 * `dart:html`. This can be the empty set if no correspondence is found. | 166 * [htmlMember] from `dart:html`. This can be the empty set if no |
155 * [domTypes] are the `dart:dom` [Type]s that correspond to [implMember]'s | 167 * correspondence is found. [domTypes] are the |
| 168 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s |
156 * defining [Type]. | 169 * defining [Type]. |
157 */ | 170 */ |
158 Set<Member> htmlToDomMembers(Member htmlMember, List<Type> domTypes) { | 171 Set<Member> htmlToDomMembers(Member htmlMember, List<Type> domTypes) { |
159 if (htmlMember.isPrivate) return new Set(); | 172 if (htmlMember.isPrivate) return new Set(); |
160 final tags = _getTags(comments.find(htmlMember.span)); | 173 final tags = _getTags(comments.find(htmlMember.span)); |
161 if (tags.containsKey('domName')) { | 174 if (tags.containsKey('domName')) { |
162 final domNames = map(tags['domName'].split(','), (s) => s.trim()); | 175 final domNames = map(tags['domName'].split(','), (s) => s.trim()); |
163 if (domNames.length == 1 && domNames[0] == 'none') return new Set(); | 176 if (domNames.length == 1 && domNames[0] == 'none') return new Set(); |
164 final members = new Set(); | 177 final members = new Set(); |
165 domNames.forEach((name) { | 178 domNames.forEach((name) { |
166 var nameMembers = _membersFromName(name, domTypes); | 179 var nameMembers = _membersFromName(name, domTypes); |
167 if (nameMembers.isEmpty()) { | 180 if (nameMembers.isEmpty()) { |
168 if (name.contains('.')) { | 181 if (name.contains('.')) { |
169 warn('no member $name'); | 182 warn('no member $name'); |
170 } else { | 183 } else { |
171 final options = Strings.join( | 184 final options = Strings.join( |
172 map(domTypes, (t) => "${t.name}.$name"), ' or '); | 185 map(domTypes, (t) => "${t.name}.$name"), ' or '); |
173 warn('no member $options'); | 186 warn('no member $options'); |
174 } | 187 } |
175 } | 188 } |
176 members.addAll(nameMembers); | 189 members.addAll(nameMembers); |
177 }); | 190 }); |
178 return members; | 191 return members; |
179 } | 192 } |
180 | 193 |
181 return new Set(); | 194 return new Set(); |
182 } | 195 } |
183 | 196 |
184 /** | 197 /** |
185 * Returns the `dart:dom` [Member]s that are indicated by [name]. [name] can | 198 * Returns the `dart:dom_deprecated` [Member]s that are indicated by |
186 * be either an unqualified member name (e.g. `createElement`), in which case | 199 * [name]. [name] can be either an unqualified member name |
187 * it's treated as the name of a member of one of [defaultTypes], or a | 200 * (e.g. `createElement`), in which case it's treated as the name of |
188 * fully-qualified member name (e.g. `Document.createElement`), in which case | 201 * a member of one of [defaultTypes], or a fully-qualified member |
189 * it's looked up in `dart:dom` and [defaultTypes] is ignored. | 202 * name (e.g. `Document.createElement`), in which case it's looked |
| 203 * up in `dart:dom_deprecated` and [defaultTypes] is ignored. |
190 */ | 204 */ |
191 Set<Member> _membersFromName(String name, List<Type> defaultTypes) { | 205 Set<Member> _membersFromName(String name, List<Type> defaultTypes) { |
192 if (!name.contains('.', 0)) { | 206 if (!name.contains('.', 0)) { |
193 if (defaultTypes.isEmpty()) { | 207 if (defaultTypes.isEmpty()) { |
194 warn('no default type for ${name}'); | 208 warn('no default type for ${name}'); |
195 return new Set(); | 209 return new Set(); |
196 } | 210 } |
197 final members = new Set<Member>(); | 211 final members = new Set<Member>(); |
198 defaultTypes.forEach((t) { | 212 defaultTypes.forEach((t) { |
199 if (t.members.containsKey(name)) members.add(t.members[name]); | 213 if (t.members.containsKey(name)) members.add(t.members[name]); |
(...skipping 29 matching lines...) Expand all Loading... |
229 Map<String, String> _getTags(String comment) { | 243 Map<String, String> _getTags(String comment) { |
230 if (comment == null) return const <String>{}; | 244 if (comment == null) return const <String>{}; |
231 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); | 245 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); |
232 final tags = <String>{}; | 246 final tags = <String>{}; |
233 for (var m in re.allMatches(comment.trim())) { | 247 for (var m in re.allMatches(comment.trim())) { |
234 tags[m[1]] = m[2]; | 248 tags[m[1]] = m[2]; |
235 } | 249 } |
236 return tags; | 250 return tags; |
237 } | 251 } |
238 } | 252 } |
OLD | NEW |