| 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 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 return readPrimaryAsHtml(transform, logger).then((doc) { | 56 return readPrimaryAsHtml(transform, logger).then((doc) { |
| 57 document = doc; | 57 document = doc; |
| 58 | 58 |
| 59 // Insert our importsWrapper. This may be removed later if not needed, but | 59 // Insert our importsWrapper. This may be removed later if not needed, but |
| 60 // it makes the logic simpler to have it in the document. | 60 // it makes the logic simpler to have it in the document. |
| 61 document.body.insertBefore(importsWrapper, document.body.firstChild); | 61 document.body.insertBefore(importsWrapper, document.body.firstChild); |
| 62 | 62 |
| 63 changed = new _UrlNormalizer(transform, docId, logger).visit(document) | 63 changed = new _UrlNormalizer(transform, docId, logger).visit(document) |
| 64 || changed; | 64 || changed; |
| 65 | 65 |
| 66 experimentalBootstrap = document.querySelectorAll('link').any((link) => | 66 experimentalBootstrap = document.querySelectorAll('link').any((link) => |
| 67 link.attributes['rel'] == 'import' && | 67 link.attributes['rel'] == 'import' && |
| 68 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML); | 68 link.attributes['href'] == POLYMER_EXPERIMENTAL_HTML); |
| 69 changed = _extractScripts(document) || changed; | 69 changed = _extractScripts(document) || changed; |
| 70 | 70 |
| 71 // We only need to move the head into the body for the entry point. | 71 // We only need to move the head into the body for the entry point. |
| 72 _moveHeadToBody(document); | 72 _moveHeadToBody(document); |
| 73 | 73 |
| 74 return _visitImports(document); | 74 return _visitImports(document); |
| 75 }).then((importsFound) { | 75 }).then((importsFound) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 if (!options.shouldInlineStylesheet(id)) return null; | 135 if (!options.shouldInlineStylesheet(id)) return null; |
| 136 | 136 |
| 137 changed = true; | 137 changed = true; |
| 138 return _inlineStylesheet(id, tag); | 138 return _inlineStylesheet(id, tag); |
| 139 } | 139 } |
| 140 }).then((_) => changed); | 140 }).then((_) => changed); |
| 141 } | 141 } |
| 142 | 142 |
| 143 /// To preserve the order of scripts with respect to inlined | 143 /// To preserve the order of scripts with respect to inlined |
| 144 /// link rel=import, we move both of those into the body before we do any | 144 /// link rel=import, we move both of those into the body before we do any |
| 145 /// inlining. | 145 /// inlining. We do not start doing this until the first import is found |
| 146 /// however, as some scripts do need to be ran in the head to work |
| 147 /// properly (platform.js for instance). |
| 146 /// | 148 /// |
| 147 /// Note: we do this for stylesheets as well to preserve ordering with | 149 /// Note: we do this for stylesheets as well to preserve ordering with |
| 148 /// respect to eachother, because stylesheets can be pulled in transitively | 150 /// respect to eachother, because stylesheets can be pulled in transitively |
| 149 /// from imports. | 151 /// from imports. |
| 150 // TODO(jmesserly): vulcanizer doesn't need this because they inline JS | 152 // TODO(jmesserly): vulcanizer doesn't need this because they inline JS |
| 151 // scripts, causing them to be naturally moved as part of the inlining. | 153 // scripts, causing them to be naturally moved as part of the inlining. |
| 152 // Should we do the same? Alternatively could we inline head into head and | 154 // Should we do the same? Alternatively could we inline head into head and |
| 153 // body into body and avoid this whole thing? | 155 // body into body and avoid this whole thing? |
| 154 void _moveHeadToBody(Document doc) { | 156 void _moveHeadToBody(Document doc) { |
| 157 var foundImport = false; |
| 155 for (var node in doc.head.nodes.toList(growable: false)) { | 158 for (var node in doc.head.nodes.toList(growable: false)) { |
| 156 if (node is! Element) continue; | 159 if (node is! Element) continue; |
| 157 var tag = node.localName; | 160 var tag = node.localName; |
| 158 var type = node.attributes['type']; | 161 var type = node.attributes['type']; |
| 159 var rel = node.attributes['rel']; | 162 var rel = node.attributes['rel']; |
| 163 if (tag == 'link' && rel == 'import') foundImport = true; |
| 164 if (!foundImport) continue; |
| 160 if (tag == 'style' || tag == 'script' && | 165 if (tag == 'style' || tag == 'script' && |
| 161 (type == null || type == TYPE_JS || type == TYPE_DART) || | 166 (type == null || type == TYPE_JS || type == TYPE_DART) || |
| 162 tag == 'link' && (rel == 'stylesheet' || rel == 'import')) { | 167 tag == 'link' && (rel == 'stylesheet' || rel == 'import')) { |
| 163 // Move the node into the importsWrapper, where its contents will be | 168 // Move the node into the importsWrapper, where its contents will be |
| 164 // placed. This wrapper is a hidden div to prevent inlined html from | 169 // placed. This wrapper is a hidden div to prevent inlined html from |
| 165 // causing a FOUC. | 170 // causing a FOUC. |
| 166 importsWrapper.append(node); | 171 importsWrapper.append(node); |
| 167 } | 172 } |
| 168 } | 173 } |
| 169 } | 174 } |
| (...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 /// style tag except these ones. | 544 /// style tag except these ones. |
| 540 const IGNORED_LINKED_STYLE_ATTRS = | 545 const IGNORED_LINKED_STYLE_ATTRS = |
| 541 const ['charset', 'href', 'href-lang', 'rel', 'rev']; | 546 const ['charset', 'href', 'href-lang', 'rel', 'rev']; |
| 542 | 547 |
| 543 /// Global RegExp objects. | 548 /// Global RegExp objects. |
| 544 final _INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); | 549 final _INVALID_LIB_CHARS_REGEX = new RegExp('[^a-z0-9_]'); |
| 545 final _NUM_REGEX = new RegExp('[0-9]'); | 550 final _NUM_REGEX = new RegExp('[0-9]'); |
| 546 final _BINDING_REGEX = new RegExp(r'(({{.*}})|(\[\[.*\]\]))'); | 551 final _BINDING_REGEX = new RegExp(r'(({{.*}})|(\[\[.*\]\]))'); |
| 547 | 552 |
| 548 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); | 553 _getSpan(SourceFile file, AstNode node) => file.span(node.offset, node.end); |
| OLD | NEW |