Skip to content

Blocks: Save untouched blocks with the HTML they were loaded with - #85780

Draft
ellatrix wants to merge 3 commits into
trunkfrom
try/save-untouched-blocks-as-loaded
Draft

ellatrix wants to merge 3 commits into
trunkfrom
try/save-untouched-blocks-as-loaded

Conversation

@ellatrix

@ellatrix ellatrix commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

What?

See #38922.

When you edit one block, every block you didn't touch now saves with exactly the HTML it was loaded with, instead of being rebuilt from its save function.

Example, on trunk: a post has this heading and a paragraph. You type one letter in the paragraph and save. The heading you never touched is rewritten too:

<!-- Loaded -->
<h2 style="color: #ff6347;" class="wp-block-heading has-text-color">Tip</h2>

<!-- Saved on trunk -->
<h2 class="wp-block-heading has-text-color" style="color:#ff6347">Tip</h2>

<!-- Saved with this PR -->
<h2 style="color: #ff6347;" class="wp-block-heading has-text-color">Tip</h2>

The same happens to the wrapper of a Group or Columns block when you type in a block inside it, and to any HTML written by hand, by another tool, or by an older version of a block's save that still validates.

Why?

Once any block in a post changes, the editor serializes every block again from save. Validation treats the two headings above as the same, so nothing breaks, but the post's HTML changes without the user doing anything: revisions show changes in blocks nobody edited, and anyone who formats their HTML by hand or with another tool loses that formatting. Loading and saving a post should give back the input, and only the blocks a user changed should serialize differently. #38922 describes the root cause: the editor rebuilds output from attributes instead of using what it loaded.

How?

  1. The parser keeps a block's loaded HTML pieces (innerContent, with a slot for each inner block) in a WeakMap, keyed by the block's attributes object, along with a shallow copy of those attributes. It does this only when the block parsed valid with no built-in fixes and no deprecation, and isn't a Classic or unknown block.
  2. The block editor gives a block a new attributes object on any change (the UPDATE_BLOCK_ATTRIBUTES reducer only copies when a value differs), so the serializer finds the loaded pieces only for blocks nobody changed. It also checks the attributes are shallow equal to the copy, which catches code that changes a parsed block's attributes in place (parse, set attributes.content, serialize). A change nested deeper, such as attributes.style.color, is not caught.
  3. getBlockInnerHTML writes those pieces, with the inner blocks serialized in their slots, as long as the block still has the same number of inner blocks. Otherwise it runs save as before.

What this means:

  • A parent such as a Group keeps its own wrapper markup when a block inside it changes. Adding or removing a child rebuilds the parent from save.
  • Migrated (deprecated) blocks and blocks fixed on load are rebuilt from save, as before.
  • Duplicated or pasted blocks, and blocks rebuilt from the real-time collaboration document, have new attributes objects and save from save, as before.
  • Save filters from plugins: validation already runs them, so a valid untouched block matches their output up to equivalent HTML.
  • The comment delimiter (<!-- wp:heading {...} -->) is still rebuilt from attributes. Not in this PR.

Why a WeakMap and not the parsed attributes on the block: real-time collaboration copies every block property into its shared document (makeBlocksSerializable in core-data/src/utils/crdt-blocks.ts), and attributes hold rich text objects it can't store.

Open questions:

  • A parent whose save reads its children would keep stale markup while only a child changes. No core block's save.js reads innerBlocks (checked by search); deprecated save functions and third-party blocks are not checked.
  • A blocks.getSaveContent.extraProps filter added after a post is parsed applies only to blocks edited afterwards, not to untouched ones.
  • The isValid condition keeps invalid blocks on their current path (they already save from their raw source). No test fails without it.

