| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** Transfomer that extracts inlined script code into separate assets. */ | 5 /** Transfomer that extracts inlined script code into separate assets. */ |
| 6 library polymer.src.transformers; | 6 library polymer.src.transformers; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:analyzer_experimental/src/generated/ast.dart'; |
| 11 import 'package:analyzer_experimental/src/generated/error.dart'; |
| 12 import 'package:analyzer_experimental/src/generated/parser.dart'; |
| 13 import 'package:analyzer_experimental/src/generated/scanner.dart'; |
| 10 import 'package:barback/barback.dart'; | 14 import 'package:barback/barback.dart'; |
| 11 import 'package:path/path.dart' as path; | 15 import 'package:path/path.dart' as path; |
| 12 | 16 |
| 13 import 'common.dart'; | 17 import 'common.dart'; |
| 14 | 18 |
| 15 /** | 19 /** |
| 16 * Transformer that extracts Dart code inlined in HTML script tags and outputs a | 20 * Transformer that extracts Dart code inlined in HTML script tags and outputs a |
| 17 * separate file for each. | 21 * separate file for each. |
| 18 */ | 22 */ |
| 19 class InlineCodeExtractor extends Transformer { | 23 class InlineCodeExtractor extends Transformer { |
| 20 Future<bool> isPrimary(Asset input) => | 24 /** Only run this transformer on .html files. */ |
| 21 new Future.value(input.id.extension == ".html"); | 25 final String allowedExtensions = ".html"; |
| 26 |
| 22 | 27 |
| 23 Future apply(Transform transform) { | 28 Future apply(Transform transform) { |
| 24 var inputId = transform.primaryId; | 29 var inputId = transform.primaryId; |
| 25 return getPrimaryContent(transform).then((content) { | 30 return getPrimaryContent(transform).then((content) { |
| 26 var document = parseHtml(content, inputId.path, transform.logger); | 31 var document = parseHtml(content, inputId.path, transform.logger); |
| 27 int count = 0; | 32 int count = 0; |
| 28 bool htmlChanged = false; | 33 bool htmlChanged = false; |
| 29 for (var tag in document.queryAll('script')) { | 34 for (var tag in document.queryAll('script')) { |
| 30 // Only process tags that have inline Dart code | 35 // Only process tags that have inline Dart code |
| 31 if (tag.attributes['type'] != 'application/dart' || | 36 if (tag.attributes['type'] != 'application/dart' || |
| 32 tag.attributes.containsKey('src')) { | 37 tag.attributes.containsKey('src')) { |
| 33 continue; | 38 continue; |
| 34 } | 39 } |
| 35 htmlChanged = true; | 40 htmlChanged = true; |
| 36 | 41 |
| 37 // Remove empty tags | 42 // Remove empty tags |
| 38 if (tag.nodes.length == 0) { | 43 if (tag.nodes.length == 0) { |
| 39 tag.remove(); | 44 tag.remove(); |
| 40 continue; | 45 continue; |
| 41 } | 46 } |
| 42 | 47 |
| 43 // TODO(sigmund): should we automatically include a library directive | |
| 44 // if it doesn't have one? | |
| 45 var filename = path.url.basename(inputId.path); | 48 var filename = path.url.basename(inputId.path); |
| 46 // TODO(sigmund): ensure this filename is unique (dartbug.com/12618). | 49 // TODO(sigmund): ensure this filename is unique (dartbug.com/12618). |
| 47 tag.attributes['src'] = '$filename.$count.dart'; | 50 tag.attributes['src'] = '$filename.$count.dart'; |
| 48 var textContent = tag.nodes.first; | 51 var textContent = tag.nodes.first; |
| 52 var code = textContent.value; |
| 49 var id = inputId.addExtension('.$count.dart'); | 53 var id = inputId.addExtension('.$count.dart'); |
| 50 transform.addOutput(new Asset.fromString(id, textContent.value)); | 54 if (!_hasLibraryDirective(code)) { |
| 55 var libname = path.withoutExtension(id.path) |
| 56 .replaceAll(new RegExp('[-./]'), '_'); |
| 57 code = "library $libname;\n$code"; |
| 58 } |
| 59 transform.addOutput(new Asset.fromString(id, code)); |
| 51 textContent.remove(); | 60 textContent.remove(); |
| 52 count++; | 61 count++; |
| 53 } | 62 } |
| 54 transform.addOutput(new Asset.fromString(inputId, | 63 transform.addOutput(new Asset.fromString(inputId, |
| 55 htmlChanged ? document.outerHtml : content)); | 64 htmlChanged ? document.outerHtml : content)); |
| 56 }); | 65 }); |
| 57 } | 66 } |
| 58 } | 67 } |
| 68 |
| 69 /** Parse [code] and determine whether it has a library directive. */ |
| 70 bool _hasLibraryDirective(String code) { |
| 71 var errorListener = new _ErrorCollector(); |
| 72 var token = new StringScanner(null, code, errorListener).tokenize(); |
| 73 var unit = new Parser(null, errorListener).parseCompilationUnit(token); |
| 74 return unit.directives.any((d) => d is LibraryDirective); |
| 75 } |
| 76 |
| 77 class _ErrorCollector extends AnalysisErrorListener { |
| 78 final errors = <AnalysisError>[]; |
| 79 onError(error) => errors.add(error); |
| 80 } |
| OLD | NEW |