Fixing Node element support

This commit is contained in:
Jean Viscogliosi-Pate 2025-08-14 20:30:18 -07:00
parent 05b866c9ad
commit 5b723e8e51
1 changed files with 28 additions and 9 deletions

View File

@ -11,14 +11,20 @@ exports.translate = function(input, options) {
options: options, options: options,
} }
if (typeof(input) == "object" && input instanceof Node) { try {
request.loop = loopNode; 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") { catch {
request.loop = loopString; console.error("Translation of Node objects is unsupported outside of a browser context");
}
else {
console.error("Invalid input type");
return; return;
} }
@ -71,15 +77,28 @@ function loopNode(request, params, method) {
child = node.firstChild; child = node.firstChild;
while (child) { while (child) {
next = child.nextSibling; next = child.nextSibling;
loopNode(node, params, method); loopNode({
input: child,
options: request.options
}, params, method);
child = next; child = next;
} }
break; break;
case 3: /* TEXT_NODE */ 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 */ /* Iterator for string elements */
function loopString(request, params, method) { function loopString(request, params, method) {
if (request.input.match(params.regexp) == null) { return; } if (request.input.match(params.regexp) == null) { return; }