Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Unified Diff: lib/build/test_compatibility.dart

Issue 1104453007: add support for x-dart-test link tags in the new test package (Closed) Base URL: git@github.com:dart-lang/web-components.git@master
Patch Set: clarify some comments, and make sure the test/dart.js script remains in the right place Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « CHANGELOG.md ('k') | lib/transformer.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..46875025c5bd8c874fea7eea6caf9fa5f2227e83
--- /dev/null
+++ b/lib/build/test_compatibility.dart
@@ -0,0 +1,79 @@
+// 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,
+/// since it doesn't use normal dart script tags in html. We get around this by
+/// using two transformers:
+/// - A transformer to convert <lint rel="x-dart-test"> intro dart script tags
+/// so that they can be processed by the rest of the transformers normally.
+/// - A second transformer to convert the output back, so that the `test`
+/// package can find a <link rel="x-dart-test"> tag after all the
+/// transformations are 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 with a
+/// special `_was_test` attribute so we know to change it back later on.
+class RewriteXDartTestToScript extends _EntryPointOnlyTransformer {
+ 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 matching elements
+/// back to `<link rel="x-dart-test">` tags.
+class RewriteScriptToXDartTest extends _EntryPointOnlyTransformer {
+ 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 _EntryPointOnlyTransformer extends Transformer {
+ final List<String> entryPoints;
+
+ _EntryPointOnlyTransformer(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');
+ }
+}
« no previous file with comments | « CHANGELOG.md ('k') | lib/transformer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698