From 5b723e8e5141f0feddfc75c07187c3807f3cceac Mon Sep 17 00:00:00 2001 From: Jean Date: Thu, 14 Aug 2025 20:30:18 -0700 Subject: [PATCH] Fixing Node element support --- metamorpov.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/metamorpov.js b/metamorpov.js index ae38e33..953c0da 100644 --- a/metamorpov.js +++ b/metamorpov.js @@ -11,14 +11,20 @@ exports.translate = function(input, options) { options: options, } - if (typeof(input) == "object" && input instanceof Node) { - request.loop = loopNode; + try { + if (typeof(input) == "object" && input instanceof Node) { + request.loop = loopNode; + } + else if (typeof(input) == "string") { + request.loop = loopString; + } + else { + console.error("Input of type " + typeof(input) + " is unsupported"); + return; + } } - else if (typeof(input) == "string") { - request.loop = loopString; - } - else { - console.error("Invalid input type"); + catch { + console.error("Translation of Node objects is unsupported outside of a browser context"); return; } @@ -71,15 +77,28 @@ function loopNode(request, params, method) { child = node.firstChild; while (child) { next = child.nextSibling; - loopNode(node, params, method); + loopNode({ + input: child, + options: request.options + }, params, method); child = next; } break; case 3: /* TEXT_NODE */ - loopString(node.nodeValue, params, method); + loopNodeValue({ + input: node, + options: request.options + }, params, method); } } +/* Iterator for strings belonging to HTML Node elements */ +function loopNodeValue(request, params, method) { + if (request.input.nodeValue.match(params.regexp) == null) { return; } + request.input.nodeValue = method(request.input.nodeValue, params); + loopNodeValue(request, params, method); +} + /* Iterator for string elements */ function loopString(request, params, method) { if (request.input.match(params.regexp) == null) { return; }