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 /// Transfomer that inlines polymer-element definitions from html imports. | 5 /// Transfomer that inlines polymer-element definitions from html imports. |
6 library polymer.src.build.import_inliner; | 6 library polymer.src.build.import_inliner; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'dart:convert'; | 9 import 'dart:convert'; |
10 | 10 |
| 11 import 'package:analyzer/src/generated/ast.dart'; |
11 import 'package:barback/barback.dart'; | 12 import 'package:barback/barback.dart'; |
12 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
13 import 'package:html5lib/dom.dart' show | 14 import 'package:html5lib/dom.dart' show |
14 Document, DocumentFragment, Element, Node; | 15 Document, DocumentFragment, Element, Node; |
15 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; | 16 import 'package:html5lib/dom_parsing.dart' show TreeVisitor; |
| 17 import 'package:source_maps/refactor.dart' show TextEditTransaction; |
16 import 'package:source_maps/span.dart'; | 18 import 'package:source_maps/span.dart'; |
17 | 19 |
18 import 'code_extractor.dart'; // import just for documentation. | |
19 import 'common.dart'; | 20 import 'common.dart'; |
20 | 21 |
21 class _HtmlInliner extends PolymerTransformer { | 22 class _HtmlInliner extends PolymerTransformer { |
22 final TransformOptions options; | 23 final TransformOptions options; |
23 final Transform transform; | 24 final Transform transform; |
24 final TransformLogger logger; | 25 final TransformLogger logger; |
25 final AssetId docId; | 26 final AssetId docId; |
26 final seen = new Set<AssetId>(); | 27 final seen = new Set<AssetId>(); |
27 final scriptIds = <AssetId>[]; | 28 final scriptIds = <AssetId>[]; |
28 | 29 |
29 static const TYPE_DART = 'application/dart'; | 30 /// The number of extracted inline Dart scripts. Used as a counter to give |
30 static const TYPE_JS = 'text/javascript'; | 31 /// unique-ish filenames. |
| 32 int inlineScriptCounter = 0; |
31 | 33 |
32 _HtmlInliner(this.options, Transform transform) | 34 _HtmlInliner(this.options, Transform transform) |
33 : transform = transform, | 35 : transform = transform, |
34 logger = transform.logger, | 36 logger = transform.logger, |
35 docId = transform.primaryInput.id; | 37 docId = transform.primaryInput.id; |
36 | 38 |
37 Future apply() { | 39 Future apply() { |
38 seen.add(docId); | 40 seen.add(docId); |
39 | 41 |
40 Document document; | 42 Document document; |
| 43 bool changed; |
41 | 44 |
42 return readPrimaryAsHtml(transform).then((document) => | 45 return readPrimaryAsHtml(transform).then((doc) { |
43 _visitImports(document, docId).then((importsFound) { | 46 document = doc; |
| 47 // Add the main script's ID, or null if none is present. |
| 48 // This will be used by ScriptCompactor. |
| 49 changed = _extractScripts(document, docId); |
| 50 return _visitImports(document); |
| 51 }).then((importsFound) { |
| 52 changed = changed || importsFound; |
44 | 53 |
45 var output = transform.primaryInput; | 54 var output = transform.primaryInput; |
46 if (importsFound) { | 55 if (changed) output = new Asset.fromString(docId, document.outerHtml); |
47 output = new Asset.fromString(docId, document.outerHtml); | |
48 } | |
49 transform.addOutput(output); | 56 transform.addOutput(output); |
50 | 57 |
51 // We produce a secondary asset with extra information for later phases. | 58 // We produce a secondary asset with extra information for later phases. |
52 transform.addOutput(new Asset.fromString( | 59 transform.addOutput(new Asset.fromString( |
53 docId.addExtension('.scriptUrls'), | 60 docId.addExtension('.scriptUrls'), |
54 JSON.encode(scriptIds, toEncodable: (id) => id.serialize()))); | 61 JSON.encode(scriptIds, toEncodable: (id) => id.serialize()))); |
55 })); | 62 }); |
56 } | 63 } |
57 | 64 |
58 /// Visits imports in [document] and add the imported documents to documents. | 65 /// Visits imports in [document] and add the imported documents to documents. |
59 /// Documents are added in the order they appear, transitive imports are added | 66 /// Documents are added in the order they appear, transitive imports are added |
60 /// first. | 67 /// first. |
61 /// | 68 /// |
62 /// Returns `true` if and only if the document was changed and should be | 69 /// Returns `true` if and only if the document was changed and should be |
63 /// written out. | 70 /// written out. |
64 Future<bool> _visitImports(Document document, AssetId sourceId) { | 71 Future<bool> _visitImports(Document document) { |
65 bool changed = false; | 72 bool changed = false; |
66 | 73 |
67 _moveHeadToBody(document); | 74 _moveHeadToBody(document); |
68 | 75 |
69 // Note: we need to preserve the import order in the generated output. | 76 // Note: we need to preserve the import order in the generated output. |
70 return Future.forEach(document.querySelectorAll('link'), (Element tag) { | 77 return Future.forEach(document.querySelectorAll('link'), (Element tag) { |
71 var rel = tag.attributes['rel']; | 78 var rel = tag.attributes['rel']; |
72 if (rel != 'import' && rel != 'stylesheet') return null; | 79 if (rel != 'import' && rel != 'stylesheet') return null; |
73 | 80 |
| 81 // Note: URL has already been normalized so use docId. |
74 var href = tag.attributes['href']; | 82 var href = tag.attributes['href']; |
75 var id = resolve(sourceId, href, transform.logger, tag.sourceSpan, | 83 var id = resolve(docId, href, transform.logger, tag.sourceSpan, |
76 allowAbsolute: rel == 'stylesheet'); | 84 allowAbsolute: rel == 'stylesheet'); |
77 | 85 |
78 if (rel == 'import') { | 86 if (rel == 'import') { |
79 changed = true; | 87 changed = true; |
80 if (id == null || !seen.add(id)) { | 88 if (id == null || !seen.add(id)) { |
81 tag.remove(); | 89 tag.remove(); |
82 return null; | 90 return null; |
83 } | 91 } |
84 return _inlineImport(id, tag); | 92 return _inlineImport(id, tag); |
85 | 93 |
(...skipping 14 matching lines...) Expand all Loading... |
100 /// respect to eachother, because stylesheets can be pulled in transitively | 108 /// respect to eachother, because stylesheets can be pulled in transitively |
101 /// from imports. | 109 /// from imports. |
102 // TODO(jmesserly): vulcanizer doesn't need this because they inline JS | 110 // TODO(jmesserly): vulcanizer doesn't need this because they inline JS |
103 // scripts, causing them to be naturally moved as part of the inlining. | 111 // scripts, causing them to be naturally moved as part of the inlining. |
104 // Should we do the same? Alternatively could we inline head into head and | 112 // Should we do the same? Alternatively could we inline head into head and |
105 // body into body and avoid this whole thing? | 113 // body into body and avoid this whole thing? |
106 void _moveHeadToBody(Document doc) { | 114 void _moveHeadToBody(Document doc) { |
107 var insertionPoint = doc.body.firstChild; | 115 var insertionPoint = doc.body.firstChild; |
108 for (var node in doc.head.nodes.toList(growable: false)) { | 116 for (var node in doc.head.nodes.toList(growable: false)) { |
109 if (node is! Element) continue; | 117 if (node is! Element) continue; |
110 var tag = node.tagName; | 118 var tag = node.localName; |
111 var type = node.attributes['type']; | 119 var type = node.attributes['type']; |
112 var rel = node.attributes['rel']; | 120 var rel = node.attributes['rel']; |
113 if (tag == 'style' || tag == 'script' && | 121 if (tag == 'style' || tag == 'script' && |
114 (type == null || type == TYPE_JS || type == TYPE_DART) || | 122 (type == null || type == TYPE_JS || type == TYPE_DART) || |
115 tag == 'link' && (rel == 'stylesheet' || rel == 'import')) { | 123 tag == 'link' && (rel == 'stylesheet' || rel == 'import')) { |
116 // Move the node into the body, where its contents will be placed. | 124 // Move the node into the body, where its contents will be placed. |
117 doc.body.insertBefore(node, insertionPoint); | 125 doc.body.insertBefore(node, insertionPoint); |
118 } | 126 } |
119 } | 127 } |
120 } | 128 } |
121 | 129 |
122 // Loads an asset identified by [id], visits its imports and collects its | 130 /// Loads an asset identified by [id], visits its imports and collects its |
123 // html imports. Then inlines it into the main document. | 131 /// html imports. Then inlines it into the main document. |
124 Future _inlineImport(AssetId id, Element link) => | 132 Future _inlineImport(AssetId id, Element link) { |
125 readAsHtml(id, transform).then((doc) => _visitImports(doc, id).then((_) { | 133 return readAsHtml(id, transform).then((doc) { |
| 134 new _UrlNormalizer(transform, id).visit(doc); |
| 135 return _visitImports(doc).then((_) { |
| 136 _extractScripts(doc, id); |
| 137 _removeScripts(doc); |
126 | 138 |
127 new _UrlNormalizer(transform, id).visit(doc); | 139 // TODO(jmesserly): figure out how this is working in vulcanizer. |
128 _extractScripts(doc); | 140 // Do they produce a <body> tag with a <head> and <body> inside? |
129 | 141 var imported = new DocumentFragment(); |
130 // TODO(jmesserly): figure out how this is working in vulcanizer. | 142 imported.nodes..addAll(doc.head.nodes)..addAll(doc.body.nodes); |
131 // Do they produce a <body> tag with a <head> and <body> inside? | 143 link.replaceWith(imported); |
132 var imported = new DocumentFragment(); | 144 }); |
133 imported.nodes..addAll(doc.head.nodes)..addAll(doc.body.nodes); | 145 }); |
134 link.replaceWith(imported); | 146 } |
135 })); | |
136 | 147 |
137 Future _inlineStylesheet(AssetId id, Element link) { | 148 Future _inlineStylesheet(AssetId id, Element link) { |
138 return transform.readInputAsString(id).then((css) { | 149 return transform.readInputAsString(id).then((css) { |
139 var url = spanUrlFor(id, transform); | 150 css = new _UrlNormalizer(transform, id).visitCss(css); |
140 css = new _UrlNormalizer(transform, id).visitCss(css, url); | |
141 link.replaceWith(new Element.tag('style')..text = css); | 151 link.replaceWith(new Element.tag('style')..text = css); |
142 }); | 152 }); |
143 } | 153 } |
144 | 154 |
145 /// Split Dart script tags from all the other elements. Now that Dartium | 155 /// Remove scripts from HTML imports, and remember their [AssetId]s for later |
146 /// only allows a single script tag per page, we can't inline script | 156 /// use. |
147 /// tags. Instead, we collect the urls of each script tag so we import | 157 /// |
148 /// them directly from the Dart bootstrap code. | 158 /// Dartium only allows a single script tag per page, so we can't inline |
149 void _extractScripts(Document document) { | 159 /// the script tags. Instead we remove them entirely. |
150 bool first = true; | 160 void _removeScripts(Document doc) { |
151 for (var script in document.querySelectorAll('script')) { | 161 for (var script in doc.querySelectorAll('script')) { |
152 if (script.attributes['type'] == TYPE_DART) { | 162 if (script.attributes['type'] == TYPE_DART) { |
153 script.remove(); | 163 script.remove(); |
| 164 var src = script.attributes['src']; |
| 165 scriptIds.add(resolve(docId, src, logger, script.sourceSpan)); |
154 | 166 |
155 // only one Dart script per document is supported in Dartium. | 167 // only the first script needs to be added. |
156 if (first) { | 168 // others are already removed by _extractScripts |
157 first = false; | 169 return; |
158 | |
159 var src = script.attributes['src']; | |
160 if (src == null) { | |
161 logger.warning('unexpected script without a src url. The ' | |
162 'ImportInliner transformer should run after running the ' | |
163 'InlineCodeExtractor', span: script.sourceSpan); | |
164 continue; | |
165 } | |
166 scriptIds.add(resolve(docId, src, logger, script.sourceSpan)); | |
167 | |
168 } else { | |
169 // TODO(jmesserly): remove this when we are running linter. | |
170 logger.warning('more than one Dart script per HTML ' | |
171 'document is not supported. Script will be ignored.', | |
172 span: script.sourceSpan); | |
173 } | |
174 } | 170 } |
175 } | 171 } |
176 } | 172 } |
| 173 |
| 174 /// Split inline scripts into their own files. We need to do this for dart2js |
| 175 /// to be able to compile them. |
| 176 /// |
| 177 /// This also validates that there weren't any duplicate scripts. |
| 178 bool _extractScripts(Document doc, AssetId sourceId) { |
| 179 bool changed = false; |
| 180 bool first = true; |
| 181 for (var script in doc.querySelectorAll('script')) { |
| 182 if (script.attributes['type'] != TYPE_DART) continue; |
| 183 |
| 184 // only one Dart script per document is supported in Dartium. |
| 185 if (!first) { |
| 186 // Remove the script. It's invalid to have more than one in Dartium. |
| 187 script.remove(); |
| 188 changed = true; |
| 189 |
| 190 // TODO(jmesserly): remove this when we are running linter. |
| 191 logger.warning('more than one Dart script per HTML ' |
| 192 'document is not supported. Script will be ignored.', |
| 193 span: script.sourceSpan); |
| 194 continue; |
| 195 } |
| 196 |
| 197 first = false; |
| 198 |
| 199 var src = script.attributes['src']; |
| 200 if (src != null) continue; |
| 201 |
| 202 final filename = path.url.basename(docId.path); |
| 203 final count = inlineScriptCounter++; |
| 204 var code = script.text; |
| 205 // TODO(sigmund): ensure this path is unique (dartbug.com/12618). |
| 206 script.attributes['src'] = src = '$filename.$count.dart'; |
| 207 script.text = ''; |
| 208 changed = true; |
| 209 |
| 210 var newId = docId.addExtension('.$count.dart'); |
| 211 // TODO(jmesserly): consolidate this check with our other parsing of the |
| 212 // Dart code, so we only parse it once. |
| 213 if (!_hasLibraryDirective(code)) { |
| 214 // Inject a library tag with an appropriate library name. |
| 215 |
| 216 // Transform AssetId into a package name. For example: |
| 217 // myPkgName|lib/foo/bar.html -> myPkgName.foo.bar_html |
| 218 // myPkgName|web/foo/bar.html -> myPkgName.web.foo.bar_html |
| 219 // This should roughly match the recommended library name conventions. |
| 220 var libName = '${path.withoutExtension(sourceId.path)}_' |
| 221 '${path.extension(sourceId.path).substring(1)}'; |
| 222 if (libName.startsWith('lib/')) libName = libName.substring(4); |
| 223 libName = libName.replaceAll('/', '.').replaceAll('-', '_'); |
| 224 libName = '${sourceId.package}.$libName'; |
| 225 |
| 226 code = "library $libName;\n$code"; |
| 227 } |
| 228 transform.addOutput(new Asset.fromString(newId, code)); |
| 229 } |
| 230 return changed; |
| 231 } |
177 } | 232 } |
178 | 233 |
| 234 /// Parse [code] and determine whether it has a library directive. |
| 235 bool _hasLibraryDirective(String code) => |
| 236 parseCompilationUnit(code).directives.any((d) => d is LibraryDirective); |
| 237 |
| 238 |
179 /// Recursively inlines the contents of HTML imports. Produces as output a | 239 /// Recursively inlines the contents of HTML imports. Produces as output a |
180 /// single HTML file that inlines the polymer-element definitions, and a text | 240 /// single HTML file that inlines the polymer-element definitions, and a text |
181 /// file that contains, in order, the URIs to each library that sourced in a | 241 /// file that contains, in order, the URIs to each library that sourced in a |
182 /// script tag. | 242 /// script tag. |
183 /// | 243 /// |
184 /// This transformer assumes that all script tags point to external files. To | 244 /// This transformer assumes that all script tags point to external files. To |
185 /// support script tags with inlined code, use this transformer after running | 245 /// support script tags with inlined code, use this transformer after running |
186 /// [InlineCodeExtractor] on an earlier phase. | 246 /// [InlineCodeExtractor] on an earlier phase. |
187 class ImportInliner extends Transformer { | 247 class ImportInliner extends Transformer { |
188 final TransformOptions options; | 248 final TransformOptions options; |
189 | 249 |
190 ImportInliner(this.options); | 250 ImportInliner(this.options); |
191 | 251 |
192 /// Only run on entry point .html files. | 252 /// Only run on entry point .html files. |
193 Future<bool> isPrimary(Asset input) => | 253 Future<bool> isPrimary(Asset input) => |
194 new Future.value(options.isHtmlEntryPoint(input.id)); | 254 new Future.value(options.isHtmlEntryPoint(input.id)); |
195 | 255 |
196 Future apply(Transform transform) => | 256 Future apply(Transform transform) => |
197 new _HtmlInliner(options, transform).apply(); | 257 new _HtmlInliner(options, transform).apply(); |
198 } | 258 } |
199 | 259 |
| 260 const TYPE_DART = 'application/dart'; |
| 261 const TYPE_JS = 'text/javascript'; |
200 | 262 |
201 /// Internally adjusts urls in the html that we are about to inline. | 263 /// Internally adjusts urls in the html that we are about to inline. |
202 class _UrlNormalizer extends TreeVisitor { | 264 class _UrlNormalizer extends TreeVisitor { |
203 final Transform transform; | 265 final Transform transform; |
204 | 266 |
205 /// Asset where the original content (and original url) was found. | 267 /// Asset where the original content (and original url) was found. |
206 final AssetId sourceId; | 268 final AssetId sourceId; |
207 | 269 |
208 _UrlNormalizer(this.transform, this.sourceId); | 270 _UrlNormalizer(this.transform, this.sourceId); |
209 | 271 |
210 visitElement(Element node) { | 272 visitElement(Element node) { |
211 for (var key in node.attributes.keys) { | 273 node.attributes.forEach((name, value) { |
212 if (_urlAttributes.contains(key)) { | 274 if (_urlAttributes.contains(name)) { |
213 var url = node.attributes[key]; | 275 if (value != '' && !value.trim().startsWith('{{')) { |
214 if (url != null && url != '' && !url.startsWith('{{')) { | 276 node.attributes[name] = _newUrl(value, node.sourceSpan); |
215 node.attributes[key] = _newUrl(url, node.sourceSpan); | |
216 } | 277 } |
217 } | 278 } |
| 279 }); |
| 280 if (node.localName == 'style') { |
| 281 node.text = visitCss(node.text); |
| 282 } else if (node.localName == 'script' && |
| 283 node.attributes['type'] == TYPE_DART) { |
| 284 // TODO(jmesserly): we might need to visit JS too to handle ES Harmony |
| 285 // modules. |
| 286 node.text = visitInlineDart(node.text); |
218 } | 287 } |
219 super.visitElement(node); | 288 super.visitElement(node); |
220 } | 289 } |
221 | 290 |
222 static final _URL = new RegExp(r'url\(([^)]*)\)', multiLine: true); | 291 static final _URL = new RegExp(r'url\(([^)]*)\)', multiLine: true); |
223 static final _QUOTE = new RegExp('["\']', multiLine: true); | 292 static final _QUOTE = new RegExp('["\']', multiLine: true); |
224 | 293 |
225 /// Visit the CSS text and replace any relative URLs so we can inline it. | 294 /// Visit the CSS text and replace any relative URLs so we can inline it. |
226 // Ported from: | 295 // Ported from: |
227 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378
acc691f/lib/vulcan.js#L149 | 296 // https://github.com/Polymer/vulcanize/blob/c14f63696797cda18dc3d372b78aa3378
acc691f/lib/vulcan.js#L149 |
228 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. | 297 // TODO(jmesserly): use csslib here instead? Parsing with RegEx is sadness. |
229 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure. | 298 // Maybe it's reliable enough for finding URLs in CSS? I'm not sure. |
230 String visitCss(String cssText, String url) { | 299 String visitCss(String cssText) { |
| 300 var url = spanUrlFor(sourceId, transform); |
231 var src = new SourceFile.text(url, cssText); | 301 var src = new SourceFile.text(url, cssText); |
232 return cssText.replaceAllMapped(_URL, (match) { | 302 return cssText.replaceAllMapped(_URL, (match) { |
233 // Extract the URL, without any surrounding quotes. | 303 // Extract the URL, without any surrounding quotes. |
234 var span = src.span(match.start, match.end); | 304 var span = src.span(match.start, match.end); |
235 var href = match[1].replaceAll(_QUOTE, ''); | 305 var href = match[1].replaceAll(_QUOTE, ''); |
236 href = _newUrl(href, span); | 306 href = _newUrl(href, span); |
237 return 'url($href)'; | 307 return 'url($href)'; |
238 }); | 308 }); |
239 } | 309 } |
240 | 310 |
241 _newUrl(String href, Span span) { | 311 String visitInlineDart(String code) { |
| 312 var unit = parseCompilationUnit(code); |
| 313 var file = new SourceFile.text(spanUrlFor(sourceId, transform), code); |
| 314 var output = new TextEditTransaction(code, file); |
| 315 |
| 316 for (Directive directive in unit.directives) { |
| 317 if (directive is UriBasedDirective) { |
| 318 var uri = directive.uri.stringValue; |
| 319 var span = _getSpan(file, directive.uri); |
| 320 |
| 321 var id = resolve(sourceId, uri, transform.logger, span); |
| 322 if (id == null) continue; |
| 323 |
| 324 var primaryId = transform.primaryInput.id; |
| 325 var newUri = assetUrlFor(id, primaryId, transform.logger); |
| 326 if (newUri != uri) { |
| 327 output.edit(span.start.offset, span.end.offset, "'$newUri'"); |
| 328 } |
| 329 } |
| 330 } |
| 331 |
| 332 if (!output.hasEdits) return code; |
| 333 |
| 334 // TODO(sigmund): emit source maps when barback supports it (see |
| 335 // dartbug.com/12340) |
| 336 return (output.commit()..build(file.url)).text; |
| 337 } |
| 338 |
| 339 String _newUrl(String href, Span span) { |
242 var uri = Uri.parse(href); | 340 var uri = Uri.parse(href); |
243 if (uri.isAbsolute) return href; | 341 if (uri.isAbsolute) return href; |
244 if (!uri.scheme.isEmpty) return href; | 342 if (!uri.scheme.isEmpty) return href; |
245 if (!uri.host.isEmpty) return href; | 343 if (!uri.host.isEmpty) return href; |
246 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. | 344 if (uri.path.isEmpty) return href; // Implies standalone ? or # in URI. |
247 if (path.isAbsolute(href)) return href; | 345 if (path.isAbsolute(href)) return href; |
248 | 346 |
249 var id = resolve(sourceId, href, transform.logger, span); | 347 var id = resolve(sourceId, href, transform.logger, span); |
250 if (id == null) return href; | 348 if (id == null) return href; |
251 var primaryId = transform.primaryInput.id; | 349 var primaryId = transform.primaryInput.id; |
(...skipping 30 matching lines...) Expand all Loading... |
282 'cite', // in blockquote, del, ins, q | 380 'cite', // in blockquote, del, ins, q |
283 'data', // in object | 381 'data', // in object |
284 'formaction', // in button, input | 382 'formaction', // in button, input |
285 'href', // in a, area, link, base, command | 383 'href', // in a, area, link, base, command |
286 'icon', // in command | 384 'icon', // in command |
287 'manifest', // in html | 385 'manifest', // in html |
288 'poster', // in video | 386 'poster', // in video |
289 'src', // in audio, embed, iframe, img, input, script, source, track, | 387 'src', // in audio, embed, iframe, img, input, script, source, track, |
290 // video | 388 // video |
291 ]; | 389 ]; |
| 390 |
| 391 _getSpan(SourceFile file, ASTNode node) => file.span(node.offset, node.end); |
OLD | NEW |