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