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

Unified Diff: pkg/polymer/lib/src/mirror_loader.dart

Issue 293023008: Bring back initPolymer, allow boot.js only if using "polymer_experimental.html". (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 7 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 | « pkg/polymer/lib/src/loader.dart ('k') | pkg/polymer/lib/src/static_loader.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/polymer/lib/src/mirror_loader.dart
diff --git a/pkg/polymer/lib/src/mirror_loader.dart b/pkg/polymer/lib/src/mirror_loader.dart
index 6e8a2bddc50411fd955e7b6a2df411e24d94da9c..c0cea5c46aa11a15844976eb30e895880f332fe0 100644
--- a/pkg/polymer/lib/src/mirror_loader.dart
+++ b/pkg/polymer/lib/src/mirror_loader.dart
@@ -22,10 +22,11 @@ import 'dart:collection' show LinkedHashMap;
import 'dart:mirrors';
import 'package:logging/logging.dart' show Logger;
-import 'package:polymer/polymer.dart';
import 'package:observe/src/dirty_check.dart';
+import 'package:polymer/polymer.dart';
+/// Used by code generated from the experimental polymer bootstrap in boot.js.
void startPolymerInDevelopment(List<String> librariesToLoad) {
dirtyCheckZone()..run(() {
startPolymer(discoverInitializers(librariesToLoad), false);
@@ -36,11 +37,16 @@ void startPolymerInDevelopment(List<String> librariesToLoad) {
/// list by crawling HTML imports, searching for script tags, and including an
/// initializer for each type tagged with a [CustomTag] annotation and for each
/// top-level method annotated with [initMethod].
+List<Function> initializers = discoverInitializers(
+ discoverLibrariesToLoad(document, window.location.href));
+
+/// True if we're in deployment mode.
+bool deployMode = false;
/// Discovers what script tags are loaded from HTML pages and collects the
/// initializers of their corresponding libraries.
// Visible for testing only.
-List<Function> discoverInitializers(List<String> librariesToLoad) {
+List<Function> discoverInitializers(Iterable<String> librariesToLoad) {
var initializers = [];
for (var lib in librariesToLoad) {
try {
@@ -54,6 +60,114 @@ List<Function> discoverInitializers(List<String> librariesToLoad) {
return initializers;
}
+/// Walks the HTML import structure to discover all script tags that are
+/// implicitly loaded. This code is only used in Dartium and should only be
+/// called after all HTML imports are resolved. Polymer ensures this by asking
+/// users to put their Dart script tags after all HTML imports (this is checked
+/// by the linter, and Dartium will otherwise show an error message).
+List<_ScriptInfo> _discoverScripts(Document doc, String baseUri, [_State state]) {
+ if (state == null) state = new _State();
+ if (doc == null) {
+ print('warning: $baseUri not found.');
+ return state.scripts;
+ }
+ if (!state.seen.add(doc)) return state.scripts;
+
+ for (var node in doc.querySelectorAll('script,link[rel="import"]')) {
+ if (node is LinkElement) {
+ _discoverScripts(node.import, node.href, state);
+ } else if (node is ScriptElement && node.type == 'application/dart') {
+ state.scripts.add(_scriptInfoFor(node, baseUri));
+ }
+ }
+ return state.scripts;
+}
+
+/// Internal state used in [_discoverScripts].
+class _State {
+ /// Documents that we have visited thus far.
+ final Set<Document> seen = new Set();
+
+ /// Scripts that have been discovered, in tree order.
+ final List<_ScriptInfo> scripts = [];
+}
+
+/// Holds information about a Dart script tag.
+class _ScriptInfo {
+ /// The original URL seen in the tag fully resolved.
+ final String resolvedUrl;
+
+ /// Whether there was no URL, but code inlined instead.
+ bool get isInline => text != null;
+
+ /// Whether it seems to be a 'package:' URL (starts with the package-root).
+ bool get isPackage => packageUrl != null;
+
+ /// The equivalent 'package:' URL, if any.
+ final String packageUrl;
+
+ /// The inlined text, if any.
+ final String text;
+
+ /// Returns a base64 `data:` uri with the contents of [text].
+ // TODO(sigmund): change back to application/dart: using text/javascript seems
+ // wrong but it hides a warning in Dartium (dartbug.com/18000).
+ String get dataUrl => 'data:text/javascript;base64,${window.btoa(text)}';
+
+ /// URL to import the contents of the original script from Dart. This is
+ /// either the source URL if the script tag had a `src` attribute, or a base64
+ /// encoded `data:` URL if the script contents are inlined, or a `package:`
+ /// URL if the script can be resolved via a package URL.
+ String get importUrl =>
+ isInline ? dataUrl : (isPackage ? packageUrl : resolvedUrl);
+
+ _ScriptInfo(this.resolvedUrl, {this.packageUrl, this.text});
+}
+
+
+// TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the
+// root library (see dartbug.com/12612)
+final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri;
+
+/// Returns [_ScriptInfo] for [script] which was seen in [baseUri].
+_ScriptInfo _scriptInfoFor(script, baseUri) {
+ var uriString = script.src;
+ if (uriString != '') {
+ var uri = _rootUri.resolve(uriString);
+ if (!_isHttpStylePackageUrl(uri)) return new _ScriptInfo('$uri');
+ // Use package: urls if available. This rule here is more permissive than
+ // how we translate urls in polymer-build, but we expect Dartium to limit
+ // the cases where there are differences. The polymer-build issues an error
+ // when using packages/ inside lib without properly stepping out all the way
+ // to the packages folder. If users don't create symlinks in the source
+ // tree, then Dartium will also complain because it won't find the file seen
+ // in an HTML import.
+ var packagePath = uri.path.substring(
+ uri.path.lastIndexOf('packages/') + 'packages/'.length);
+ return new _ScriptInfo('$uri', packageUrl: 'package:$packagePath');
+ }
+
+ return new _ScriptInfo(baseUri, text: script.text);
+}
+
+/// Whether [uri] is an http URI that contains a 'packages' segment, and
+/// therefore could be converted into a 'package:' URI.
+bool _isHttpStylePackageUrl(Uri uri) {
+ var uriPath = uri.path;
+ return uri.scheme == _rootUri.scheme &&
+ // Don't process cross-domain uris.
+ uri.authority == _rootUri.authority &&
+ uriPath.endsWith('.dart') &&
+ (uriPath.contains('/packages/') || uriPath.startsWith('packages/'));
+}
+
+Iterable<String> discoverLibrariesToLoad(Document doc, String baseUri) =>
+ _discoverScripts(doc, baseUri).map(
+ (info) => _packageUrlExists(info) ? info.packageUrl : info.resolvedUrl);
+
+bool _packageUrlExists(_ScriptInfo info) =>
+ info.isPackage && _libs[Uri.parse(info.packageUrl)] != null;
+
/// All libraries in the current isolate.
final _libs = currentMirrorSystem().libraries;
« no previous file with comments | « pkg/polymer/lib/src/loader.dart ('k') | pkg/polymer/lib/src/static_loader.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698