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, | |
Siggi Cherem (dart-lang)
2015/04/23 20:52:04
nit: Some transformers => Transformers
jakemac
2015/04/24 14:35:56
Done.
| |
6 /// since it doesn't use normal dart script tags in html. | |
Siggi Cherem (dart-lang)
2015/04/23 20:52:03
minor nit: consider adding here a comment saying t
jakemac
2015/04/24 14:35:56
Done.
| |
7 library web_components.build.test_compatability.dart; | |
8 | |
9 import 'dart:async'; | |
10 import 'package:barback/barback.dart'; | |
11 import 'package:html/dom.dart'; | |
12 import 'package:html/parser.dart'; | |
13 | |
14 /// The name of the attribute that will be added to script tags that started out | |
15 /// as <link rel="x-dart-test"> tags. | |
16 const testAttribute = '_was_test'; | |
17 | |
18 /// The first transformer that should be ran, this does a query selector for | |
19 /// link[rel="x-dart-test"] and changes them to a normal dart script tag. | |
20 class RewriteXDartTestToScript extends _BaseTestCompatTransformer { | |
21 RewriteXDartTestToScript(List<String> entryPoints) : super(entryPoints); | |
22 | |
23 Future apply(Transform transform) { | |
24 return transform.primaryInput.readAsString().then((String html) { | |
25 var doc = parse(html); | |
26 for (var tag in doc.querySelectorAll('link[rel="x-dart-test"]')) { | |
27 tag.replaceWith(new Element.tag('script') | |
28 ..attributes['type'] = 'application/dart' | |
29 ..attributes['src'] = tag.attributes['href'] | |
30 ..attributes[testAttribute] = ''); | |
31 } | |
32 transform.addOutput( | |
33 new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); | |
34 }); | |
35 } | |
36 } | |
37 | |
38 /// The last transformer that should be ran, this does a query selector for | |
39 /// script[type="application/dart"][_was_test] and changes them to a | |
40 /// link[rel="x-dart-test"] tag. | |
Siggi Cherem (dart-lang)
2015/04/23 20:52:03
minor nit:
a link[rel="x-dart-test"] tag
=>
a
jakemac
2015/04/24 14:35:56
Done.
| |
41 class RewriteScriptToXDartTest extends _BaseTestCompatTransformer { | |
42 RewriteScriptToXDartTest(List<String> entryPoints) : super(entryPoints); | |
43 | |
44 Future apply(Transform transform) { | |
45 return transform.primaryInput.readAsString().then((String html) { | |
46 var doc = parse(html); | |
47 var scripts = doc.querySelectorAll( | |
48 'script[type="application/dart"][$testAttribute]'); | |
49 for (var tag in scripts) { | |
50 tag.replaceWith(new Element.tag('link') | |
51 ..attributes['rel'] = 'x-dart-test' | |
52 ..attributes['href'] = tag.attributes['src']); | |
53 } | |
54 transform.addOutput( | |
55 new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); | |
56 }); | |
57 } | |
58 } | |
59 | |
60 /// Internal base class to encapsulate the isPrimary logic. | |
61 abstract class _BaseTestCompatTransformer extends Transformer { | |
Siggi Cherem (dart-lang)
2015/04/23 20:52:03
maybe rename to: _EntryPointOnlyTransformer?
(it'
jakemac
2015/04/24 14:35:56
Done.
| |
62 final List<String> entryPoints; | |
63 | |
64 _BaseTestCompatTransformer(this.entryPoints) : super(); | |
65 | |
66 bool isPrimary(AssetId id) { | |
67 if (!id.path.startsWith('test/')) return false; | |
68 if (entryPoints != null) return entryPoints.contains(id.path); | |
69 // If no entry point is supplied, then any html file is an entry point. | |
70 return id.path.endsWith('.html'); | |
71 } | |
72 } | |
OLD | NEW |