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