diff --git a/pkgs/html/CHANGELOG.md b/pkgs/html/CHANGELOG.md index 79010ecfb2..49551962fe 100644 --- a/pkgs/html/CHANGELOG.md +++ b/pkgs/html/CHANGELOG.md @@ -12,6 +12,7 @@ - Fix `ParseError.message` throwing a null-check `TypeError` on `unexpected-EOF-after-attribute-value` tokenizer errors, and align tokenizer error codes `this-closing-flag-on-end-tag` -> `self-closing-flag-on-end-tag` and `expected-end-of-tag-but-got-eof` -> `expected-end-of-tag-name-but-got-eof` with `errorMessages`. - Fix `Node.remove` and `NodeList.remove` leaving a stale `parentNode`, `Node.insertBefore`, `NodeList.insert`, `insertAll` and `operator []=` using a shifted index when moving an earlier sibling, and `reparentChildren` emptying a node reparented to itself. Moving all children of another node (`append` of a `DocumentFragment`, `reparentChildren`, `innerHtml`) now runs in linear time. - `getElementById`, `getElementsByTagName` and `getElementsByClassName` no longer parse a CSS selector. IDs and class names that are not valid CSS identifiers (such as `a:b`, `a.b` or `1`) now match exactly instead of throwing or matching the wrong element, `getElementById('')` returns `null`, class names are split on HTML ASCII whitespace, and deeply nested documents no longer overflow the stack. +- Reduce allocations when decoding numeric and named character references and when sniffing `` declarations. ## 0.15.7 diff --git a/pkgs/html/lib/src/encoding_parser.dart b/pkgs/html/lib/src/encoding_parser.dart index 7909f7457e..bcb9fa85c5 100644 --- a/pkgs/html/lib/src/encoding_parser.dart +++ b/pkgs/html/lib/src/encoding_parser.dart @@ -95,12 +95,9 @@ class EncodingBytes { if (_bytes.length < p + bytes.length) { return false; } - final data = _bytes.substring(p, p + bytes.length); - if (data == bytes) { - _position += bytes.length; - return true; - } - return false; + if (!_bytes.startsWith(bytes, p)) return false; + _position += bytes.length; + return true; } /// Look for the next sequence of bytes matching a given sequence. If diff --git a/pkgs/html/lib/src/tokenizer.dart b/pkgs/html/lib/src/tokenizer.dart index e8eeb11bc0..e42dc0a9bd 100644 --- a/pkgs/html/lib/src/tokenizer.dart +++ b/pkgs/html/lib/src/tokenizer.dart @@ -168,18 +168,17 @@ class HtmlTokenizer implements Iterator { radix = 16; } - final charStack = []; - // Consume all the characters that are in range while making sure we // don't hit an EOF. + final digits = StringBuffer(); var c = stream.char(); while (allowed(c) && c != eof) { - charStack.add(c); + digits.write(c); c = stream.char(); } - // Convert the set of characters consumed to an int. - final charAsInt = int.tryParse(charStack.join(), radix: radix) ?? 0x110000; + // Convert the characters consumed to an int; overflow becomes U+FFFD. + final charAsInt = int.tryParse(digits.toString(), radix: radix) ?? 0x110000; // Certain characters get replaced with others var char = replacementCharacters[charAsInt]; @@ -299,10 +298,11 @@ class HtmlTokenizer implements Iterator { String? entityName; // Try to find the longest entity the string will match to take care - // of ¬i for instance. + // of ¬i for instance. All but the last char matched the ASCII trie. + final consumed = charStack.join(); int entityLen; for (entityLen = charStack.length - 1; entityLen > 1; entityLen--) { - final possibleEntityName = charStack.sublist(0, entityLen).join(); + final possibleEntityName = consumed.substring(0, entityLen); if (entities.containsKey(possibleEntityName)) { entityName = possibleEntityName; break; diff --git a/pkgs/html/test/character_reference_test.dart b/pkgs/html/test/character_reference_test.dart new file mode 100644 index 0000000000..b1de2539e8 --- /dev/null +++ b/pkgs/html/test/character_reference_test.dart @@ -0,0 +1,82 @@ +// Copyright (c) project authors. All rights reserved. +// Licensed under the MIT license. +// See LICENSE file in the project root for details. + +import 'dart:convert'; + +import 'package:html/parser.dart'; +import 'package:html/src/encoding_parser.dart'; +import 'package:test/test.dart'; + +void main() { + group('numeric character references', () { + test('decode decimal and hexadecimal Hebrew and astral code points', () { + final fragment = parseFragment( + '

אבנ 😀 ד

', + ); + expect(fragment.text, 'אבנ 😀 ד'); + }); + + test('replace overflowing values with U+FFFD', () { + final fragment = parseFragment( + '

�' + '�

', + ); + expect(fragment.text, '��'); + }); + + test('decode a reference without a semicolon at EOF', () { + expect(parseFragment('א').text, 'א'); + expect(parseFragment('ב').text, 'ב'); + }); + + test('keep text order across many references', () { + final html = List.filled(300, 'א').join(); + expect(parseFragment('$html').text, + List.filled(300, 'א').join()); + }); + }); + + group('named character references', () { + test('match the longest entity name', () { + final fragment = parseFragment( + '

∉ ¬ ¬i; ¬it & © &x &unknown;

', + ); + expect(fragment.text, '∉ ¬ ¬i; ¬it & © &x &unknown;'); + }); + + test('match a legacy entity at EOF', () { + expect(parseFragment('a¬').text, 'a¬'); + expect(parseFragment('a¬i').text, 'a¬i'); + }); + + test('match a legacy entity followed by a surrogate pair', () { + expect(parseFragment('¬😀').text, '¬😀'); + }); + + test('keep legacy entities followed by alphanumerics in attributes', () { + final fragment = + parseFragment('x'); + expect(fragment.firstChild!.attributes['title'], '¬it=1©x&¬i'); + }); + }); + + group('meta charset detection', () { + test('skips comments and unrelated tags', () { + const html = '' + 'x' + ''; + expect(EncodingParser(utf8.encode(html)).getEncoding(), 'utf-8'); + }); + + test('recognizes an uppercase declaration', () { + const html = ''; + expect(EncodingParser(utf8.encode(html)).getEncoding(), 'utf-8'); + }); + + test('ignores a truncated tag prefix at the end of the input', () { + expect(EncodingParser(utf8.encode('