Skip to content

fix(block-editor): preserve hard breaks on load for restricted fields - #36600

Merged
oidacra merged 2 commits into
mainfrom
issue-35985-block-editor-strips-hard-breaks-shift-enter-when
Jul 15, 2026
Merged

fix(block-editor): preserve hard breaks on load for restricted fields#36600
oidacra merged 2 commits into
mainfrom
issue-35985-block-editor-strips-hard-breaks-shift-enter-when

Conversation

@oidacra

@oidacra oidacra commented Jul 15, 2026

Copy link
Copy Markdown
Member

Summary

CleanShot 2026-07-15 at 16 23 16@2x

The legacy Block Editor silently stripped hardBreak (Shift+Enter) nodes when re-opening content for editing on fields configured with allowed-block restrictions. setEditorJSONContent runs loaded JSON through removeInvalidNodespurifyNodeTree, which drops any node whose type isn't in the allowed map. hardBreak was never in the map, so every line break was lost on load — causing silent formatting/data loss on the next save.

This adds hardBreak to the always-allowed basicNodes map and fixes the latent paragrahparagraph typo in the same map (the paragraph basic-node fallback was previously dead code).

Only affects fields with allowedBlocks.length > 1; unrestricted fields never entered the filtering path. The new Block Editor (@dotcms/new-block-editor) does not filter on load and was never affected.

Closes #35985

Acceptance Criteria

  • Hard breaks preserved when re-opening content, including on restricted-block fields
  • hardBreak treated as an always-allowed basic node so purifyNodeTree/removeInvalidNodes never strips it
  • paragrahparagraph typo fixed
  • Saving previously-saved content no longer drops line breaks (same load path)
  • Regression coverage: hardBreak survives removeInvalidNodes on a restricted editor

Test Plan

  • pnpm nx test block-editor --testPathPatterns=parser.utils — 8 tests pass (parser.utils.ts at 97% coverage)
  • Verified manually in the editor: restricted-block field with a multi-line paragraph (Shift+Enter) now retains its line breaks after save + re-open

Changed Files

  • core-web/libs/block-editor/src/lib/shared/utils/parser.utils.ts
  • core-web/libs/block-editor/src/lib/shared/utils/parser.utils.spec.ts (new)

This PR fixes: #35985

Add `hardBreak` to the always-allowed `basicNodes` map so
`purifyNodeTree`/`removeInvalidNodes` no longer strips Shift+Enter line
breaks when Block Editor content is re-opened for editing on a field
configured with allowed-block restrictions (allowedBlocks.length > 1).

Also fixes the latent `paragrah` -> `paragraph` typo so the paragraph
basic-node fallback works as intended. Adds parser.utils regression
coverage.

Closes #35985
@alwaysmeticulous

Copy link
Copy Markdown

Meticulous was unable to execute a test run for this PR because the most recent commit is associated with multiple PRs. To execute a test run, please try pushing up a new commit that is only associated with this PR.

Last updated for commit 5503f34. This comment will update as new commits are pushed.

@github-actions github-actions Bot added the Area : Frontend PR changes Angular/TypeScript frontend code label Jul 15, 2026
@claude

claude Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Claude finished @oidacra's task in 3m 7s —— View job


Claude finished @oidacra's task in 1m 3s —— View job


Code Review

  • Read changed files (parser.utils.ts, parser.utils.spec.ts)
  • Verify the fix against the load/filter path (removeInvalidNodespurifyNodeTree)
  • Check for regressions and shared-state issues
  • Review test coverage

I reviewed the diff. This is a correct, well-scoped fix.

New Issues

No issues found.

Notes

  • parser.utils.ts:41-46 — Adding hardBreak to basicNodes is the right layer. getBlockMap seeds its reduce with basicNodes, so any allowed-block config now always carries paragraph/text/doc/hardBreak. The reduce spreads ({ ...blocks, ... }) rather than mutating, so the shared basicNodes object is never corrupted across calls — no shared-state hazard from making it a named const.
  • parser.utils.ts:42 — The paragrahparagraph typo fix is real and consequential: the previous key never matched NodeTypes.PARAGRAPH ('paragraph'), so the paragraph basic-node fallback was dead code. Using the NodeTypes enum here removes the whole class of typo. The new test at parser.utils.spec.ts:137 (paragraph survives via fallback when not in allowedBlocks) locks this in.
  • Scope — Correctly limited to the restricted-field load path. removeInvalidNodes is only invoked when allowedBlocks.length > 1 (dot-block-editor.component.ts:710), so unrestricted fields are untouched, and the new block editor doesn't filter on load.
  • Tests — Coverage is solid: getBlockMap basics, purifyNodeTree nesting/heading-level composite keys, and the removeInvalidNodes regression for Block Editor: strips hard breaks (Shift+Enter) when re-opening content for edit #35985 (both doc-node and bare-array inputs), plus the negative case that non-basic/non-allowed nodes are still stripped.

Optional (non-blocking): the recursion in purifyNodeTree and the isHeading helper are still untyped (node/blocksMap are implicit any). Not introduced by this PR, but if you're already touching this file, adding JSONContent/BlockMap annotations would be a cheap progressive-enhancement win.

LGTM. ✅
· issue-35985-block-editor-strips-hard-breaks-shift-enter-when

@oidacra
oidacra marked this pull request as ready for review July 15, 2026 20:05
…ading/array paths

Address code review on #35985 fix:
- Reference NodeTypes.PARAGRAPH/TEXT/DOC/HARD_BREAK instead of string
  literals in basicNodes (prevents the typo class that caused the bug).
- Add parser.utils tests for the isHeading composite-key branch and the
  bare-array input branch of removeInvalidNodes.
@oidacra
oidacra enabled auto-merge July 15, 2026 20:28
@oidacra
oidacra added this pull request to the merge queue Jul 15, 2026
@mergify

mergify Bot commented Jul 15, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

Merged via the queue into main with commit f104e79 Jul 15, 2026
43 checks passed
@oidacra
oidacra deleted the issue-35985-block-editor-strips-hard-breaks-shift-enter-when branch July 15, 2026 22:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI: Safe To Rollback Area : Frontend PR changes Angular/TypeScript frontend code

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Block Editor: strips hard breaks (Shift+Enter) when re-opening content for edit

3 participants