diff --git a/packages/blocks/CHANGELOG.md b/packages/blocks/CHANGELOG.md index 9b8aad47d3de45..54eee3a4845266 100644 --- a/packages/blocks/CHANGELOG.md +++ b/packages/blocks/CHANGELOG.md @@ -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) diff --git a/packages/blocks/src/api/raw-handling/format-space-corrector.ts b/packages/blocks/src/api/raw-handling/format-space-corrector.ts new file mode 100644 index 00000000000000..9efb99764d5b92 --- /dev/null +++ b/packages/blocks/src/api/raw-handling/format-space-corrector.ts @@ -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 textafter`). That leading/trailing space is purely + * presentational and ends up underlined/linked, so it is hoisted to the + * surrounding text instead (`before textafter`). + * + * @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 ); +} diff --git a/packages/blocks/src/api/raw-handling/paste-handler.ts b/packages/blocks/src/api/raw-handling/paste-handler.ts index c8095d510f2c90..c33ce5322c1662 100644 --- a/packages/blocks/src/api/raw-handling/paste-handler.ts +++ b/packages/blocks/src/api/raw-handling/paste-handler.ts @@ -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'; @@ -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 ); @@ -231,7 +236,12 @@ export function pasteHandler( { piece = normaliseBlocks( piece ); piece = deepFilterHTML( piece, - [ htmlFormattingRemover, brRemover, emptyParagraphRemover ], + [ + htmlFormattingRemover, + formatSpaceCorrector, + brRemover, + emptyParagraphRemover, + ], blockContentSchema ); diff --git a/test/integration/__snapshots__/blocks-raw-handling.test.js.snap b/test/integration/__snapshots__/blocks-raw-handling.test.js.snap index 89689173c8d966..e68f1d47253d9d 100644 --- a/test/integration/__snapshots__/blocks-raw-handling.test.js.snap +++ b/test/integration/__snapshots__/blocks-raw-handling.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`Blocks raw handling pasteHandler apple 1`] = `"This is a title


This is a heading


This is a paragraph with a link.


A
Bulleted
Indented
List


One
Two
Three


One
Two
Three
1
2
3
I
II
III


An image:
"`; +exports[`Blocks raw handling pasteHandler apple 1`] = `"This is a title


This is a heading


This is a paragraph with a link.


A
Bulleted
Indented
List


One
Two
Three


One
Two
Three
1
2
3
I
II
III


An image:
"`; exports[`Blocks raw handling pasteHandler classic 1`] = `"First paragraph

Second paragraph
Third paragraph
Fourth paragraph
Fifth paragraph
Sixth paragraph"`; @@ -8,6 +8,8 @@ exports[`Blocks raw handling pasteHandler evernote 1`] = `"This is a paragra exports[`Blocks raw handling pasteHandler google-docs 1`] = `"This is a title

This is a heading

Formatting test: bold, italic, link, strikethrough, superscript, subscript, nested.

A
Bulleted
Indented
List

One
Two
Three




One
Two
Three
1
2
3
I
II
III





An image:


"`; +exports[`Blocks raw handling pasteHandler google-docs-link-spacing 1`] = `"This is a paragraph with a link in the middle of the sentence, and it ends with another link before the final words."`; + exports[`Blocks raw handling pasteHandler google-docs-list-only 1`] = `"My first list item
A sub list item
A second sub list item
My second list item
My third list item"`; exports[`Blocks raw handling pasteHandler google-docs-table 1`] = `"



One
Two
Three
1
2
3
I
II
III"`; @@ -34,7 +36,7 @@ exports[`Blocks raw handling pasteHandler ms-word 1`] = `"This is a title
&nb exports[`Blocks raw handling pasteHandler ms-word-list 1`] = `"This is a headline?
This is a text:
One
Two
Three
Lorem Ipsum.
 "`; -exports[`Blocks raw handling pasteHandler ms-word-online 1`] = `"This is a heading 
This is a paragraph with a link. 
A 
Bulleted 
Indented 
List 
 
One 
Two 
Three 

One 
Two 
Three 
1 
2 
3 
I 
II 
III 
 
An image: 
 "`; +exports[`Blocks raw handling pasteHandler ms-word-online 1`] = `"This is a heading 
This is a paragraph with a link. 
A 
Bulleted 
Indented 
List 
 
One 
Two 
Three 

One 
Two 
Three 
1 
2 
3 
I 
II 
III 
 
An image: 
 "`; exports[`Blocks raw handling pasteHandler ms-word-styled 1`] = `"
Lorem ipsum dolor sit amet, consectetur adipiscing elit 


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."`; diff --git a/test/integration/blocks-raw-handling.test.js b/test/integration/blocks-raw-handling.test.js index 552ffc53afc518..93ac42b4e2e5bf 100644 --- a/test/integration/blocks-raw-handling.test.js +++ b/test/integration/blocks-raw-handling.test.js @@ -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', diff --git a/test/integration/fixtures/documents/apple-out.html b/test/integration/fixtures/documents/apple-out.html index d6112309af9f03..0ce2865747deb6 100644 --- a/test/integration/fixtures/documents/apple-out.html +++ b/test/integration/fixtures/documents/apple-out.html @@ -1,5 +1,5 @@ -

This is a title

+

This is a title

diff --git a/test/integration/fixtures/documents/google-docs-link-spacing-in.html b/test/integration/fixtures/documents/google-docs-link-spacing-in.html new file mode 100644 index 00000000000000..bb668800e5caa0 --- /dev/null +++ b/test/integration/fixtures/documents/google-docs-link-spacing-in.html @@ -0,0 +1 @@ +This is a paragraph with a link in the middle of the sentence, and it ends with another link before the final words. diff --git a/test/integration/fixtures/documents/google-docs-link-spacing-out.html b/test/integration/fixtures/documents/google-docs-link-spacing-out.html new file mode 100644 index 00000000000000..3b4d8e23e62506 --- /dev/null +++ b/test/integration/fixtures/documents/google-docs-link-spacing-out.html @@ -0,0 +1 @@ +This is a paragraph with a link in the middle of the sentence, and it ends with another link before the final words. diff --git a/test/integration/fixtures/documents/ms-word-online-out.html b/test/integration/fixtures/documents/ms-word-online-out.html index c873e435bcbff6..5adf6be3b51c19 100644 --- a/test/integration/fixtures/documents/ms-word-online-out.html +++ b/test/integration/fixtures/documents/ms-word-online-out.html @@ -3,7 +3,7 @@

This is a heading 

-

This is a paragraph with a link. 

+

This is a paragraph with a link.