Skip to content

Tests for inserting text either side of an inline element - #4346

Merged
zurfyx merged 9 commits into
meta:mainfrom
shirakaba:insertnodes-after-inline-element
May 9, 2023
Merged

zurfyx merged 9 commits into
meta:mainfrom
shirakaba:insertnodes-after-inline-element

Conversation

@shirakaba

Copy link
Copy Markdown
Contributor

Fixes: #4295

(Well, it's a strawman implementation.)

CLA signing currently pending response from [email protected].

@vercel

vercel Bot commented Apr 18, 2023 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
lexical ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 9, 2023 1:05am
lexical-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 9, 2023 1:05am

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Hi @shirakaba!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@zurfyx zurfyx left a comment

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.

Thank you, appreciate the time investment on the tests!

cc @fantactuka

const children = element.getChildren();
const childrenLength = children.length;
if ($isElementNode(target)) {
if ($isElementNode(target) && !target.isInline()) {

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.

Isn't the problem here the fact that we're trying to paste to an Element that has canInsertAfter() === false yet it does add the content to it?

It also seems that this solutions break the case when pasting text in front the link (the link becomes normal text).

[link] and {paste}[link] becomes [text]

Additionally, prior to this fix, it also seems like the text after is getting merged into the previous target element. It sems like the root cause is deeper than this.

Screen.Recording.2023-04-18.at.11.01.59.AM.mov

Can we check why this is the case?

@shirakaba shirakaba Apr 20, 2023 •

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.

Isn't the problem here the fact that we're trying to paste to an Element that has canInsertAfter() === false yet it does add the content to it?

I am not clear what the meaning of the canInsertTextAfter method is. Does it apply to insertions of text both by typing and by pasting? And by 'after', does it refer to "after this element's last child" or "after this element itself"? Some explanation of the intention and examples of use-case would be valuable here.

At any rate, applying this change to the current patch:

- if ($isElementNode(target) && !target.isInline()) {
+ if ($isElementNode(target) && target.canInsertTextAfter()) {

... does seem to pass all the unit tests and at least the e2e tests belonging to LexicalSelection. As for the remaining E2E tests, I'm a bit unclear as there's a lot of flakiness:

E2E test results
  41 failed
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:196:3 › Tables › Can select cells using Table selection 
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:402:3 › Tables › Can style text using Table selection 
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:493:3 › Tables › Can copy + paste (internal) using Table selection 
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:575:3 › Tables › Can clear text using Table selection 
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:954:3 › Tables › Merge/unmerge cells (1) 
    [chromium] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:1168:3 › Tables › Merge with content 
    [firefox] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:441:3 › Images › Can be dragged and dropped correctly when the image is clicked 
    [firefox] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:538:3 › Images › Cannot be dragged without being clicked 
    [firefox] › packages/lexical-playground/__tests__/e2e/Keyboard.spec.mjs:26:3 › Keyboard shortcuts › handles "insertTranspose" event from Control+T on MAC 
    [webkit] › packages/lexical-playground/__tests__/e2e/BlockWithAlignableContents.spec.mjs:24:3 › BlockWithAlignableContents › Can create full width blocks for YouTube videos 
    [webkit] › packages/lexical-playground/__tests__/e2e/BlockWithAlignableContents.spec.mjs:67:3 › BlockWithAlignableContents › Can align contents within full width blocks 
    [webkit] › packages/lexical-playground/__tests__/e2e/ClearFormatting.spec.mjs:34:3 › Clear All Formatting › Can clear BIU formatting 
    [webkit] › packages/lexical-playground/__tests__/e2e/ClearFormatting.spec.mjs:57:3 › Clear All Formatting › Should preserve the default styling of links and quoted text 
    [webkit] › packages/lexical-playground/__tests__/e2e/DraggableBlock.spec.mjs:21:3 › DraggableBlock › Paragraph one can be successfully dragged below paragraph two 
    [webkit] › packages/lexical-playground/__tests__/e2e/DraggableBlock.spec.mjs:70:3 › DraggableBlock › Dragging a paragraph to the end of itself does not change the content 
    [webkit] › packages/lexical-playground/__tests__/e2e/DraggableBlock.spec.mjs:112:3 › DraggableBlock › Drag a paragraph to the bottom of its previous paragraph and nothing happens 
    [webkit] › packages/lexical-playground/__tests__/e2e/File.spec.mjs:26:3 › File › Can import/export 
    [webkit] › packages/lexical-playground/__tests__/e2e/Focus.spec.mjs:34:3 › Focus › selection remains internally when clicking outside the editor 
    [webkit] › packages/lexical-playground/__tests__/e2e/HorizontalRule.spec.mjs:168:3 › HorizontalRule › Will add a horizontal rule at the end of a current TextNode and move selection to the new ParagraphNode. 
    [webkit] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:441:3 › Images › Can be dragged and dropped correctly when the image is clicked 
    [webkit] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:538:3 › Images › Cannot be dragged without being clicked 
    [webkit] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:582:3 › Images › Select image, then select text - EditorState._selection updates with mousedown #2901 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1140:3 › Links › Can type text before and after 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1198:3 › Links › Can delete text up to a link and then add text after 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1265:3 › Links › Can convert part of a text node into a link with forwards selection 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1380:3 › Links › Can convert part of a text node into a link with backwards selection 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1495:3 › Links › Can convert part of a text node into a link and change block type 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:1617:3 › Links › Can handle pressing Enter inside a Link containing multiple TextNodes 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:99:3 › Nested List › Can create a list and indent/outdent it 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:156:3 › Nested List › Can create a list and partially copy some content out of it 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:207:3 › Nested List › Should outdent if indented when the backspace key is pressed 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:238:3 › Nested List › Can indent/outdent mutliple list nodes in a list with multiple levels of indentation 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:365:3 › Nested List › Can create a list and then toggle it back to original state. 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:819:3 › Nested List › Can create an indented multi-line unordered list and convert it to an ordered list  
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:862:3 › Nested List › Can create an indented multi-line unordered list and convert individual lists in the nested structure to a numbered list.  
    [webkit] › packages/lexical-playground/__tests__/e2e/ListMaxIndentLevel.spec.mjs:83:3 › Nested List › Can only indent until the max depth a list with nested lists 
    [webkit] › packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs:960:3 › Markdown › can adjust selection after text match transformer 
    [webkit] › packages/lexical-playground/__tests__/e2e/Selection.spec.mjs:118:3 › Selection › can wrap post-linebreak nodes into new element 
    [webkit] › packages/lexical-playground/__tests__/e2e/TextFormatting.spec.mjs:318:3 › TextFormatting › Can select text and underline+strikethrough 
    [webkit] › packages/lexical-playground/__tests__/e2e/TextFormatting.spec.mjs:472:3 › TextFormatting › Can select text and change the font-size and font-family 
    [webkit] › packages/lexical-playground/__tests__/regression/1083-backspace-with-element-at-front.spec.mjs:68:3 › Regression test #1083 › Backspace with ElementNode at the front of the selection 
  17 flaky
    [chromium] › packages/lexical-playground/__tests__/e2e/Headings.spec.mjs:62:3 › Headings › Stays as a heading when you press enter in the middle of a heading 
    [chromium] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:538:3 › Images › Cannot be dragged without being clicked 
    [chromium] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:582:3 › Images › Select image, then select text - EditorState._selection updates with mousedown #2901 
    [chromium] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:424:3 › Links › Can create a link with some text after, insert paragraph, then backspace, it should merge correctly 
    [chromium] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:156:3 › Nested List › Can create a list and partially copy some content out of it 
    [chromium] › packages/lexical-playground/__tests__/regression/429-swapping-emoji.spec.mjs:21:3 › Regression test #429 › Can add new lines before the line with emoji 
    [firefox] › packages/lexical-playground/__tests__/e2e/ClearFormatting.spec.mjs:34:3 › Clear All Formatting › Can clear BIU formatting 
    [firefox] › packages/lexical-playground/__tests__/e2e/Images.spec.mjs:582:3 › Images › Select image, then select text - EditorState._selection updates with mousedown #2901 
    [firefox] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:156:3 › Nested List › Can create a list and partially copy some content out of it 
    [firefox] › packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs:659:5 › Markdown › can convert "# " shortcut 
    [firefox] › packages/lexical-playground/__tests__/e2e/MaxLength.spec.mjs:73:3 › MaxLength › can restrict emojis on boundaries 
    [webkit] › packages/lexical-playground/__tests__/e2e/CopyAndPaste.spec.mjs:1485:3 › CopyAndPaste › Copy and paste an inline element into a leaf node 
    [webkit] › packages/lexical-playground/__tests__/e2e/Links.spec.mjs:39:3 › Links › Can convert a text node into a link 
    [webkit] › packages/lexical-playground/__tests__/e2e/List.spec.mjs:1502:3 › Nested List › remove list breaks when selection in empty nested list item 2 
    [webkit] › packages/lexical-playground/__tests__/e2e/Markdown.spec.mjs:894:3 › Markdown › can export text format next to a newline 
    [webkit] › packages/lexical-playground/__tests__/e2e/Tables.spec.mjs:685:3 › Tables › Select All when document contains tables adds custom table styles. 
    [webkit] › packages/lexical-playground/__tests__/e2e/Toolbar.spec.mjs:37:3 › Toolbar › Insert image caption + table 
  28 skipped
  994 passed (1h)

But I'm not too confident about the change, as I'd thought the opposite condition && !target.canInsertTextAfter() made more sense (though it would depend on whether it means "after this element itself" or "after this element's last child").

It also seems that this solutions break the case when pasting text in front the link (the link becomes normal text).

Drat. I'll add some regression tests to help investigate that.

Additionally, prior to this fix, it also seems like the text after is getting merged into the previous target element. It sems like the root cause is deeper than this.

I've looked into it and added that detail to the original issue thread: #4295 (comment)

@facebook-github-bot

Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 18, 2023
@shirakaba
shirakaba force-pushed the insertnodes-after-inline-element branch from f4dea24 to aa031d2 Compare April 20, 2023 06:25
@shirakaba

shirakaba commented Apr 20, 2023 •

Copy link
Copy Markdown
Contributor Author

It also seems that this solutions break the case when pasting text in front the link (the link becomes normal text).

I've added e2e tests for inserting text before/after links placed at the start, middle, or end of a paragraph. Working on unit tests now.

@shirakaba

shirakaba commented Apr 20, 2023 •

Copy link
Copy Markdown
Contributor Author

I've now added accompanying unit tests.

Note that the comments mark which tests had been failing prior to the strawman fix. The strawman fix doesn't fix all cases, unfortunately, so not too sure how to proceed from here.

@zurfyx

zurfyx commented Apr 22, 2023

Copy link
Copy Markdown
Member

Thank you. I think the tests are very valuable and we should merge them regardless (even if commented).

I had a chat with @fantactuka since he's evaluating a major refactor of the insertNodes function and the implementation likely is not a one-liner. While in most cases we can make assumptions around block level and inline Elements, it is valid to have inline Elements that can host other Nodes and behave just like block Elements in this respect. This doesn't happen often because in most cases you would use a block Element for this purpose.

At the same time, LinkNode is also special because text can't be inserted after so there's logic around the boundaries that will prevent that.

Since this fix likely is not trivial, I see 2 ways to proceed with this:

  1. Address the inline Elements root cause (explore canInsertAfter = true and the video I shared above)
  2. We merge the E2E test cases with a .skip and TODO and commented out unit test with a TODO

cc @fantactuka

@shirakaba

shirakaba commented Apr 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Thank you. I think the tests are very valuable and we should merge them regardless (even if commented).

Since this fix likely is not trivial, I see 2 ways to proceed with this:

  1. We merge the E2E test cases with a .skip and TODO and commented out unit test with a TODO

My pleasure! I've added commits to revert the strawman fix and skip/comment-out the failing tests in case this is the way we end up going; can revert if not.

I had a chat with @fantactuka since he's evaluating a major refactor of the insertNodes function and the implementation likely is not a one-liner.

Of interest, RangeSelection.prototype.insertText seemed to pass all test cases (inserting both before and after the link), so may be a useful point of reference if the logic for insertNodes is overhauled.

@shirakaba

Copy link
Copy Markdown
Contributor Author

@zurfyx Oops, looks like I'd simply neglected to skip two of the E2E tests that had been known to fail. I've corrected that now and rebased; just waiting for CI.

@shirakaba shirakaba changed the title For selections just after an inline element, make RangeSelection.prototype.insertNodes insert after the element rather than into it Tests for inserting text either side of an inline element May 2, 2023

@zurfyx zurfyx left a comment

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.

Two more

[firefox] › packages/lexical-playground/tests/e2e/Links.spec.mjs:917:9 › Links › Inserting text either side of links › Inserting text after links › Start-of-paragraph links › Can insert text after a start-of-paragraph link, via typing
[firefox] › packages/lexical-playground/tests/e2e/Links.spec.mjs:1068:9 › Links › Inserting text either side of links › Inserting text after links › End-of-paragraph links › Can insert text after an end-of-paragraph link, via typing

@shirakaba

shirakaba commented May 2, 2023 •

Copy link
Copy Markdown
Contributor Author

@zurfyx Should we attribute those two failures to flakiness? Those two passed when running the E2E tests manually just on macOS Chromium. I tested those failing test cases manually in macOS Firefox and they do pass, so I can only imagine it's down to flakiness.

I've just altered the tests to be based on the text 'ab' rather than just 'a' so that the start-of-paragraph and end-of-paragraph test cases actually look distinct. Here's a video of the new 'ab' test cases both passing in macOS Firefox:

startend.mp4

@shirakaba

Copy link
Copy Markdown
Contributor Author

Just to give another poke as I believe this is ready to merge @zurfyx.

@zurfyx

zurfyx commented May 9, 2023

Copy link
Copy Markdown
Member

Thanks for iterating on this and adding the TODOs throughout the code!

@zurfyx
zurfyx merged commit 3f1f561 into meta:main May 9, 2023
This was referenced May 23, 2023

This branch was successfully deployed

2 active deployments
Preview – lexical-playground — b996e2f9 Deployed May 9, 2023 by vercel[bot]
Preview – lexical — b996e2f9 Deployed May 9, 2023 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Pasting text immediately after an inline element (e.g. link) causes the text and its following siblings to be moved into the inline element

3 participants