The full-content fixture test now saves copies of the parsed blocks, so the .serialized.html fixtures keep recording what save writes. Without that, 48 fixtures would change to match their hand-formatted input. I checked every one of those changes: all are equivalent HTML (whitespace, attribute and class order, &quot; vs ", &hellip; vs …, controls vs controls="", /> vs />).

Testing Instructions

A Playground link will be added here once CI has built the plugin for this PR.

  1. Open the post "Untouched blocks keep their HTML".
  2. Click at the end of the paragraph and type a few letters.
  3. Open the Code editor (Options menu, Code editor).
  4. The heading still reads <h2 style="color: #ff6347;" class="wp-block-heading has-text-color">, and the Group still reads <div style="color: #ff6347;" class="wp-block-group has-text-color">. On trunk both are rewritten.
  5. Back in the Visual editor, add a paragraph inside the Group. The Group is now rebuilt from save.

The e2e tests in test/e2e/specs/editor/various/untouched-blocks.spec.js cover this, and unit tests in packages/blocks/src/api/parser/test/index.jsdom.test.jsx cover serialize( parse( content ) ) and attributes changed in place. The first two fail on trunk; the last two check that a parent is rebuilt when a child is added and that an unknown block keeps its delimiters.

Testing Instructions for Keyboard

Same steps; nothing in the interface changes.

Screenshots or screencast

No visible change in the editor. The difference is in the saved HTML, shown in the table above.

Use of AI Tools

Claude Code (Claude Opus) researched prior work, wrote the code, the tests and this description, directed by @ellatrix.

🤖 Generated with Claude Code

ellatrix and others added 2 commits October 27, 2026 13:07
When one block changes, the editor serializes every block again from
`save`, so blocks the user never touched could come out with different
attribute order or style formatting.

The parser now keeps the loaded HTML pieces of each block that parsed
valid with no fixes or migrations, keyed by its attributes object. Any
edit gives a block a new attributes object, so the serializer writes the
loaded pieces only for untouched blocks, with inner blocks serialized in
their slots while their number is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the [Package] Blocks /packages/blocks label Oct 27, 2026
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

📦 Bundle size

Size Change: +96 B (0%)

Total Size: 8.56 MB

📦 View Changed
Filename Size Change
build/scripts/blocks/index.min.js 44.9 kB +96 B (+0.21%)

f99c4d5 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 418d9e5 trunk % Change
timeToFirstByte 49.75 ms +3.12% -2.81% 48.35 ms +4.65% -4.34% 2.9%
largestContentfulPaint 86 ms +6.98% -4.65% 84 ms +7.14% -4.76% 2.38%
lcpMinusTtfb 35.35 ms +12.59% -3.54% 34.05 ms +17.33% -5.29% 3.82%
wpBeforeTemplate 24.66 ms +8.03% -1.54% 23.95 ms +7.18% -1.42% 2.96%
wpTemplate 20.56 ms +7% -7.05% 19.34 ms +9.31% -3.36% 6.31%
wpTotal 45.98 ms +2.87% -2.74% 44.38 ms +4.28% -4.51% 3.61%
wpMemoryUsage 7.70 MB +0% -0% 7.67 MB +0% -0% 0.44%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 418d9e5 trunk % Change
timeToFirstByte 41.9 ms +6.09% -2.86% 40.35 ms +4.09% -1.98% 3.84%
largestContentfulPaint 96 ms +4.17% -0% 92 ms +2.17% -0% 4.35%
lcpMinusTtfb 55.5 ms +3.06% -3.69% 52.3 ms +1.53% -1.72% 6.12%
wpBeforeTemplate 22.31 ms +7.53% -1.84% 21.65 ms +4.76% -0.83% 3.05%
wpTemplate 16.01 ms +4.87% -2.56% 15.33 ms +2.87% -3.46% 4.44%
wpTotal 38.89 ms +6.4% -3.29% 37.24 ms +4.24% -2.34% 4.43%
wpMemoryUsage 6.40 MB +0% -0% 6.36 MB +0% -0% 0.54%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 418d9e5 trunk % Change
mediaProcessingJpeg 417.12 ms +0.47% -0.28% 417.27 ms +0.63% -0.65% -0.04%
mediaProcessingAvif 6300.92 ms +0.35% -0.1% 6308.98 ms +0.33% -0.12% -0.13%
mediaProcessingJpegToAvif 4346.55 ms +0.04% -0.2% 4342.6 ms +0.09% -0.24% 0.09%

media-upload

Metric 418d9e5 trunk % Change
jpegUploadProcessing 1455.75 ms +2.44% -0.91% 1459.53 ms +1.3% -1.36% -0.26%
pngUploadProcessing 225.05 ms +9.48% -3.24% 221.35 ms +7.53% -3.33% 1.67%
largeJpegUploadProcessing 1445.96 ms +0.77% -0.64% 1439.17 ms +1.28% -0.49% 0.47%
multipleImageUploadProcessing 1567.41 ms +0.63% -0.26% 1556.43 ms +0.65% -1.6% 0.71%

post-editor

Metric 418d9e5 trunk % Change
serverResponse 522.45 ms +4.25% -7.84% 504.78 ms +7% -4.91% 3.5%
firstPaint 515.99 ms +4.63% -49.81% 287.37 ms +88.42% -11.08% 79.56%
domContentLoaded 920.02 ms +1.18% -0.9% 923.72 ms +1.46% -1.32% -0.4%
loaded 921.25 ms +1.15% -0.9% 924.98 ms +1.5% -1.32% -0.4%
firstContentfulPaint 537.44 ms +2.9% -2.38% 542.14 ms +0.39% -2.78% -0.87%
firstBlock 3146.93 ms +2.25% -1.01% 3140.35 ms +1.14% -0.6% 0.21%
type 18.5 ms +4.11% -2.97% 18.01 ms +4.5% -7.27% 2.72%
typeWithoutInspector 18.08 ms +5.09% -4.26% 19.76 ms +2.38% -5.01% -8.5%
typeWithTopToolbar 23.78 ms +9.42% -5.89% 23.52 ms +6.38% -4.97% 1.11%
typeContainer 8.19 ms +9.28% -3.54% 8.76 ms +2.74% -7.99% -6.51%
focus 119.83 ms +18.81% -2.66% 128.07 ms +3.56% -6.93% -6.43%
firstFocus 239.05 ms +0% -0% 242.87 ms +0% -0% -1.57%
selectAll 500.81 ms +1.11% -4.51% 463.64 ms +4.97% -1.05% 8.02%
undo 33.7 ms +8.49% -5.73% 32.58 ms +7.18% -2.12% 3.44%
listViewOpen 83.29 ms +2.2% -6.87% 80.58 ms +6.06% -6.29% 3.36%
inserterOpen 42.83 ms +5.86% -8.62% 40.93 ms +16.54% -6.65% 4.64%
inserterHover 11.95 ms +9.54% -6.78% 11.56 ms +12.98% -7.01% 3.37%
inserterSearch 8.92 ms +6.17% -9.3% 8.31 ms +13.96% -4.09% 7.34%
loadPatterns 669.2 ms +0.84% -1.98% 650.55 ms +4.08% -1.19% 2.87%
wpTotal 512.79 ms +4.27% -8.03% 494.65 ms +7.04% -4.92% 3.67%
wpMemoryUsage 14.75 MB +0% -0% 14.72 MB +0% -0% 0.24%
wpDbQueries 55 +0% -1.82% 55 +0% -1.82% 0%

site-editor

Metric 418d9e5 trunk % Change
serverResponse 515.13 ms +4.79% -7.74% 530.26 ms +2.96% -4.99% -2.85%
firstPaint 292.57 ms +11.76% -11.46% 267.61 ms +10% -8.83% 9.33%
domContentLoaded 965.39 ms +2.79% -4.2% 944.43 ms +1.88% -1.52% 2.22%
loaded 966.81 ms +2.81% -4.21% 945.74 ms +1.89% -1.56% 2.23%
firstContentfulPaint 537.13 ms +4.42% -5.92% 523.01 ms +2.02% -3.24% 2.7%
firstBlock 4327.54 ms +2.43% -3.81% 4141.86 ms +1.17% -0.67% 4.48%
type 20.98 ms +7.29% -3% 19.57 ms +2.81% -6.95% 7.2%
navigate 145.21 ms +3.48% -3.06% 128.41 ms +6.56% -3.22% 13.08%
loadPatterns 1396.98 ms +12.82% -5.35% 1326.68 ms +19.75% -5.81% 5.3%
loadPages 1057.11 ms +2.06% -1.98% 1072.66 ms +3.73% -5.55% -1.45%
wpTotal 504.65 ms +5.06% -7.82% 520.49 ms +2.81% -5.13% -3.04%
wpMemoryUsage 12.77 MB +0% -0% 12.74 MB +0% -0% 0.24%
wpDbQueries 44 +2.27% -0% 44 +2.27% -0% 0%

f99c4d5 Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Undo restores the post selection when another synced entity is loaded (RTC WebSocket) in /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 6,
+   "offset": 6,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
should be immediately saved on indentation in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 1
+ Received  + 1

@@ -1,8 +1,8 @@
  <!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
- <li>one<!-- wp:list -->
+ <li>ne<!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
  <li></li>
  <!-- /wp:list-item --></ul>
  <!-- /wp:list --></li>
  <!-- /wp:list-item --></ul>

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:694:52
should create paragraph on Enter in quote block in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
TimeoutError: locator.click: Timeout 10000ms exceeded.
Call log:
  - waiting for getByRole('option', { name: 'List', exact: true })

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:751:67
should cut and paste individual blocks with collapsed selection in /test/e2e/specs/editor/various/copy-cut-paste.spec.js, passed after 1 failed attempt.
Error: expect(string).toMatchSnapshot(expected) failed

  @@ -1,3 +1,3 @@
   <!-- wp:paragraph -->
  -<p>2Cut - collapsed selection</p>
  +<p>2</p>
   <!-- /wp:paragraph -->


    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/copy-cut-paste.spec.js:45:49
should gradually multi-select in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 3
+ Received  + 7

  Array [
    Object {
-     "name": "core/column",
+     "attributes": Object {
+       "content": "1",
+       "dropCap": false,
      },
-   Object {
-     "name": "core/column",
+     "clientId": "a6f76129-14dc-4516-adbd-1edf8eb5ade8",
+     "innerBlocks": Array [],
+     "isValid": true,
+     "name": "core/paragraph",
    },
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:1200:5
should insert a line break on shift+enter in a non trailing list item in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 10
+ Received  + 10

  <!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
- <li>a</li>
+ <li></li>
- <!-- /wp:list-item -->
-
+ <!-- /wp:list-item --></ul>
- <!-- wp:list-item -->
+ <!-- /wp:list -->
+
+ <!-- wp:paragraph -->
- <li>b<br></li>
+ <p>b<br></p>
- <!-- /wp:list-item -->
+ <!-- /wp:paragraph -->

- <!-- wp:list-item -->
+ <!-- wp:paragraph -->
- <li>c</li>
+ <p>c</p>
- <!-- /wp:list-item --></ul>
- <!-- /wp:list -->
+ <!-- /wp:paragraph -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:1232:52
should leave the rest of the list intact when merging into an empty list item in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  -  0
+ Received  + 13

@@ -5,27 +5,40 @@
          "attributes": Object {
            "content": "1",
          },
          "innerBlocks": Array [
            Object {
+             "attributes": Object {
+               "ordered": false,
+               "values": "",
+             },
              "innerBlocks": Array [
                Object {
                  "attributes": Object {
                    "content": "a",
                  },
+                 "innerBlocks": Array [],
                  "name": "core/list-item",
                },
                Object {
                  "attributes": Object {
                    "content": "b",
                  },
+                 "innerBlocks": Array [],
                  "name": "core/list-item",
                },
              ],
              "name": "core/list",
            },
          ],
+         "name": "core/list-item",
+       },
+       Object {
+         "attributes": Object {
+           "content": "",
+         },
+         "innerBlocks": Array [],
          "name": "core/list-item",
        },
      ],
      "name": "core/list",
    },
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:3117:38
should merge a following sibling list into the outermost list with Delete from a nested item (#77245) in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

@@ -8,11 +8,11 @@
          "innerBlocks": Array [
            Object {
              "innerBlocks": Array [
                Object {
                  "attributes": Object {
-                   "content": "b‸",
+                   "content": "b",
                  },
                  "name": "core/list-item",
                },
              ],
              "name": "core/list",
@@ -25,11 +25,11 @@
    },
    Object {
      "innerBlocks": Array [
        Object {
          "attributes": Object {
-           "content": "1",
+           "content": "‸1",
          },
          "innerBlocks": Array [
            Object {
              "innerBlocks": Array [
                Object {

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:2853:41
should select by dragging into nested block in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 2
+ Received  + 1

  Array [
-   1,
-   2,
+   3,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:568:5
should select by dragging in /test/e2e/specs/editor/various/multi-block-selection.spec.js, passed after 1 failed attempt.
Error: expect(received).toEqual(expected) // deep equality

- Expected  - 1
+ Received  + 0

  Array [
-   1,
    2,
  ]

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/various/multi-block-selection.spec.js:532:5
should split indented list item in /test/e2e/specs/editor/blocks/list.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 1
+ Received  + 1

@@ -1,8 +1,8 @@
  <!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
- <li>one<!-- wp:list -->
+ <li>e<!-- wp:list -->
  <ul class="wp-block-list"><!-- wp:list-item -->
  <li>two</li>
  <!-- /wp:list-item -->

  <!-- wp:list-item -->

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/list.spec.js:668:52

f99c4d5 Run

Keep a shallow copy of the parsed attributes and compare against it, so
code that changes a parsed block's attributes in place before
serializing gets the new markup instead of the loaded one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Blocks /packages/blocks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant