// HTML Formatter // https://github.com/uznam8x/html-formatter // MIT License const single = [ 'br', 'hr', 'img', 'input', 'meta', 'link', 'col', 'base', 'param', 'track', 'source', 'wbr', 'command', 'keygen', 'area', 'embed', 'menuitem' ]; const closing = function(el) { el = el.replace(/<([a-zA-Z\-0-9]+)[^>]*>/g, function(match, capture) { if (single.indexOf(capture) > -1) { return (match.substring(0, match.length - 1) + ' />').replace(/\/\s\//g, '/'); } return match.replace(/[\s]?\/>/g, '>'); }); return el; }; const entity = function(el) { el = el.replace(/(]*>)\n\s+/g, '$1'); el = el.replace(/\s+<\/textarea>/g, ''); el = el.replace(/]*>((.|\n)*?)<\/textarea>/g, function(match, capture) { return match.replace(capture, function(match) { return match .replace(//g, '>') .replace(/\//g, '/') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/\n/g, ' ') .replace(/%/g, '%') .replace(/\{/g, '{') .replace(/\}/g, '}') .replace(/\s/g, ' '); }); }); return el; }; const minify = function(el) { return el .replace(/\n|\t/g, '') .replace(/[a-z]+="\s*"/ig, '') .replace(/>\s+<') .replace(/\s+/g, ' ') .replace(/\s>/g, '>') .replace(/class=["']\s/g, function(match) { return match.replace(/\s/g, ''); }) ; }; const convert = { comment: [], line: [] }; const comment = function (el) { convert.comment = []; el = el.replace(/()/g, function (match) { convert.comment.push(match); return ''; }); return el; }; const line = function (el) { convert.line = []; let i = -1; el = el.replace(/<[^>]*>/g, function (match) { convert.line.push(match); i++; return '\n[#-# : ' + i + ' : ' + match + ' : #-#]\n'; }); el = el.replace(/\n\n/g, '\n'); return el; }; const tidy = function (el) { let tab = ''; convert.line.forEach(function (source, index) { el = el.replace('[#-# : ' + index + ' : ' + source + ' : #-#]', function (match) { const prevLine = '[#-# : ' + (index - 1) + ' : ' + convert.line[index - 1] + ' : #-#]'; tab += '\t'; let remove = 0; if (index === 0) remove++; if (match.indexOf('#-# : ' + (index) + ' : -1) remove++; if (prevLine.indexOf(' -1) remove++; if (prevLine.indexOf('', 'g'), function (match) { const cnt = /%(\d+)%/g.exec(match); const t = generateTab(cnt[1]); return source.replace(/\n/g, '\n'+t); }); }); return el.substring(1, el.length - 1); }; const render = function (el, opt) { el = closing(el); el = comment(el); el = entity(el); el = minify(el); el = line(el); el = tidy(el); return el; }; // Export for ES modules export { closing, entity, minify, render }; export default render;