Skip to content

fix: stop Document.getPlainText repeating content past document length - #2752

Open
Yusufihsangorgel wants to merge 2 commits into
singerdmx:masterfrom
Yusufihsangorgel:fix/2704-getplaintext-repeats-past-length
Open

fix: stop Document.getPlainText repeating content past document length#2752
Yusufihsangorgel wants to merge 2 commits into
singerdmx:masterfrom
Yusufihsangorgel:fix/2704-getplaintext-repeats-past-length

Conversation

@Yusufihsangorgel

Copy link
Copy Markdown

Closes #2704.

The problem

Document.getPlainText(index, len) repeats the document's content when len is
larger than the document:

final doc = Document()..insert(0, 'This is string 1');
doc.getPlainText(0, 50);
// "This is string 1\nThis is string 1\nThis is st..."
// expected: "This is string 1\n"

A blank document repeats its trailing newline the same way.

Root cause

Line._getPlainText (lib/src/document/nodes/line.dart) wrapped its whole body
in while (len0 > 0). That body already traverses the entire reachable document:
the inner while walks the current line's leaves, and the nextLine recursion
walks every following line. Once the document was shorter than len, the outer
loop ran again and re-processed the same nodes from the original offset,
padding the result by repeating the content. It is now a single pass.

Tests

Added a "getPlainText beyond document length" group to
test/document/document_test.dart, covering an oversized len and a blank
document. Existing tests are unaffected: every current getPlainText assertion
requests len equal to the available content, so none hit the repeat branch.

A note on how I verified

My machine is on Flutter 3.41 and this package's editor widgets need 3.44, so I
verified on the v11.5.0 tag, where Line._getPlainText is identical to master.
There the repro fails before the fix and passes after it, and the document suite
is green (17 tests). The change itself is a single while becoming if, so CI
on 3.44 is the final gate.

Closes singerdmx#2704. Line._getPlainText wrapped its body in while (len0 > 0), but the
body already walks the whole reachable document (inner loop over this line's
leaves, nextLine recursion over following lines). When the document was shorter
than len, the outer loop re-processed the same nodes and repeated the content.
Make it a single pass. Verified on the v11.5.0 tag (this method byte-identical):
repro fails before, passes after; document suite green.
Place the singerdmx#2704 entry under the ### Fixed subsection in the repo's
keep-a-changelog prose style, instead of a bare bullet under [Unreleased].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document.getPlainText(index, len) has a weird bug where it may repeat the document content to satisfy the specified length in parameter.

1 participant