-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Make sure comments keep their location when inside paragraph #31374
Conversation
Size Change: +142 B (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
@gwwar Kerry can you take a look? |
0a95688
to
698e018
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one manually tests well for me @vdwijngaert ! Let's get another +1 from folks a little more familiar with this package. From recent history, perhaps @ellatrix or @mcsf could 👀 on code changes here.
* | ||
* @param {Node} node The node to be processed. | ||
* @param {Document} doc The document of the node. | ||
* @return {void} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional: Note that if we return {void} it's fine to leave the return off JSDoc.
@@ -49,6 +49,44 @@ describe( 'specialCommentConverter', () => { | |||
<p>Third paragraph</p>` | |||
); | |||
} ); | |||
describe( 'when more comment is inside paragraph', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 Thanks for adding useful test cases!
childNodes.slice( 0, nodeIndex ).reduce( paragraphBuilder, null ), | ||
moreBlock, | ||
childNodes.slice( nodeIndex + 1 ).reduce( paragraphBuilder, null ), | ||
].forEach( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do folks know if we've polyfilled https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/replaceWith ? It's 💯 optional, but might be nice here.
eg
paragraphWithMore.replaceWith( firstParagraph, moreBlock, secondParagraph );
const nodeIndex = childNodes.indexOf( node ); | ||
const wrapperNode = node.parentNode.parentNode || doc.body; | ||
|
||
const paragraphBuilder = ( acc, child ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can read this okay, but do other reviewers prefer something more declarative? The reducer here did stick out a little bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's not a big deal, but the reducer feels unnecessarily abstract. I don't mind the use of reduce
itself, but it might be clearer to use its initialValue
argument and inline the reducer, e.g.
childNodes.slice( 0, nodeIndex ).reduce(
( acc, child ) => { acc.appendChild( child ); return acc; },
doc.createElement( 'p' )
);
prefer something more declarative?
The other option, in my head, kind of goes in the opposite direction: making this clearer by following local conventions. In raw-handling
, a lot of DOM manipulations are done very imperatively, mostly because performance is paramount but also because it's also syntactically convenient, especially iterating through or unwrapping child nodes. So paradoxically, in this module, it might be more readable to write something like:
for ( let i = 0; i < nodeIndex; i++ ) insert( childNodes[i], node.parentNode );
insert( moreBlock, node.parentNode );
for ( let i = nodeIndex; i < l; i++ ) insert( childNodes[i], node.parentNode );
I have no idea if modern JIT compilers can optimise [ e0, e1, ... ].forEach( fn )
into something that doesn't require allocating memory for that temporary array (e.g. optimise it as a loop). So I can't comment on the performance impact of what we're doing with Array.from
+Array#reduce
in this PR, and whether it matters at all. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks really good to me — the added tests are most welcome!
packages/blocks/src/api/raw-handling/test/special-comment-converter.js
Outdated
Show resolved
Hide resolved
const nodeIndex = childNodes.indexOf( node ); | ||
const wrapperNode = node.parentNode.parentNode || doc.body; | ||
|
||
const paragraphBuilder = ( acc, child ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's not a big deal, but the reducer feels unnecessarily abstract. I don't mind the use of reduce
itself, but it might be clearer to use its initialValue
argument and inline the reducer, e.g.
childNodes.slice( 0, nodeIndex ).reduce(
( acc, child ) => { acc.appendChild( child ); return acc; },
doc.createElement( 'p' )
);
prefer something more declarative?
The other option, in my head, kind of goes in the opposite direction: making this clearer by following local conventions. In raw-handling
, a lot of DOM manipulations are done very imperatively, mostly because performance is paramount but also because it's also syntactically convenient, especially iterating through or unwrapping child nodes. So paradoxically, in this module, it might be more readable to write something like:
for ( let i = 0; i < nodeIndex; i++ ) insert( childNodes[i], node.parentNode );
insert( moreBlock, node.parentNode );
for ( let i = nodeIndex; i < l; i++ ) insert( childNodes[i], node.parentNode );
I have no idea if modern JIT compilers can optimise [ e0, e1, ... ].forEach( fn )
into something that doesn't require allocating memory for that temporary array (e.g. optimise it as a loop). So I can't comment on the performance impact of what we're doing with Array.from
+Array#reduce
in this PR, and whether it matters at all. :)
Whoops, I think we lost track of this one. Sorry about that! Pretty sure this is still an issue in trunk, @vdwijngaert if you're still interested let's rebase the PR to fix the conflict and we can try landing this one. If you're busy, I can also rebase and land for you. |
As a result of the bug scrub in the editor chat, I will continue to test. |
Co-authored-by: Miguel Fonseca <[email protected]>
Everything looks good to me 👍 |
Description
Fixes an issue where
<!--more-->
comments did not keep their location when inside a paragraph, after being converted to blocks. This mostly affects those cases where a classic editor block gets converted. See the linked issue for details.My code addresses the special case where the
<!--more-->
was placed inside of a paragraph. In order to obtain the expected behavior, we split the paragraph in two and insert the block in the middle instead.Closes #18079
Checklist:
*.native.js
files for terms that need renaming or removal). (not applicable)