Behaviour SwRS-MDOKFSTUDIO-TOOLBAR-012
Replace edits only the matched source spans
When the reader replaces matches from the find bar, the extension SHALL replace the source span of each match that maps one-to-one to the source, and no other text, in one undo step; the extension SHALL refuse every replacement with the reason, without editing the document, while a block editor is open (the find bar keeps finding), and, while the find bar holds a query and its replace row is open, count in its note, before any replacement, the matches that cannot be replaced from the preview.
Step 3 of path 1 in StRS-MDOKFSTUDIO-TOOLBAR-003: from “The matches are walked” to “The text is replaced in the document”.
Scenarios 7
The situations the tests check; open one to read it.
nominalReplace one and replace all
Giventhe bar holds a query with matches in a heading and two paragraphs, a replacement text and a preview
Whenthe reader presses Replace (or Enter in the replace field), or Replace all (Cmd/Ctrl+Alt+Enter)
Thenthe page sends, for each match, the source lines of its block, the matched text, which occurrence of it that is, and the final replacement text (with the "$1" groups of a regular expression expanded); the host replaces exactly those source spans, all in one undo step, and answers with the number replaced and skipped
boundaryMatches that do not map one-to-one to the source
| Input | Outcome |
|---|---|
| A match in a link label whose target or title repeats the text (`[needle](https://example.com/needle)`) | Replaced; the target and the title are kept: link and image targets and titles, a whole inline image, alt text included (the page does not show it) and reference definition lines (`[label]: url`) are left out of the source occurrences |
| The text inside an inline image's alt text or a reference definition | Neither counted nor replaced |
| The text of an autolink (`<https://example.com/needle>`) or of a link written in a code span | Counted and replaced: the reader sees it |
| A link or a reference definition inside a fenced code block (``` or ~~~), or a definition line indented by 4 spaces or a tab | Counted as code text; the fence's info string is counted too, so a match whose block shows fewer occurrences than its source is skipped |
| A link written inside the label of another link | Only the inner link's target is left out: Markdown shows the outer target as text |
| A match in a formula, in generated text (an admonition title), or across markup | Skipped: the visible source holds another number of occurrences than the page shows |
| A match in the front matter, an included file or outside a mapped block | Not sent; counted by the page |
| A matched line break of a wrapped paragraph | The source span runs over the line break and the whole span is replaced |
| Two edits that overlap | The later one is skipped |
| Every match skipped | The host refuses with a reason and the document is unchanged |
boundaryThe note and the rest of the file
Givena replacement in which the page or the host skipped N matches
Whenthe answer arrives
Thenthe note of the bar reads "Replaced R matches; N matches cannot be replaced from the preview", and everything outside the replaced spans is byte-identical, the host having checked the lines and the revision again
failureOpen editor
Givena preview with a block editor open, whatever the trust
Whenthe reader opens the replace row or presses Replace or Replace all
Thenthe replace controls are marked aria-disabled with the reason as title, the note shows the reason, the host is not asked and the document is unchanged; finding keeps working
failureThe host refuses
Givena request that reaches the host with a Markdown edit open or from an older render
Whenthe host handles it
Thenit answers with the reason and does not read or change the document
nominalThe note counts the skipped matches before the click
Givena preview, the replace row open, and a query with matches of which the page cannot map some (front matter, an included file, a formula) and the host would skip others (an admonition title, markup inside the match)
Whenthe reader types the query, opens the replace row, or the content is refreshed
Then150 ms after the last of these the page asks the host to plan Replace all of every mapped match without applying it (the `previewFindReplace` request with `dryRun: true` and an empty replacement text: the counts do not depend on it), the host answers the counts of the plan without editing the document, and the note reads "N matches cannot be replaced from the preview", N being the page's unmapped matches plus the host's skipped ones
boundaryWhen the page does not ask, and stale or refused answers
| Input | Outcome |
|---|---|
| The replace row is closed (the reader only searches) | No request; the note counts the page's unmapped matches only |
| The replace row is closed again | The host's count is dropped; the note counts the page's unmapped matches only |
| Another search, an option toggle or a content refresh before the answer | The older answer is ignored; a new request follows 150 ms later |
| Several searches within 150 ms | One request, for the last one |
| An open edit | No request from the page; the host refuses a dry run with its reason, and the note keeps the page's count |
| The host refuses or the request times out | The note keeps the page's count |
| No mapped match maps to the source | The host answers every sent match as skipped |
| A replacement in flight | No request; the next search after it asks again |
| A replacement text longer than a replace accepts | The request is sent all the same (it holds no replacement text); the click refuses the replacement as too long |
| The reader clicks Replace or Replace all | The note after the answer still reads "Replaced R matches; N matches cannot be replaced from the preview" (SwRS-MDOKFSTUDIO-TOOLBAR-012) |
Implemented in 5
Tested by 8
- test/core/edits/findReplace.test.tsunit
- test/core/find/findMatches.test.tsunit
- test/core/protocol/pageToHost.test.tsunit
- test/integration/findInPreview.test.tsintegration
- test/integration/realInput/qa210Replays.tsintegration
- test/page/modules/findBar.test.tsunit
- test/session/findReplace.test.tsunit
- test/visual/capture_find_checks.pyvisual
Replaces
SwRS-MDOKFSTUDIO-TOOLBAR-014Replace is unavailable while a block editor is open superseded superseded by SwRS-MDOKFSTUDIO-TOOLBAR-012 Markdown file
SwRS-MDOKFSTUDIO-TOOLBAR-131Replace all announces the matches it will skip superseded superseded by SwRS-MDOKFSTUDIO-TOOLBAR-012 Markdown file