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

Side by Side Diff: third_party/pkg/html5lib/lib/parser_console.dart

Issue 22375011: move html5lib code into dart svn repo (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /**
2 * This library adds `dart:io` support to the HTML5 parser. Call
3 * [initDartIOSupport] before calling the [parse] methods and they will accept
4 * a [RandomAccessFile] as input, in addition to the other input types.
5 */
6 library parser_console;
7
8 import 'dart:io';
9 import 'parser.dart';
10 import 'src/inputstream.dart' as inputstream;
11
12 /**
13 * Adds support to the [HtmlParser] for running on a console VM. In particular
14 * this means it will be able to handle `dart:io` and [RandomAccessFile]s as
15 * input to the various [parse] methods.
16 */
17 void useConsole() {
18 inputstream.consoleSupport = new _ConsoleSupport();
19 }
20
21 class _ConsoleSupport extends inputstream.ConsoleSupport {
22 List<int> bytesFromFile(source) {
23 if (source is! RandomAccessFile) return null;
24 return readAllBytesFromFile(source);
25 }
26 }
27
28 // TODO(jmesserly): this should be `RandomAccessFile.readAllBytes`.
29 /** Synchronously reads all bytes from the [file]. */
30 List<int> readAllBytesFromFile(RandomAccessFile file) {
31 int length = file.lengthSync();
32 var bytes = new List<int>(length);
33
34 int bytesRead = 0;
35 while (bytesRead < length) {
36 int read = file.readIntoSync(bytes, bytesRead, length - bytesRead);
37 if (read <= 0) {
38 // This could happen if, for example, the file was resized while
39 // we're reading. Just shrink the bytes array and move on.
40 bytes = bytes.sublist(0, bytesRead);
41 break;
42 }
43 bytesRead += read;
44 }
45 return bytes;
46 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698