Skip to content

fix: commit a projected escape as the text it spells - #268

Merged
Azganoth merged 1 commit into
mainfrom
bug/projected-escape-literal
Aug 18, 2026
Merged

fix: commit a projected escape as the text it spells#268
Azganoth merged 1 commit into
mainfrom
bug/projected-escape-literal

Conversation

@Azganoth

Copy link
Copy Markdown
Owner

Summary

A backslash typed into projected link source turned the link literal, which the specification asks for, but the backslash itself landed in the document as an ordinary character. The run then saved with three backslashes where one was enough, and the document differed from the same file authored by hand.

The literal commit now reads the source as the text it spells rather than as the characters it is written with, which is what a file holding those same characters would hold. decodeSourceProjectionEscapes consumes a backslash before CommonMark's escapable punctuation and keeps every other backslash, so a \ b and C:\Users\me still reach the document intact.

  • The fix is defined by outcome rather than by position, so it also covers a backslash typed before the destination: [a]\(b) and \[a](b) produce the same document, exactly as the two files do.
  • The footnote-reference adapter shared the defect through the same commit shape and is fixed with it. A backslash typed into projected link source becomes literal text instead of an escape #265 lists the other adapters as unprobed rather than out of scope; the mark adapters do not share it, measured and recorded in this comment.
  • Consuming a character shortens the committed text, so the literal branch of selection mapping maps through the decode instead of assuming the document and its source share offsets.

An escaped backslash is where this stops short of what the file means. \\[a](b) in a file is a literal backslash followed by a live link; committed out of projection it spells one backslash and leaves the run literal, because committing an object out of the invalid path is the one thing the projection rule forbids.

Related Issue

Closes #265
Refs #238, #255

Verification

src/features/editor/plugins/sourceProjection.test.tsx gains five cases in source editing. A backslash typed at a link's source start and one typed before its destination each produce a document equal to mounting \[a](b) tail, compared as ProseMirror documents rather than as strings, and each saves as \[a](b) tail. The same holds for a footnote reference against text\[^a] tail. A backslash the author means as text survives, and an escaped backslash spells one character. Four of the five fail with the adapters stashed and the tests otherwise untouched; the fifth is the guard against consuming too much, and passes either way.

src/features/editor/tests/sourceProjectionClipboard.test.tsx pins the consequence for an in-app copy: the editor flavor of escaped invalid source carries the text it spells, while the plain-text flavor keeps the exact projected characters the event writes from the selection.

The corpus round trip and the rest of the editor suites are unchanged.

Not verified: no manual pass in the Tauri application. The gesture is text input and serialization, both driven directly by the editor mount.

Notes

docs/specification.md said invalid edits become exact literal text and docs/decisions.md said no projected character is lost. Both now carry the escape rule, since this drops the backslash deliberately.

Files already saved with the extra backslashes are not repaired. \\\[a](b) reopens as a document holding the backslash and saves back unchanged, so an author who hit this deletes the stray character by hand.

This does not touch what #246 does with the run afterwards. The literal commit now lands text that is exactly valid link source inside a range that branch records as written and does not protect, so the escape is spent on the next caret move; measured there and recorded in that pull request. The protection belongs with whichever of the two lands second, conditional on the fallback having consumed an escape rather than blanket over every literal commit.

@Azganoth Azganoth added the Bug Something isn't working label Aug 18, 2026
@Azganoth Azganoth self-assigned this Aug 18, 2026
An edit that stopped being valid committed the raw source, so a backslash
typed to escape a run landed in the document as an ordinary character and
saved as an escaped backslash beside the escape the text still needed.

