Skip to content

Commit

Permalink
Raw handling: fix pasting special spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Jan 10, 2021
1 parent efbee9f commit 6eb1ea1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/blocks/src/api/raw-handling/normalise-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function normaliseBlocks( HTML ) {

// Text nodes: wrap in a paragraph, or append to previous.
if ( node.nodeType === node.TEXT_NODE ) {
if ( ! node.nodeValue.trim() ) {
if ( isEmpty( node ) ) {
decu.removeChild( node );
} else {
if ( ! accu.lastChild || accu.lastChild.nodeName !== 'P' ) {
Expand Down
9 changes: 5 additions & 4 deletions packages/blocks/src/api/raw-handling/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,16 @@ export function pasteHandler( {
} )
);

// If we're allowed to return inline content, and there is only one inlineable block,
// and the original plain text content does not have any line breaks, then
// treat it as inline paste.
// If we're allowed to return inline content, and there is only one
// inlineable block, and the original plain text content does not have any
// line breaks, then treat it as inline paste.
if (
mode === 'AUTO' &&
blocks.length === 1 &&
hasBlockSupport( blocks[ 0 ].name, '__unstablePasteTextInline', false )
) {
const trimmedPlainText = plainText.trim();
// Don't catch line breaks at the start or end.
const trimmedPlainText = plainText.replace( /^[\n]+|[\n]+$/g, '' );

if (
trimmedPlainText !== '' &&
Expand Down
31 changes: 13 additions & 18 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,27 +961,22 @@ function cleanNodeList( nodeList, doc, schema, inline ) {
* @return {boolean} Wether or not the element is empty.
*/
export function isEmpty( element ) {
if ( ! element.hasChildNodes() ) {
return true;
}

return Array.from( element.childNodes ).every( ( node ) => {
if ( node.nodeType === node.TEXT_NODE ) {
return ! node.nodeValue.trim();
}

if ( node.nodeType === node.ELEMENT_NODE ) {
if ( node.nodeName === 'BR' ) {
return true;
} else if ( node.hasAttributes() ) {
switch ( element.nodeType ) {
case element.TEXT_NODE:
// We cannot use \s since it includes special spaces which we want
// to preserve.
return /^[ \f\n\r\t\v\u00a0]*$/.test( element.nodeValue );
case element.ELEMENT_NODE:
if ( element.hasAttributes() ) {
return false;
} else if ( ! element.hasChildNodes() ) {
return true;
}

return isEmpty( node );
}

return true;
} );
return Array.from( element.childNodes ).every( isEmpty );
default:
return true;
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions test/integration/blocks-raw-handling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ describe( 'Blocks raw handling', () => {
expect( console ).toHaveLogged();
} );

it( 'should paste special whitespace', () => {
const filtered = pasteHandler( {
HTML: '<p>&thinsp;</p>',
plainText: ' ',
mode: 'AUTO',
} );

expect( filtered ).toBe( ' ' );
expect( console ).toHaveLogged();
} );

it( 'should parse Markdown', () => {
const filtered = pasteHandler( {
HTML: '* one<br>* two<br>* three',
Expand Down

0 comments on commit 6eb1ea1

Please sign in to comment.