Skip to content

fix(muya): regex search freezes on zero-width patterns and splits emoji - #5333

Merged
Jocs merged 5 commits into
developfrom
fix/muya-regex-search-matches
Sep 14, 2026
Merged

Jocs merged 5 commits into
developfrom
fix/muya-regex-search-matches

Conversation

@Jocs

@Jocs Jocs commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Related to #4926 (same crash, different entry point). Found while verifying #4931 / #5332; no existing issue.

Summary

Search and replace bugs in matchString (packages/muya/src/utils/search.ts), all reachable from the desktop find bar: a freeze and an emoji-splitting crash with the regex toggle on, and replace breaking multi-code-point characters apart. Plus two cleanup commits that drop the execall dependency the first fix made unused.

1. Zero-width patterns freeze the editor

matchString collected matches with execall, which calls exec until it returns null. A zero-width match leaves a global RegExp's lastIndex where it was, so exec returns the same empty match forever.

The desktop find bar already refuses patterns that match the empty string (RegExp(value).test('')), which covers ^, $ and x*, but a zero-width pattern that doesn't match '' still goes through:

Regex search in the built desktop app (abc / xyz) develop this PR
a responsive, 1 / 1 responsive, 1 / 1
\b renderer frozen (no response in 5 s) responsive, 1 / 4
(?=b) renderer frozen responsive, 1 / 1

Fix: our own exec loop that steps lastIndex past an empty match.

2. . / [^…] split emoji, and Replace crashes

User regexps were compiled without the u flag, so . and character classes match single UTF-16 code units. Finding [^a-z] in abc🙂 matched each half of the emoji; Replace then left a lone surrogate (abcx\ude42), and the next state flush threw Invalid offset - splits unicode bytes — the #4926 crash through a different door. In real Chrome on develop (with #4931):

Regex Action develop this PR
. replace the 4th match abcx\ude42, crash abcx
[^a-z] replace one abcx\ude42, crash abcx
c. replace one abC\ude42, crash abC
. replace all xxxxx (5 for 4 characters) xxxx
literal 🙂 replace one abcx abcx

Fix:

  • Compile user patterns with the u flag so they match whole code points.
  • Fall back to the plain flags for patterns that are only valid without it (identity escapes such as \- are a SyntaxError in Unicode mode), so existing patterns keep working.
  • In Unicode mode, step a zero-width match over a whole pair (a Unicode RegExp resumes from the start of a pair, so bumping lastIndex by one would loop again).
  • A fallback match that would still cut an emoji is handled by the grapheme alignment in commit 5.

3–4. Drop the unused execall dependency

  • chore(muya): @muyajs/core no longer imports execall; remove it from packages/muya/package.json.
  • chore(desktop): nothing in the desktop package imports it either (only a leftover declare module 'execall' shim); remove the dependency and the shim.

packages/muyajs (legacy engine) still imports execall and keeps its own dependency, so the package stays in the lockfile; each commit only removes that importer's 3-line entry from pnpm-lock.yaml (pnpm install --lockfile-only produces the identical file).

5. A grapheme cluster is replaced as one unit

Even with u, a regexp matches code points, and 🧑‍🧑‍🧒‍🧒 is one character on screen but 7 code points (four people joined by ZWJs). . or [^a-z] matched each piece and Replace broke the emoji apart; a literal search for 🧑 also matched inside it.

