From 108a40b8ab7c6719a860c9c5d8eceb095c36ee3f Mon Sep 17 00:00:00 2001 From: tfrommen Date: Mon, 16 Sep 2019 22:33:14 +0200 Subject: [PATCH] Fix fixtures for MS Word, and properly handle newlines --- .../api/raw-handling/ms-newline-normaliser.js | 11 +++++++++++ .../blocks/src/api/raw-handling/paste-handler.js | 2 ++ test/integration/fixtures/ms-word-in.html | 16 ++++++++++++---- test/integration/fixtures/ms-word-out.html | 15 +++++++++++---- 4 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 packages/blocks/src/api/raw-handling/ms-newline-normaliser.js diff --git a/packages/blocks/src/api/raw-handling/ms-newline-normaliser.js b/packages/blocks/src/api/raw-handling/ms-newline-normaliser.js new file mode 100644 index 0000000000000..556554301c747 --- /dev/null +++ b/packages/blocks/src/api/raw-handling/ms-newline-normaliser.js @@ -0,0 +1,11 @@ +export default function( node ) { + if ( node.nodeName !== 'P' ) { + return; + } + + if ( ! [ ...node.classList ].find( ( className ) => className.match( /^Mso[A-Z].*/ ) ) ) { + return; + } + + node.innerHTML = node.innerHTML.replace( /(?)\n/g, ' ' ); +} diff --git a/packages/blocks/src/api/raw-handling/paste-handler.js b/packages/blocks/src/api/raw-handling/paste-handler.js index fdea17bd0eba2..525251871bb74 100644 --- a/packages/blocks/src/api/raw-handling/paste-handler.js +++ b/packages/blocks/src/api/raw-handling/paste-handler.js @@ -16,6 +16,7 @@ import isInlineContent from './is-inline-content'; import phrasingContentReducer from './phrasing-content-reducer'; import headRemover from './head-remover'; import msListConverter from './ms-list-converter'; +import msNewlineNormaliser from './ms-newline-normaliser'; import listReducer from './list-reducer'; import imageCorrector from './image-corrector'; import blockquoteNormaliser from './blockquote-normaliser'; @@ -201,6 +202,7 @@ export function pasteHandler( { HTML = '', plainText = '', mode = 'AUTO', tagNam const filters = [ googleDocsUIDRemover, msListConverter, + msNewlineNormaliser, headRemover, listReducer, imageCorrector, diff --git a/test/integration/fixtures/ms-word-in.html b/test/integration/fixtures/ms-word-in.html index d65299287e642..85eb9d6c6c6b3 100644 --- a/test/integration/fixtures/ms-word-in.html +++ b/test/integration/fixtures/ms-word-in.html @@ -1,11 +1,9 @@ -

This is a -title

+

This is a title

 

-

This is a -subtitle

+

This is a subtitle

 

@@ -21,6 +19,16 @@

This is a heading level 2paragraph with a link.

+

This is a longer paragraph with quite a few lines +of text to be able to check the correct handling of all the newlines, which are +automatically injected by Microsoft Word, but which we do not want to land in the +editor.

+ +

This is a paragraph with a manual
+newline.

+

 

-

This is a -title

+

This is a title

-

This is a -subtitle

+

This is a subtitle

@@ -20,6 +18,15 @@

This is a heading level 2

This is a paragraph with a link.

+ +

This is a longer paragraph with quite a few lines of text to be able to check the correct handling of all the newlines, which are automatically injected by Microsoft Word, but which we do not want to land in the editor.

+ + + +

This is a paragraph with a manual
+newline.

+ +