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


This is a heading


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



  1. One

  2. Two

  3. Three


One

Two

Three

1

2

3

I

II

III




An image:



\ No newline at end of file +

This is a title


This is a heading


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



  1. One

  2. Two

  3. Three


One

Two

Three

1

2

3

I

II

III




An image:



\ No newline at end of file diff --git a/test/integration/fixtures/google-docs-table-in.html b/test/integration/fixtures/google-docs-table-in.html index 8a6b117fa6ed55..ad03a2dff05172 100644 --- a/test/integration/fixtures/google-docs-table-in.html +++ b/test/integration/fixtures/google-docs-table-in.html @@ -1 +1 @@ -

One

Two

Three

1

2

3

I

II

III

+

One

Two

Three

1

2

3

I

II

III

\ No newline at end of file diff --git a/test/integration/fixtures/google-docs-table-with-comments-in.html b/test/integration/fixtures/google-docs-table-with-comments-in.html new file mode 100644 index 00000000000000..a23b54c17a5e33 --- /dev/null +++ b/test/integration/fixtures/google-docs-table-with-comments-in.html @@ -0,0 +1,4 @@ + +

One

Two

Three

1

2

3

I

II

III

+ + \ No newline at end of file diff --git a/test/integration/fixtures/google-docs-table-with-comments-out.html b/test/integration/fixtures/google-docs-table-with-comments-out.html new file mode 100644 index 00000000000000..697c2d41ea5cd9 --- /dev/null +++ b/test/integration/fixtures/google-docs-table-with-comments-out.html @@ -0,0 +1,3 @@ + +
OneTwoThree
123
IIIIII
+ diff --git a/test/integration/fixtures/google-docs-with-comments-in.html b/test/integration/fixtures/google-docs-with-comments-in.html new file mode 100644 index 00000000000000..e838e0198fe8f4 --- /dev/null +++ b/test/integration/fixtures/google-docs-with-comments-in.html @@ -0,0 +1,4 @@ + +

This is a title


This is a heading


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



  1. One

  2. Two

  3. Three


One

Two

Three

1

2

3

I

II

III




An image:



+ + \ No newline at end of file diff --git a/test/integration/fixtures/google-docs-with-comments-out.html b/test/integration/fixtures/google-docs-with-comments-out.html new file mode 100644 index 00000000000000..7733ca660bdd02 --- /dev/null +++ b/test/integration/fixtures/google-docs-with-comments-out.html @@ -0,0 +1,35 @@ + +

This is a title

+ + + +

This is a heading

+ + + +

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

+ + + + + + + +
  1. One
  2. Two
  3. Three
+ + + +
OneTwoThree
123
IIIIII
+ + + +
+ + + +

An image:

+ + + +
+