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
Expand Up @@ -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 `<meta charset>` declarations.

## 0.15.7

Expand Down
9 changes: 3 additions & 6 deletions pkgs/html/lib/src/encoding_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions pkgs/html/lib/src/tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,17 @@ class HtmlTokenizer implements Iterator<Token> {
radix = 16;
}

final charStack = <String?>[];

// 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];
Expand Down Expand Up @@ -299,10 +298,11 @@ class HtmlTokenizer implements Iterator<Token> {
String? entityName;

// Try to find the longest entity the string will match to take care
// of &noti for instance.
// of &noti 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;
Expand Down
82 changes: 82 additions & 0 deletions pkgs/html/test/character_reference_test.dart
Original file line number Diff line number Diff line change
@@ -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(
'<p>&#1488;&#x5D1;&#1504; &#x1F600; &#x05D3;</p>',
);
expect(fragment.text, 'אבנ 😀 ד');
});

test('replace overflowing values with U+FFFD', () {
final fragment = parseFragment(
'<p>&#99999999999999999999999999999999999999;'
'&#xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;</p>',
);
expect(fragment.text, '��');
});

test('decode a reference without a semicolon at EOF', () {
expect(parseFragment('&#1488').text, 'א');
expect(parseFragment('&#x5D1').text, 'ב');
});

test('keep text order across many references', () {
final html = List.filled(300, '&#1488;').join();
expect(parseFragment('<span>$html</span>').text,
List.filled(300, 'א').join());
});
});

group('named character references', () {
test('match the longest entity name', () {
final fragment = parseFragment(
'<p>&notin; &not; &noti; &notit &amp; &copy; &ampx &unknown;</p>',
);
expect(fragment.text, '∉ ¬ ¬i; ¬it & © &x &unknown;');
});

test('match a legacy entity at EOF', () {
expect(parseFragment('a&not').text, 'a¬');
expect(parseFragment('a&noti').text, 'a¬i');
});

test('match a legacy entity followed by a surrogate pair', () {
expect(parseFragment('&not😀').text, '¬😀');
});

test('keep legacy entities followed by alphanumerics in attributes', () {
final fragment =
parseFragment('<a title="&notit=1&copy;x&amp&noti">x</a>');
expect(fragment.firstChild!.attributes['title'], '&notit=1©x&&noti');
});
});

group('meta charset detection', () {
test('skips comments and unrelated tags', () {
const html = '<!-- <meta charset="ascii"> -->'
'<html><head><title>x</title><meta charset="utf-8">'
'</head></html>';
expect(EncodingParser(utf8.encode(html)).getEncoding(), 'utf-8');
});

test('recognizes an uppercase declaration', () {
const html = '<!DOCTYPE html><META CHARSET="UTF-8">';
expect(EncodingParser(utf8.encode(html)).getEncoding(), 'utf-8');
});

test('ignores a truncated tag prefix at the end of the input', () {
expect(EncodingParser(utf8.encode('<!--')).getEncoding(), isNull);
expect(EncodingParser(utf8.encode('<me')).getEncoding(), isNull);
});
});
}
Loading