| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 library web_components.build.common; | |
| 5 | |
| 6 import 'package:analyzer/analyzer.dart'; | |
| 7 import 'package:barback/barback.dart'; | |
| 8 import 'package:code_transformers/messages/build_logger.dart'; | |
| 9 import 'package:html/dom.dart' as dom; | |
| 10 import 'package:html/parser.dart'; | |
| 11 import 'package:source_span/source_span.dart'; | |
| 12 import 'package:path/path.dart' as path; | |
| 13 import 'messages.dart'; | |
| 14 | |
| 15 /// Generate the import url for a file described by [id], referenced by a file | |
| 16 /// with [sourceId]. | |
| 17 // TODO(sigmund): this should also be in barback (dartbug.com/12610) | |
| 18 // TODO(jakemac): This is copied from polymer, we should move it to | |
| 19 // the code_transformers package so it can be shared. | |
| 20 String assetUrlFor(AssetId id, AssetId sourceId, BuildLogger logger, | |
| 21 {bool allowAssetUrl: false}) { | |
| 22 // use package: and asset: urls if possible | |
| 23 if (id.path.startsWith('lib/')) { | |
| 24 return 'package:${id.package}/${id.path.substring(4)}'; | |
| 25 } | |
| 26 | |
| 27 if (id.path.startsWith('asset/')) { | |
| 28 if (!allowAssetUrl) { | |
| 29 logger.error(internalErrorDontKnowHowToImport.create({ | |
| 30 'target': id, | |
| 31 'source': sourceId, | |
| 32 'extra': ' (asset urls not allowed.)' | |
| 33 })); | |
| 34 return null; | |
| 35 } | |
| 36 return 'asset:${id.package}/${id.path.substring(6)}'; | |
| 37 } | |
| 38 | |
| 39 // Use relative urls only if it's possible. | |
| 40 if (id.package != sourceId.package) { | |
| 41 logger.error("don't know how to refer to $id from $sourceId"); | |
| 42 return null; | |
| 43 } | |
| 44 | |
| 45 var builder = path.url; | |
| 46 return builder.relative(builder.join('/', id.path), | |
| 47 from: builder.join('/', builder.dirname(sourceId.path))); | |
| 48 } | |
| 49 | |
| 50 /// Gets the appropriate URL to use in a span to produce messages (e.g. | |
| 51 /// warnings) for users. This will attempt to format the URL in the most useful | |
| 52 /// way: | |
| 53 /// | |
| 54 /// - If the asset is within the primary package, then use the [id.path], | |
| 55 /// the user will know it is a file from their own code. | |
| 56 /// - If the asset is from another package, then use [assetUrlFor], this will | |
| 57 /// likely be a "package:" url to the file in the other package, which is | |
| 58 /// enough for users to identify where the error is. | |
| 59 String spanUrlFor(AssetId id, AssetId sourceId, logger) { | |
| 60 bool samePackage = id.package == sourceId.package; | |
| 61 return samePackage | |
| 62 ? id.path | |
| 63 : assetUrlFor(id, sourceId, logger, allowAssetUrl: true); | |
| 64 } | |
| 65 | |
| 66 /// Return the span in a file for an [AstNode]. | |
| 67 FileSpan getSpan(SourceFile file, AstNode node) => | |
| 68 file.span(node.offset, node.end); | |
| 69 | |
| 70 /// Parses an HTML file [contents] and returns a DOM-like tree. | |
| 71 dom.Document parseHtml(String contents, String sourcePath) { | |
| 72 var parser = new HtmlParser(contents, | |
| 73 encoding: 'utf8', generateSpans: true, sourceUrl: sourcePath); | |
| 74 return parser.parse(); | |
| 75 } | |
| 76 | |
| 77 const String dartType = "application/dart"; | |
| 78 const String jsType = "text/javascript"; | |
| OLD | NEW |