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 /** Collects several code emitters for the template tool. */ | 5 /** Collects several code emitters for the template tool. */ |
6 library emitters; | 6 library emitters; |
7 | 7 |
8 import 'package:html5lib/dom.dart'; | 8 import 'package:html5lib/dom.dart'; |
9 import 'package:html5lib/dom_parsing.dart'; | 9 import 'package:html5lib/dom_parsing.dart'; |
10 | 10 |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 | 344 |
345 var code = codeInfo.code; | 345 var code = codeInfo.code; |
346 var match = new RegExp('class ${info.constructor}[^{]*{').firstMatch(code); | 346 var match = new RegExp('class ${info.constructor}[^{]*{').firstMatch(code); |
347 if (match != null) { | 347 if (match != null) { |
348 var printer = new CodePrinter(); | 348 var printer = new CodePrinter(); |
349 var libraryName = (codeInfo.libraryName != null) | 349 var libraryName = (codeInfo.libraryName != null) |
350 ? codeInfo.libraryName | 350 ? codeInfo.libraryName |
351 : info.tagName.replaceAll(new RegExp('[-./]'), '_'); | 351 : info.tagName.replaceAll(new RegExp('[-./]'), '_'); |
352 printer.add(codegen.header(info.declaringFile.path, libraryName)); | 352 printer.add(codegen.header(info.declaringFile.path, libraryName)); |
353 | 353 |
354 // Add exisitng import, export, and part directives. | 354 // Add existing import, export, and part directives. |
355 for (var directive in codeInfo.directives) { | 355 for (var directive in codeInfo.directives) { |
356 printer.add(codegen.directiveText(directive, info, pathInfo)); | 356 printer.add(codegen.directiveText(directive, info, pathInfo)); |
357 } | 357 } |
358 | 358 |
359 // Add imports only for those components used by this component. | 359 // Add imports only for those components used by this component. |
360 var imports = info.usedComponents.keys.mappedBy( | 360 var imports = info.usedComponents.keys.mappedBy( |
361 (c) => pathInfo.relativePath(info, c)); | 361 (c) => pathInfo.relativePath(info, c)); |
362 | 362 |
363 if (hasExtends) { | 363 if (hasExtends) { |
364 // Inject an import to the base component. | 364 // Inject an import to the base component. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 | 414 |
415 // Inject library name if not pressent. | 415 // Inject library name if not pressent. |
416 var codeInfo = _fileInfo.userCode; | 416 var codeInfo = _fileInfo.userCode; |
417 if (codeInfo == null) { | 417 if (codeInfo == null) { |
418 codeInfo = new DartCodeInfo(null, null, [], 'main(){\n}'); | 418 codeInfo = new DartCodeInfo(null, null, [], 'main(){\n}'); |
419 } | 419 } |
420 var libraryName = codeInfo.libraryName != null | 420 var libraryName = codeInfo.libraryName != null |
421 ? codeInfo.libraryName : _fileInfo.libraryName; | 421 ? codeInfo.libraryName : _fileInfo.libraryName; |
422 printer.add(codegen.header(_fileInfo.path, libraryName)); | 422 printer.add(codegen.header(_fileInfo.path, libraryName)); |
423 | 423 |
424 // Add exisitng import, export, and part directives. | 424 // Add existing import, export, and part directives. |
425 for (var directive in codeInfo.directives) { | 425 for (var directive in codeInfo.directives) { |
426 printer.add(codegen.directiveText(directive, _fileInfo, pathInfo)); | 426 printer.add(codegen.directiveText(directive, _fileInfo, pathInfo)); |
427 } | 427 } |
428 | 428 |
429 // Import only those components used by the page. | 429 // Import only those components used by the page. |
430 var imports = _fileInfo.usedComponents.keys.mappedBy( | 430 var imports = _fileInfo.usedComponents.keys.mappedBy( |
431 (c) => pathInfo.relativePath(_fileInfo, c)); | 431 (c) => pathInfo.relativePath(_fileInfo, c)); |
432 var mainCode = codegen.mainDartCode( | 432 var mainCode = codegen.mainDartCode( |
433 codeInfo.code, _context.declarations.formatString(1), | 433 codeInfo.code, _context.declarations.formatString(1), |
434 _context.printer.formatString(1)); | 434 _context.printer.formatString(1)); |
435 printer.add(codegen.importList(imports)).addRaw(mainCode); | 435 printer.add(codegen.importList(imports)).addRaw(mainCode); |
436 return printer.formatString(); | 436 return printer.formatString(); |
437 } | 437 } |
438 } | 438 } |
439 | 439 |
| 440 /** Emits a .dart file using the [DartCodeInfo]. */ |
| 441 String emitDartFile(FileInfo fileInfo, PathInfo pathInfo) { |
| 442 var printer = new CodePrinter(); |
| 443 |
| 444 // Inject library name if not pressent. |
| 445 var codeInfo = fileInfo.userCode; |
| 446 var libraryName = codeInfo.libraryName; |
| 447 printer.add(codegen.header(fileInfo.path, codeInfo.libraryName)); |
| 448 |
| 449 // Add existing import, export, and part directives. |
| 450 for (var directive in codeInfo.directives) { |
| 451 printer.add(codegen.directiveText(directive, fileInfo, pathInfo)); |
| 452 } |
| 453 printer.addRaw(codeInfo.code); |
| 454 return printer.formatString(); |
| 455 } |
| 456 |
440 /** Clears all fields in [declarations]. */ | 457 /** Clears all fields in [declarations]. */ |
441 String _clearFields(Declarations declarations) { | 458 String _clearFields(Declarations declarations) { |
442 if (declarations.declarations.isEmpty) return ''; | 459 if (declarations.declarations.isEmpty) return ''; |
443 var buff = new StringBuffer(); | 460 var buff = new StringBuffer(); |
444 for (var d in declarations.declarations) { | 461 for (var d in declarations.declarations) { |
445 buff.add('${d.name} = '); | 462 buff.add('${d.name} = '); |
446 } | 463 } |
447 buff.add('null;'); | 464 buff.add('null;'); |
448 return buff.toString(); | 465 return buff.toString(); |
449 } | 466 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 } | 520 } |
504 typeName = htmlElementExtends[typeName]; | 521 typeName = htmlElementExtends[typeName]; |
505 } | 522 } |
506 // If we didn't find a DOM setter, and this is a component, set a property on | 523 // If we didn't find a DOM setter, and this is a component, set a property on |
507 // the component. | 524 // the component. |
508 if (info.component != null && !name.startsWith('data-')) { | 525 if (info.component != null && !name.startsWith('data-')) { |
509 return 'xtag.${toCamelCase(name)}'; | 526 return 'xtag.${toCamelCase(name)}'; |
510 } | 527 } |
511 return "attributes['$name']"; | 528 return "attributes['$name']"; |
512 } | 529 } |
OLD | NEW |