Skip to content

[Mobile] Fix omission of pasted image with some clipboard content#15016

Closed
mkevins wants to merge 1 commit intomasterfrom
fix/omission-of-pasted-image
Closed

[Mobile] Fix omission of pasted image with some clipboard content#15016
mkevins wants to merge 1 commit intomasterfrom
fix/omission-of-pasted-image

Conversation

@mkevins
Copy link
Copy Markdown
Contributor

@mkevins mkevins commented Apr 17, 2019

Description

This PR aims to fix an issue on mobile (wordpress-mobile/gutenberg-mobile#827) where pasted content from certain sources omits the images. Related comment: wordpress-mobile/gutenberg-mobile#617 (review), and a clipboard snippet of html that reproduces the issue on mobile here: wordpress-mobile/gutenberg-mobile#827 (comment).

How has this been tested?

Steps to test:

  1. Select content from this site, including text above and below the image above "The Visual Editor.". Alternatively, if you are using an emulator with shared clipboard, it suffices to copy the html from the snippet referenced above.
  2. Create an empty paragraph block.
  3. Paste the contents.

Screenshots (before: left, after: right)

Types of changes

This PR modifies figure-content-reducer.js to use parentNode instead of parentElement when finding the containing <p> element (ancestor) surrounding an image element. Because parentElement is not implemented in the DOM for the mobile environment, <img> elements contained within <p> elements are not properly unwrapped, and are thus removed in a subsequent call to cleanNodeList.

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.

@mkevins mkevins added Mobile Web Viewport sizes for mobile and tablet devices [Feature] Paste labels Apr 17, 2019
@mkevins mkevins requested review from ellatrix, etoledom and mzorz April 17, 2019 12:10

while ( wrapper && wrapper.nodeName !== 'P' ) {
wrapper = wrapper.parentElement;
wrapper = wrapper.parentNode;
Copy link
Copy Markdown
Contributor Author

@mkevins mkevins Apr 17, 2019

Choose a reason for hiding this comment

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

Hi @ellatrix 👋 Do you foresee any issues with this single line change on the web side of things? My concern is, as we traverse upwards, that this could alter the behavior on web, because it may traverse all the way to the document, rather than stopping at the body. WDYT?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Upon further inspection, I think it should exhibit the same behavior, as even in the case where it traverses slightly higher, it ultimately leaves wrapper undefined or null, which then adds the figure to the original parent (i.e. the only case where wrapper is used, it is guaranteed to be a <p> element, where the figure is removed from it, and prepended to it). Can you confirm if my understanding is correct?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change seems ok to me, but it would be great if you could write a test case that fails in master and doesn't fail here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking a look! :)

To clarify, do you mean a test case that fails on mobile? Maybe I should first clarify that this image omission only seems to occur on mobile (i.e. I tried pasting the same on web Gutenberg, and it successfully handles the image).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps, yes. How do we prevent this from happening in the future?

@gziolo gziolo added the Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) label Apr 24, 2019
Copy link
Copy Markdown
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.

Would love if the change could at least receive an inline comment.

Ideally, there should also be a test to prevent it from happening in the future, or some lint rules to prevent us from using some DOM APIs in the raw handling component.

Even better, it should be fixed so parentElement works on mobile too. I wouldn't be surprised if there are more instances of e.g. previousElementSibling. Does this work properly?

@ellatrix
Copy link
Copy Markdown
Member

Do these all work?

const parentElement = node.parentNode;

const prevNode = node.previousElementSibling;

const prevElement = node.previousElementSibling;

if ( inline && ! isPhrasingContent( node ) && node.nextElementSibling ) {

element = element.nextElementSibling || element;

@mkevins
Copy link
Copy Markdown
Contributor Author

mkevins commented May 16, 2019

Hi @ellatrix 👋 Sorry for the delay in getting back to you on this. Thank you very much for pointing out the other code paths in the project where our mobile DOM implementation may be deficient. All of your suggestions are valuable, and I think it would be great if we can follow all of them. We've been working on a plan to bring tests that will hopefully improve this process, and I've also been looking into what is possible with the current implementation we are using: jsdom-jscore, as there may be settings that can be tuned to satisfy some of the deficiencies.

Once we better understand the limitations of the DOM implementation used in the React Native project, perhaps we can discuss the lint-rules that would be appropriate, and ensure that they don't interfere with development on gutenberg.

@mkevins
Copy link
Copy Markdown
Contributor Author

mkevins commented Jun 11, 2019

I'm closing this, as we have now implemented the relevant level 4 DOM traversal functions via wordpress-mobile/gutenberg-mobile#1083, which fix the underlying issue.

@mkevins mkevins closed this Jun 11, 2019
@mkevins mkevins deleted the fix/omission-of-pasted-image branch June 11, 2019 05:13
@gziolo gziolo removed the Mobile Web Viewport sizes for mobile and tablet devices label Oct 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Paste Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants