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'; |
| 11 import 'package:html5lib/dom.dart' show Document; | 11 import 'package:html5lib/dom.dart' show Document; |
| 12 import 'package:html5lib/parser.dart' show HtmlParser; | 12 import 'package:html5lib/parser.dart' show HtmlParser; |
| 13 import 'package:path/path.dart' as path; | 13 import 'package:path/path.dart' as path; |
| 14 import 'package:source_maps/span.dart' show Span; | 14 import 'package:source_maps/span.dart' show Span; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * Parses an HTML file [contents] and returns a DOM-like tree. Adds emitted | 17 * Parses an HTML file [contents] and returns a DOM-like tree. Adds emitted |
| 18 * error/warning to [logger]. | 18 * error/warning to [logger]. |
| 19 */ | 19 */ |
| 20 Document parseHtml(String contents, String sourcePath, TransformLogger logger, | 20 Document _parseHtml(String contents, String sourcePath, TransformLogger logger, |
| 21 {bool checkDocType: false}) { | 21 {bool checkDocType: false}) { |
| 22 // TODO(jmesserly): make HTTP encoding configurable | 22 // TODO(jmesserly): make HTTP encoding configurable |
| 23 var parser = new HtmlParser(contents, encoding: 'utf8', generateSpans: true, | 23 var parser = new HtmlParser(contents, encoding: 'utf8', generateSpans: true, |
| 24 sourceUrl: sourcePath); | 24 sourceUrl: sourcePath); |
| 25 var document = parser.parse(); | 25 var document = parser.parse(); |
| 26 | 26 |
| 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) { | |
| 38 var asset = transform.primaryInput; | |
| 39 var id = asset.id; | |
| 40 return asset.readAsString().then((content) { | |
| 41 return _parseHtml(content, id.path, transform.logger, | |
| 42 checkDocType: isHtmlInWebOrTest(id)); | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 Future<Document> readAsHtml(AssetId id, Transform transform) { | |
| 47 var primaryId = transform.primaryInput.id; | |
| 48 var url = (id.package == primaryId.package) ? id.path | |
| 49 : assetUrlFor(id, primaryId, transform.logger); | |
| 50 return transform.readInputAsString(id).then((content) { | |
| 51 return _parseHtml(content, url, transform.logger, | |
| 52 checkDocType: isHtmlInWebOrTest(id)); | |
| 53 }); | |
| 54 } | |
| 55 | |
| 37 /** Create an [AssetId] for a [url] seen in the [source] asset. */ | 56 /** Create an [AssetId] for a [url] seen in the [source] asset. */ |
| 38 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) | 57 // TODO(sigmund): delete once this is part of barback (dartbug.com/12610) |
| 39 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span) { | 58 AssetId resolve(AssetId source, String url, TransformLogger logger, Span span) { |
| 40 if (url == null || url == '') return null; | 59 if (url == null || url == '') return null; |
| 41 var uri = Uri.parse(url); | 60 var uri = Uri.parse(url); |
| 42 var urlBuilder = path.url; | 61 var urlBuilder = path.url; |
| 43 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { | 62 if (uri.host != '' || uri.scheme != '' || urlBuilder.isAbsolute(url)) { |
| 44 logger.error('absolute paths not allowed: "$url"', span); | 63 logger.error('absolute paths not allowed: "$url"', span); |
| 45 return null; | 64 return null; |
| 46 } | 65 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 66 targetPath = urlBuilder.join('asset', | 85 targetPath = urlBuilder.join('asset', |
| 67 urlBuilder.joinAll(segments.sublist(2))); | 86 urlBuilder.joinAll(segments.sublist(2))); |
| 68 } else { | 87 } else { |
| 69 package = source.package; | 88 package = source.package; |
| 70 targetPath = urlBuilder.normalize( | 89 targetPath = urlBuilder.normalize( |
| 71 urlBuilder.join(urlBuilder.dirname(source.path), url)); | 90 urlBuilder.join(urlBuilder.dirname(source.path), url)); |
| 72 } | 91 } |
| 73 return new AssetId(package, targetPath); | 92 return new AssetId(package, targetPath); |
| 74 } | 93 } |
| 75 | 94 |
| 76 Future<bool> isHtmlInWebOrTest(AssetId id) => | 95 bool isHtmlInWebOrTest(AssetId id) => id.extension == '.html' && |
| 77 new Future.value(id.extension == '.html' && | 96 (id.path.startsWith('web/') || id.path.startsWith('test/')); |
| 78 (id.path.startsWith('web/') || id.path.startsWith('test/'))); | 97 |
| 98 /** | |
| 99 * Generate the import url for a file described by [id], referenced by a file | |
| 100 * with [sourceId]. | |
| 101 */ | |
| 102 String assetUrlFor(AssetId id, AssetId sourceId, TransformLogger logger) { | |
|
Jennifer Messerly
2013/09/04 03:13:27
TODO: this should be in barback, e.g. AssetId.uri
Siggi Cherem (dart-lang)
2013/09/04 17:57:27
Done. Added TODO and more comments in bug/12610
| |
| 103 // use package: and asset: urls if possible | |
| 104 if (id.path.startsWith('lib/')) { | |
| 105 return 'package:${id.package}/${id.path.substring(4)}'; | |
| 106 } | |
| 107 | |
| 108 if (id.path.startsWith('asset/')) { | |
| 109 return 'asset:${id.package}/${id.path.substring(6)}'; | |
|
Jennifer Messerly
2013/09/04 03:13:27
Hmm, this won't work if we're in a Dart file?
Doe
Siggi Cherem (dart-lang)
2013/09/04 17:57:27
Correct. I added more details about this in the bu
| |
| 110 } | |
| 111 | |
| 112 // Use relative urls only if it's possible. | |
| 113 if (id.package != sourceId.package) { | |
| 114 logger.error("don't know how to import $id from $sourceId"); | |
| 115 return null; | |
| 116 } | |
| 117 | |
| 118 var builder = path.url; | |
| 119 return builder.relative(builder.join('/', id.path), | |
| 120 from: builder.join('/', builder.dirname(sourceId.path))); | |
| 121 } | |
| OLD | NEW |