Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
RichText: Reuse DOM document across calls to createEmpty
  • Loading branch information
aduth committed Nov 30, 2018
commit 038769c184f3bcf4948faee16e32077d9e8e8e27
6 changes: 6 additions & 0 deletions packages/rich-text/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 3.0.3 (Unreleased)

### Internal

- Internal performance optimizations to avoid excessive expensive creation of DOM documents.

## 3.0.2 (2018-11-21)

## 3.0.1 (2018-11-20)
Expand Down
23 changes: 21 additions & 2 deletions packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,28 @@ function getNodeByPath( node, path ) {
};
}

/**
* Returns a new instance of a DOM tree upon which RichText operations can be
* applied.
*
* Note: The current implementation will return a shared reference, reset on
* each call to `createEmpty`. Therefore, you should not hold a reference to
* the value to operate upon asynchronously, as it may have unexpected results.
*
* @return {WPRichTextTree} RichText tree.
*/
function createEmpty() {
Copy link
Member

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?

Copy link
Member

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.

const { body } = document.implementation.createHTMLDocument( '' );
return body;
// Because `createHTMLDocument` is an expensive operation, and with this
// 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 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Nice 👍

createEmpty.body.innerHTML = '';
} else {
createEmpty.body = document.implementation.createHTMLDocument( '' ).body;
}

return createEmpty.body;
}

function append( element, child ) {
Expand Down