Matches are now aligned to grapheme cluster boundaries (Intl.Segmenter, via the graphemeClusters helper from #4931):

  • Regex: a match reaching into a cluster expands to the whole cluster; expansions that overlap merge, so each character is replaced once.
  • Literal: a match covering only part of a cluster is dropped — the query didn't find that character (expanding would let replacing 🧑 wipe out the whole family).
  • A zero-width match inside a cluster is dropped, since inserting there would split it.

On 🧑‍🧑‍🧒‍🧒z in Chromium and WebKit:

Search Before this commit After
regex ., replace all with x 8 matches → xxxxxxxx 2 matches → xx
regex [^a-z], replace one x‍🧑‍🧒‍🧒z xz
literal 🧑 2 matches inside the family none
literal 🧑‍🧑‍🧒‍🧒 / z correct unchanged

Segmentation only runs for a block that has matches and contains a character at or above U+0300 (or a CR), so plain ASCII search is unaffected. This alignment also subsumes the lone-surrogate filter from commit 2.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added
    • utils/__tests__/search.spec.ts: zero-width patterns (\b, (?=b), an empty alternative inside an emoji), surrogate pairs (., [^a-z], c.), the \- fallback, and grapheme clusters (regex . / [^a-z] / .{2} on a ZWJ family, a base letter with a combining mark, a zero-width match inside a cluster, literal partial matches dropped, literal whole cluster found). RegExp#exec is capped at 1000 calls so a looping regression fails instead of hanging the runner. Every new case except the \- and whole-cluster controls fails without its fix.
    • search/__tests__/search.spec.ts: search('[^a-z]') + replace('x') on abc🙂 yields abcx; regex . replace-all on 🧑‍🧑‍🧒‍🧒z yields xx; a literal 🧑 search inside the family finds nothing.
  • muya: vitest run 224 files / 1570 tests pass; lint (no new warnings), lint:types, check-circular pass.
  • Real browsers (muya e2e host, Chromium + WebKit): the replace tables above, and ^, \b, x*, q?, (?=b) searches finish in a few hundred exec calls (develop: unbounded).
  • Built desktop app (build:unpack + Playwright/Electron): the find-bar freeze table above, plus regex Replace of [^a-z] in abc🙂 → abcx with no renderer errors (develop: abcx\ude42 + Invalid offset - splits unicode bytes).
  • Manually tested on: macOS (desktop dev build, including the ZWJ family cases)

Notes for reviewers [optional]

  • The execall commits: pnpm install --frozen-lockfile, desktop typecheck / build:unpack / validate-licenses pass. THIRD-PARTY-LICENSES.txt is not touched: regenerating it still lists execall (bundled via @marktext/muyajs), and the checked-in file is already out of date with unrelated entries, so a regeneration belongs in its own change.
  • With u, case-insensitive regex search uses Unicode case folding (e.g. /k/iu also matches the Kelvin sign K). That's the intended semantics for a Unicode-aware search.
  • Behaviour change from commit 5 for decomposed text: a literal e no longer matches the e of e + U+0301, while regex e matches the whole é.

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn

Jocs and others added 4 commits September 14, 2026 11:42
`matchString` looped over `execall`, which keeps calling `exec` until it
returns null. A zero-width match leaves a global RegExp's `lastIndex` where
it was, so a pattern such as `\b` or `(?=b)` returned the same empty match
forever. The desktop find bar only rejects patterns that match the empty
string, so enabling regex and searching `\b` or `(?=b)` froze the editor.

Collect matches with our own loop that bumps `lastIndex` after an empty
match.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
A user regexp was compiled without the Unicode flag, so `.`, `[^a-z]` and
friends matched single UTF-16 code units. With the regex toggle on, finding
`[^a-z]` in `abc🙂` matched each half of the emoji, and "Replace" left a lone
surrogate in the block text: the next state flush threw "Invalid offset -
splits unicode bytes", the same crash as #4926.

Compile user patterns with the `u` flag so they match whole code points,
falling back to the plain flags for patterns that are only valid without it
(such as `\-`). In that fallback, drop matches that start or end inside a
surrogate pair, and step a zero-width match over a whole pair in Unicode
mode so `exec` does not resume on it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
`matchString` now collects matches with its own `exec` loop, so
@muyajs/core no longer imports `execall`. The package stays in the lockfile
for the desktop and legacy muyajs packages.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
Nothing in the desktop package imports `execall`; only a leftover module
shim declared it. The legacy muyajs package still imports it and keeps its
own dependency.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Website preview ready: https://pr-5333-marktext-website.ransixi.workers.dev

Built from 0c850737cc3e573583a97809fec8b60f42fc7a77 · Worker: marktext-website · Version: 7f309f0d-1a36-43a0-8f8b-5a4351121005 · Alias: pr-5333

@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Build artifacts for PR #5333:

Run: https://github.com/marktext/marktext/actions/runs/34809362643

Artifact Size Link
marktext-linux 637.9 MB Download
marktext-windows-x64 287.6 MB Download
marktext-macos-arm64 287.3 MB Download
marktext-windows-arm64 279.4 MB Download
marktext-macos-x64 297.6 MB Download

Regex search matched code points, so `.` or `[^a-z]` matched each of the seven
code points of `🧑‍🧑‍🧒‍🧒` and Replace broke the emoji apart; a literal search
for `🧑` also matched inside it.

A grapheme cluster (an emoji ZWJ sequence, a letter with combining marks) is
one character on screen, so align matches to cluster boundaries: a literal
match covering only part of a cluster is dropped, a regexp match reaching into
one expands to the whole cluster (overlapping expansions merge), and a
zero-width match inside a cluster is dropped. This also covers the lone
surrogate case the previous filter handled.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
@Jocs
Jocs merged commit 10f4329 into develop Sep 14, 2026
18 checks passed
@Jocs
Jocs deleted the fix/muya-regex-search-matches branch September 14, 2026 05:34
@Jocs Jocs mentioned this pull request Nov 4, 2026
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.

1 participant