From 1e7ff03897730095b56953c463b0b830e7135214 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 11:41:02 +0100 Subject: [PATCH 1/6] Add method forEachDescendant and use it for filterDescendants and getTexts --- src/models/node.js | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index a1da75608..8cbe2ddd1 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -162,6 +162,19 @@ const Node = { return descendantFound || found }, + /** + * Recursively iterate over all descendant nodes with `iterator`. + * + * @param {Function} iterator + */ + + forEachDescendant(iterator) { + return this.nodes.forEach((child, i, nodes) => { + iterator(child, i, nodes) + if (child.kind != 'text') child.forEachDescendant(iterator) + }) + }, + /** * Recursively filter all descendant nodes with `iterator`. * @@ -170,11 +183,13 @@ const Node = { */ filterDescendants(iterator) { - return this.nodes.reduce((matches, child, i, nodes) => { - if (iterator(child, i, nodes)) matches = matches.push(child) - if (child.kind != 'text') matches = matches.concat(child.filterDescendants(iterator)) - return matches - }, Block.createList()) + const matches = [] + + this.forEachDescendant((child, i, nodes) => { + if (iterator(child, i, nodes)) matches.push(child) + }) + + return List(matches) }, /** @@ -920,11 +935,7 @@ const Node = { */ getTexts() { - return this.nodes.reduce((texts, node) => { - return node.kind == 'text' - ? texts.push(node) - : texts.concat(node.getTexts()) - }, Block.createList()) + return this.filterDescendants(node => node.kind == 'text') }, /** From 6f90515c964b4a7e6f68485e5681b8082dd04f05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 12:04:22 +0100 Subject: [PATCH 2/6] Use Node.forEachDescendant in Node.getKeys --- src/models/node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/node.js b/src/models/node.js index 8cbe2ddd1..7f7483b1c 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -29,7 +29,7 @@ const Node = { getKeys() { const keys = [] - this.filterDescendants(desc => { + this.forEachDescendant(desc => { keys.push(desc.key) }) From 663522139cd8919a463956e933c3822fe3254704 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 12:05:16 +0100 Subject: [PATCH 3/6] Allow Node.forEachDescendant to exit early --- src/models/node.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/models/node.js b/src/models/node.js index 7f7483b1c..dff96ee50 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -170,7 +170,9 @@ const Node = { forEachDescendant(iterator) { return this.nodes.forEach((child, i, nodes) => { - iterator(child, i, nodes) + if (iterator(child, i, nodes) === false) { + return false + } if (child.kind != 'text') child.forEachDescendant(iterator) }) }, From bf66749c4d904ad79eefc51b07d956e8b8e3a48a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 12:15:33 +0100 Subject: [PATCH 4/6] Rollback `getTexts` to use a recursive method --- src/models/node.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/models/node.js b/src/models/node.js index dff96ee50..e4ff92ec5 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -937,7 +937,11 @@ const Node = { */ getTexts() { - return this.filterDescendants(node => node.kind == 'text') + return this.nodes.reduce((texts, node) => { + return node.kind == 'text' + ? texts.push(node) + : texts.concat(node.getTexts()) + }, List()) }, /** From 738383cb3bbe4f0d71f2e774448f7b180af53bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 13:06:07 +0100 Subject: [PATCH 5/6] Memoize getText on nodes --- src/models/block.js | 4 +--- src/models/document.js | 4 +--- src/models/inline.js | 4 +--- src/models/node.js | 12 ++++++++++++ src/models/text.js | 3 +-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/models/block.js b/src/models/block.js index a4560b541..f91549396 100644 --- a/src/models/block.js +++ b/src/models/block.js @@ -109,9 +109,7 @@ class Block extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/document.js b/src/models/document.js index 4c9a36535..f3f663af8 100644 --- a/src/models/document.js +++ b/src/models/document.js @@ -83,9 +83,7 @@ class Document extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/inline.js b/src/models/inline.js index 4ffa22ed1..0c8e8bebe 100644 --- a/src/models/inline.js +++ b/src/models/inline.js @@ -109,9 +109,7 @@ class Inline extends new Record(DEFAULTS) { */ get text() { - return this.nodes - .map(node => node.text) - .join('') + return this.getText() } } diff --git a/src/models/node.js b/src/models/node.js index e4ff92ec5..052a6b863 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -36,6 +36,17 @@ const Node = { return Set(keys) }, + /** + * Get the concatenated text `string` of all child nodes. + * + * @return {String} text + */ + + getText() { + return this.nodes + .reduce((result, node) => result + node.text, '') + }, + /** * Assert that a node has a child by `key` and return it. * @@ -1315,6 +1326,7 @@ const Node = { */ memoize(Node, [ + 'getText', 'getAncestors', 'getBlocks', 'getBlocksAtRange', diff --git a/src/models/text.js b/src/models/text.js index 343e24a9a..5e821f900 100644 --- a/src/models/text.js +++ b/src/models/text.js @@ -112,8 +112,7 @@ class Text extends new Record(DEFAULTS) { get text() { return this.characters - .map(char => char.text) - .join('') + .reduce((result, char) => result + char.text, '') } /** From af911fd0fff7e61e51f3f5cf790d514c71485567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 3 Nov 2016 17:03:46 +0100 Subject: [PATCH 6/6] Use Node.forEachDescendant in Node.findDescendantDeep --- src/models/node.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/models/node.js b/src/models/node.js index 052a6b863..8a17364ed 100644 --- a/src/models/node.js +++ b/src/models/node.js @@ -159,18 +159,16 @@ const Node = { */ findDescendantDeep(iterator) { - let descendantFound = null + let found - const found = this.nodes.find(node => { - if (node.kind != 'text') { - descendantFound = node.findDescendantDeep(iterator) - return descendantFound || iterator(node) + this.forEachDescendant(node => { + if (iterator(node)) { + found = node + return false } - - return iterator(node) ? node : null }) - return descendantFound || found + return found }, /**