Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/html/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
- `</br>` and `</p>` end tags in foreign content (`<svg>`, `<math>`) now exit foreign content before reprocessing in HTML mode, matching the WHATWG HTML standard.
- `<` and `>` are now escaped as `&lt;` and `&gt;` 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.
Expand Down
48 changes: 22 additions & 26 deletions pkgs/html/lib/html_escape.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '&amp;';
break;
case '\u00A0' /*NO-BREAK SPACE*/ :
replace = '&nbsp;';
break;
case '"':
if (attributeMode) replace = '&quot;';
break;
case '<':
replace = '&lt;';
break;
case '>':
replace = '&gt;';
break;
String? replacement;
switch (text.codeUnitAt(i)) {
case 0x26: // &
replacement = '&amp;';
case 0xA0: // NO-BREAK SPACE
replacement = '&nbsp;';
case 0x22: // "
if (attributeMode) replacement = '&quot;';
case 0x3C: // <
replacement = '&lt;';
case 0x3E: // >
replacement = '&gt;';
}
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();
}
57 changes: 36 additions & 21 deletions pkgs/html/lib/src/html_input_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,18 @@ class HtmlInputStream {

List<int>? _rawBytes;

/// Raw UTF-16 codes, used if a Dart String is passed in.
List<int>? _rawChars;
/// Decoded text, or the Dart String passed in.
String? _rawText;

Queue<String> errors = Queue<String>();

SourceFile? fileInfo;

var _chars = <int>[];

/// The String whose code units are [_chars], if normalization kept them.
String? _text;

var _offset = 0;

/// Initialise an HtmlInputStream.
Expand All @@ -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<int>) {
Expand All @@ -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];
Expand Down Expand Up @@ -130,15 +134,21 @@ class HtmlInputStream {
c = Charcode.lineFeed;
}

_chars[i - deletedChars] = c;
if (c != rawChars[i] && identical(chars, rawChars)) {
chars = List<int>.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);
Expand Down Expand Up @@ -187,7 +197,7 @@ class HtmlInputStream {
} else {
charEncodingName = newEncoding;
charEncodingCertain = true;
_rawChars = null;
_rawText = null;
reset();
throw ReparseException(
'Encoding changed from $charEncodingName to $newEncoding');
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]) {
Expand All @@ -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,
Expand All @@ -312,7 +322,7 @@ class HtmlInputStream {
_offset += 1;
}

return String.fromCharCodes(_chars.sublist(start, _offset));
return _substring(start, _offset);
}

String charsUntilAsciiLetter([bool opposite = false]) {
Expand All @@ -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 ||
Expand All @@ -341,7 +356,7 @@ class HtmlInputStream {
_offset += 1;
}

return String.fromCharCodes(_chars.sublist(start, _offset));
return _substring(start, _offset);
}

void unget(String? ch) {
Expand Down Expand Up @@ -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<int> _decodeBytes(String encoding, List<int> bytes) {
String _decodeBytes(String encoding, List<int> 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')
};
}
76 changes: 76 additions & 0 deletions pkgs/html/test/html_stream_performance_regression_test.dart
Original file line number Diff line number Diff line change
@@ -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('שלום <b>עברית</b>');
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('שלום <b>עברית</b>'));
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גד'), 'אב&amp;&lt; &gt;&nbsp;גד');
});

test('quotes are escaped only in attributes', () {
expect(htmlSerializeEscape('a"b&c'), 'a"b&amp;c');
expect(
htmlSerializeEscape('a"b&c', attributeMode: true), 'a&quot;b&amp;c');
});

test('escapes adjacent and trailing characters', () {
expect(htmlSerializeEscape('&&<>'), '&amp;&amp;&lt;&gt;');
expect(htmlSerializeEscape('<'), '&lt;');
expect(htmlSerializeEscape(''), '');
});
});
}
Loading