Skip to content

fix(muya): start an image resize only from the resize bar's own handles (#5116) - #5382

Merged
Jocs merged 3 commits into
developfrom
fix/5116-resize-bar-foreign-handles
Sep 16, 2026
Merged

Jocs merged 3 commits into
developfrom
fix/5116-resize-bar-foreign-handles

Conversation

@Jocs

@Jocs Jocs commented Sep 15, 2026

Copy link
Copy Markdown
Member

Summary

The image resize bar started a resize on a mousedown on any element with class bar anywhere in the page, not only on its own two handles. Document content can have that class: inline HTML (<span class="bar">), an HTML block, or a mermaid node styled with classDef bar. On v0.20.0-rc.3 and develop:

This PR makes a resize start only from the bar's own handles. It also makes a drag without a left or right handle compute no width.

How this relates to the two reports:

Steps to reproduce

Desktop app, packaged v0.20.0-rc.3 or a develop (8bcb1e0) build:

  1. Create doc.md next to any pic.png:
    ![](pic.png)
    
    Inline <span class="bar">progress bar</span> here.
    
    End text.
  2. Open it. Click the image so the resize handles appear, then click in "End text." so they disappear.
  3. Press the mouse on "progress bar", drag to select, and release. Then save.

Actual: every mouse move while the button is held raises Cannot read properties of null (reading 'style') (at _update < _mouseMove), and nothing is selected. After release the image line is <img src="pic.png" title="" alt="" width="NaN" />, and saving writes that line to disk.

Expected: a plain text selection, with the document unchanged.

