diff --git a/packages/blocks/src/api/raw-handling/comment-remover.js b/packages/blocks/src/api/raw-handling/comment-remover.js new file mode 100644 index 00000000000000..fbfa319ccb8316 --- /dev/null +++ b/packages/blocks/src/api/raw-handling/comment-remover.js @@ -0,0 +1,21 @@ +/** + * WordPress dependencies + */ +import { remove } from '@wordpress/dom'; + +/** + * Browser dependencies + */ +const { COMMENT_NODE } = window.Node; + +/** + * Looks for comments, and removes them. + * + * @param {Node} node The node to be processed. + * @return {void} + */ +export default function( node ) { + if ( node.nodeType === COMMENT_NODE ) { + remove( node ); + } +} diff --git a/packages/blocks/src/api/raw-handling/paste-handler.js b/packages/blocks/src/api/raw-handling/paste-handler.js index 1d67540c0b9225..c99b4ea5c5d0a2 100644 --- a/packages/blocks/src/api/raw-handling/paste-handler.js +++ b/packages/blocks/src/api/raw-handling/paste-handler.js @@ -11,6 +11,7 @@ import { getBlockContent } from '../serializer'; import { getBlockAttributes, parseWithGrammar } from '../parser'; import normaliseBlocks from './normalise-blocks'; import specialCommentConverter from './special-comment-converter'; +import commentRemover from './comment-remover'; import isInlineContent from './is-inline-content'; import phrasingContentReducer from './phrasing-content-reducer'; import headRemover from './head-remover'; @@ -44,7 +45,7 @@ const { console } = window; * @return {string} HTML only containing phrasing content. */ function filterInlineHTML( HTML ) { - HTML = deepFilterHTML( HTML, [ googleDocsUIDRemover, phrasingContentReducer ] ); + HTML = deepFilterHTML( HTML, [ googleDocsUIDRemover, phrasingContentReducer, commentRemover ] ); HTML = removeInvalidHTML( HTML, getPhrasingContentSchema(), { inline: true } ); // Allows us to ask for this information when we get a report. @@ -204,6 +205,7 @@ export function pasteHandler( { HTML = '', plainText = '', mode = 'AUTO', tagNam imageCorrector, phrasingContentReducer, specialCommentConverter, + commentRemover, figureContentReducer, blockquoteNormaliser, ]; diff --git a/packages/blocks/src/api/raw-handling/test/comment-remover.js b/packages/blocks/src/api/raw-handling/test/comment-remover.js new file mode 100644 index 00000000000000..6721e20bec23b6 --- /dev/null +++ b/packages/blocks/src/api/raw-handling/test/comment-remover.js @@ -0,0 +1,43 @@ +/** + * Internal dependencies + */ +import commentRemover from '../comment-remover'; +import { deepFilterHTML } from '../utils'; + +describe( 'commentRemover', () => { + it( 'should remove a single comment', () => { + expect( deepFilterHTML( + '', + [ commentRemover ] + ) ).toEqual( + '' + ); + } ); + it( 'should remove multiple comments', () => { + expect( deepFilterHTML( + '
First paragraph.
Second paragraph.
', + [ commentRemover ] + ) ).toEqual( + 'First paragraph.
Second paragraph.
' + ); + } ); + it( 'should remove nested comments', () => { + expect( deepFilterHTML( + 'Paragraph.
', + [ commentRemover ] + ) ).toEqual( + 'Paragraph.
' + ); + } ); + it( 'should remove multi-line comments', () => { + expect( deepFilterHTML( + `First paragraph.
Second paragraph.
`, + [ commentRemover ] + ) ).toEqual( + 'First paragraph.
Second paragraph.
' + ); + } ); +} ); diff --git a/test/integration/blocks-raw-handling.spec.js b/test/integration/blocks-raw-handling.spec.js index 83315ba0d38a68..fda576a042cbb3 100644 --- a/test/integration/blocks-raw-handling.spec.js +++ b/test/integration/blocks-raw-handling.spec.js @@ -236,6 +236,8 @@ describe( 'Blocks raw handling', () => { 'apple', 'google-docs', 'google-docs-table', + 'google-docs-table-with-comments', + 'google-docs-with-comments', 'ms-word', 'ms-word-styled', 'ms-word-online', diff --git a/test/integration/fixtures/google-docs-in.html b/test/integration/fixtures/google-docs-in.html index c667fddceab447..e70f79825bf305 100644 --- a/test/integration/fixtures/google-docs-in.html +++ b/test/integration/fixtures/google-docs-in.html @@ -1 +1 @@ -This is a title
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:
This is a title
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:
One | Two | Three |
1 | 2 | 3 |
I | II | III |
One | Two | Three |
1 | 2 | 3 |
I | II | III |
One | Two | Three |
1 | 2 | 3 |
I | II | III |
One | Two | Three |
1 | 2 | 3 |
I | II | III |
This is a title
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:
This is a title
Formatting test: bold, italic, link, strikethrough, superscript, subscript, nested.
One | Two | Three |
1 | 2 | 3 |
I | II | III |
An image: