Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/blocks/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fix

- Fix `mergeSchemas` in `getBlockContentSchemaFromTransforms` to preserve array values (e.g. `classes`) instead of converting them to objects with numeric string keys when merging schemas ([#70615](https://github.com/WordPress/gutenberg/pull/70615)).
- Paste: Move leading and trailing spaces out of inline formatting elements (such as links) so the formatting wraps only its meaningful content. This corrects markup from editors like Google Docs that place the separating space inside the link.

## 15.19.0 (2026-05-14)

Expand Down
75 changes: 75 additions & 0 deletions packages/blocks/src/api/raw-handling/format-space-corrector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* WordPress dependencies
*/
import { isPhrasingContent } from '@wordpress/dom';

/**
* Moves a space from one edge of an element out to the surrounding text.
*
* This runs alongside `htmlFormattingRemover`, which already strips an edge
* space whenever the outside is separated (a block edge, a line break, or a
* neighbour that ends/starts with a space). So an edge space only survives to
* here when it abuts real content with no separator, in which case it always
* needs hoisting out — no further checks required.
*
* `htmlFormattingRemover` also runs first, collapsing every run of `[ \r\n\t]`
* to a single regular space, so the edge can only be a lone `' '` by now. A
* non-breaking space (` `) is intentionally left untouched by both filters,
* hence the strict `=== ' '` check rather than a general whitespace test.
*
* @param node The element to be processed.
* @param doc The document of the node.
* @param isLeading Whether to process the leading (`true`) or trailing edge.
*/
function moveEdgeSpace( node: Node, doc: Document, isLeading: boolean ): void {
const child = isLeading ? node.firstChild : node.lastChild;

if ( ! child || child.nodeType !== node.TEXT_NODE ) {
return;
}

const text = child as Text;
const edgeIndex = isLeading ? 0 : text.data.length - 1;

if ( text.data[ edgeIndex ] !== ' ' ) {
return;
}

// Strip the edge space from inside the element…
text.data = isLeading ? text.data.slice( 1 ) : text.data.slice( 0, -1 );

if ( ! text.data ) {
node.removeChild( text );
}

// …and re-insert a single space just outside it.
node.parentNode!.insertBefore(
doc.createTextNode( ' ' ),
isLeading ? node : node.nextSibling
);
}

/**
* Moves leading and trailing spaces out of inline formatting elements, so that
* the formatting (a link, bold, italic…) wraps only its meaningful content.
*
* Some editors, notably Google Docs, place the space that separates a word from
* an adjacent link *inside* the formatting element (e.g.
* `before<a> text</a>after`). That leading/trailing space is purely
* presentational and ends up underlined/linked, so it is hoisted to the
* surrounding text instead (`before <a>text</a>after`).
*
* @param node The node to be processed.
* @param doc The document of the node.
*/
export default function formatSpaceCorrector(
node: Node,
doc: Document
): void {
if ( node.nodeType !== node.ELEMENT_NODE || ! isPhrasingContent( node ) ) {
return;
}

moveEdgeSpace( node, doc, true );
moveEdgeSpace( node, doc, false );
}
14 changes: 12 additions & 2 deletions packages/blocks/src/api/raw-handling/paste-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import markdownConverter from './markdown-converter';
import iframeRemover from './iframe-remover';
import googleDocsUIDRemover from './google-docs-uid-remover';
import htmlFormattingRemover from './html-formatting-remover';
import formatSpaceCorrector from './format-space-corrector';
import brRemover from './br-remover';
import { deepFilterHTML, isPlain, getBlockContentSchema } from './utils';
import emptyParagraphRemover from './empty-paragraph-remover';
Expand Down Expand Up @@ -62,7 +63,11 @@ function filterInlineHTML( HTML: string ): string {
true
);

HTML = deepFilterHTML( HTML, [ htmlFormattingRemover, brRemover ] );
HTML = deepFilterHTML( HTML, [
htmlFormattingRemover,
formatSpaceCorrector,
brRemover,
] );

// Allows us to ask for this information when we get a report.
log( 'Processed inline HTML:\n\n', HTML );
Expand Down Expand Up @@ -231,7 +236,12 @@ export function pasteHandler( {
piece = normaliseBlocks( piece );
piece = deepFilterHTML(
piece,
[ htmlFormattingRemover, brRemover, emptyParagraphRemover ],
[
htmlFormattingRemover,
formatSpaceCorrector,
brRemover,
emptyParagraphRemover,
],
blockContentSchema
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Blocks raw handling pasteHandler apple 1`] = `"<strong>This is a </strong>title<br><br><br><strong>This is a <em>heading</em></strong><br><br><br>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.<br><br><br>A<br>Bulleted<br>Indented<br>List<br><br><br>One<br>Two<br>Three<br><br><br>One<br>Two<br>Three<br>1<br>2<br>3<br>I<br>II<br>III<br><br><br>An image:<br>"`;
exports[`Blocks raw handling pasteHandler apple 1`] = `"<strong>This is a</strong> title<br><br><br><strong>This is a <em>heading</em></strong><br><br><br>This is a <strong>paragraph</strong> with a <a href="https://w.org">link</a>.<br><br><br>A<br>Bulleted<br>Indented<br>List<br><br><br>One<br>Two<br>Three<br><br><br>One<br>Two<br>Three<br>1<br>2<br>3<br>I<br>II<br>III<br><br><br>An image:<br>"`;

exports[`Blocks raw handling pasteHandler classic 1`] = `"First paragraph<br><br>Second paragraph<br>Third paragraph<br>Fourth paragraph<br>Fifth paragraph<br>Sixth paragraph"`;

exports[`Blocks raw handling pasteHandler evernote 1`] = `"This is a <em>paragraph</em>.<br><br><br>This is a <a href="https://w.org">link</a>.<br><br><br>An<br>Unordered<br>Indented<br>List<br><br><br>One<br>Two<br>Indented<br>Three<br><br><br><br><br><br><br>One<br>Two<br>Three<br>Four<br>Five<br>Six<br><img src="data:image/jpeg;base64,###">"`;

exports[`Blocks raw handling pasteHandler google-docs 1`] = `"This is a <strong>title</strong><br><br>This is a <em>heading</em><br><br>Formatting test: <strong>bold</strong>, <em>italic</em>, <a href="https://w.org/">link</a>, <s>strikethrough</s>, <sup>superscript</sup>, <sub>subscript</sub>, <strong><em>nested</em></strong>.<br><br>A<br>Bulleted<br>Indented<br>List<br><br>One<br>Two<br>Three<br><br><br><br><br>One<br>Two<br>Three<br>1<br>2<br>3<br>I<br>II<br>III<br><br><br><br><br><br>An image:<br><br><img src="https://lh4.googleusercontent.com/ID" width="544" height="184"><br>"`;

exports[`Blocks raw handling pasteHandler google-docs-link-spacing 1`] = `"This is a paragraph with a <a href="https://example.com/one">link in the middle</a> of the sentence, and it ends with <a href="https://example.com/two">another link</a> before the final words."`;

exports[`Blocks raw handling pasteHandler google-docs-list-only 1`] = `"My first list item<br>A sub list item<br>A second sub list item<br>My second list item<br>My third list item"`;

exports[`Blocks raw handling pasteHandler google-docs-table 1`] = `"<br><br><br><br>One<br>Two<br>Three<br>1<br>2<br>3<br>I<br>II<br>III"`;
Expand All @@ -34,7 +36,7 @@ exports[`Blocks raw handling pasteHandler ms-word 1`] = `"This is a title<br>&nb

exports[`Blocks raw handling pasteHandler ms-word-list 1`] = `"<a>This is a headline?</a><br>This is a text:<br>One<br>Two<br>Three<br><a>Lorem Ipsum.</a><br>&nbsp;"`;

exports[`Blocks raw handling pasteHandler ms-word-online 1`] = `"This is a <em>heading</em>&nbsp;<br>This is a <strong>paragraph </strong>with a <a href="https://w.org/" target="_blank" rel="noopener">link</a>.&nbsp;<br>A&nbsp;<br>Bulleted&nbsp;<br>Indented&nbsp;<br>List&nbsp;<br>&nbsp;<br>One&nbsp;<br>Two&nbsp;<br>Three&nbsp;<br><br>One&nbsp;<br>Two&nbsp;<br>Three&nbsp;<br>1&nbsp;<br>2&nbsp;<br>3&nbsp;<br>I&nbsp;<br>II&nbsp;<br>III&nbsp;<br>&nbsp;<br>An image:&nbsp;<br><img src="data:image/jpeg;base64,###">&nbsp;"`;
exports[`Blocks raw handling pasteHandler ms-word-online 1`] = `"This is a <em>heading</em>&nbsp;<br>This is a <strong>paragraph</strong> with a <a href="https://w.org/" target="_blank" rel="noopener">link</a>.&nbsp;<br>A&nbsp;<br>Bulleted&nbsp;<br>Indented&nbsp;<br>List&nbsp;<br>&nbsp;<br>One&nbsp;<br>Two&nbsp;<br>Three&nbsp;<br><br>One&nbsp;<br>Two&nbsp;<br>Three&nbsp;<br>1&nbsp;<br>2&nbsp;<br>3&nbsp;<br>I&nbsp;<br>II&nbsp;<br>III&nbsp;<br>&nbsp;<br>An image:&nbsp;<br><img src="data:image/jpeg;base64,###">&nbsp;"`;

exports[`Blocks raw handling pasteHandler ms-word-styled 1`] = `"<br><strong>Lorem ipsum dolor sit amet, consectetur adipiscing elit&nbsp;</strong><br><br><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque aliquet hendrerit auctor. Nam lobortis, est vel lacinia tincidunt, purus tellus vehicula ex, nec pharetra justo dui sed lorem. Nam congue laoreet massa, quis varius est tincidunt ut."`;

Expand Down
1 change: 1 addition & 0 deletions test/integration/blocks-raw-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ describe( 'Blocks raw handling', () => {
'google-docs-table-with-rowspan',
'google-docs-table-with-comments',
'google-docs-with-comments',
'google-docs-link-spacing',
'ms-word',
'ms-word-list',
'ms-word-styled',
Expand Down
2 changes: 1 addition & 1 deletion test/integration/fixtures/documents/apple-out.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- wp:paragraph -->
<p><strong>This is a </strong>title</p>
<p><strong>This is a</strong> title</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<head><meta charset="UTF-8"></head><b id="docs-internal-guid-79f27194-7fff-02a4-9be2-5a02a915438f" style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-style: normal; font-variant-caps: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-line: none; text-decoration-thickness: auto; text-decoration-style: solid; font-weight: normal;"><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(0, 0, 0); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;">This is a paragraph with a</span><a href="https://example.com/one" style="text-decoration: none;"><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(0, 0, 0); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(17, 85, 204); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: underline; text-decoration-skip-ink: none; vertical-align: baseline; white-space: pre-wrap;">link in the middle</span></a><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(0, 0, 0); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> of the sentence, and it ends with</span><a href="https://example.com/two" style="text-decoration: none;"><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(0, 0, 0); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> </span><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(17, 85, 204); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: underline; text-decoration-skip-ink: none; vertical-align: baseline; white-space: pre-wrap;">another link</span></a><span style="font-size: 11pt; font-family: Arial, sans-serif; color: rgb(0, 0, 0); background-color: transparent; font-weight: 400; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-alternates: normal; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-position: normal; font-variant-emoji: normal; text-decoration: none; vertical-align: baseline; white-space: pre-wrap;"> before the final words.</span></b>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a paragraph with a <a href="https://example.com/one">link in the middle</a> of the sentence, and it ends with <a href="https://example.com/two">another link</a> before the final words.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h1 class="wp-block-heading">This is a <em>heading</em>&nbsp;</h1>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>This is a <strong>paragraph </strong>with a <a href="https://w.org/" target="_blank" rel="noopener">link</a>.&nbsp;</p>
<p>This is a <strong>paragraph</strong> with a <a href="https://w.org/" target="_blank" rel="noopener">link</a>.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:list -->
Expand Down
Loading