Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: pkg/analyzer/lib/src/task/html.dart

Issue 1182303006: Re-enable and add new HTML support (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Move a private constant Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 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 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 library analyzer.src.task.html; 5 library analyzer.src.task.html;
6 6
7 import 'dart:collection';
8
7 import 'package:analyzer/src/generated/engine.dart' hide AnalysisTask; 9 import 'package:analyzer/src/generated/engine.dart' hide AnalysisTask;
10 import 'package:analyzer/src/generated/error.dart';
8 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
9 import 'package:analyzer/src/task/general.dart'; 12 import 'package:analyzer/src/task/general.dart';
10 import 'package:analyzer/task/general.dart'; 13 import 'package:analyzer/task/general.dart';
11 import 'package:analyzer/task/html.dart'; 14 import 'package:analyzer/task/html.dart';
12 import 'package:analyzer/task/model.dart'; 15 import 'package:analyzer/task/model.dart';
13 //import 'package:html/dom.dart'; 16 import 'package:html/dom.dart';
14 //import 'package:html/parser.dart'; 17 import 'package:html/parser.dart';
18 import 'package:source_span/source_span.dart';
19
20 /**
21 * The errors found while parsing an HTML file.
22 */
23 final ListResultDescriptor<AnalysisError> DOCUMENT_ERRORS =
Paul Berry 2015/06/15 21:34:39 I'm concerned about the use of the term "document"
Brian Wilkerson 2015/06/16 16:28:08 Done
24 new ListResultDescriptor<AnalysisError>(
25 'DOCUMENT_ERRORS', AnalysisError.NO_ERRORS);
26
27 /**
28 * A task that merges all of the errors for a single source into a single list
29 * of errors.
30 */
31 class HtmlErrorsTask extends SourceBasedAnalysisTask {
32 /**
33 * The name of the [DOCUMENT_ERRORS] input.
34 */
35 static const String DOCUMENT_ERRORS_INPUT = 'DOCUMENT_ERRORS';
36
37 /**
38 * The task descriptor describing this kind of task.
39 */
40 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('HtmlErrorsTask',
41 createTask, buildInputs, <ResultDescriptor>[HTML_ERRORS]);
42
43 HtmlErrorsTask(InternalAnalysisContext context, AnalysisTarget target)
44 : super(context, target);
45
46 @override
47 TaskDescriptor get descriptor => DESCRIPTOR;
48
49 @override
50 void internalPerform() {
51 //
52 // Prepare inputs.
53 //
54 List<AnalysisError> errors = getRequiredInput(DOCUMENT_ERRORS_INPUT);
55 //
56 // Record outputs.
57 //
58 outputs[HTML_ERRORS] = errors;
59 }
60
61 /**
62 * Return a map from the names of the inputs of this kind of task to the task
63 * input descriptors describing those inputs for a task with the
64 * given [target].
65 */
66 static Map<String, TaskInput> buildInputs(Source target) {
67 return <String, TaskInput>{
68 DOCUMENT_ERRORS_INPUT: DOCUMENT_ERRORS.of(target)
69 };
70 }
71
72 /**
73 * Create an [HtmlErrorsTask] based on the given [target] in the given
74 * [context].
75 */
76 static HtmlErrorsTask createTask(
77 AnalysisContext context, AnalysisTarget target) {
78 return new HtmlErrorsTask(context, target);
79 }
80 }
15 81
16 /** 82 /**
17 * A task that scans the content of a file, producing a set of Dart tokens. 83 * A task that scans the content of a file, producing a set of Dart tokens.
18 */ 84 */
19 class ParseHtmlTask extends SourceBasedAnalysisTask { 85 class ParseHtmlTask extends SourceBasedAnalysisTask {
20 /** 86 /**
21 * The name of the input whose value is the content of the file. 87 * The name of the input whose value is the content of the file.
22 */ 88 */
23 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; 89 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME';
24 90
25 /** 91 /**
26 * The task descriptor describing this kind of task. 92 * The task descriptor describing this kind of task.
27 */ 93 */
28 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('ParseHtmlTask', 94 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('ParseHtmlTask',
29 createTask, buildInputs, 95 createTask, buildInputs, <ResultDescriptor>[DOCUMENT, DOCUMENT_ERRORS]);
30 <ResultDescriptor>[/*DOCUMENT, DOCUMENT_ERRORS*/]);
31 96
32 /** 97 /**
33 * Initialize a newly created task to access the content of the source 98 * Initialize a newly created task to access the content of the source
34 * associated with the given [target] in the given [context]. 99 * associated with the given [target] in the given [context].
35 */ 100 */
36 ParseHtmlTask(InternalAnalysisContext context, AnalysisTarget target) 101 ParseHtmlTask(InternalAnalysisContext context, AnalysisTarget target)
37 : super(context, target); 102 : super(context, target);
38 103
39 @override 104 @override
40 TaskDescriptor get descriptor => DESCRIPTOR; 105 TaskDescriptor get descriptor => DESCRIPTOR;
41 106
42 @override 107 @override
43 void internalPerform() { 108 void internalPerform() {
44 // String content = getRequiredInput(CONTENT_INPUT_NAME); 109 String content = getRequiredInput(CONTENT_INPUT_NAME);
45 // 110
46 // HtmlParser parser = new HtmlParser(content); 111 HtmlParser parser = new HtmlParser(content);
47 // Document document = parser.parse(); 112 parser.compatMode = 'quirks';
48 // List<ParseError> errors = parser.errors; 113 Document document = parser.parse();
49 // 114 List<ParseError> parseErrors = parser.errors;
50 // outputs[DOCUMENT] = document; 115 List<AnalysisError> errors = <AnalysisError>[];
51 // outputs[DOCUMENT_ERRORS] = errors; 116 for (ParseError parseError in parseErrors) {
117 SourceSpan span = parseError.span;
118 errors.add(new AnalysisError(target.source, span.start.offset,
119 span.length, HtmlErrorCode.PARSE_ERROR, [parseError.message]));
120 }
121
122 outputs[DOCUMENT] = document;
123 outputs[DOCUMENT_ERRORS] = errors;
52 } 124 }
53 125
54 /** 126 /**
55 * Return a map from the names of the inputs of this kind of task to the task 127 * Return a map from the names of the inputs of this kind of task to the task
56 * input descriptors describing those inputs for a task with the given 128 * input descriptors describing those inputs for a task with the given
57 * [source]. 129 * [source].
58 */ 130 */
59 static Map<String, TaskInput> buildInputs(Source source) { 131 static Map<String, TaskInput> buildInputs(Source source) {
60 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; 132 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)};
61 } 133 }
62 134
63 /** 135 /**
64 * Create a [ParseHtmlTask] based on the given [target] in the given [context] . 136 * Create a [ParseHtmlTask] based on the given [target] in the given [context] .
65 */ 137 */
66 static ParseHtmlTask createTask( 138 static ParseHtmlTask createTask(
67 AnalysisContext context, AnalysisTarget target) { 139 AnalysisContext context, AnalysisTarget target) {
68 return new ParseHtmlTask(context, target); 140 return new ParseHtmlTask(context, target);
69 } 141 }
70 } 142 }
143
144 /**
145 * A task that computes the Dart libraries that are referenced by an HTML file.
146 */
147 class ReferencedLibrariesTask extends SourceBasedAnalysisTask {
148 /**
149 * The name of the [DOCUMENT] input.
150 */
151 static const String DOCUMENT_INPUT = 'DOCUMENT';
152
153 /**
154 * The task descriptor describing this kind of task.
155 */
156 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor(
157 'ReferencedLibrariesTask', createTask, buildInputs,
158 <ResultDescriptor>[REFERENCED_LIBRARIES]);
159
160 ReferencedLibrariesTask(
161 InternalAnalysisContext context, AnalysisTarget target)
162 : super(context, target);
163
164 @override
165 TaskDescriptor get descriptor => DESCRIPTOR;
166
167 @override
168 void internalPerform() {
169 //
170 // Prepare inputs.
171 //
172 List<Source> libraries = <Source>[];
173 Document document = getRequiredInput(DOCUMENT_INPUT);
174 List<Element> scripts = document.getElementsByTagName('script');
175 for (Element script in scripts) {
176 LinkedHashMap<dynamic, String> attributes = script.attributes;
177 if (attributes['type'] == 'application/dart') {
178 String src = attributes['src'];
179 if (AnalysisEngine.isDartFileName(src)) {
180 Source source = context.sourceFactory.resolveUri(target.source, src);
Paul Berry 2015/06/15 21:34:39 This presumes that all the rules for resolving URI
Brian Wilkerson 2015/06/16 16:28:08 I don't know whether package: or dart: URI's are a
181 if (source != null) {
182 libraries.add(source);
183 }
184 }
185 }
186 }
187 //
188 // Record outputs.
189 //
190 outputs[REFERENCED_LIBRARIES] =
191 libraries.isEmpty ? Source.EMPTY_LIST : libraries;
Paul Berry 2015/06/15 21:34:39 Again, it seems of dubious benefit to me to canoni
Brian Wilkerson 2015/06/16 16:28:08 I know that we're storing these lists, so the valu
192 }
193
194 /**
195 * Return a map from the names of the inputs of this kind of task to the task
196 * input descriptors describing those inputs for a task with the
197 * given [target].
198 */
199 static Map<String, TaskInput> buildInputs(Source target) {
200 return <String, TaskInput>{DOCUMENT_INPUT: DOCUMENT.of(target)};
201 }
202
203 /**
204 * Create a [ReferencedLibrariesTask] based on the given [target] in the given
205 * [context].
206 */
207 static ReferencedLibrariesTask createTask(
208 AnalysisContext context, AnalysisTarget target) {
209 return new ReferencedLibrariesTask(context, target);
210 }
211 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698