svg

This tag HTML encodes and decodes.

Created by: RabihDigital
Installed 1 times

Category: Convert

Created on: Wednesday, February 26, 2025 at 1:49:03 AM

Updated on: Tuesday, March 11, 2025 at 1:59:20 PM

Tag arguments
[]
Code
class svg {
  encode(input) {
    const keys = Object.keys(hv.variables.htmlEntities).reverse();
    return input
      .split("")
      .map((chr) => {
        if (chr === "<") {
          return "&lt;";
        }
        if (chr === ">") {
          return "&gt;";
        }
        const entity = keys.find(
          (key) => hv.variables.htmlEntities[key] === chr.codePointAt(),
        );
        return entity ? "&" + entity + ";" : chr;
      })
      .join("");
  }

  decode(input) {
    const textarea = document.createElement("textarea");
    textarea.innerHTML = input;
    return textarea.value;
  }

  matches(input) {
    return /^(?:&[a-zA-Z]{1,20};)+/.exec(input);
  }
}