Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent stray line breaks in RichText after pasting (e.g., from Google Doc) #15372

Closed
wants to merge 2 commits into from

Conversation

tfrommen
Copy link
Member

@tfrommen tfrommen commented May 1, 2019

Description

When pasting something from a Google Doc into a RichText-based block, I ended up having a stray (leading) line break.

Having chased through quite a few files and functions, I finally found that the culprit is the (internal) cleanNodeList function in @wordpress/blocks (src/api/raw-handling/utils.js).
This function inserts a line break ( i.e., ultimately, a <br> tag) after non-phrasing-content elements.

The problem with Google Docs, as with potential other sources, is that there are HTML comments, and they, too, would trigger insertion of line breaks.

Steps to Reproduce

  1. Create a Google Doc and input some content, for example, this:.
Some

Content

Here
  1. Select "Content", copy and paste into a RichText component.
  2. The console will show something like that:
Received HTML:

 <html><body>
<!--StartFragment--><meta charset="utf-8"><span style="font-size:11pt;font-family:Arial;color:#000000;background-color:transparent;font-weight:400;font-style:normal;font-variant:normal;text-decoration:none;vertical-align:baseline;white-space:pre;white-space:pre-wrap;" id="docs-internal-guid-27e8f390-7fff-ad20-17df-378c134770be">Content</span><!--EndFragment-->
</body>
</html>
Received plain text:

 Content
Processed inline HTML:

 
<br>Content

The actual problem is the <!--StartFragment--> comment node.

How has this been tested?

Copy-and-paste content from a Google Doc into a RichText component. No leading line break. 😉

Screenshots

gdoc-gutenberg

Types of changes

Insertion of line breaks after comment nodes is now being prevented.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.

@nerrad nerrad added [Type] Bug An existing feature does not function as intended [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable [Package] Rich text /packages/rich-text labels May 6, 2019
@nerrad nerrad added [Package] Blocks /packages/blocks and removed [Package] Rich text /packages/rich-text [Feature] Rich Text Related to the Rich Text component that allows developers to render a contenteditable labels May 6, 2019
@tfrommen tfrommen changed the title Prevent stray leading line breaks in RichText after pasting (e.g., from Google Doc) Prevent stray line breaks in RichText after pasting (e.g., from Google Doc) May 9, 2019
@tfrommen
Copy link
Member Author

tfrommen commented May 9, 2019

@youknowriad @jorgefilipecosta @nerrad I just added some unit tests, and slightly changed the title (as it's not really about leading line breaks).

Is there anything missing here?

// are not phrasing content.
if ( inline && ! isPhrasingContent( node ) && node.nextElementSibling ) {
// are not phrasing content and no comments.
if ( inline && node.nodeType !== COMMENT_NODE && ! isPhrasingContent( node ) && node.nextElementSibling ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can these comments not be stripped beforehand? I think we'll need to add a filter to deepFilterHTML, right after specialCommentConverter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I can take a stab at it, sure. However, that same new filter would also be required in filterInlineHTML, right? This function, too, calls removeInvalidHTML.

Copy link
Member

@ellatrix ellatrix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tfrommen
Copy link
Member Author

@ellatrix I just created a separate PR including the other approach: #15557.

@tfrommen
Copy link
Member Author

Closing in favor of #15557.

@tfrommen tfrommen closed this May 14, 2019
@tfrommen tfrommen deleted the patch-1 branch May 14, 2019 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Blocks /packages/blocks [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants