From 8f746f9ddeb0bef7b4ef55a71b3e97e6de0a1907 Mon Sep 17 00:00:00 2001 From: Jermolene Date: Sat, 1 Aug 2015 13:14:32 +0100 Subject: [PATCH] Add parse tree utility for collecting text --- core/modules/utils/parsetree.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/modules/utils/parsetree.js b/core/modules/utils/parsetree.js index 26c7f9a1b..c61c728fd 100644 --- a/core/modules/utils/parsetree.js +++ b/core/modules/utils/parsetree.js @@ -56,4 +56,24 @@ exports.findParseTreeNode = function(nodeArray,search) { return undefined; }; +/* +Helper to get the text of a parse tree node or array of nodes +*/ +exports.getParseTreeText = function getParseTreeText(tree) { + var output = []; + if($tw.utils.isArray(tree)) { + $tw.utils.each(tree,function(node) { + output.push(getParseTreeText(node)); + }); + } else { + if(tree.type === "text") { + output.push(tree.text); + } + if(tree.children) { + return getParseTreeText(tree.children); + } + } + return output.join(""); +}; + })();