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 library web_components.build.import_crawler; | 4 library web_components.build.import_crawler; |
5 | 5 |
6 import 'dart:async'; | 6 import 'dart:async'; |
7 import 'dart:collection' show LinkedHashMap; | 7 import 'dart:collection' show LinkedHashMap; |
8 import 'package:code_transformers/assets.dart'; | 8 import 'package:code_transformers/assets.dart'; |
9 import 'package:code_transformers/messages/build_logger.dart'; | 9 import 'package:code_transformers/messages/build_logger.dart'; |
10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
11 import 'package:html5lib/dom.dart' show Document, Element; | 11 import 'package:html5lib/dom.dart' show Document, Element; |
12 import 'common.dart'; | 12 import 'common.dart'; |
13 import 'messages.dart'; | 13 import 'messages.dart'; |
14 | 14 |
15 /// Information about an html import found in a document. | 15 /// Information about an html import found in a document. |
16 class ImportData { | 16 class ImportData { |
17 /// The [AssetId] where the html import appeared. | |
18 final AssetId fromId; | |
19 | |
17 /// The [Document] where the html import appeared. | 20 /// The [Document] where the html import appeared. |
18 final Document document; | 21 final Document document; |
19 | 22 |
20 /// The html import element itself. | 23 /// The html import element itself. |
21 final Element element; | 24 final Element element; |
22 | 25 |
23 ImportData(this.document, this.element); | 26 ImportData(this.document, this.element, {this.fromId}); |
24 } | 27 } |
25 | 28 |
26 /// A crawler for html imports. | 29 /// A crawler for html imports. |
27 class ImportCrawler { | 30 class ImportCrawler { |
28 // Can be either an AggregateTransform or Transform. | 31 // Can be either an AggregateTransform or Transform. |
29 final _transform; | 32 final _transform; |
30 final BuildLogger _logger; | 33 final BuildLogger _logger; |
31 final AssetId _primaryInputId; | 34 final AssetId _primaryInputId; |
32 | 35 |
33 // Optional parsed document for the primary id if available. | 36 // Optional parsed document for the primary id if available. |
34 final Document _primaryDocument; | 37 final Document _primaryDocument; |
35 | 38 |
36 ImportCrawler(this._transform, this._primaryInputId, this._logger, | 39 ImportCrawler(this._transform, this._primaryInputId, this._logger, |
37 {Document primaryDocument}) | 40 {Document primaryDocument}) |
38 : _primaryDocument = primaryDocument; | 41 : _primaryDocument = primaryDocument; |
39 | 42 |
40 /// Returns a post-ordered map of [AssetId]'s to [ImportData]. The [AssetId]'s | 43 /// Returns a post-ordered map of [AssetId]'s to [ImportData]. The [AssetId]'s |
41 /// represent an asset which was discovered via an html import, and the | 44 /// represent an asset which was discovered via an html import, and the |
42 /// [ImportData] represents the [Document] where it was discovered and the | 45 /// [ImportData] represents the [Document] where it was discovered and the |
43 /// html import [Element] itself. | 46 /// html import [Element] itself. |
44 Future<LinkedHashMap<AssetId, ImportData>> crawlImports() { | 47 Future<LinkedHashMap<AssetId, ImportData>> crawlImports() { |
45 var documents = new LinkedHashMap<AssetId, ImportData>(); | 48 var documents = new LinkedHashMap<AssetId, ImportData>(); |
46 var seen = new Set<AssetId>(); | 49 var seen = new Set<AssetId>(); |
47 | 50 |
48 Future doCrawl(AssetId assetId, [Element import, Document document]) { | 51 Future doCrawl(AssetId assetId, {Element import, Document document, AssetId from}) { |
Siggi Cherem (dart-lang)
2015/03/11 21:47:54
formatter?
| |
49 if (seen.contains(assetId)) return null; | 52 if (seen.contains(assetId)) return null; |
50 seen.add(assetId); | 53 seen.add(assetId); |
51 | 54 |
52 Future crawlImports(Document document) { | 55 Future crawlImports(Document document) { |
53 var imports = document.querySelectorAll('link[rel="import"]'); | 56 var imports = document.querySelectorAll('link[rel="import"]'); |
54 var done = | 57 var done = Future.forEach(imports, |
55 Future.forEach(imports, (i) => doCrawl(_importId(assetId, i), i)); | 58 (i) => doCrawl(_importId(assetId, i), import: i, from: assetId)); |
56 | 59 |
57 // Add this document after its dependencies. | 60 // Add this document after its dependencies. |
58 return done.then((_) { | 61 return done.then((_) { |
59 documents[assetId] = new ImportData(document, import); | 62 documents[assetId] = new ImportData(document, import, fromId: from); |
60 }); | 63 }); |
61 } | 64 } |
62 | 65 |
63 if (document != null) { | 66 if (document != null) { |
64 return crawlImports(document); | 67 return crawlImports(document); |
65 } else { | 68 } else { |
66 return _transform.readInputAsString(assetId).then((html) { | 69 return _transform.readInputAsString(assetId).then((html) { |
67 return crawlImports(parseHtml(html, assetId.path)); | 70 return crawlImports(parseHtml(html, assetId.path)); |
68 }).catchError((error) { | 71 }).catchError((error) { |
69 var span; | 72 var span; |
70 if (import != null) span = import.sourceSpan; | 73 if (import != null) span = import.sourceSpan; |
71 _logger.error(inlineImportFail.create({'error': error}), span: span); | 74 _logger.error(inlineImportFail.create({'error': error}), span: span); |
72 }); | 75 }); |
73 } | 76 } |
74 } | 77 } |
75 | 78 |
76 return | 79 return |
77 doCrawl(_primaryInputId, null, _primaryDocument).then((_) => documents); | 80 doCrawl(_primaryInputId, document: _primaryDocument).then((_) => documents ); |
78 } | 81 } |
79 | 82 |
80 AssetId _importId(AssetId source, Element import) { | 83 AssetId _importId(AssetId source, Element import) { |
81 var url = import.attributes['href']; | 84 var url = import.attributes['href']; |
82 return uriToAssetId(source, url, _transform.logger, import.sourceSpan); | 85 return uriToAssetId(source, url, _transform.logger, import.sourceSpan); |
83 } | 86 } |
84 } | 87 } |
OLD | NEW |