-
Notifications
You must be signed in to change notification settings - Fork 76
Fix(html): Handle <br> elements to insert line breaks in text
#1950
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
Open
Dhruv-Maradiya
wants to merge
6
commits into
dart-lang:main
Choose a base branch
from
Dhruv-Maradiya:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
90fea07
Implement handling of <br> elements in DOM parsing to insert line bre…
Dhruv-Maradiya b6126c1
update tests to handle <br> elements for line breaks
Dhruv-Maradiya 9beee02
update changelog to include fix for <br> newline handling
Dhruv-Maradiya 2b721ea
bump version to 0.15.5+1 in html package
Dhruv-Maradiya aafc3f9
feat: add spec-compliant textContent with BR conversion flag
Dhruv-Maradiya 5a001ab
Merge branch 'main' into main
Dhruv-Maradiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -214,6 +214,9 @@ abstract class Node { | |
| } | ||
|
|
||
| // Implemented per: http://dom.spec.whatwg.org/#dom-node-textcontent | ||
| String? textContent({bool convertBRsToNewlines = false}) => | ||
| _getTextContent(this, convertBRsToNewlines: convertBRsToNewlines); | ||
|
|
||
| String? get text => null; | ||
|
|
||
| set text(String? value) {} | ||
|
|
@@ -1099,8 +1102,36 @@ class FilteredElementList extends IterableBase<Element> | |
| } | ||
|
|
||
| // http://dom.spec.whatwg.org/#dom-node-textcontent | ||
| // For Element and DocumentFragment | ||
| String _getText(Node node) => (_ConcatTextVisitor()..visit(node)).toString(); | ||
| String? _getTextContent(Node node, {bool convertBRsToNewlines = false}) { | ||
| // DocumentFragment or Element: return descendant text content | ||
| if (node is DocumentFragment || node is Element) { | ||
| return _getText(node, convertBRsToNewlines: convertBRsToNewlines); | ||
| } | ||
| // CharacterData (Text, Comment): return data | ||
| if (node is Text) { | ||
| return node.data; | ||
| } | ||
| if (node is Comment) { | ||
| return node.data; | ||
| } | ||
| // Otherwise: return null | ||
| return null; | ||
| } | ||
|
|
||
| /// Returns true if the element is an HTML <br> element. | ||
| /// Checks both the local name and namespace to ensure it's a proper HTML br element. | ||
| /// Note: null namespace is treated as HTML namespace for elements created by the HTML parser. | ||
| bool isElementBr(Element element) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be private. |
||
| if (element.localName != 'br') return false; | ||
| final ns = element.namespaceUri; | ||
| return ns == null || ns == Namespaces.html; | ||
| } | ||
|
|
||
| // For Element and DocumentFragment (legacy helper) | ||
| String _getText(Node node, {bool convertBRsToNewlines = false}) => | ||
| (_ConcatTextVisitor(convertBRsToNewlines: convertBRsToNewlines) | ||
| ..visit(node)) | ||
| .toString(); | ||
|
|
||
| void _setText(Node node, String? value) { | ||
| node.nodes.clear(); | ||
|
|
@@ -1109,6 +1140,9 @@ void _setText(Node node, String? value) { | |
|
|
||
| class _ConcatTextVisitor extends TreeVisitor { | ||
| final _str = StringBuffer(); | ||
| final bool convertBRsToNewlines; | ||
|
|
||
| _ConcatTextVisitor({this.convertBRsToNewlines = false}); | ||
|
|
||
| @override | ||
| String toString() => _str.toString(); | ||
|
|
@@ -1117,4 +1151,13 @@ class _ConcatTextVisitor extends TreeVisitor { | |
| void visitText(Text node) { | ||
| _str.write(node.data); | ||
| } | ||
|
|
||
| @override | ||
| void visitElement(Element node) { | ||
| if (convertBRsToNewlines && isElementBr(node)) { | ||
| _str.write('\n'); | ||
| return; | ||
| } | ||
| super.visitElement(node); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's no longer a bug then, change the changelog entry to convey adding a new
textContentmethod instead.