OLD | NEW |
1 /// This library contains token types used by the html5 tokenizer. | 1 /// This library contains token types used by the html5 tokenizer. |
2 library token; | 2 library token; |
3 | 3 |
4 import 'dart:collection'; | 4 import 'dart:collection'; |
5 import 'package:source_span/source_span.dart'; | 5 import 'package:source_span/source_span.dart'; |
6 | 6 |
7 /// An html5 token. | 7 /// An html5 token. |
8 abstract class Token { | 8 abstract class Token { |
9 FileSpan span; | 9 FileSpan span; |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 } | 40 } |
41 | 41 |
42 class EndTagToken extends TagToken { | 42 class EndTagToken extends TagToken { |
43 EndTagToken(String name, {bool selfClosing: false}) | 43 EndTagToken(String name, {bool selfClosing: false}) |
44 : super(name, selfClosing); | 44 : super(name, selfClosing); |
45 | 45 |
46 int get kind => TokenKind.endTag; | 46 int get kind => TokenKind.endTag; |
47 } | 47 } |
48 | 48 |
49 abstract class StringToken extends Token { | 49 abstract class StringToken extends Token { |
50 String data; | 50 StringBuffer _buffer; |
51 StringToken(this.data); | 51 |
| 52 String _string; |
| 53 String get data { |
| 54 if (_string == null) { |
| 55 _string = _buffer.toString(); |
| 56 _buffer = null; |
| 57 } |
| 58 return _string; |
| 59 } |
| 60 |
| 61 StringToken(string) |
| 62 : _string = string, |
| 63 _buffer = string == null ? new StringBuffer() : null; |
| 64 |
| 65 StringToken add(String data) { |
| 66 _buffer.write(data); |
| 67 return this; |
| 68 } |
52 } | 69 } |
53 | 70 |
54 class ParseErrorToken extends StringToken { | 71 class ParseErrorToken extends StringToken { |
55 /// Extra information that goes along with the error message. | 72 /// Extra information that goes along with the error message. |
56 Map messageParams; | 73 Map messageParams; |
57 | 74 |
58 ParseErrorToken(String data, {this.messageParams}) : super(data); | 75 ParseErrorToken(String data, {this.messageParams}) : super(data); |
59 | 76 |
60 int get kind => TokenKind.parseError; | 77 int get kind => TokenKind.parseError; |
61 } | 78 } |
62 | 79 |
63 class CharactersToken extends StringToken { | 80 class CharactersToken extends StringToken { |
64 CharactersToken([String data]) : super(data); | 81 CharactersToken([String data]) : super(data); |
65 | 82 |
66 int get kind => TokenKind.characters; | 83 int get kind => TokenKind.characters; |
| 84 |
| 85 /// Replaces the token's [data]. This should only be used to wholly replace |
| 86 /// data, not to append data. |
| 87 void replaceData(String newData) { |
| 88 _string = newData; |
| 89 _buffer = null; |
| 90 } |
67 } | 91 } |
68 | 92 |
69 class SpaceCharactersToken extends StringToken { | 93 class SpaceCharactersToken extends StringToken { |
70 SpaceCharactersToken([String data]) : super(data); | 94 SpaceCharactersToken([String data]) : super(data); |
71 | 95 |
72 int get kind => TokenKind.spaceCharacters; | 96 int get kind => TokenKind.spaceCharacters; |
73 } | 97 } |
74 | 98 |
75 class CommentToken extends StringToken { | 99 class CommentToken extends StringToken { |
76 CommentToken([String data]) : super(data); | 100 CommentToken([String data]) : super(data); |
(...skipping 19 matching lines...) Expand all Loading... |
96 String name; | 120 String name; |
97 String value; | 121 String value; |
98 | 122 |
99 // The spans of the attribute. This is not used unless we are computing an | 123 // The spans of the attribute. This is not used unless we are computing an |
100 // attribute span on demand. | 124 // attribute span on demand. |
101 int start; | 125 int start; |
102 int end; | 126 int end; |
103 int startValue; | 127 int startValue; |
104 int endValue; | 128 int endValue; |
105 | 129 |
106 TagAttribute(this.name, [this.value = '']); | 130 TagAttribute(); |
107 } | 131 } |
108 | 132 |
109 class TokenKind { | 133 class TokenKind { |
110 static const int spaceCharacters = 0; | 134 static const int spaceCharacters = 0; |
111 static const int characters = 1; | 135 static const int characters = 1; |
112 static const int startTag = 2; | 136 static const int startTag = 2; |
113 static const int endTag = 3; | 137 static const int endTag = 3; |
114 static const int comment = 4; | 138 static const int comment = 4; |
115 static const int doctype = 5; | 139 static const int doctype = 5; |
116 static const int parseError = 6; | 140 static const int parseError = 6; |
117 } | 141 } |
OLD | NEW |