Skip to content

fix: validate getSibling's blockIndexMap index against the value#2878

Draft
christianhg wants to merge 1 commit into
mainfrom
fix-get-sibling-block-index-map-fallback
Draft

fix: validate getSibling's blockIndexMap index against the value#2878
christianhg wants to merge 1 commit into
mainfrom
fix-get-sibling-block-index-map-fallback

Conversation

@christianhg

@christianhg christianhg commented Jun 29, 2026

Copy link
Copy Markdown
Member

Found by the traversal audit that followed the getNode path-contract fix (#2876, EDEX-1425), same family: a util over-trusting blockIndexMap without the value fallback the rest of the traversal layer carries.

getSibling looked up the current node's index with blockIndexMap.get(serializePath(path)) and trusted it blindly, then indexed the value-derived sibling array (from getChildren) with it. blockIndexMap can miss (an unkeyed transient node) or disagree with the traversed value (a snapshot that pairs the live map with a pre-apply value, e.g. textPatch):

  • On a miss, the lookup returned undefined, so getSibling reported no sibling where one exists.
  • On a disagreement, the stale map index pointed at the wrong slot in the value-derived array, returning the wrong sibling.

getNode, getChildren, and getAncestors all take the map as a fast path and fall back to a value scan when it misses or disagrees; getSibling was the lone holdout trusting it unconditionally. It now validates the mapped index against the resolved children (does the child at that index carry the path's last keyed _key?) and scans only when it doesn't.

The O(1) lookup is preserved in the common case; the O(n) scan runs only on the stale-map snapshots. children was already materialized by getChildren (itself O(n)) to return the sibling node + path, so the fast path adds no asymptotic cost over what the function already did.

TDD: the new test (resolves the sibling from the value when the blockIndexMap misses) pairs the testbed value with an empty map and asserts next/previous/nested-span siblings still resolve. It fails on main (returns undefined) and passes with the fix; the existing 21 cases are unchanged.

Net behavior is unchanged when the map and value agree. 816 editor unit tests pass; types, lint, knip green.

@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5a5950f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@portabletext/editor Patch
@portabletext/plugin-character-pair-decorator Patch
@portabletext/plugin-dnd Patch
@portabletext/plugin-emoji-picker Patch
@portabletext/plugin-input-rule Patch
@portabletext/plugin-list-index Patch
@portabletext/plugin-markdown-shortcuts Patch
@portabletext/plugin-one-line Patch
@portabletext/plugin-paste-link Patch
@portabletext/plugin-sdk-value Patch
@portabletext/plugin-table Patch
@portabletext/plugin-typeahead-picker Patch
@portabletext/plugin-typography Patch
@portabletext/toolbar Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Jun 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
portable-text-editor-documentation Ready Ready Preview, Comment Jun 29, 2026 1:21pm
portable-text-example-basic Ready Ready Preview, Comment Jun 29, 2026 1:21pm
portable-text-playground Ready Ready Preview, Comment Jun 29, 2026 1:21pm

Request Review

@github-actions

github-actions Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

📦 Bundle Stats — @portabletext/editor

Compared against main (24996df6)

@portabletext/editor

Metric Value vs main (24996df)
Internal (raw) 792.6 KB +327 B, +0.0%
Internal (gzip) 151.5 KB +63 B, +0.0%
Bundled (raw) 1.40 MB +331 B, +0.0%
Bundled (gzip) 315.6 KB +75 B, +0.0%
Import time 95ms -1ms, -1.0%

@portabletext/editor/behaviors

Metric Value vs main (24996df)
Internal (raw) 467 B -
Internal (gzip) 207 B -
Bundled (raw) 424 B -
Bundled (gzip) 171 B -
Import time 2ms -0ms, -0.7%

@portabletext/editor/plugins

Metric Value vs main (24996df)
Internal (raw) 2.7 KB -
Internal (gzip) 894 B -
Bundled (raw) 2.5 KB -
Bundled (gzip) 827 B -
Import time 7ms -0ms, -1.4%

@portabletext/editor/selectors

Metric Value vs main (24996df)
Internal (raw) 79.7 KB +327 B, +0.4%
Internal (gzip) 14.6 KB +62 B, +0.4%
Bundled (raw) 75.2 KB +327 B, +0.4%
Bundled (gzip) 13.5 KB +73 B, +0.5%
Import time 8ms -0ms, -0.9%

@portabletext/editor/traversal

Metric Value vs main (24996df)
Internal (raw) 25.6 KB +327 B, +1.3%
Internal (gzip) 5.1 KB +63 B, +1.2%
Bundled (raw) 25.5 KB +327 B, +1.3%
Bundled (gzip) 5.0 KB +65 B, +1.3%
Import time 6ms +0ms, +3.0%

@portabletext/editor/utils

Metric Value vs main (24996df)
Internal (raw) 29.4 KB -
Internal (gzip) 6.2 KB -
Bundled (raw) 27.0 KB -
Bundled (gzip) 5.8 KB -
Import time 6ms +0ms, +0.3%

🗺️ . · ./behaviors · ./plugins · ./selectors · ./traversal · ./utils · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

📦 Bundle Stats — @portabletext/markdown

Compared against main (24996df6)

Metric Value vs main (24996df)
Internal (raw) 53.0 KB -
Internal (gzip) 9.6 KB -
Bundled (raw) 348.2 KB -
Bundled (gzip) 96.1 KB -
Import time 40ms -0ms, -0.7%

🗺️ View treemap · Artifacts

Details
  • Import time regressions over 10% are flagged with ⚠️
  • Sizes shown as raw / gzip 🗜️. Internal bytes = own code only. Total bytes = with all dependencies. Import time = Node.js cold-start median.

`getSibling` looked up the current node's index with
`blockIndexMap.get(serializePath(path))` and trusted it blindly, then
indexed the value-derived sibling array (from `getChildren`) with it.
`blockIndexMap` can miss (an unkeyed transient node) or disagree with the
traversed value (a snapshot that pairs the live map with a pre-apply
value, e.g. `textPatch`). On a miss the lookup returned `undefined`, so
`getSibling` reported no sibling where one exists; on a disagreement the
stale index pointed at the wrong slot in the value-derived array,
returning the wrong sibling.

`getNode`, `getChildren`, and `getAncestors` all take the map as a fast
path and fall back to a value scan when it misses or disagrees;
`getSibling` was the lone holdout trusting it unconditionally. It now
validates the mapped index against the resolved `children` (does the
child at that index carry the path's last keyed `_key`?) and scans only
when it doesn't. The O(1) lookup is preserved in the common case; the
O(n) scan runs only on the stale-map snapshots. `children` itself was
already materialized (and O(n)) to return the sibling, so the fast path
adds no asymptotic cost.

Surfaced by the traversal audit that followed the `getNode` path-contract
fix. Net behavior unchanged when the map and value agree.
@christianhg christianhg force-pushed the fix-get-sibling-block-index-map-fallback branch from 9ada9b3 to 5a5950f Compare June 29, 2026 13:19
@christianhg christianhg changed the title fix: resolve getSibling from the value rather than blockIndexMap fix: validate getSibling's blockIndexMap index against the value Jun 29, 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