Skip to content
Open
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

- Fix a bug in DOM parsing where `<br>` tags does not create a new line when html is converted to text.

## 0.15.5

- Require Dart `3.2`.
Expand Down
9 changes: 8 additions & 1 deletion pkgs/html/lib/dom_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ class TreeVisitor {
void visitText(Text node) => visitNodeFallback(node);

// TODO(jmesserly): visit attributes.
void visitElement(Element node) => visitNodeFallback(node);
void visitElement(Element node) {
if (node.localName == 'br') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be separate isElementBr function. Is this if statement enough to recognize it?

visitText(Text('\n'));
return;
}

visitNodeFallback(node);
}

void visitComment(Comment node) => visitNodeFallback(node);

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
8 changes: 4 additions & 4 deletions pkgs/html/test/parser_feature_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ On line 4, column 3 of ParseError: Unexpected DOCTYPE. Ignored.
});

test('Element.text', () {
final doc = parseFragment('<div>foo<div>bar</div>baz</div>');
final doc = parseFragment('<div>foo<div><br>bar</div>baz<br></div>');
final e = doc.firstChild!;
final text = e.firstChild!;
expect((text as Text).data, 'foo');
expect(e.text, 'foobarbaz');
expect(e.text, 'foo\nbarbaz\n');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't change the behavior of .text, in the link you shared we had a function with an explicit boolean like textContent({bool convertBRsToNewlines = false}), I'd prefer to add that instead.


e.text = 'FOO';
expect(e.nodes.length, 1);
Expand All @@ -279,15 +279,15 @@ On line 4, column 3 of ParseError: Unexpected DOCTYPE. Ignored.
});

test('Text.text', () {
final doc = parseFragment('<div>foo<div>bar</div>baz</div>');
final doc = parseFragment('<div>foo<div>bar</div><br>baz</div>');
final e = doc.firstChild!;
final text = e.firstChild as Text;
expect(text.data, 'foo');
expect(text.text, 'foo');

text.text = 'FOO';
expect(text.data, 'FOO');
expect(e.text, 'FOObarbaz');
expect(e.text, 'FOObar\nbaz');
expect(text.text, 'FOO');
});

Expand Down
Loading