The literal commit now decodes escapes the way a file holding those same
characters would. An escaped backslash spells one backslash and leaves the
run literal rather than reviving the link such a file would carry, since
committing an object out of the invalid path is what the rule forbids.
@Azganoth
Azganoth force-pushed the bug/projected-escape-literal branch from 93359fb to 5513894 Compare August 18, 2026 03:15
@Azganoth
Azganoth enabled auto-merge (squash) August 18, 2026 03:15
@Azganoth
Azganoth merged commit 6a6942e into main Aug 18, 2026
3 checks passed
@Azganoth
Azganoth deleted the bug/projected-escape-literal branch August 18, 2026 03:19
Azganoth added a commit that referenced this pull request Aug 18, 2026
## Summary

Typing `[a](b)`, `https://example.com`, or `<https://example.com>` left
literal text while pasting the same characters created a link. A run of
text that is exactly one link's source now becomes that link when the
caret leaves it, for inline links, autolink literals, and URI autolinks.

- The run is never projected. It is already the source, so the caret
sitting in it changes nothing; the link adapter validates it through the
same `parseLinkSource` that already turns edited projected source into a
link, and the engine's existing `appendTransaction` trigger commits it.
- The caret has left once whitespace, a line break, or another block
separates it from the run. Until then the next character can still move
where the source ends, which is what keeps a bare URL whole while it is
typed: the parser reads `https://example.com/path.` as a link stopping
before the dot the author is still typing.
- Only source the session wrote commits, and never a run the file
escaped. A file can hold escaped source because its author wanted the
characters, and the escape does not survive parsing, so the document
alone cannot tell the two apart. The engine records the ranges the
session writes, and the source run a write lands in while the document
there still reads as the file wrote it; a commit needs the first and
must avoid the second.
- A run an escape spent in projection also stays literal. #265 makes a
backslash typed into projected source commit the run as the text it
spells, and that text is exactly the source this rule commits, written
by the session and named by nothing else. The commit that spends an
escape records the range it wrote, which puts the run where a
file-escaped one already sits.
- History clears that record, so an undone commit stays undone rather
than returning on the next caret move.
- A committed link projects its source again whenever the caret returns,
so the visible text never changes.

## Related Issue

Closes #239
Refs #238, #265

## Verification

Focused tests in `sourceProjectionTypedLink.test.tsx` cover each form
committing when the caret leaves it and when the sentence continues past
it; a typed URL committing once at its full length; `Enter` and
`Shift+Enter`; backslash-escaped, incomplete, and code-block source
staying literal; typing and plain-text paste of the same characters
reaching the same document; a committed link projecting its source
again, and the link the caret lands in projecting while the run it left
commits.

Nine tests pin the escape hatch. Source the file escaped stays literal
through a caret visit, through an edit elsewhere in its paragraph,
through an edit inside it, through one change that writes into it twice,
and through a word dropped into it by either a move or a copy, while
replacing it outright commits it. Source an escape spent in projection
stays literal through repeated caret moves and through an edit inside
it, matching what the file-escaped run does. One guards the gesture that
the strict alternative would have broken: source written by hand around
words the file already held still commits. One pins reversal: `Undo`
returns a committed link to the source it was written as, and a caret
move afterwards leaves it reverted.

`markdownCompatibility.test.tsx` carries #234's typed-source fixtures
forward. With the caret still on the source, all three forms still
serialize escaped; once a space follows, the same fixtures assert the
live form, which is the behavior this change accepts.

Nine assertions in `sourceProjectionTypedLink.test.tsx` moved to the
single escape #263 now writes, from `\[test link]\(./test.html)` to
`\[test link](./test.html)`. Each still asserts what it always did, that
the run stays literal, since the subject is literalness rather than
which character carries the escape. The label split by a line ending
keeps its pair, where the opening bracket sits in an earlier sibling
than that analysis reads.

Measured the added cost against `main` by driving the editor mount used
by the plugin tests, medians of seven 120-operation batches per
workload, each in its own mount (ms per keystroke or caret move):

| Workload | `main` | This branch |
| --- | --- | --- |
| Typing in a 600-character prose paragraph | 0.140 | 0.150 |
| Typing in a paragraph holding link syntax | 0.098 | 0.196 |
| Typing in a 10k-character paragraph whose link syntax is far from the
caret | 0.066 | 0.078 |
| Typing in a paragraph of 200 links | 1.194 | 1.191 |
| Caret moves through an edited document | 0.204 | 0.277 |
| First character written into an untouched region | 0.310 | 0.694 |

