Chromium Code Reviews| Index: pkg/analyzer/lib/src/task/html.dart |
| diff --git a/pkg/analyzer/lib/src/task/html.dart b/pkg/analyzer/lib/src/task/html.dart |
| index ab0b9b53b1f800d16e56ec5e2e1d82358f5fa85c..da124c410aebf2bf45a8923c5f541611438f3957 100644 |
| --- a/pkg/analyzer/lib/src/task/html.dart |
| +++ b/pkg/analyzer/lib/src/task/html.dart |
| @@ -4,14 +4,80 @@ |
| library analyzer.src.task.html; |
| +import 'dart:collection'; |
| + |
| import 'package:analyzer/src/generated/engine.dart' hide AnalysisTask; |
| +import 'package:analyzer/src/generated/error.dart'; |
| import 'package:analyzer/src/generated/source.dart'; |
| import 'package:analyzer/src/task/general.dart'; |
| import 'package:analyzer/task/general.dart'; |
| import 'package:analyzer/task/html.dart'; |
| import 'package:analyzer/task/model.dart'; |
| -//import 'package:html/dom.dart'; |
| -//import 'package:html/parser.dart'; |
| +import 'package:html/dom.dart'; |
| +import 'package:html/parser.dart'; |
| +import 'package:source_span/source_span.dart'; |
| + |
| +/** |
| + * The errors found while parsing an HTML file. |
| + */ |
| +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
|
| + new ListResultDescriptor<AnalysisError>( |
| + 'DOCUMENT_ERRORS', AnalysisError.NO_ERRORS); |
| + |
| +/** |
| + * A task that merges all of the errors for a single source into a single list |
| + * of errors. |
| + */ |
| +class HtmlErrorsTask extends SourceBasedAnalysisTask { |
| + /** |
| + * The name of the [DOCUMENT_ERRORS] input. |
| + */ |
| + static const String DOCUMENT_ERRORS_INPUT = 'DOCUMENT_ERRORS'; |
| + |
| + /** |
| + * The task descriptor describing this kind of task. |
| + */ |
| + static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('HtmlErrorsTask', |
| + createTask, buildInputs, <ResultDescriptor>[HTML_ERRORS]); |
| + |
| + HtmlErrorsTask(InternalAnalysisContext context, AnalysisTarget target) |
| + : super(context, target); |
| + |
| + @override |
| + TaskDescriptor get descriptor => DESCRIPTOR; |
| + |
| + @override |
| + void internalPerform() { |
| + // |
| + // Prepare inputs. |
| + // |
| + List<AnalysisError> errors = getRequiredInput(DOCUMENT_ERRORS_INPUT); |
| + // |
| + // Record outputs. |
| + // |
| + outputs[HTML_ERRORS] = errors; |
| + } |
| + |
| + /** |
| + * Return a map from the names of the inputs of this kind of task to the task |
| + * input descriptors describing those inputs for a task with the |
| + * given [target]. |
| + */ |
| + static Map<String, TaskInput> buildInputs(Source target) { |
| + return <String, TaskInput>{ |
| + DOCUMENT_ERRORS_INPUT: DOCUMENT_ERRORS.of(target) |
| + }; |
| + } |
| + |
| + /** |
| + * Create an [HtmlErrorsTask] based on the given [target] in the given |
| + * [context]. |
| + */ |
| + static HtmlErrorsTask createTask( |
| + AnalysisContext context, AnalysisTarget target) { |
| + return new HtmlErrorsTask(context, target); |
| + } |
| +} |
| /** |
| * A task that scans the content of a file, producing a set of Dart tokens. |
| @@ -26,8 +92,7 @@ class ParseHtmlTask extends SourceBasedAnalysisTask { |
| * The task descriptor describing this kind of task. |
| */ |
| static final TaskDescriptor DESCRIPTOR = new TaskDescriptor('ParseHtmlTask', |
| - createTask, buildInputs, |
| - <ResultDescriptor>[/*DOCUMENT, DOCUMENT_ERRORS*/]); |
| + createTask, buildInputs, <ResultDescriptor>[DOCUMENT, DOCUMENT_ERRORS]); |
| /** |
| * Initialize a newly created task to access the content of the source |
| @@ -41,14 +106,21 @@ class ParseHtmlTask extends SourceBasedAnalysisTask { |
| @override |
| void internalPerform() { |
| -// String content = getRequiredInput(CONTENT_INPUT_NAME); |
| -// |
| -// HtmlParser parser = new HtmlParser(content); |
| -// Document document = parser.parse(); |
| -// List<ParseError> errors = parser.errors; |
| -// |
| -// outputs[DOCUMENT] = document; |
| -// outputs[DOCUMENT_ERRORS] = errors; |
| + String content = getRequiredInput(CONTENT_INPUT_NAME); |
| + |
| + HtmlParser parser = new HtmlParser(content); |
| + parser.compatMode = 'quirks'; |
| + Document document = parser.parse(); |
| + List<ParseError> parseErrors = parser.errors; |
| + List<AnalysisError> errors = <AnalysisError>[]; |
| + for (ParseError parseError in parseErrors) { |
| + SourceSpan span = parseError.span; |
| + errors.add(new AnalysisError(target.source, span.start.offset, |
| + span.length, HtmlErrorCode.PARSE_ERROR, [parseError.message])); |
| + } |
| + |
| + outputs[DOCUMENT] = document; |
| + outputs[DOCUMENT_ERRORS] = errors; |
| } |
| /** |
| @@ -68,3 +140,72 @@ class ParseHtmlTask extends SourceBasedAnalysisTask { |
| return new ParseHtmlTask(context, target); |
| } |
| } |
| + |
| +/** |
| + * A task that computes the Dart libraries that are referenced by an HTML file. |
| + */ |
| +class ReferencedLibrariesTask extends SourceBasedAnalysisTask { |
| + /** |
| + * The name of the [DOCUMENT] input. |
| + */ |
| + static const String DOCUMENT_INPUT = 'DOCUMENT'; |
| + |
| + /** |
| + * The task descriptor describing this kind of task. |
| + */ |
| + static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| + 'ReferencedLibrariesTask', createTask, buildInputs, |
| + <ResultDescriptor>[REFERENCED_LIBRARIES]); |
| + |
| + ReferencedLibrariesTask( |
| + InternalAnalysisContext context, AnalysisTarget target) |
| + : super(context, target); |
| + |
| + @override |
| + TaskDescriptor get descriptor => DESCRIPTOR; |
| + |
| + @override |
| + void internalPerform() { |
| + // |
| + // Prepare inputs. |
| + // |
| + List<Source> libraries = <Source>[]; |
| + Document document = getRequiredInput(DOCUMENT_INPUT); |
| + List<Element> scripts = document.getElementsByTagName('script'); |
| + for (Element script in scripts) { |
| + LinkedHashMap<dynamic, String> attributes = script.attributes; |
| + if (attributes['type'] == 'application/dart') { |
| + String src = attributes['src']; |
| + if (AnalysisEngine.isDartFileName(src)) { |
| + 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
|
| + if (source != null) { |
| + libraries.add(source); |
| + } |
| + } |
| + } |
| + } |
| + // |
| + // Record outputs. |
| + // |
| + outputs[REFERENCED_LIBRARIES] = |
| + 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
|
| + } |
| + |
| + /** |
| + * Return a map from the names of the inputs of this kind of task to the task |
| + * input descriptors describing those inputs for a task with the |
| + * given [target]. |
| + */ |
| + static Map<String, TaskInput> buildInputs(Source target) { |
| + return <String, TaskInput>{DOCUMENT_INPUT: DOCUMENT.of(target)}; |
| + } |
| + |
| + /** |
| + * Create a [ReferencedLibrariesTask] based on the given [target] in the given |
| + * [context]. |
| + */ |
| + static ReferencedLibrariesTask createTask( |
| + AnalysisContext context, AnalysisTarget target) { |
| + return new ReferencedLibrariesTask(context, target); |
| + } |
| +} |