Variants (each measured on rc.3 and on the develop build):

  • Skip the image click in step 2. Every move throws Cannot read properties of null (reading 'querySelector') at _mouseMove ([Bug] Unexpected error with message Cannot read properties of null (reading 'querySelector') #5116's message). The markdown is unchanged.
  • Handles still showing (don't click "End text."). No error, but width="NaN" is written silently.
  • HTML block or mermaid label. An HTML block <div class="bar">block bar</div>, or a mermaid label (A[Start here]:::bar --> B[Finish] with classDef bar fill:#f96), gives the same result as the main steps.
  • Right press on "progress bar" (the context menu takes the mouseup):
  • Image nested in the span (Start <span class="bar">![](pic.png)</span> tail text.). Clicking the image and then pressing and dragging on it writes width="NaN" silently.
  • Two tabs. Click the image in tab 1, switch to a tab 2 that holds the span, and drag over "progress bar":
    • Every move throws 'style'.
    • The release throws Cannot destructure property 'path' of 'this.parent' as it is null. from ParagraphContent.updateImage on tab 1's detached block.
    • Neither tab's markdown changes.

Root cause

ImageResizeBar._mouseDown listens on document.body and checked only event.target.closest('.bar'). A press on content therefore attached the drag's mousemove and mouseup listeners, with _movingAnchor null because content has no data-position.

_mouseMove computes a width only for a left or right anchor. For any other anchor:

  • No image clicked yet: _reference is null, so _reference.querySelector throws.
  • Image clicked, handles hidden: _reference is stale. The width stays '', Number.parseInt('') gives NaN, NaN is written to the <img>, and _update throws because the handles are gone.
  • Handles shown: _update succeeds, so there is no error.

In both image cases, _mouseUp saw typeof NaN === 'number' and committed width="NaN" through updateImage. A right press never delivers its mouseup to the page, so its listeners stayed attached.

closest('.bar') here was the only class matcher on a document-wide listener in packages/muya/src/ui:

  • paragraphQuickInsertMenu's closest('.item') listens on its own container and already checks container.contains(...).
  • tooltip's closest('[data-tooltip]') is not imported anywhere.

The legacy packages/muyajs transformer has the same pattern with .circle. The desktop app no longer uses it, and it is unchanged.

Fix

  • _mouseDown starts a resize only when the pressed .bar is inside this bar's own .mu-transformer container, and it reads the handle side from that element. Handles exist only between _render() and hide(), so this also implies the bar is showing for an image.
  • _mouseMove returns for any anchor other than left/right (default: return). width is now typed number, so the compiler rejects reading it on a path that computed none.

Behaviour changes

  • A press on content with class bar is now an ordinary press. In the steps above, drag-selecting selects "progress bar" again, and the press no longer hides the image toolbar.
  • The bar's own handles are unchanged: in the real app a right-handle drag still resizes 400 → 319 px, and Save writes width="319".

Known gaps

  • The reporters' own triggers are unproven.
  • The right-press variant was emulated: Menu.popup was stubbed and the mouseup withheld through CDP. A real NSMenu was not driven.
  • Not run: Windows, Linux, and the Firefox/WebKit e2e projects.

Test plan

  • Unit imageResizeBar.spec.ts: 6 new tests.
    • Content .bar with no image clicked, after the bar hides, and while it is shown.
    • A content press with no mouseup attaches no body mousemove listener.
    • A handle without a side does not resize.
    • The bar's own handle commits the computed width ('195' for clientX 200).
    • With develop's index.ts, 5 fail; the own-handle test passes.
    • With only the container check removed, 2 fail (no image clicked, and no listener after a press).
    • With only default: return removed, or with commit 1's index.ts, the no-side test fails; tsc also reports TS2454 when default: return is removed.
  • e2e tests/drag/image-resize-bar-foreign-bar-5116.spec.ts (Chromium): 4 of 5 fail with develop's index.ts, and 5/5 pass with this PR.
  • muya e2e tests/drag in Chromium: 21/21 pass.
  • Two-editor probe (muya e2e host, Chromium; a second editor on the page, dragging the first editor's handle):
    • With develop's index.ts: 8× getBoundingClientRect when the second editor's bar was shown then hidden, and 8× querySelector when it was never shown.
    • With this PR: 0.
  • pnpm -C packages/muya test: 234 files, 1669 tests pass.
  • pnpm -C packages/muya lint:types; eslint on the changed files (e2e with --no-ignore); pnpm -C packages/muya check-circular.
  • Real app, Playwright-driven. Each run used a fresh --user-data-dir and MARKTEXT_ERROR_INTERACTION=1, and a positive-control error reached the main-process sink first.
    • Packaged v0.20.0-rc.3 and a develop build:unpack reproduce the main steps (including width="NaN" on disk) and every variant above.
    • This PR's build:unpack gives 0 errors and unchanged markdown in all of them. That includes both right-press variants and the nested-image and two-tab variants. The own-handle resize still commits and saves width="319".
  • Firefox / WebKit e2e
  • Windows / Linux

Fixes #5116
Refs #5352

🤖 Generated with Claude Code

Jocs and others added 3 commits September 15, 2026 20:02
…es (#5116)

The resize bar listens for mousedown on the whole document body and began
a resize for any element inside `.bar`. Document content can carry that
class too: inline raw HTML (`<span class="bar">`), an HTML block, or a
mermaid node styled with `classDef bar`. Pressing and drag-selecting over
such content attached the resize drag listeners, so every mouse move threw
"Cannot read properties of null (reading 'querySelector')" when no image had
been clicked, or "(reading 'style')" after an image had been clicked and its
handles hidden. Because the press carried no handle side, the computed
width was NaN, and releasing the mouse rewrote the last clicked image as
`<img ... width="NaN" />`, even while its handles were still showing. A
right press on such content never gets its mouseup (the context menu takes
it), so the listeners stayed attached and every later hover threw.

Start a resize only when the pressed `.bar` lies inside this bar's own
`.mu-transformer` container, and read the handle side from that element.
With several editors on one page, a press on one editor's handle no
longer starts a drag in the others, which threw the same way. The
container check also covers the bar's shown state: its handles exist
only between `_render()` and `hide()`.

The unit spec now detaches each bar's document listeners after every test,
so a bar left over from one test cannot react to the next test's events.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`_mouseMove` computes a width only for a `left` or `right` handle. For any
other drag the width stayed `''`, `Number.parseInt('')` produced NaN, and
that was written to the image element and, because
`typeof NaN === 'number'`, committed to the markdown as `width="NaN"` on
mouseup. That is how a press on content with class "bar" corrupted the
image in #5116.

The previous commit stops such presses from starting a resize. Return
from any move without a handle side as well, so no other route can store
NaN in the document. With `width` typed as a number, the compiler rejects
reading it on a path that computed none.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Drag-select over inline raw HTML, an HTML block and a mermaid node that
carry the resize handles' `bar` class: with no image clicked, after an
image was clicked and its handles hidden, and while the handles are
shown. None may throw or change the image's markdown, and the bar's own
handle must still commit a width.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Jocs
Jocs merged commit 6356e01 into develop Sep 16, 2026
7 checks passed
@Jocs
Jocs deleted the fix/5116-resize-bar-foreign-handles branch September 16, 2026 01:14
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.

[Bug] Unexpected error with message Cannot read properties of null (reading 'querySelector')

1 participant