'use strict';

const encodeURL = require('./encode_url');
const escapeHTML = require('./escape_html');

function htmlTag(tag, attrs, text, escape = true) {
  if (!tag) throw new TypeError('tag is required!');

  let result = `<${escapeHTML(tag)}`;

  for (const i in attrs) {
    if (attrs[i] === null || typeof attrs[i] === 'undefined') result += '';
    else {
      if (i.includes('href') || i.includes('src') || i.includes('url')) result += ` ${i}="${encodeURL(attrs[i])}"`;
      else result += ` ${escapeHTML(i)}="${escapeHTML(String(attrs[i]))}"`;
    }
  }

  if (escape && text) text = escapeHTML(String(text));

  if (text === null || typeof text === 'undefined') result += '>';
  else result += `>${text}</${escapeHTML(tag)}>`;

  return result;
}

module.exports = htmlTag;

@@ -368,24 +368,24 @@
   if (i.
-includes
+endsWith
 ('href')
@@ -390,24 +390,24 @@
 ') %7C%7C i.
-includes
+endsWith
 ('src') 
@@ -415,16 +415,16 @@
 %7C i.
-includes
+endsWith
 ('ur

use String.endsWith() to not match srcset attribute