OLD | NEW |
(Empty) | |
| 1 /// This library contains token types used by the html5 tokenizer. |
| 2 library token; |
| 3 |
| 4 import 'dart:collection'; |
| 5 import 'package:source_span/source_span.dart'; |
| 6 |
| 7 /// An html5 token. |
| 8 abstract class Token { |
| 9 FileSpan span; |
| 10 |
| 11 int get kind; |
| 12 } |
| 13 |
| 14 abstract class TagToken extends Token { |
| 15 String name; |
| 16 |
| 17 bool selfClosing; |
| 18 |
| 19 TagToken(this.name, this.selfClosing); |
| 20 } |
| 21 |
| 22 class StartTagToken extends TagToken { |
| 23 /// The tag's attributes. A map from the name to the value, where the name |
| 24 /// can be a [String] or [AttributeName]. |
| 25 LinkedHashMap<dynamic, String> data; |
| 26 |
| 27 /// The attribute spans if requested. Otherwise null. |
| 28 List<TagAttribute> attributeSpans; |
| 29 |
| 30 bool selfClosingAcknowledged; |
| 31 |
| 32 /// The namespace. This is filled in later during tree building. |
| 33 String namespace; |
| 34 |
| 35 StartTagToken(String name, {this.data, bool selfClosing: false, |
| 36 this.selfClosingAcknowledged: false, this.namespace}) |
| 37 : super(name, selfClosing); |
| 38 |
| 39 int get kind => TokenKind.startTag; |
| 40 } |
| 41 |
| 42 class EndTagToken extends TagToken { |
| 43 EndTagToken(String name, {bool selfClosing: false}) |
| 44 : super(name, selfClosing); |
| 45 |
| 46 int get kind => TokenKind.endTag; |
| 47 } |
| 48 |
| 49 abstract class StringToken extends Token { |
| 50 StringBuffer _buffer; |
| 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 } |
| 69 } |
| 70 |
| 71 class ParseErrorToken extends StringToken { |
| 72 /// Extra information that goes along with the error message. |
| 73 Map messageParams; |
| 74 |
| 75 ParseErrorToken(String data, {this.messageParams}) : super(data); |
| 76 |
| 77 int get kind => TokenKind.parseError; |
| 78 } |
| 79 |
| 80 class CharactersToken extends StringToken { |
| 81 CharactersToken([String data]) : super(data); |
| 82 |
| 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 } |
| 91 } |
| 92 |
| 93 class SpaceCharactersToken extends StringToken { |
| 94 SpaceCharactersToken([String data]) : super(data); |
| 95 |
| 96 int get kind => TokenKind.spaceCharacters; |
| 97 } |
| 98 |
| 99 class CommentToken extends StringToken { |
| 100 CommentToken([String data]) : super(data); |
| 101 |
| 102 int get kind => TokenKind.comment; |
| 103 } |
| 104 |
| 105 class DoctypeToken extends Token { |
| 106 String publicId; |
| 107 String systemId; |
| 108 String name = ""; |
| 109 bool correct; |
| 110 |
| 111 DoctypeToken({this.publicId, this.systemId, this.correct: false}); |
| 112 |
| 113 int get kind => TokenKind.doctype; |
| 114 } |
| 115 |
| 116 /// These are used by the tokenizer to build up the attribute map. |
| 117 /// They're also used by [StartTagToken.attributeSpans] if attribute spans are |
| 118 /// requested. |
| 119 class TagAttribute { |
| 120 String name; |
| 121 String value; |
| 122 |
| 123 // The spans of the attribute. This is not used unless we are computing an |
| 124 // attribute span on demand. |
| 125 int start; |
| 126 int end; |
| 127 int startValue; |
| 128 int endValue; |
| 129 |
| 130 TagAttribute(); |
| 131 } |
| 132 |
| 133 class TokenKind { |
| 134 static const int spaceCharacters = 0; |
| 135 static const int characters = 1; |
| 136 static const int startTag = 2; |
| 137 static const int endTag = 3; |
| 138 static const int comment = 4; |
| 139 static const int doctype = 5; |
| 140 static const int parseError = 6; |
| 141 } |
OLD | NEW |