Chromium Code Reviews| Index: lib/src/emitters.dart |
| diff --git a/lib/src/emitters.dart b/lib/src/emitters.dart |
| index 51e767d286e32d3af311d8b058e208b8607cf4df..2c653a4916c0c3175b726122c31ff2ca675216de 100644 |
| --- a/lib/src/emitters.dart |
| +++ b/lib/src/emitters.dart |
| @@ -13,9 +13,10 @@ import 'package:html5lib/dom_parsing.dart'; |
| import 'code_printer.dart'; |
| import 'codegen.dart' as codegen; |
| -import 'messages.dart'; |
| +import 'file_system/path.dart'; |
| import 'files.dart'; |
| import 'info.dart'; |
| +import 'messages.dart'; |
| /** |
| * An emitter for a web component feature. It collects all the logic for |
| @@ -527,7 +528,7 @@ class RecursiveEmitter extends TreeVisitor { |
| class WebComponentEmitter extends RecursiveEmitter { |
| WebComponentEmitter(FileInfo info) : super(info); |
| - String run(ComponentInfo info) { |
| + String run(ComponentInfo info, PathInfo pathInfo) { |
| // If this derives from another component, ensure the lifecycle methods are |
| // called in the superclass. |
| if (info.extendsComponent != null) { |
| @@ -561,45 +562,43 @@ class WebComponentEmitter extends RecursiveEmitter { |
| visit(info.element); |
| - var code = info.userCode; |
| - if (code == null) { |
| - if (info.extendsComponent != null) { |
| - var superclass = info.extendsComponent.constructor; |
| - // TODO(jmesserly): should we add this import even if you have your own |
| - // script tag? |
| - var relativePath = PathInfo.relativePath(info, info.extendsComponent); |
| - code = ''' |
| - ${codegen.importList([relativePath])} |
| - class ${info.constructor} extends $superclass {\n} |
| - '''; |
| - } else { |
| - code = ''' |
| - import 'package:web_components/web_component.dart' as autogenerated; |
| - class ${info.constructor} extends autogenerated.WebComponent {\n} |
| - '''; |
| - } |
| + bool hasExtends = info.extendsComponent != null; |
| + var codeInfo = info.userCode; |
| + if (codeInfo == null) { |
| + var superclass = hasExtends ? info.extendsComponent.constructor |
| + : 'autogenerated.WebComponent'; |
| + var imports = hasExtends ? [] : [new DartDirectiveInfo('import', |
| + 'package:web_components/web_component.dart', 'autogenerated')]; |
| + codeInfo = new DartCodeInfo(null, null, imports, |
| + 'class ${info.constructor} extends $superclass {\n}'); |
| } |
| + |
| + var code = codeInfo.code; |
| var match = new RegExp('class ${info.constructor}[^{]*{').firstMatch(code); |
| if (match != null) { |
| - // TODO(sigmund): clean up and make this more robust. Issue #59. |
| var printer = new CodePrinter(); |
| - var libMatch = const RegExp('^library .*;').firstMatch(code); |
| - int startPos = 0; |
| - if (libMatch == null) { |
| - var libraryName = info.tagName.replaceAll(const RegExp('[-./]'), '_'); |
| - printer.add(codegen.header(info.declaringFile.path, libraryName)); |
| - } else { |
| - printer.add('// Generated from ${info.inputPath.filename}\n' |
| - '// DO NOT EDIT.'); |
| - printer.add(code.substring(0, libMatch.end())); |
| - printer.add(codegen.imports); |
| - startPos = libMatch.end(); |
| + var libraryName = (codeInfo.libraryName != null) |
| + ? codeInfo.libraryName |
| + : info.tagName.replaceAll(const RegExp('[-./]'), '_'); |
| + printer.add(codegen.header(info.declaringFile.path, libraryName)); |
| + |
| + // Add exisitng import, export, and part directives. |
| + for (var directive in codeInfo.directives) { |
| + printer.add(codegen.directiveText(directive, info, pathInfo)); |
| } |
| - // Import only those components used by this component. |
| + |
| + // Add imports only for those components used by this component. |
| var imports = info.usedComponents.getKeys().map( |
| (c) => PathInfo.relativePath(info, c)); |
| + |
| + if (hasExtends) { |
| + // Inject an import to the base component. |
| + printer.add(codegen.importList( |
| + [PathInfo.relativePath(info, info.extendsComponent)])); |
| + } |
| + |
| printer.add(codegen.importList(imports)) |
| - .add(code.substring(startPos, match.end())) |
| + .add(code.substring(0, match.end())) |
| .add('\n') |
| .add(codegen.componentCode(info.constructor, |
| _context.declarations.formatString(1), |
| @@ -612,7 +611,7 @@ class WebComponentEmitter extends RecursiveEmitter { |
| messages.error('please provide a class definition ' |
| 'for ${info.constructor}:\n $code', info.element.span, |
| file: info.inputPath); |
| - return code; |
| + return ''; |
| } |
| } |
| } |
| @@ -621,35 +620,46 @@ class WebComponentEmitter extends RecursiveEmitter { |
| class MainPageEmitter extends RecursiveEmitter { |
| MainPageEmitter(FileInfo info) : super(info); |
| - String run(Document document) { |
| + String run(Document document, PathInfo pathInfo) { |
| // The body of an element tag will not be part of the main HTML page. Each |
| // element will be generated programatically as a dart web component by |
| // [WebComponentEmitter] above. |
| document.queryAll('element').forEach((tag) => tag.remove()); |
| + |
| + // fix up the URLs to content that is not modified by the compiler |
| + document.queryAll('script').forEach((tag) { |
| + var src = tag.attributes["src"]; |
| + if (tag.attributes['type'] != 'application/dart' && src != null) { |
| + tag.attributes["src"] = pathInfo.transferDirectiveUrl(_info, src); |
| + } |
| + }); |
| + document.queryAll('link').forEach((tag) { |
| + var href = tag.attributes['href']; |
| + if (tag.attributes['rel'] == 'stylesheet' && href != null) { |
|
Jennifer Messerly
2012/10/25 04:26:25
I think this should be != "components". there are
Siggi Cherem (dart-lang)
2012/10/25 05:17:29
Done.
|
| + tag.attributes['href'] = pathInfo.transferDirectiveUrl(_info, href); |
| + } |
| + }); |
| + |
| visit(document); |
| var printer = new CodePrinter(); |
| - var startPos = 0; |
| // Inject library name if not pressent. |
| - // TODO(sigmund): consider parsing the top-level syntax of a dart file |
| - // instead of this ad-hoc regex (issue #95). |
| - var code = _info.userCode; |
| - var match = const RegExp('^library .*;').firstMatch(code); |
| - if (match == null) { |
| - printer.add(codegen.header(_info.path, _info.libraryName)); |
| - } else { |
| - printer.add('// Generated from ${_info.inputPath}\n// DO NOT EDIT.') |
| - .add(code.substring(0, match.end())) |
| - .add(codegen.imports); |
| - startPos = match.end(); |
| + var codeInfo = _info.userCode; |
| + var libraryName = codeInfo.libraryName != null |
| + ? codeInfo.libraryName : _info.libraryName; |
| + printer.add(codegen.header(_info.path, libraryName)); |
| + |
| + // Add exisitng import, export, and part directives. |
| + for (var directive in codeInfo.directives) { |
| + printer.add(codegen.directiveText(directive, _info, pathInfo)); |
| } |
| // Import only those components used by the page. |
| var imports = _info.usedComponents.getKeys().map( |
| (c) => PathInfo.relativePath(_info, c)); |
| printer.add(codegen.importList(imports)) |
| - .add(codegen.mainDartCode(code.substring(startPos), |
| + .add(codegen.mainDartCode(codeInfo.code, |
| _context.declarations.formatString(0), |
| _context.createdMethod.formatString(1), |
| _context.insertedMethod.formatString(1), |