Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(772)

Side by Side Diff: client/html/scripts/html_diff.dart

Issue 8771054: Add a script to generate HTML and DOM docs with cross-links to one another. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More code review changes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « client/html/scripts/html-diff.dart ('k') | client/html/scripts/html_doc.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
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.
4
5 /**
6 * A script to assist in documenting the difference between the dart:html API
7 * and the old DOM API.
8 */
9 #library('html_diff');
10
11 #import('../../../frog/lang.dart');
12 #import('../../../frog/file_system_node.dart');
13 #import('../../../frog/file_system.dart');
14 #import('../../../utils/dartdoc/dartdoc.dart');
15
16 void main() {
17 var files = new NodeFileSystem();
18 parseOptions('../../frog', [] /* args */, files);
19 initializeWorld(files);
20 initializeDartDoc();
21 HtmlDiff.initialize();
22
23 var diff = new HtmlDiff();
24 diff.run();
25
26 diff.domToHtml.forEach((domMember, htmlMembers) {
27 for (var htmlMember in htmlMembers) {
28 if (diff.sameName(domMember, htmlMember)) continue;
29 var htmlTypeName = htmlMember.declaringType.name;
30 var htmlName = '$htmlTypeName.${htmlMember.name}';
31 if (htmlMember.isConstructor || htmlMember.isFactory) {
32 final separator = htmlMember.constructorName == '' ? '' : '.';
33 htmlName = 'new $htmlTypeName$separator${htmlMember.constructorName}';
34 }
35 print('${domMember.declaringType.name}.${domMember.name} -> ${htmlName}');
36 }
37 });
38
39 for (var type in world.dom.types.getValues()) {
40 if (type.name == null) continue;
41 if (type.definition is FunctionTypeDefinition) continue;
42 for (var member in type.members.getValues()) {
43 if (!member.isPrivate && member.name != 'typeName' &&
44 !diff.domToHtml.containsKey(member) &&
45 (member is MethodMember || member is PropertyMember)) {
46 print('No dart:html wrapper for ${type.name}.${member.name}');
47 }
48 }
49 }
50 }
51
52 // TODO(nweiz): document this
53 class HtmlDiff {
54 final Map<Member, Set<Member>> domToHtml;
55 final Map<Member, Set<Member>> htmlToDom;
56 final Map<Type, Set<Type>> domTypesToHtml;
57 final Map<Type, Set<Type>> htmlTypesToDom;
58
59 static void initialize() {
60 world.processDartScript('dart:htmlimpl');
61 world.resolveAll();
62 }
63
64 HtmlDiff() :
Bob Nystrom 2011/12/07 22:54:30 For future reference, I think we usually wrap that
nweiz 2011/12/07 23:01:12 Noted.
65 domToHtml = new Map<Member, Set<Member>>(),
66 htmlToDom = new Map<Member, Set<Member>>(),
67 domTypesToHtml = new Map<Type, Set<Type>>(),
68 htmlTypesToDom = new Map<Type, Set<Type>>();
69
70 void run() {
71 final htmlLib = world.libraries['dart:htmlimpl'];
72 for (var implType in htmlLib.types.getValues()) {
73 final domTypes = htmlToDomTypes(implType);
74 final htmlType = htmlImplToHtmlType(implType);
75 if (htmlType == null) continue;
76
77 htmlTypesToDom.putIfAbsent(htmlType, () => new Set()).addAll(domTypes);
78 domTypes.forEach((t) =>
79 domTypesToHtml.putIfAbsent(t, () => new Set()).add(htmlType));
80
81 final members = new List.from(implType.members.getValues());
82 members.addAll(implType.constructors.getValues());
83 implType.factories.forEach((f) => members.add(f));
84 members.forEach((m) => _addMemberDiff(m, domTypes));
85 }
86 }
87
88 bool sameName(Member domMember, Member htmlMember) {
89 var domTypeName = domMember.declaringType.name;
90 if (domTypeName == 'DOMWindow') domTypeName = 'Window';
91 domTypeName = domTypeName.replaceFirst(new RegExp('^(HTML|WebKit)'), '');
92 var htmlTypeName = htmlMember.declaringType.name;
93
94 var domName = domMember.name;
95 var htmlName = htmlMember.name;
96 if (htmlName.startsWith('get:') || htmlName.startsWith('set:')) {
97 htmlName = htmlName.substring(4);
98 }
99
100 return domTypeName == htmlTypeName && domName == htmlName;
101 }
102
103 void _addMemberDiff(Member implMember, List<Type> domTypes) {
104 if (implMember.isProperty) {
105 if (implMember.canGet) _addMemberDiff(implMember.getter, domTypes);
106 if (implMember.canSet) _addMemberDiff(implMember.setter, domTypes);
107 }
108
109 var domMembers = htmlToDomMembers(implMember, domTypes);
110 var htmlMember = htmlImplToHtmlMember(implMember);
111 if (htmlMember == null && !domMembers.isEmpty()) {
112 print('Warning: dart:htmlimpl member ${implMember.declaringType.name}.' +
113 '${implMember.name} has no corresponding dart:html member.');
114 }
115
116 if (htmlMember == null) return;
117 if (!domMembers.isEmpty()) htmlToDom[htmlMember] = domMembers;
118 domMembers.forEach((m) =>
119 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember));
120 }
121
122 Type htmlImplToHtmlType(Type implType) {
123 if (implType == null || implType.isTop || implType.interfaces.isEmpty() ||
124 implType.interfaces[0].library.name != 'html') {
125 return null;
126 }
127
128 return implType.interfaces[0];
129 }
130
131 Member htmlImplToHtmlMember(Member implMember) {
132 var htmlType = htmlImplToHtmlType(implMember.declaringType);
133 if (htmlType == null) return null;
134
135 bool getter, setter;
136 if (implMember.isConstructor || implMember.isFactory) {
137 var constructor = htmlType.constructors[implMember.name];
138 if (constructor != null) return constructor;
139 return htmlType.factories[implMember.name];
140 } else if ((getter = implMember.name.startsWith('get:')) ||
141 (setter = implMember.name.startsWith('set:'))) {
142 // Use getMember to follow interface inheritance chains. If it's a
143 // ConcreteMember, though, it's an implementation of some data structure
144 // and we don't care about it.
145 var htmlProperty = htmlType.getMember(implMember.name.substring(4));
146 if (htmlProperty != null && htmlProperty is! ConcreteMember) {
147 return getter ? htmlProperty.getter : htmlProperty.setter;
148 } else {
149 return null;
150 }
151 } else {
152 return htmlType.getMember(implMember.name);
153 }
154 }
155
156 List<Type> htmlToDomTypes(Type htmlType) {
157 if (htmlType.name == null) return [];
158 final tags = _getTags(findComment(htmlType.span));
159
160 if (tags.containsKey('domName')) {
161 var domNames = map(tags['domName'].split(','), (s) => s.trim());
162 if (domNames.length == 1 && domNames[0] == 'none') return [];
163 return map(domNames, (domName) {
164 // DOMWindow is Chrome-specific, so we don't use it in our annotations.
165 if (domName == 'Window') domName = 'DOMWindow';
166 final domType = world.dom.types[domName];
167 if (domType == null) print('Warning: no dart:dom type named $domName');
168 return domType;
169 });
170 } else {
171 if (!htmlType.name.endsWith('WrappingImplementation')) return [];
172 final domName = htmlType.name.replaceFirst('WrappingImplementation', '');
173 var domType = world.dom.types[domName];
174 if (domType == null && domName.endsWith('Element')) {
175 domType = world.dom.types['HTML$domName'];
176 }
177 if (domType == null) domType = world.dom.types['WebKit$domName'];
178 if (domType == null) {
179 print('Warning: no dart:dom type matches dart:htmlimpl ' +
180 htmlType.name);
181 return [];
182 }
183 return [domType];
184 }
185 }
186
187 Set<Member> htmlToDomMembers(Member htmlMember, List<Type> domTypes) {
188 if (htmlMember.isPrivate || htmlMember is! MethodMember) return new Set();
189 final tags = _getTags(findComment(htmlMember.span));
190 if (tags.containsKey('domName')) {
191 final domNames = map(tags['domName'].split(','), (s) => s.trim());
192 if (domNames.length == 1 && domNames[0] == 'none') return new Set();
193 final members = new Set();
194 domNames.forEach((name) {
195 var nameMembers = _membersFromName(name, domTypes);
196 if (nameMembers.isEmpty()) {
197 if (name.contains('.')) {
198 print('Warning: no member $name');
199 } else {
200 final options = Strings.join(
201 map(domTypes, (t) => "${t.name}.$name"), ' or ');
202 print('Warning: no member $options');
203 }
204 }
205 members.addAll(nameMembers);
206 });
207 return members;
208 }
209
210 if (domTypes.isEmpty() || htmlMember.definition == null) return new Set();
211 if (htmlMember.name == 'get:on') {
212 final members = _membersFromName('addEventListener', domTypes);
213 members.addAll(_membersFromName('dispatchEvent', domTypes));
214 members.addAll(_membersFromName('removeEventListener', domTypes));
215 return members;
216 }
217
218 if (htmlMember.isFactory && htmlMember.name == '' &&
219 domTypes.length == 1 && domTypes[0].name.endsWith('Event')) {
220 return _membersFromName('init${domTypes[0].name}', domTypes);
221 }
222
223 return _getDomMembers(htmlMember.definition.body, domTypes);
224 }
225
226 Set<Member> _membersFromName(String name, List<Type> defaultTypes) {
227 if (!name.contains('.', 0)) {
228 if (defaultTypes.isEmpty()) {
229 print('Warning: no default type for ${name}');
230 return new Set();
231 }
232 final members = new Set<Member>();
233 defaultTypes.forEach((t) {
234 if (t.members.containsKey(name)) members.add(t.members[name]);
235 });
236 return members;
237 }
238
239 final splitName = name.split('.');
240 if (splitName.length != 2) {
241 print('Warning: invalid member name ${name}');
242 return new Set();
243 }
244 var typeName = splitName[0];
245 if (typeName == 'Window') typeName = 'DOMWindow';
246 final type = world.dom.types[typeName];
247 if (type == null) return new Set();
248 final member = type.members[splitName[1]];
249 if (member == null) return new Set();
250 return new Set.from([member]);
251 }
252
253 Set<Member> _getDomMembers(Statement stmt, List<Type> domTypes) {
254 if (stmt is BlockStatement) {
255 final body = stmt.body.filter((s) => !_ignorableStatement(s));
256 if (body.length != 1) return new Set();
257 return _getDomMembers(stmt.body[0], domTypes);
258 } else if (stmt is ReturnStatement) {
259 return _domMembersFromExpression(stmt.value, domTypes);
260 } else if (stmt is ExpressionStatement) {
261 return _domMembersFromExpression(stmt.body, domTypes);
262 } else if (stmt is TryStatement) {
263 return _getDomMembers(stmt.body, domTypes);
264 } else if (stmt is IfStatement) {
265 final members = _getDomMembers(stmt.trueBranch, domTypes);
266 members.addAll(_getDomMembers(stmt.falseBranch, domTypes));
267 return members;
268 } else {
269 return new Set();
270 }
271 }
272
273 /**
274 * Whether a statement can be ignored for the purpose of determining the DOM
275 * name of the enclosing method. The Webkit-to-Dart conversion process leaves
276 * behind various throws and returns that we want to ignore.
277 */
278 bool _ignorableStatement(Statement stmt) {
279 if (stmt is BlockStatement) {
280 return Collections.every(stmt.body, (s) => _ignorableStatement(s));
281 } else if (stmt is TryStatement) {
282 return _ignorableStatement(stmt.body);
283 } else if (stmt is IfStatement) {
284 return _ignorableStatement(stmt.trueBranch) &&
285 _ignorableStatement(stmt.falseBranch);
286 } else if (stmt is ReturnStatement) {
287 return stmt.value == null || stmt.value is ThisExpression;
288 } else {
289 return stmt is ThrowStatement;
290 }
291 }
292
293 Set<Member> _domMembersFromExpression(Expression expr, List<Type> domTypes) {
294 if (expr is BinaryExpression && expr.op.kind == TokenKind.ASSIGN) {
295 return _domMembersFromExpression(expr.x, domTypes);
296 } else if (expr is CallExpression) {
297 if (expr.target is DotExpression && expr.target.self is VarExpression &&
298 expr.target.self.name.name == 'LevelDom' &&
299 (expr.target.name.name.startsWith('wrap') ||
300 expr.target.name.name == 'unwrap')) {
301 return _domMembersFromExpression(expr.arguments[0].value, domTypes);
302 }
303 return _domMembersFromExpression(expr.target, domTypes);
304 } else if (expr is DotExpression) {
305 if (expr.self is NewExpression && expr.name.name == '_wrap' &&
306 expr.self.arguments.length == 1) {
307 return _domMembersFromExpression(expr.self.arguments[0].value,
308 domTypes);
309 } else if (expr.self is VarExpression && expr.self.name.name == '_ptr') {
310 return _membersFromName(expr.name.name, domTypes);
311 }
312 final bases = _domMembersFromExpression(expr.self, domTypes);
313 return new Set.from(map(bases, (base) {
314 if (base == null || base.returnType == null) return null;
315 return base.returnType.members[expr.name.name];
316 }).filter((m) => m != null));
317 } else if (expr is NewExpression && expr.arguments.length == 1) {
318 return _domMembersFromExpression(expr.arguments[0].value, domTypes);
319 } else {
320 return new Set();
321 }
322 }
323
324 Map<String, String> _getTags(String comment) {
325 if (comment == null) return const <String>{};
326 final re = new RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)");
327 final tags = <String>{};
328 for (var m in re.allMatches(comment.trim())) {
329 tags[m[1]] = m[2];
330 }
331 return tags;
332 }
333 }
OLDNEW
« no previous file with comments | « client/html/scripts/html-diff.dart ('k') | client/html/scripts/html_doc.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698