fix $.isEmpty performance (#1096)

* fix $.isEmpty performance

* add changelog

* upd bundle

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
This commit is contained in:
tasuku-s 2020-04-26 00:33:28 +09:00 committed by GitHub
parent 7eb642d5eb
commit 72213f2cd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 34 deletions

2
dist/editor.js vendored

File diff suppressed because one or more lines are too long

View file

@ -9,6 +9,7 @@
- `Fix` - Editor's styles won't be appended to the `<head>` when another instance have already do that [#1079](https://github.com/codex-team/editor.js/issues/1079)
- `New` *I18n API* — Ability to provide internalization for Editor.js core and tools. [#751](https://github.com/codex-team/editor.js/issues/751)
- `Fix` - Fixed wrong toolbar icon centering in Firefox [#1120](https://github.com/codex-team/editor.js/pull/1120)
- `Improvements` - Improve performance of DOM traversing at the `isEmpty()` method [#1095](https://github.com/codex-team/editor.js/issues/1095)
- `Fix` - Toolbox: Tool's order in Toolbox now saved in accordance with `tools` object keys order [#1073](https://github.com/codex-team/editor.js/issues/1073)
### 2.17

View file

@ -384,23 +384,12 @@ export default class Dom {
* @returns {boolean}
*/
public static isEmpty(node: Node): boolean {
const treeWalker = [],
leafs = [];
if (!node) {
return true;
}
if (!node.childNodes.length) {
return this.isNodeEmpty(node);
}
/**
* Normalize node to merge several text nodes to one to reduce tree walker iterations
*/
node.normalize();
treeWalker.push(node.firstChild);
const treeWalker = [ node ];
while (treeWalker.length > 0) {
node = treeWalker.shift();
@ -409,31 +398,16 @@ export default class Dom {
continue;
}
if (this.isLeaf(node)) {
leafs.push(node);
} else {
treeWalker.push(node.firstChild);
}
while (node && node.nextSibling) {
node = node.nextSibling;
if (!node) {
continue;
}
treeWalker.push(node);
}
/**
* If one of child is not empty, checked Node is not empty too
*/
if (node && !this.isNodeEmpty(node)) {
if (this.isLeaf(node) && !this.isNodeEmpty(node)) {
return false;
}
if (node.childNodes) {
treeWalker.push(...Array.from(node.childNodes));
}
}
return leafs.every((leaf) => this.isNodeEmpty(leaf));
return true;
}
/**