Not verified: those figures were taken at `f0d1da57`, before the rebase
onto #263, #267, and #268, and they were not taken again after it. What
those releases change is what the serializer writes and what reaches
projected source, while the scan these numbers describe parses and is
untouched. Also not verified: the desktop E2E suite and manual
verification in the Tauri application. The rest of the change is
document text and serialization, both observable in the automated DOM
tests.

## Notes

Caret-leave is the only trigger, including when the caret leaves by
writing. Typing a space after `[a](b)` commits it, because that space is
what puts the caret off the run; without that, source typed mid-sentence
would never commit, since the caret leaves such a run by writing rather
than by moving. #239 rules out an eager commit on a boundary character,
and no boundary character is a trigger here: the same separation rule
decides every case.

`Enter` and `Shift+Enter` commit through that rule rather than through
`finalizeSourceProjection`, which serves an active session and there is
none. The line break separates the caret from the run, so the behavior
#239 asks for holds without a second path.

The escape-hatch tests exist because the first implementation broke
#238's shared decision that `\[a](b)` round-trips as literal text: a
caret passing through `corpus/commonmark/links-and-images.md`'s
`\[intentionally literal](garden.md)` converted it and the save rewrote
the file. Requiring the whole run to be written would close that hole
too, but it refuses source wrapped by hand around words the file already
held, so the run a write lands in is recorded instead. Recognizing it
costs the extra time in the last row of the table, once per region
rather than once per keystroke. It locates the change by its two ends,
which name the same place in the document before and after it however
many steps ran, and stands down under an active projection, where the
source belongs to the engine rather than to the file.

Standing down there is also why an escape spent in projection needs the
commit to report itself. The engine writes that run while its own
session is still open, so nothing classifies it as source a file holds,
and the record the commit leaves is what a later caret move reads. The
condition is that the commit consumed an escape rather than that it
committed literal text at all: an author who edits projected source into
`[a](b) x` typed those characters, and committing that run is this rule
working rather than failing.

An edit inside a protected run leaves it protected here, as it already
did for source the file escaped, and only replacing the run outright
commits it. If writing into a run was meant to spend its escape instead,
the assertion that pins it is the one to change.

Before parsing a text block, the adapter looks for link markers within
1000 characters of the caret. That radius is a ceiling rather than a
guarantee: source whose markers all sit further away, or a form the
marker pattern does not describe, stays the literal text it already was.
Without it, a URL anywhere in a 10k-character paragraph cost 1.2 ms of
parsing per keystroke; the table above is measured with it.

The written-range record grows with the number of distinct places a
session edits, not with the number of edits — continuous typing merges
into one range. Two thousand scattered single-character edits leave 1911
ranges and add about 0.1 ms per keystroke. It is uncapped: every safe
drop policy loses coverage, and the cost of growth is gradual rather
than a cliff, while dropping the wrong range would withhold a commit the
author expects.

What the engine now knows, the author cannot see. A run the file escaped
and a run typed a moment ago read identically, yet only one of them
commits, and the escape has no character to remove: converting a
protected run means deleting it and retyping it. The consequence reaches
the clipboard too. An in-app copy carries characters rather than
literalness, so pasting a copy of literal link source now commits it to
a link where it stayed literal before, and the Markdown flavor of such a
copy is written unescaped whatever this branch does. #255 supplies the
gesture that answers the first half, and #244 covers the clipboard
writing live source. Neither is a precondition for this change: the
protections here hold without them, and both were reachable before it.

The changelog's #234 entry claimed typed source stays literal when a
space follows it. It now describes the escaping fix that entry was
really about, since the space case is what this change alters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A backslash typed into projected link source becomes literal text instead of an escape

1 participant