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
5 changes: 5 additions & 0 deletions pkgs/markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

* Treat horizontal tabs as delimiters between fenced code language and
metadata, and skip punctuation unescaping when no backslashes are present.
* Compute `Element.textContent` iteratively into a single buffer, so deeply
nested ASTs no longer overflow the stack and nested text is not copied at
every level. Overridden `textContent` getters on `Element` subclasses are
still used.
* Avoid regular-expression work when classifying blank lines and dedenting Markdown blocks; scan ASCII whitespace directly while retaining partial-tab handling.
* Reduce allocations when rendering single-line HTML text nodes and reset renderer traversal state correctly when a `HtmlRenderer` instance is reused.
* Reduce allocations when rendering single-line text nodes in `HtmlRenderer`.
Expand All @@ -19,6 +23,7 @@
* Reuse HTML escape converters and skip HTML entity matching for strings without ampersands, reducing allocations during inline and code-block parsing.
* Sort footnote definitions by their original case-insensitive labels rather than URI-encoded HTML IDs, preserving first-reference order for Unicode and escaped labels.
* Preserve existing percent-encoded sequences in extended autolink destinations instead of double-encoding their percent signs; still escape invalid percent sequences.
* Flatten nested AST element text iteratively into one buffer instead of repeatedly concatenating intermediate strings; preserve specialized text getters on subclasses.
* Skip the regular expression in `EmojiSyntax`, `AutolinkSyntax`,
`CodeSyntax`, and `ColorSwatchSyntax` when the character at the current
position cannot start a match. This makes inline parsing faster, mostly on
Expand Down
24 changes: 21 additions & 3 deletions pkgs/markdown/lib/src/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,27 @@ class Element implements Node {
@override
String get textContent {
final children = this.children;
return children == null
? ''
: children.map((child) => child.textContent).join();
if (children == null) return '';

final text = StringBuffer();
final stack = <Iterator<Node>>[children.iterator];
while (stack.isNotEmpty) {
final iterator = stack.last;
if (!iterator.moveNext()) {
stack.removeLast();
continue;
}
final child = iterator.current;
if (child is Element && child.runtimeType == Element) {
// Flatten ordinary nested elements without recursively materializing
// the text of every parent. Honor overridden getters on subclasses.
final nested = child.children;
if (nested != null) stack.add(nested.iterator);
} else {
text.write(child.textContent);
}
}
return text.toString();
}
}

Expand Down
45 changes: 45 additions & 0 deletions pkgs/markdown/test/ast_text_content_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2026, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:markdown/markdown.dart';
import 'package:test/test.dart';

void main() {
test('Element.textContent concatenates nested and unparsed text', () {
final root = Element('p', [
Text('a'),
Element('strong', [
Text('b'),
Element('em', [Text('c')]),
]),
UnparsedContent('d'),
]);
expect(root.textContent, 'abcd');
expect(Element.empty('hr').textContent, isEmpty);
});

test('deeply nested AST text extraction does not recurse', () {
Node node = Text('שלום');
for (var i = 0; i < 10000; i++) {
node = Element('em', [node]);
}
expect(node.textContent, 'שלום');
});

test('Element.textContent respects subclass overrides', () {
final parent = Element('p', [
Text('before-'),
_OverriddenElement(),
Text('-after'),
]);
expect(parent.textContent, 'before-custom-after');
});
}

class _OverriddenElement extends Element {
_OverriddenElement() : super('span', [Text('ignored')]);

@override
String get textContent => 'custom';
}
Loading