Chromium Code Reviews| 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 /** Common methods used by transfomers. */ | 5 /** Common methods used by transfomers. */ |
| 6 library polymer.src.transform.common; | 6 library polymer.src.transform.common; |
| 7 | 7 |
| 8 import 'dart:async'; | 8 import 'dart:async'; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 27 // Note: errors aren't fatal in HTML (unless strict mode is on). | 27 // Note: errors aren't fatal in HTML (unless strict mode is on). |
| 28 // So just print them as warnings. | 28 // So just print them as warnings. |
| 29 for (var e in parser.errors) { | 29 for (var e in parser.errors) { |
| 30 if (checkDocType || e.errorCode != 'expected-doctype-but-got-start-tag') { | 30 if (checkDocType || e.errorCode != 'expected-doctype-but-got-start-tag') { |
| 31 logger.warning(e.message, e.span); | 31 logger.warning(e.message, e.span); |
| 32 } | 32 } |
| 33 } | 33 } |
| 34 return document; | 34 return document; |
| 35 } | 35 } |
| 36 | 36 |
| 37 Future<Document> readPrimaryAsHtml(Transform transform) { | 37 /** Additional options used by polymer transformers */ |
| 38 var asset = transform.primaryInput; | 38 class TransformOptions { |
| 39 var id = asset.id; | 39 String currentPackage; |
| 40 return asset.readAsString().then((content) { | 40 List<String> entrypoints; |
| 41 return _parseHtml(content, id.path, transform.logger, | 41 |
| 42 checkDocType: isPrimaryHtml(id)); | 42 TransformOptions([this.currentPackage, this.entrypoints]); |
| 43 }); | 43 |
| 44 /** Whether an asset with [id] is an entry point HTML file. */ | |
| 45 bool isHtmlEntrypoint(AssetId id) { | |
|
Jennifer Messerly
2013/09/10 04:16:51
nit: entry point is two words? so entryPoint
(fun
Siggi Cherem (dart-lang)
2013/09/11 01:45:26
Done.
| |
| 46 if (id.extension != '.html') return false; | |
| 47 | |
| 48 // Note: [id.path] is a relative path from the root of a package. | |
| 49 if (!id.path.startsWith('web/') && | |
| 50 !id.path.startsWith('test/')) return false; | |
| 51 | |
| 52 if (currentPackage == null || entrypoints == null) return true; | |
| 53 return id.package == currentPackage && entrypoints.contains(id.path); | |
| 54 } | |
| 44 } | 55 } |
| 45 | 56 |
| 46 Future<Document> readAsHtml(AssetId id, Transform transform) { | 57 /** Mixin for polymer transformers. */ |
| 47 var primaryId = transform.primaryInput.id; | 58 abstract class PolymerTransformer { |
| 48 var url = (id.package == primaryId.package) ? id.path | 59 TransformOptions get options; |
| 49 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); | 60 |
| 50 return transform.readInputAsString(id).then((content) { | 61 Future<Document> readPrimaryAsHtml(Transform transform) { |
| 51 return _parseHtml(content, url, transform.logger, | 62 var asset = transform.primaryInput; |
| 52 checkDocType: isPrimaryHtml(id)); | 63 var id = asset.id; |
| 53 }); | 64 return asset.readAsString().then((content) { |
| 65 return _parseHtml(content, id.path, transform.logger, | |
| 66 checkDocType: options.isHtmlEntrypoint(id)); | |
| 67 }); | |
| 68 } | |
| 69 | |
| 70 Future<Document> readAsHtml(AssetId id, Transform transform) { | |
| 71 var primaryId = transform.primaryInput.id; | |
| 72 var url = (id.package == primaryId.package) ? id.path | |
| 73 : assetUrlFor(id, primaryId, transform.logger, allowAssetUrl: true); | |
| 74 return transform.readInputAsString(id).then((content) { | |
| 75 return _parseHtml(content, url, transform.logger, | |
| 76 checkDocType: options.isHtmlEntrypoint(id)); | |
| 77 }); | |
| 78 } | |
| 54 } | 79 } |
| 55 | 80 |
| 56 /** Create an [AssetId] for a [url] seen in the [source] asset. */ | 81 /** Create an [AssetId] for a [url] seen in the [source] asset. */ |
| 57 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | 82 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) |
| 58 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span) { | 83 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span) { |
| 59 if (url == null || url == '') return null; | 84 if (url == null || url == '') return null; |
| 60 var uri = Uri.parse(url); | 85 var uri = Uri.parse(url); |
| 61 var urlBuilder = path.url; | 86 var urlBuilder = path.url; |
| 62 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { | 87 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { |
| 63 logger.error('absolute paths not allowed: "$url"', span); | 88 logger.error('absolute paths not allowed: "$url"', span); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 85 targetPath = urlBuilder.join('asset', | 110 targetPath = urlBuilder.join('asset', |
| 86 urlBuilder.joinAll(segments.sublist(2))); | 111 urlBuilder.joinAll(segments.sublist(2))); |
| 87 } else { | 112 } else { |
| 88 package = source.package; | 113 package = source.package; |
| 89 targetPath = urlBuilder.normalize( | 114 targetPath = urlBuilder.normalize( |
| 90 urlBuilder.join(urlBuilder.dirname(source.path), url)); | 115 urlBuilder.join(urlBuilder.dirname(source.path), url)); |
| 91 } | 116 } |
| 92 return new AssetId(package, targetPath); | 117 return new AssetId(package, targetPath); |
| 93 } | 118 } |
| 94 | 119 |
| 95 /** Whether an asset with [id] is considered a primary entry point HTML file. */ | |
| 96 bool isPrimaryHtml(AssetId id) => id.extension == '.html' && | |
| 97 // Note: [id.path] is a relative path from the root of a package. | |
| 98 (id.path.startsWith('web/') || id.path.startsWith('test/')); | |
| 99 | |
| 100 /** | 120 /** |
| 101 * Generate the import url for a file described by [id], referenced by a file | 121 * Generate the import url for a file described by [id], referenced by a file |
| 102 * with [sourceId]. | 122 * with [sourceId]. |
| 103 */ | 123 */ |
| 104 // TODO(sigmund): this should also be in barback (dartbug.com/12610) | 124 // TODO(sigmund): this should also be in barback (dartbug.com/12610) |
| 105 String assetUrlFor(AssetId id, AssetId sourceId, TransformLogger logger, | 125 String assetUrlFor(AssetId id, AssetId sourceId, TransformLogger logger, |
| 106 {bool allowAssetUrl: false}) { | 126 {bool allowAssetUrl: false}) { |
| 107 // use package: and asset: urls if possible | 127 // use package: and asset: urls if possible |
| 108 if (id.path.startsWith('lib/')) { | 128 if (id.path.startsWith('lib/')) { |
| 109 return 'package:${id.package}/${id.path.substring(4)}'; | 129 return 'package:${id.package}/${id.path.substring(4)}'; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 121 // Use relative urls only if it's possible. | 141 // Use relative urls only if it's possible. |
| 122 if (id.package != sourceId.package) { | 142 if (id.package != sourceId.package) { |
| 123 logger.error("don't know how to refer to $id from $sourceId"); | 143 logger.error("don't know how to refer to $id from $sourceId"); |
| 124 return null; | 144 return null; |
| 125 } | 145 } |
| 126 | 146 |
| 127 var builder = path.url; | 147 var builder = path.url; |
| 128 return builder.relative(builder.join('/', id.path), | 148 return builder.relative(builder.join('/', id.path), |
| 129 from: builder.join('/', builder.dirname(sourceId.path))); | 149 from: builder.join('/', builder.dirname(sourceId.path))); |
| 130 } | 150 } |
| OLD | NEW |