From 3be7fea061cda503c430a5f3ae155c673e0e5260 Mon Sep 17 00:00:00 2001 From: palmoni5 Date: Wed, 11 Nov 2026 19:07:57 +0200 Subject: [PATCH] perf(markdown): flatten nested Element.textContent iteratively Otzaria/tools#39 --- pkgs/markdown/CHANGELOG.md | 5 +++ pkgs/markdown/lib/src/ast.dart | 24 ++++++++-- pkgs/markdown/test/ast_text_content_test.dart | 45 +++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 pkgs/markdown/test/ast_text_content_test.dart diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index ea3564288d..eed8ebab2a 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -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`. @@ -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 diff --git a/pkgs/markdown/lib/src/ast.dart b/pkgs/markdown/lib/src/ast.dart index 64684d8d27..7623714ca0 100644 --- a/pkgs/markdown/lib/src/ast.dart +++ b/pkgs/markdown/lib/src/ast.dart @@ -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 = >[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(); } } diff --git a/pkgs/markdown/test/ast_text_content_test.dart b/pkgs/markdown/test/ast_text_content_test.dart new file mode 100644 index 0000000000..3eda47de61 --- /dev/null +++ b/pkgs/markdown/test/ast_text_content_test.dart @@ -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'; +}