| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of polymer; | 5 part of polymer; |
| 6 | 6 |
| 7 /** Annotation used to automatically register polymer elements. */ | 7 /** Annotation used to automatically register polymer elements. */ |
| 8 class CustomTag { | 8 class CustomTag { |
| 9 final String tagName; | 9 final String tagName; |
| 10 const CustomTag(this.tagName); | 10 const CustomTag(this.tagName); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 [Set<Document> seen, List<String> scripts]) { | 105 [Set<Document> seen, List<String> scripts]) { |
| 106 if (seen == null) seen = new Set<Document>(); | 106 if (seen == null) seen = new Set<Document>(); |
| 107 if (scripts == null) scripts = <String>[]; | 107 if (scripts == null) scripts = <String>[]; |
| 108 if (doc == null) { | 108 if (doc == null) { |
| 109 print('warning: $baseUri not found.'); | 109 print('warning: $baseUri not found.'); |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 if (seen.contains(doc)) return scripts; | 112 if (seen.contains(doc)) return scripts; |
| 113 seen.add(doc); | 113 seen.add(doc); |
| 114 | 114 |
| 115 var inlinedScriptCount = 0; | 115 bool scriptSeen = false; |
| 116 for (var node in doc.queryAll('script,link[rel="import"]')) { | 116 for (var node in doc.queryAll('script,link[rel="import"]')) { |
| 117 if (node is LinkElement) { | 117 if (node is LinkElement) { |
| 118 _discoverScripts(node.import, node.href, seen, scripts); | 118 _discoverScripts(node.import, node.href, seen, scripts); |
| 119 } else if (node is ScriptElement && node.type == 'application/dart') { | 119 } else if (node is ScriptElement && node.type == 'application/dart') { |
| 120 var url = node.src; | 120 if (!scriptSeen) { |
| 121 if (url != '') { | 121 var url = node.src; |
| 122 // TODO(sigmund): consider either normalizing package: urls or add a | 122 scripts.add(url == '' ? baseUri : url); |
| 123 // warning to let users know about cannonicalization issues. | 123 scriptSeen = true; |
| 124 scripts.add(url); | |
| 125 } else { | 124 } else { |
| 126 // We generate a unique identifier for inlined scripts which we later | 125 print('warning: more than one Dart script tag in $baseUri. Dartium ' |
| 127 // translate to the unique identifiers used by Dartium. Dartium uses | 126 'currently only allows a single Dart script tag per document.'); |
| 128 // line/column number information which we can't compute here. | |
| 129 scripts.add('$baseUri:$inlinedScriptCount'); | |
| 130 inlinedScriptCount++; | |
| 131 } | 127 } |
| 132 } | 128 } |
| 133 } | 129 } |
| 134 return scripts; | 130 return scripts; |
| 135 } | 131 } |
| 136 | 132 |
| 137 /** All libraries in the current isolate. */ | 133 /** All libraries in the current isolate. */ |
| 138 final _libs = currentMirrorSystem().libraries; | 134 final _libs = currentMirrorSystem().libraries; |
| 139 | 135 |
| 140 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the | 136 // TODO(sigmund): explore other (cheaper) ways to resolve URIs relative to the |
| 141 // root library (see dartbug.com/12612) | 137 // root library (see dartbug.com/12612) |
| 142 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; | 138 final _rootUri = currentMirrorSystem().isolate.rootLibrary.uri; |
| 143 | 139 |
| 144 final String _packageRoot = | 140 final String _packageRoot = |
| 145 '${path.dirname(Uri.parse(window.location.href).path)}/packages/'; | 141 '${path.dirname(Uri.parse(window.location.href).path)}/packages/'; |
| 146 | 142 |
| 147 /** Regex that matches urls used to represent inlined scripts. */ | |
| 148 final RegExp _inlineScriptRegExp = new RegExp('\(.*\.html.*\):\([0-9]\+\)'); | |
| 149 | |
| 150 /** | |
| 151 * Map URLs fabricated by polymer to URLs fabricated by Dartium to represent | |
| 152 * inlined scripts. Polymer uses baseUri:script#, Dartium uses baseUri:line# | |
| 153 */ | |
| 154 // TODO(sigmund): figure out if we can generate the same URL and expose it. | |
| 155 final Map<Uri, List<Uri>> _inlinedScriptMapping = () { | |
| 156 var map = {}; | |
| 157 for (var uri in _libs.keys) { | |
| 158 var uriString = uri.toString(); | |
| 159 var match = _inlineScriptRegExp.firstMatch(uriString); | |
| 160 if (match == null) continue; | |
| 161 var baseUri = Uri.parse(match.group(1)); | |
| 162 if (map[baseUri] == null) map[baseUri] = []; | |
| 163 map[baseUri].add(uri); | |
| 164 } | |
| 165 return map; | |
| 166 }(); | |
| 167 | |
| 168 /** Returns a new Uri that replaces [path] in [uri]. */ | |
| 169 Uri _replacePath(Uri uri, String path) { | |
| 170 return new Uri(scheme: uri.scheme, host: uri.host, port: uri.port, | |
| 171 path: path, query: uri.query, fragment: uri.fragment); | |
| 172 } | |
| 173 | |
| 174 /** Returns the Uri in [href] without query parameters or fragments. */ | |
| 175 String _baseUri(String href) { | |
| 176 var uri = Uri.parse(window.location.href); | |
| 177 var trimUri = new Uri(scheme: uri.scheme, host: uri.host, | |
| 178 port: uri.port, path: uri.path); | |
| 179 return trimUri.toString(); | |
| 180 } | |
| 181 | |
| 182 /** | 143 /** |
| 183 * Reads the library at [uriString] (which can be an absolute URI or a relative | 144 * Reads the library at [uriString] (which can be an absolute URI or a relative |
| 184 * URI from the root library), and: | 145 * URI from the root library), and: |
| 185 * | 146 * |
| 186 * * If present, invokes any top-level and static functions marked | 147 * * If present, invokes any top-level and static functions marked |
| 187 * with the [initMethod] annotation (in the order they appear). | 148 * with the [initMethod] annotation (in the order they appear). |
| 188 * | 149 * |
| 189 * * Registers any [PolymerElement] that is marked with the [CustomTag] | 150 * * Registers any [PolymerElement] that is marked with the [CustomTag] |
| 190 * annotation. | 151 * annotation. |
| 191 */ | 152 */ |
| 192 void _loadLibrary(String uriString) { | 153 void _loadLibrary(String uriString) { |
| 193 var uri = _rootUri.resolve(uriString); | 154 var uri = _rootUri.resolve(uriString); |
| 194 var lib; | 155 var lib = _libs[uri]; |
| 195 var match = _inlineScriptRegExp.firstMatch(uriString); | 156 if (uri.path.startsWith(_packageRoot) && uri.path.endsWith('.dart')) { |
| 196 if (match != null) { | |
| 197 var baseUri = Uri.parse(match.group(1)); | |
| 198 var list = _inlinedScriptMapping[baseUri]; | |
| 199 var pos = int.parse(match.group(2), onError: (_) => -1); | |
| 200 if (list != null && pos >= 0 && pos < list.length && list[pos] != null) { | |
| 201 lib = _libs[list[pos]]; | |
| 202 } | |
| 203 } else if (uri.path.startsWith(_packageRoot)) { | |
| 204 var packageUri = | 157 var packageUri = |
| 205 Uri.parse('package:${uri.path.substring(_packageRoot.length)}'); | 158 Uri.parse('package:${uri.path.substring(_packageRoot.length)}'); |
| 206 lib = _libs[packageUri]; | 159 var canonicalLib = _libs[packageUri]; |
| 207 if (lib == null) { | 160 if (canonicalLib != null) { |
| 208 lib = _libs[uri]; | 161 lib = canonicalLib; |
| 209 } | 162 } |
| 210 } else { | |
| 211 lib = _libs[uri]; | |
| 212 } | 163 } |
| 164 |
| 213 if (lib == null) { | 165 if (lib == null) { |
| 214 print('warning: $uri library not found'); | 166 print('warning: $uri library not found'); |
| 215 return; | 167 return; |
| 216 } | 168 } |
| 217 | 169 |
| 218 // Search top-level functions marked with @initMethod | 170 // Search top-level functions marked with @initMethod |
| 219 for (var f in lib.functions.values) { | 171 for (var f in lib.functions.values) { |
| 220 _maybeInvoke(lib, f); | 172 _maybeInvoke(lib, f); |
| 221 } | 173 } |
| 222 | 174 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 256 print("warning: methods marked with @initMethod should take no " | 208 print("warning: methods marked with @initMethod should take no " |
| 257 "arguments, ${method.simpleName} expects some."); | 209 "arguments, ${method.simpleName} expects some."); |
| 258 return; | 210 return; |
| 259 } | 211 } |
| 260 obj.invoke(method.simpleName, const []); | 212 obj.invoke(method.simpleName, const []); |
| 261 } | 213 } |
| 262 | 214 |
| 263 class _InitMethodAnnotation { | 215 class _InitMethodAnnotation { |
| 264 const _InitMethodAnnotation(); | 216 const _InitMethodAnnotation(); |
| 265 } | 217 } |
| OLD | NEW |