| 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:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| 11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 12 | 12 |
| 13 import 'common.dart'; | 13 import 'common.dart'; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Transformer that extracts Dart code inlined in HTML script tags and outputs a | 16 * Transformer that extracts Dart code inlined in HTML script tags and outputs a |
| 17 * separate file for each. | 17 * separate file for each. |
| 18 */ | 18 */ |
| 19 class InlineCodeExtractor extends Transformer { | 19 class InlineCodeExtractor extends Transformer { |
| 20 Future<bool> isPrimary(Asset input) => | 20 Future<bool> isPrimary(Asset input) => |
| 21 new Future.value(input.id.extension == ".html"); | 21 new Future.value(input.id.extension == ".html"); |
| 22 | 22 |
| 23 Future apply(Transform transform) { | 23 Future apply(Transform transform) { |
| 24 var inputId = transform.primaryId; | 24 var inputId = transform.primaryInput.id; |
| 25 return getPrimaryContent(transform).then((content) { | 25 return transform.primaryInput.readAsString().then((content) { |
| 26 var document = parseHtml(content, inputId.path, transform.logger); | 26 var document = parseHtml(content, inputId.path, transform.logger); |
| 27 int count = 0; | 27 int count = 0; |
| 28 bool htmlChanged = false; | 28 bool htmlChanged = false; |
| 29 for (var tag in document.queryAll('script')) { | 29 for (var tag in document.queryAll('script')) { |
| 30 // Only process tags that have inline Dart code | 30 // Only process tags that have inline Dart code |
| 31 if (tag.attributes['type'] != 'application/dart' || | 31 if (tag.attributes['type'] != 'application/dart' || |
| 32 tag.attributes.containsKey('src')) { | 32 tag.attributes.containsKey('src')) { |
| 33 continue; | 33 continue; |
| 34 } | 34 } |
| 35 htmlChanged = true; | 35 htmlChanged = true; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 49 var id = inputId.addExtension('.$count.dart'); | 49 var id = inputId.addExtension('.$count.dart'); |
| 50 transform.addOutput(new Asset.fromString(id, textContent.value)); | 50 transform.addOutput(new Asset.fromString(id, textContent.value)); |
| 51 textContent.remove(); | 51 textContent.remove(); |
| 52 count++; | 52 count++; |
| 53 } | 53 } |
| 54 transform.addOutput(new Asset.fromString(inputId, | 54 transform.addOutput(new Asset.fromString(inputId, |
| 55 htmlChanged ? document.outerHtml : content)); | 55 htmlChanged ? document.outerHtml : content)); |
| 56 }); | 56 }); |
| 57 } | 57 } |
| 58 } | 58 } |
| OLD | NEW |