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

Fix(html): Handle <br> elements to insert line breaks in text #1950

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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') {
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');

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