html

This tag HTML encodes and decodes.

Created by: hackvertor
Installed 1 times

Category: Convert

Created on: Thursday, October 3, 2024 at 9:13:40 PM

Updated on: Friday, December 20, 2024 at 8:08:04 PM

This is a built in tag
Tag arguments
[]
Code
class html {
  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);
  }
}