diff --git a/pkgs/html/CHANGELOG.md b/pkgs/html/CHANGELOG.md index ae09a646d8..c7caf3f444 100644 --- a/pkgs/html/CHANGELOG.md +++ b/pkgs/html/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.15.8-wip +- Reduce allocations when parsing and serializing: `HtmlInputStream` no longer copies the input when it needs no newline or surrogate normalization, and `htmlSerializeEscape` copies unescaped runs at once and returns the input unchanged when nothing needs escaping. - `
` and `

` end tags in foreign content (``, ``) now exit foreign content before reprocessing in HTML mode, matching the WHATWG HTML standard. - `<` and `>` are now escaped as `<` and `>` when serializing attribute values in `htmlSerializeEscape`, `innerHtml`, and `outerHtml`, matching the WHATWG HTML standard. - Fix `Node.attributeSpans` and `Node.attributeValueSpans` throwing a `TypeError` when a start tag emits tokenizer parse errors, and preserve the first occurrence's span on duplicate attributes. diff --git a/pkgs/html/lib/html_escape.dart b/pkgs/html/lib/html_escape.dart index 93fbf3a350..e186b3e331 100644 --- a/pkgs/html/lib/html_escape.dart +++ b/pkgs/html/lib/html_escape.dart @@ -19,36 +19,32 @@ /// [1]: https://html.spec.whatwg.org/multipage/parsing.html#serializing-html-fragments /// [2]: https://html.spec.whatwg.org/multipage/parsing.html#escapingString String htmlSerializeEscape(String text, {bool attributeMode = false}) { - // TODO(jmesserly): is it faster to build up a list of codepoints? - // StringBuffer seems cleaner assuming Dart can unbox 1-char strings. + // Copy unchanged runs at once instead of one-character strings. StringBuffer? result; + var last = 0; for (var i = 0; i < text.length; i++) { - final ch = text[i]; - String? replace; - switch (ch) { - case '&': - replace = '&'; - break; - case '\u00A0' /*NO-BREAK SPACE*/ : - replace = ' '; - break; - case '"': - if (attributeMode) replace = '"'; - break; - case '<': - replace = '<'; - break; - case '>': - replace = '>'; - break; + String? replacement; + switch (text.codeUnitAt(i)) { + case 0x26: // & + replacement = '&'; + case 0xA0: // NO-BREAK SPACE + replacement = ' '; + case 0x22: // " + if (attributeMode) replacement = '"'; + case 0x3C: // < + replacement = '<'; + case 0x3E: // > + replacement = '>'; } - if (replace != null) { - result ??= StringBuffer(text.substring(0, i)); - result.write(replace); - } else if (result != null) { - result.write(ch); + if (replacement != null) { + result ??= StringBuffer(); + result.write(text.substring(last, i)); + result.write(replacement); + last = i + 1; } } - return result != null ? result.toString() : text; + if (result == null) return text; + result.write(text.substring(last)); + return result.toString(); } diff --git a/pkgs/html/lib/src/html_input_stream.dart b/pkgs/html/lib/src/html_input_stream.dart index 8c5335147c..6cb34b1de8 100644 --- a/pkgs/html/lib/src/html_input_stream.dart +++ b/pkgs/html/lib/src/html_input_stream.dart @@ -36,8 +36,8 @@ class HtmlInputStream { List? _rawBytes; - /// Raw UTF-16 codes, used if a Dart String is passed in. - List? _rawChars; + /// Decoded text, or the Dart String passed in. + String? _rawText; Queue errors = Queue(); @@ -45,6 +45,9 @@ class HtmlInputStream { var _chars = []; + /// The String whose code units are [_chars], if normalization kept them. + String? _text; + var _offset = 0; /// Initialise an HtmlInputStream. @@ -68,7 +71,7 @@ class HtmlInputStream { this.sourceUrl]) : charEncodingName = codecName(encoding) { if (source is String) { - _rawChars = source.codeUnits; + _rawText = source; charEncodingName = 'utf-8'; charEncodingCertain = true; } else if (source is List) { @@ -91,15 +94,16 @@ class HtmlInputStream { _offset = 0; - final rawChars = _rawChars ??= _decodeBytes(charEncodingName!, _rawBytes!); + final rawText = _rawText ??= _decodeBytes(charEncodingName!, _rawBytes!); + final rawChars = rawText.codeUnits; - // Optimistically allocate array, trim it later if there are changes - _chars = List.filled(rawChars.length, 0, growable: true); + // Most input needs no CR/CRLF or surrogate normalization, so keep the + // decoded String and copy its code units only when one of them changes. + var chars = rawChars; var skipNewline = false; var wasSurrogatePair = false; var deletedChars = 0; - /// CodeUnits.length is not free final charsLength = rawChars.length; for (var i = 0; i < charsLength; i++) { var c = rawChars[i]; @@ -130,15 +134,21 @@ class HtmlInputStream { c = Charcode.lineFeed; } - _chars[i - deletedChars] = c; + if (c != rawChars[i] && identical(chars, rawChars)) { + chars = List.of(rawChars, growable: true); + } + if (!identical(chars, rawChars)) { + chars[i - deletedChars] = c; + } } if (deletedChars > 0) { - // Remove the null bytes from the end - _chars.removeRange(_chars.length - deletedChars, _chars.length); + chars.length -= deletedChars; } + _chars = chars; + _text = identical(chars, rawChars) ? rawText : null; // Free decoded characters if they aren't needed anymore. - if (_rawBytes != null) _rawChars = null; + if (_rawBytes != null) _rawText = null; if (generateSpans) { fileInfo = SourceFile.decoded(_chars, url: sourceUrl); @@ -187,7 +197,7 @@ class HtmlInputStream { } else { charEncodingName = newEncoding; charEncodingCertain = true; - _rawChars = null; + _rawText = null; reset(); throw ReparseException( 'Encoding changed from $charEncodingName to $newEncoding'); @@ -279,7 +289,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } String charsUntil1(int charCode, [bool opposite = false]) { @@ -289,7 +299,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } String charsUntil2(int charCode1, int charCode2, [bool opposite = false]) { @@ -300,7 +310,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } String charsUntil3(int charCode1, int charCode2, int charCode3, @@ -312,7 +322,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } String charsUntilAsciiLetter([bool opposite = false]) { @@ -324,9 +334,14 @@ class HtmlInputStream { opposite) { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } + // String.fromCharCodes copies a String's code units by iterating from the + // first one on the VM, so slice the String itself when it is available. + String _substring(int start, int end) => + _text?.substring(start, end) ?? String.fromCharCodes(_chars, start, end); + static bool _isSpaceCharacter(int c) => c == Charcode.space || c == Charcode.lineFeed || @@ -341,7 +356,7 @@ class HtmlInputStream { _offset += 1; } - return String.fromCharCodes(_chars.sublist(start, _offset)); + return _substring(start, _offset); } void unget(String? ch) { @@ -432,13 +447,13 @@ bool _isSupportedEncoding(String? encoding) => /// Decodes the [bytes] with the provided [encoding] and returns a list for /// the codepoints. Supports ascii and utf-8 encodings. -List _decodeBytes(String encoding, List bytes) { +String _decodeBytes(String encoding, List bytes) { return switch (encoding) { - 'ascii' => ascii.decode(bytes, allowInvalid: true).codeUnits, + 'ascii' => ascii.decode(bytes, allowInvalid: true), 'utf-8' => // NOTE: To match the behavior of the other decode functions, we eat the // UTF-8 BOM here. This is the default behavior of `utf8.decode`. - utf8.decode(bytes, allowMalformed: true).codeUnits, + utf8.decode(bytes, allowMalformed: true), _ => throw ArgumentError('Encoding $encoding not supported') }; } diff --git a/pkgs/html/test/html_stream_performance_regression_test.dart b/pkgs/html/test/html_stream_performance_regression_test.dart new file mode 100644 index 0000000000..7246fa9a0f --- /dev/null +++ b/pkgs/html/test/html_stream_performance_regression_test.dart @@ -0,0 +1,76 @@ +// 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/dom_parsing.dart'; +import 'package:html/src/html_input_stream.dart'; +import 'package:test/test.dart'; + +void main() { + group('HTML input stream normalization', () { + test('unchanged Hebrew and ASCII content can be read in chunks', () { + final stream = HtmlInputStream('שלום עברית'); + expect(stream.charsUntil1(0x3C), 'שלום '); + expect(stream.char(), '<'); + expect(stream.charsUntil2(0x3E, 0x21), 'b'); + expect(stream.char(), '>'); + expect(stream.charsUntil3(0x3C, 0x26, 0x21), 'עברית'); + expect(stream.char(), '<'); + }); + + test('decoded bytes can be read in chunks after reset', () { + final stream = HtmlInputStream(utf8.encode('שלום עברית')); + expect(stream.charsUntil1(0x3C), 'שלום '); + stream.reset(); + expect(stream.charsUntil1(0x3C), 'שלום '); + expect(stream.char(), '<'); + expect(stream.charsUntilAsciiLetter(true), 'b'); + }); + + test('normalizes CRLF and lone CR identically after reset', () { + final stream = HtmlInputStream('first\r\nsecond\rthird\n'); + expect(stream.charsUntil1(0x0), 'first\nsecond\nthird\n'); + stream.reset(); + expect(stream.charsUntil1(0x0), 'first\nsecond\nthird\n'); + }); + + test('preserves surrogate pairs and replaces malformed surrogates', () { + final stream = HtmlInputStream('א\uD83D\uDE00\uD800ב'); + expect(stream.charsUntil1(0x0), 'א\uD83D\uDE00\uFFFDב'); + expect(stream.errors, contains('invalid-codepoint')); + stream.reset(); + expect(stream.charsUntil1(0x0), 'א\uD83D\uDE00\uFFFDב'); + expect(stream.errors, contains('invalid-codepoint')); + }); + + test('source spans keep the normalized source text', () { + final stream = HtmlInputStream('אב\r\nגד', null, true, true); + expect(stream.fileInfo!.getText(0, 5), 'אב\nגד'); + }); + }); + + group('HTML serializer escaping', () { + test('returns text unchanged without escapable characters', () { + final plain = 'שלום עולם 😀'; + expect(htmlSerializeEscape(plain), same(plain)); + }); + + test('escapes markup, ampersands and nonbreaking spaces', () { + expect(htmlSerializeEscape('אב&< >\u00A0גד'), 'אב&< > גד'); + }); + + test('quotes are escaped only in attributes', () { + expect(htmlSerializeEscape('a"b&c'), 'a"b&c'); + expect( + htmlSerializeEscape('a"b&c', attributeMode: true), 'a"b&c'); + }); + + test('escapes adjacent and trailing characters', () { + expect(htmlSerializeEscape('&&<>'), '&&<>'); + expect(htmlSerializeEscape('<'), '<'); + expect(htmlSerializeEscape(''), ''); + }); + }); +}