<commit_before>import htmlparser from 'htmlparser';

const VOID_HTML_ELEMENT_MAP = {
  area: true,
  base: true,
  br: true,
  col: true,
  command: true,
  embed: true,
  hr: true,
  img: true,
  input: true,
  keygen: true,
  link: true,
  meta: true,
  param: true,
  source: true,
  track: true,
  wbr: true
};

function isStringURL(item) {
  return typeof item === 'string' &&
    item !== '';
}

export default class HTMLEntrypoint {
  html;
  nodes;

  constructor(html) {
    this.html = html;
    var handler = new htmlparser.DefaultHandler();
    var parser = new htmlparser.Parser(handler);
    parser.parseComplete(this.html);
    this.nodes = handler.dom;
  }

  toHTML(nodeSet) {
    const html = [];
    const targetNodes = nodeSet || this.nodes;

    if (targetNodes instanceof Array) {
      targetNodes.forEach(node => {
        switch (node.type) {
          case 'text':
            html.push(node.data);
            break;
          case 'directive':
            html.push('<' + node.data + '>');
            break;
          case 'comment':
            html.push('<!-- ' + node.data + ' -->');
            break;
          default:
            html.push('<' + node.data.replace(/\/$/, ''));
            if (!/\/$/.test(node.data) && !VOID_HTML_ELEMENT_MAP[node.name]) {
              html.push('>');
              if (node.children instanceof Array) {
                html.push(this.toHTML(node.children));
              }
              html.push('</' + node.name + '>');
            } else if (!/\/$/.test(node.data)) {
              html.push('>');
            } else {
              html.push('/>');
            }
            break;
        }
      });
    }

    return html.join('');
  }

  getNodesByName(name, node) {
    let nodes = [];

    if (typeof name === 'string' && node instanceof Object) {
      if (node instanceof Array) {
        for (let i = 0; i < node.length; i++) {
          const child = node[i];

          if (child instanceof Object) {
            const childList = this.getNodesByName(name, child);

            nodes = [
              ...nodes,
              ...childList<commit_msg>feat(HTMLEntrypoint toHTML) Add hash.<commit_after>import htmlparser from 'htmlparser';
import Path from 'path';

const VOID_HTML_ELEMENT_MAP = {
  area: true,
  base: true,
  br: true,
  col: true,
  command: true,
  embed: true,
  hr: true,
  img: true,
  input: true,
  keygen: true,
  link: true,
  meta: true,
  param: true,
  source: true,
  track: true,
  wbr: true
};

function isStringURL(item) {
  return typeof item === 'string' &&
    item !== '';
}

export default class HTMLEntrypoint {
  static getPathWithHash(path, hash) {
    let newPath = path;

    if (
      typeof path === 'string' &&
      typeof hash === 'string'
    ) {
      newPath = `${Path.basename(path, Path.extname(path))}.${hash}${Path.extname(path)}`;
    }

    return newPath;
  }

  html;
  nodes;

  constructor(html) {
    this.html = html;
    var handler = new htmlparser.DefaultHandler();
    var parser = new htmlparser.Parser(handler);
    parser.parseComplete(this.html);
    this.nodes = handler.dom;
  }

  toHTML(nodeSet, hash) {
    const html = [];
    const targetNodes = nodeSet || this.nodes;

    if (targetNodes instanceof Array) {
      targetNodes.forEach(node => {
        switch (node.type) {
          case 'text':
            html.push(node.data);
            break;
          case 'directive':
            html.push('<' + node.data + '>');
            break;
          case 'comment':
            html.push('<!-- ' + node.data + ' -->');
            break;
          default:
            if (node.attribs instanceof Object) {
              const attribList = [];

              for (const k in node.attribs) {
                if (node.attribs.hasOwnProperty(k)) {
                  let attrValue = node.attribs[k];

                  if (
                    (
                      node.name.toLowerCase() === 'link' &&
                      k.toLowerCase() === 'href'
                    ) ||
                    (
                      (
                        node.name.toLowerCase() === 'script' ||
                        node.name.toLowerCase() === 'img'
                      ) &&
                      k.toLowerCase() === 'src'
                    )
                  ) {
                    attrValue = HTMLEntrypoint.getPathWithHash(attrValue, hash);
                  }

                  attribList.push(`${k}="${attrValue}"`);
                }
              }

              html.push(`<${node.name} ${attribList.join(' ')}`);
            } else {
              html.push('<' + node.data.replace(/\/$/, ''));
            }

            if (!/\/$/.test(node.data) && !VOID_HTML_ELEMENT_MAP[node.name]) {
              html.push('>');
              if (node.children instanceof Array) {
                html.push(this.toHTML(node.children, hash));
              }
              html.push('</' + node.name + '>');
            } else if (!/\/$/.test(node.data)) {
              html.push('>');
            } else {
              html.push('/>');
            }
            break;
        }
      });
    }

    return html.join('');
  }

  getNodesByName(name, node) {
    let nodes = [];

    if (typeof name === 'string' && node instanceof Object) {
      if (node instanceof Array) {
        for (let i = 0; i < node.length; i++) {
          const child = node[i];

          if (child instanceof Object) {
            const childList = this.getNodesByName(name, child);

            nodes = [
              ...nodes,
              ...childList
{'fraction_non_alphanumeric': 0.15118559458013905, 'fraction_numerical': 0.00035656979853806385, 'mean_word_length': 1.5224820143884892, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '662727', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1657, 'n_words': 364}