Chromium Code Reviews| Index: lib/build/test_compatibility.dart |
| diff --git a/lib/build/test_compatibility.dart b/lib/build/test_compatibility.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4eb81f303a5cd38bf4df63871ebbad9aa79e1929 |
| --- /dev/null |
| +++ b/lib/build/test_compatibility.dart |
| @@ -0,0 +1,72 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +/// 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.
|
| +/// 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.
|
| +library web_components.build.test_compatability.dart; |
| + |
| +import 'dart:async'; |
| +import 'package:barback/barback.dart'; |
| +import 'package:html/dom.dart'; |
| +import 'package:html/parser.dart'; |
| + |
| +/// The name of the attribute that will be added to script tags that started out |
| +/// as <link rel="x-dart-test"> tags. |
| +const testAttribute = '_was_test'; |
| + |
| +/// The first transformer that should be ran, this does a query selector for |
| +/// link[rel="x-dart-test"] and changes them to a normal dart script tag. |
| +class RewriteXDartTestToScript extends _BaseTestCompatTransformer { |
| + RewriteXDartTestToScript(List<String> entryPoints) : super(entryPoints); |
| + |
| + Future apply(Transform transform) { |
| + return transform.primaryInput.readAsString().then((String html) { |
| + var doc = parse(html); |
| + for (var tag in doc.querySelectorAll('link[rel="x-dart-test"]')) { |
| + tag.replaceWith(new Element.tag('script') |
| + ..attributes['type'] = 'application/dart' |
| + ..attributes['src'] = tag.attributes['href'] |
| + ..attributes[testAttribute] = ''); |
| + } |
| + transform.addOutput( |
| + new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); |
| + }); |
| + } |
| +} |
| + |
| +/// The last transformer that should be ran, this does a query selector for |
| +/// script[type="application/dart"][_was_test] and changes them to a |
| +/// 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.
|
| +class RewriteScriptToXDartTest extends _BaseTestCompatTransformer { |
| + RewriteScriptToXDartTest(List<String> entryPoints) : super(entryPoints); |
| + |
| + Future apply(Transform transform) { |
| + return transform.primaryInput.readAsString().then((String html) { |
| + var doc = parse(html); |
| + var scripts = doc.querySelectorAll( |
| + 'script[type="application/dart"][$testAttribute]'); |
| + for (var tag in scripts) { |
| + tag.replaceWith(new Element.tag('link') |
| + ..attributes['rel'] = 'x-dart-test' |
| + ..attributes['href'] = tag.attributes['src']); |
| + } |
| + transform.addOutput( |
| + new Asset.fromString(transform.primaryInput.id, doc.outerHtml)); |
| + }); |
| + } |
| +} |
| + |
| +/// Internal base class to encapsulate the isPrimary logic. |
| +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.
|
| + final List<String> entryPoints; |
| + |
| + _BaseTestCompatTransformer(this.entryPoints) : super(); |
| + |
| + bool isPrimary(AssetId id) { |
| + if (!id.path.startsWith('test/')) return false; |
| + if (entryPoints != null) return entryPoints.contains(id.path); |
| + // If no entry point is supplied, then any html file is an entry point. |
| + return id.path.endsWith('.html'); |
| + } |
| +} |