OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, 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 /// Some Transformers to maintain compatibility with the new `test` package, |
| 6 /// since it doesn't use normal dart script tags in html. We get around this by |
| 7 /// using two transformers: |
| 8 /// - A transformer to convert <lint rel="x-dart-test"> intro dart script tags |
| 9 /// so that they can be processed by the rest of the transformers normally. |
| 10 /// - A second transformer to convert the output back, so that the `test` |
| 11 /// package can find a <link rel="x-dart-test"> tag after all the |
| 12 /// transformations are done. |
| 13 library web_components.build.test_compatability.dart; |
| 14 |
| 15 import 'dart:async'; |
| 16 import 'package:barback/barback.dart'; |
| 17 import 'package:html/dom.dart'; |
| 18 import 'package:html/parser.dart'; |
| 19 |
| 20 /// The name of the attribute that will be added to script tags that started out |
| 21 /// as <link rel="x-dart-test"> tags. |
| 22 const testAttribute = '_was_test'; |
| 23 |
| 24 /// The first transformer that should be ran, this does a query selector for |
| 25 /// link[rel="x-dart-test"] and changes them to a normal dart script tag with a |
| 26 /// special `_was_test` attribute so we know to change it back later on. |
| 27 class RewriteXDartTestToScript extends _EntryPointOnlyTransformer { |
| 28 RewriteXDartTestToScript(List<String> entryPoints) : super(entryPoints); |
| 29 |
| 30 Future apply(Transform transform) { |
| 31 return transform.primaryInput.readAsString().then((String html) { |
| 32 var doc = parse(html); |
| 33 for (var tag in doc.querySelectorAll('link[rel="x-dart-test"]')) { |
| 34 tag.replaceWith(new Element.tag('script') |
| 35 ..attributes['type'] = 'application/dart' |
| 36 ..attributes['src'] = tag.attributes['href'] |
| 37 ..attributes[testAttribute] = ''); |
| 38 } |
| 39 transform.addOutput( |
| 40 new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); |
| 41 }); |
| 42 } |
| 43 } |
| 44 |
| 45 /// The last transformer that should be ran, this does a query selector for |
| 46 /// `script[type="application/dart"][_was_test]` and changes matching elements |
| 47 /// back to `<link rel="x-dart-test">` tags. |
| 48 class RewriteScriptToXDartTest extends _EntryPointOnlyTransformer { |
| 49 RewriteScriptToXDartTest(List<String> entryPoints) : super(entryPoints); |
| 50 |
| 51 Future apply(Transform transform) { |
| 52 return transform.primaryInput.readAsString().then((String html) { |
| 53 var doc = parse(html); |
| 54 var scripts = doc.querySelectorAll( |
| 55 'script[type="application/dart"][$testAttribute]'); |
| 56 for (var tag in scripts) { |
| 57 tag.replaceWith(new Element.tag('link') |
| 58 ..attributes['rel'] = 'x-dart-test' |
| 59 ..attributes['href'] = tag.attributes['src']); |
| 60 } |
| 61 transform.addOutput( |
| 62 new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); |
| 63 }); |
| 64 } |
| 65 } |
| 66 |
| 67 /// Internal base class to encapsulate the isPrimary logic. |
| 68 abstract class _EntryPointOnlyTransformer extends Transformer { |
| 69 final List<String> entryPoints; |
| 70 |
| 71 _EntryPointOnlyTransformer(this.entryPoints) : super(); |
| 72 |
| 73 bool isPrimary(AssetId id) { |
| 74 if (!id.path.startsWith('test/')) return false; |
| 75 if (entryPoints != null) return entryPoints.contains(id.path); |
| 76 // If no entry point is supplied, then any html file is an entry point. |
| 77 return id.path.endsWith('.html'); |
| 78 } |
| 79 } |
OLD | NEW |