-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
RichText: Reuse DOM document across calls to createEmpty #12402
Conversation
It seems that one e2e test fails consistently... Most likely some other commit causing it.
|
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.
Looks good!
packages/rich-text/CHANGELOG.md
Outdated
|
||
### Internal | ||
|
||
- `unstableToDom` will no longer create a new DOM document on each call; a single document will be shared across each invocation. |
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 thought we were not documenting unstable APIs?
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 hesitated, but ultimately decided that it's good to include something in the CHANGELOG for any pull request.
I wouldn't so much call this out as documenting the unstable API, since a CHANGELOG is not documentation. The "Internal" heading was meant to indicate that it's more a refactoring / housekeeping than anything else.
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.
Maybe I can revise this a bit to avoid specifically mentioning unstableToDom
, while keeping the essence of the note otherwise intact.
packages/rich-text/src/to-dom.js
Outdated
// function being internal to `rich-text` (full control in avoiding a risk | ||
// of asynchronous operations on the shared reference), a single document | ||
// is reused and reset for each call to the function. | ||
if ( createEmpty.body ) { |
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.
Nice 👍
@aduth I see this only happening once for every key press? |
Judging by #12460, were you able to reproduce? It appears to be resolved after (and only after) that pull request was merged. Testing diff: diff --git a/packages/rich-text/src/to-dom.js b/packages/rich-text/src/to-dom.js
index b582c7708..93e4a61e0 100644
--- a/packages/rich-text/src/to-dom.js
+++ b/packages/rich-text/src/to-dom.js
@@ -60,6 +60,7 @@ function getNodeByPath( node, path ) {
function createEmpty() {
const { body } = document.implementation.createHTMLDocument( '' );
+ console.count( 'createEmpty' );
return body;
} |
@aduth Yes. I mistakenly thought this PR was updating |
packages/rich-text/src/to-dom.js
Outdated
* the value to operate upon asynchronously, as it may have unexpected results. | ||
* | ||
* @return {WPRichTextTree} RichText tree. | ||
*/ | ||
function createEmpty() { |
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.
There's also createElement
, see #12470. It would be great if we could merge these two functions into one?
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.
Maybe we can update the PR here to also address createElement
and only keep one.
de614ed
to
038769c
Compare
@iseulde I cherry-picked f0c9aa5 from #12470 into this branch and added revisions of both #12470 (comment) (bdce846) and #12470 (comment) (f9e9e89). How does it look? |
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.
Looks good to me!
Related: #7890 (comment)
This pull request seeks to optimize the behavior of RichText's internal
createEmpty
implementation for DOM tree operations. It reuses a single reference to a DOM document rather than creating a new instance for each invocation.Implementation notes:
Using a shared reference could potentially have unintended side effects if the reference is held for later use. Given that this function is internal to the module, I think it's an acceptable compromise. I made an attempt to be as clear as possible about this potential gotcha through inline code comments.
As an aside, I noted that this function is called 6 times for every key press in a paragraph. This is something which should be evaluated for future optimization, but is not strictly relevant to the changes proposed here.
Possibly related: #11602
Testing instructions:
Ensure unit tests pass:
Verify there are no regressions in the general behavior of text input in RichText (e.g. paragraph).