deserialize method
Implementation
@override
void deserialize(HtmlExternalScannerState state, Uint8List data) {
state._tags.clear();
if (data.isEmpty) return;
if (data.length < 4) {
throw const FormatException('Truncated HTML external-scanner state');
}
final bytes = ByteData.sublistView(data);
final serializedTagCount = bytes.getUint16(0, Endian.host);
final tagCount = bytes.getUint16(2, Endian.host);
if (serializedTagCount > tagCount) {
throw const FormatException('Invalid HTML external-scanner tag counts');
}
var offset = 4;
for (var index = 0; index < serializedTagCount; index++) {
if (offset >= data.length) {
throw const FormatException('Truncated HTML external-scanner tag');
}
final type = data[offset++];
if (type == _custom) {
if (offset >= data.length) {
throw const FormatException(
'Truncated HTML external-scanner custom tag',
);
}
final nameLength = data[offset++];
if (offset + nameLength > data.length) {
throw const FormatException(
'Truncated HTML external-scanner custom tag name',
);
}
state._tags.add(
_HtmlTag(
type,
utf8.decode(data.sublist(offset, offset + nameLength)),
),
);
offset += nameLength;
} else {
state._tags.add(_HtmlTag(type, ''));
}
}
// Upstream resizes the vector before restoring the serialized prefix. Any
// suffix that did not fit in Tree-sitter's buffer receives Tag's default
// END_OF_VOID_TAGS value.
while (state._tags.length < tagCount) {
state._tags.add(const _HtmlTag(_endOfVoidTags, ''));
}
}