| OLD | NEW |
| 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'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/src/context/cache.dart'; | 9 import 'package:analyzer/src/context/cache.dart'; |
| 10 import 'package:analyzer/src/generated/engine.dart' hide AnalysisTask; | 10 import 'package:analyzer/src/generated/engine.dart' hide AnalysisTask; |
| 11 import 'package:analyzer/src/generated/error.dart'; | 11 import 'package:analyzer/src/generated/error.dart'; |
| 12 import 'package:analyzer/src/generated/java_engine.dart'; | 12 import 'package:analyzer/src/generated/java_engine.dart'; |
| 13 import 'package:analyzer/src/generated/scanner.dart'; | 13 import 'package:analyzer/src/generated/scanner.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/plugin/engine_plugin.dart'; | 15 import 'package:analyzer/src/plugin/engine_plugin.dart'; |
| 16 import 'package:analyzer/src/task/dart.dart'; | |
| 17 import 'package:analyzer/src/task/general.dart'; | 16 import 'package:analyzer/src/task/general.dart'; |
| 18 import 'package:analyzer/task/dart.dart'; | 17 import 'package:analyzer/task/dart.dart'; |
| 19 import 'package:analyzer/task/general.dart'; | 18 import 'package:analyzer/task/general.dart'; |
| 20 import 'package:analyzer/task/html.dart'; | 19 import 'package:analyzer/task/html.dart'; |
| 21 import 'package:analyzer/task/model.dart'; | 20 import 'package:analyzer/task/model.dart'; |
| 22 import 'package:html/dom.dart'; | 21 import 'package:html/dom.dart'; |
| 23 import 'package:html/parser.dart'; | 22 import 'package:html/parser.dart'; |
| 24 import 'package:source_span/source_span.dart'; | 23 import 'package:source_span/source_span.dart'; |
| 25 | 24 |
| 26 /** | 25 /** |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 */ | 272 */ |
| 274 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; | 273 static const String CONTENT_INPUT_NAME = 'CONTENT_INPUT_NAME'; |
| 275 | 274 |
| 276 /** | 275 /** |
| 277 * The task descriptor describing this kind of task. | 276 * The task descriptor describing this kind of task. |
| 278 */ | 277 */ |
| 279 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( | 278 static final TaskDescriptor DESCRIPTOR = new TaskDescriptor( |
| 280 'ParseHtmlTask', | 279 'ParseHtmlTask', |
| 281 createTask, | 280 createTask, |
| 282 buildInputs, | 281 buildInputs, |
| 283 <ResultDescriptor>[HTML_DOCUMENT, HTML_DOCUMENT_ERRORS]); | 282 <ResultDescriptor>[HTML_DOCUMENT, HTML_DOCUMENT_ERRORS, LINE_INFO]); |
| 284 | 283 |
| 285 /** | 284 /** |
| 286 * Initialize a newly created task to access the content of the source | 285 * Initialize a newly created task to access the content of the source |
| 287 * associated with the given [target] in the given [context]. | 286 * associated with the given [target] in the given [context]. |
| 288 */ | 287 */ |
| 289 ParseHtmlTask(InternalAnalysisContext context, AnalysisTarget target) | 288 ParseHtmlTask(InternalAnalysisContext context, AnalysisTarget target) |
| 290 : super(context, target); | 289 : super(context, target); |
| 291 | 290 |
| 292 @override | 291 @override |
| 293 TaskDescriptor get descriptor => DESCRIPTOR; | 292 TaskDescriptor get descriptor => DESCRIPTOR; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 305 if (exception != null) { | 304 if (exception != null) { |
| 306 message = exception.toString(); | 305 message = exception.toString(); |
| 307 } | 306 } |
| 308 } | 307 } |
| 309 | 308 |
| 310 outputs[HTML_DOCUMENT] = new Document(); | 309 outputs[HTML_DOCUMENT] = new Document(); |
| 311 outputs[HTML_DOCUMENT_ERRORS] = <AnalysisError>[ | 310 outputs[HTML_DOCUMENT_ERRORS] = <AnalysisError>[ |
| 312 new AnalysisError( | 311 new AnalysisError( |
| 313 target.source, 0, 0, ScannerErrorCode.UNABLE_GET_CONTENT, [message]) | 312 target.source, 0, 0, ScannerErrorCode.UNABLE_GET_CONTENT, [message]) |
| 314 ]; | 313 ]; |
| 314 outputs[LINE_INFO] = new LineInfo(<int>[0]); |
| 315 } else { | 315 } else { |
| 316 HtmlParser parser = new HtmlParser(content, generateSpans: true); | 316 HtmlParser parser = new HtmlParser(content, generateSpans: true); |
| 317 parser.compatMode = 'quirks'; | 317 parser.compatMode = 'quirks'; |
| 318 Document document = parser.parse(); | 318 Document document = parser.parse(); |
| 319 // |
| 320 // Convert errors. |
| 321 // |
| 319 List<ParseError> parseErrors = parser.errors; | 322 List<ParseError> parseErrors = parser.errors; |
| 320 List<AnalysisError> errors = <AnalysisError>[]; | 323 List<AnalysisError> errors = <AnalysisError>[]; |
| 321 for (ParseError parseError in parseErrors) { | 324 for (ParseError parseError in parseErrors) { |
| 322 SourceSpan span = parseError.span; | 325 SourceSpan span = parseError.span; |
| 323 errors.add(new AnalysisError(target.source, span.start.offset, | 326 errors.add(new AnalysisError(target.source, span.start.offset, |
| 324 span.length, HtmlErrorCode.PARSE_ERROR, [parseError.message])); | 327 span.length, HtmlErrorCode.PARSE_ERROR, [parseError.message])); |
| 325 } | 328 } |
| 326 | 329 // |
| 330 // Record outputs. |
| 331 // |
| 327 outputs[HTML_DOCUMENT] = document; | 332 outputs[HTML_DOCUMENT] = document; |
| 328 outputs[HTML_DOCUMENT_ERRORS] = errors; | 333 outputs[HTML_DOCUMENT_ERRORS] = errors; |
| 334 outputs[LINE_INFO] = _computeLineInfo(content); |
| 329 } | 335 } |
| 330 } | 336 } |
| 331 | 337 |
| 332 /** | 338 /** |
| 333 * Return a map from the names of the inputs of this kind of task to the task | 339 * Return a map from the names of the inputs of this kind of task to the task |
| 334 * input descriptors describing those inputs for a task with the given | 340 * input descriptors describing those inputs for a task with the given |
| 335 * [source]. | 341 * [source]. |
| 336 */ | 342 */ |
| 337 static Map<String, TaskInput> buildInputs(Source source) { | 343 static Map<String, TaskInput> buildInputs(Source source) { |
| 338 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; | 344 return <String, TaskInput>{CONTENT_INPUT_NAME: CONTENT.of(source)}; |
| 339 } | 345 } |
| 340 | 346 |
| 341 /** | 347 /** |
| 342 * Create a [ParseHtmlTask] based on the given [target] in the given [context]
. | 348 * Create a [ParseHtmlTask] based on the given [target] in the given [context]
. |
| 343 */ | 349 */ |
| 344 static ParseHtmlTask createTask( | 350 static ParseHtmlTask createTask( |
| 345 AnalysisContext context, AnalysisTarget target) { | 351 AnalysisContext context, AnalysisTarget target) { |
| 346 return new ParseHtmlTask(context, target); | 352 return new ParseHtmlTask(context, target); |
| 347 } | 353 } |
| 354 |
| 355 /** |
| 356 * Compute [LineInfo] for the given [content]. |
| 357 */ |
| 358 static LineInfo _computeLineInfo(String content) { |
| 359 List<int> lineStarts = <int>[0]; |
| 360 for (int index = 0; index < content.length; index++) { |
| 361 if (content.codeUnitAt(index) == 0x0A) { |
| 362 lineStarts.add(index + 1); |
| 363 } |
| 364 } |
| 365 return new LineInfo(lineStarts); |
| 366 } |
| 348 } | 367 } |
| 349 | 368 |
| 350 /** | 369 /** |
| 351 * A fragment of a [DartScript]. | 370 * A fragment of a [DartScript]. |
| 352 */ | 371 */ |
| 353 class ScriptFragment { | 372 class ScriptFragment { |
| 354 /** | 373 /** |
| 355 * The offset of the first character of the fragment, relative to the start of | 374 * The offset of the first character of the fragment, relative to the start of |
| 356 * the containing source. | 375 * the containing source. |
| 357 */ | 376 */ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 372 * The content of the fragment. | 391 * The content of the fragment. |
| 373 */ | 392 */ |
| 374 final String content; | 393 final String content; |
| 375 | 394 |
| 376 /** | 395 /** |
| 377 * Initialize a newly created script fragment to have the given [offset] and | 396 * Initialize a newly created script fragment to have the given [offset] and |
| 378 * [content]. | 397 * [content]. |
| 379 */ | 398 */ |
| 380 ScriptFragment(this.offset, this.line, this.column, this.content); | 399 ScriptFragment(this.offset, this.line, this.column, this.content); |
| 381 } | 400 } |
| OLD | NEW |