Skip to content

[html] Allow ampersands in attribute values. #2036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 13, 2025
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
4 changes: 4 additions & 0 deletions pkgs/html/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.15.5+1

- Support "ambiguous ampersand" in attribute values.

## 0.15.5

- Require Dart `3.2`.
Expand Down
7 changes: 5 additions & 2 deletions pkgs/html/lib/src/tokenizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ class HtmlTokenizer implements Iterator<Token> {

// Try to find the longest entity the string will match to take care
// of &noti for instance.

int entityLen;
for (entityLen = charStack.length - 1; entityLen > 1; entityLen--) {
final possibleEntityName = charStack.sublist(0, entityLen).join();
Expand All @@ -340,7 +339,11 @@ class HtmlTokenizer implements Iterator<Token> {
output = '$output${slice(charStack, entityLen).join()}';
}
} else {
_addToken(ParseErrorToken('expected-named-entity'));
if (!fromAttribute) {
// Only emit this error token when we're consuming this NOT as part of an attribute.
// See: https://html.spec.whatwg.org/multipage/parsing.html#ambiguous-ampersand-state
_addToken(ParseErrorToken('expected-named-entity'));
}
stream.unget(charStack.removeLast());
output = '&${charStack.join()}';
}
Expand Down
2 changes: 1 addition & 1 deletion pkgs/html/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: html
version: 0.15.5
version: 0.15.5+1
description: APIs for parsing and manipulating HTML content outside the browser.
repository: https://github.com/dart-lang/tools/tree/main/pkgs/html
issue_tracker: https://github.com/dart-lang/tools/issues?q=is%3Aissue+is%3Aopen+label%3Apackage%3Ahtml
Expand Down
4 changes: 4 additions & 0 deletions pkgs/html/test/data/tokenizer/test4.test
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"input":"<z ====>",
"output":["ParseError", "ParseError", "ParseError", ["StartTag", "z", {"=": "=="}]]},

{"description":"Ambiguous ampersand in attribute value",
"input":"<tag attr=\"foo?a=b&c=d\">",
"output":[["StartTag", "tag", {"attr": "foo?a=b&c=d"}]]},

{"description":"Allowed \" after ampersand in attribute value",
"input":"<z z=\"&\">",
"output":[["StartTag", "z", {"z": "&"}]]},
Expand Down
12 changes: 12 additions & 0 deletions pkgs/html/test/parser_feature_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ On line 4, column 3 of ParseError: Unexpected DOCTYPE. Ignored.
expect(elem.attributeSpans!['extends'], null);
});

test('attribute spans if value contains & (ambiguous ampersand)', () {
final expectedUrl = 'foo?key=value&key2=value2';
final text = '<script src="$expectedUrl"></script>';

final doc = parse(text, generateSpans: true);
final elem = doc.querySelector('script')!;
final span = elem.attributeValueSpans!['src']!;

expect(span.start.offset, text.indexOf('foo'));
expect(span.text, expectedUrl);
});

test('void element innerHTML', () {
var doc = parse('<div></div>');
expect(doc.body!.innerHtml, '<div></div>');
Expand Down