nest

This tag will nest XML nodes to a given depth. E.g. if the input is <a>test</a> the result will be: <a><a><a>test</a></a></a> for a depth of 3

Created by: hackvertor
Installed 1 times

Viewed: 0

Category: XML

Created on: Friday, September 5, 2025 at 10:50:21 AM

Updated on: Friday, September 5, 2025 at 12:30:27 PM

This is a built in tag
Tag arguments
[
   {
      "type": "number",
      "help": "This provides the depth amount",
      "defaultValue": "512"
   }
]
Code
class nest {
  encode(input, depth) {
    if (depth <= 0) return input;
    const m = input.match(/^<([a-zA-Z][\w:-]*)([^>]*)>([\s\S]*)<\/\1>$/);
    if (!m) throw new Error("Input must be a single root element");
    const tag = m[1];
    const attrs = m[2];
    const open = `<${tag}${attrs}>`;
    const close = `</${tag}>`;
    let content = m[3];
    for (let i = 0; i < depth; i++) {
      content = open + content + close;
    }
    return content;
  }
}