1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Released under the MIT License.
// Copyright, 2026, by Samuel Williams.
//! Byte recognition for raw inline HTML and complete HTML block tags.
//! Input starts immediately after `<`. Grammar callers supply normalized bytes;
//! compact callers stop at physical line boundaries and retain source ranges.
#[derive(Clone, Copy, Debug, Default)]
pub(crate) enum HtmlText {
#[default]
Open,
DeclarationOpen,
CommentOpen,
Comment,
CommentDash,
CommentEnd,
CdataOpen(u8),
Cdata,
CdataBracket,
CdataEnd,
Declaration,
Instruction,
InstructionQuestion,
CloseStart,
CloseName,
CloseSpace,
Name,
Between,
Attribute,
AfterAttribute,
BeforeValue,
Quoted(u8),
AfterQuoted,
Unquoted,
End,
}
impl HtmlText {
/// Consume one byte: `Some(true)` completes HTML, `None` rejects it.
pub fn advance(&mut self, byte: u8, namespaces: bool) -> Option<bool> {
use HtmlText::{
AfterAttribute, AfterQuoted, Attribute, BeforeValue, Between, Cdata, CdataBracket,
CdataEnd, CdataOpen, CloseName, CloseSpace, CloseStart, Comment, CommentDash,
CommentEnd, CommentOpen, Declaration, DeclarationOpen, End, Instruction,
InstructionQuestion, Name, Open, Quoted, Unquoted,
};
loop {
*self = match *self {
Open => match byte {
b'!' => DeclarationOpen,
b'/' => CloseStart,
b'?' => Instruction,
b'A'..=b'Z' | b'a'..=b'z' => Name,
_ => return None,
},
DeclarationOpen => match byte {
b'-' => CommentOpen,
b'[' => CdataOpen(0),
b'A'..=b'Z' | b'a'..=b'z' => Declaration,
_ => return None,
},
CommentOpen => {
if byte != b'-' {
return None;
}
CommentEnd
}
Comment | CommentDash | CommentEnd => match byte {
b'>' if matches!(self, CommentEnd) => return Some(true),
b'-' if matches!(self, CommentDash | CommentEnd) => CommentEnd,
b'-' => CommentDash,
_ => Comment,
},
CdataOpen(index) => {
if byte != b"CDATA["[usize::from(index)] {
return None;
}
if index == 5 {
Cdata
} else {
CdataOpen(index + 1)
}
}
Cdata | CdataBracket | CdataEnd => match byte {
b'>' if matches!(self, CdataEnd) => return Some(true),
b']' if matches!(self, CdataBracket | CdataEnd) => CdataEnd,
b']' => CdataBracket,
_ => Cdata,
},
Declaration => {
if byte == b'>' {
return Some(true);
}
Declaration
}
Instruction | InstructionQuestion => match byte {
b'>' if matches!(self, InstructionQuestion) => return Some(true),
b'?' => InstructionQuestion,
_ => Instruction,
},
CloseStart => {
if !byte.is_ascii_alphabetic() {
return None;
}
CloseName
}
Name | CloseName
if byte.is_ascii_alphanumeric()
|| byte == b'-'
|| (namespaces && byte == b':') =>
{
*self
}
CloseName => {
*self = CloseSpace;
continue;
}
CloseSpace => match byte {
b' ' | b'\t' | b'\n' => CloseSpace,
b'>' => return Some(true),
_ => return None,
},
Name => {
if !matches!(byte, b' ' | b'\t' | b'\n' | b'/' | b'>') {
return None;
}
*self = Between;
continue;
}
Between => match byte {
b' ' | b'\t' | b'\n' => Between,
b'/' => End,
b':' | b'_' | b'A'..=b'Z' | b'a'..=b'z' => Attribute,
b'>' => return Some(true),
_ => return None,
},
Attribute
if byte.is_ascii_alphanumeric()
|| matches!(byte, b'-' | b'.' | b':' | b'_') =>
{
Attribute
}
Attribute => {
*self = AfterAttribute;
continue;
}
AfterAttribute => match byte {
b' ' | b'\t' | b'\n' => AfterAttribute,
b'=' => BeforeValue,
_ => {
*self = Between;
continue;
}
},
BeforeValue => match byte {
b'<' | b'=' | b'>' | b'`' => return None,
b' ' | b'\t' | b'\n' => BeforeValue,
b'"' | b'\'' => Quoted(byte),
_ => Unquoted,
},
Quoted(quote) => {
if byte == quote {
AfterQuoted
} else {
*self
}
}
AfterQuoted => {
if !matches!(byte, b' ' | b'\t' | b'\n' | b'/' | b'>') {
return None;
}
*self = Between;
continue;
}
Unquoted => match byte {
b'"' | b'\'' | b'<' | b'=' | b'`' => return None,
b' ' | b'\t' | b'\n' | b'/' | b'>' => {
*self = Between;
continue;
}
_ => Unquoted,
},
End => {
if byte == b'>' {
return Some(true);
}
return None;
}
};
return Some(false);
}
}
}