html-minifier
Version:
Highly effective, super-configurable, well-tested web page minifier
280 lines (255 loc) • 7.86 kB
JavaScript
/**
* HTML element tag omission rules
*/
import {
headerElements,
descriptionElements,
pBlockElements,
rubyEndTagOmission,
rubyRtcEndTagOmission,
optionElements,
tableContentElements,
tableSectionElements,
cellElements
} from './constants.js';
import { hasAttrName, nameAttrOut, removesEmptyAttribute } from './attributes.js';
/** @import { HTMLAttribute } from './attributes.js' */
/** @import { ProcessedOptions } from './options.js' */
/**
* @param {string} optionalStartTag
* @param {string} tag
*/
function canRemoveParentTag(optionalStartTag, tag) {
switch (optionalStartTag) {
case 'html':
case 'head':
return true;
case 'body':
return !headerElements.has(tag);
case 'colgroup':
return tag === 'col';
case 'tbody':
return tag === 'tr';
}
return false;
}
/**
* @param {string} optionalEndTag
* @param {string} tag
*/
function isStartTagMandatory(optionalEndTag, tag) {
switch (tag) {
case 'colgroup':
return optionalEndTag === 'colgroup';
case 'tbody':
return tableSectionElements.has(optionalEndTag);
}
return false;
}
/**
* @param {string} optionalEndTag
* @param {string} tag
*/
function canRemovePrecedingTag(optionalEndTag, tag) {
switch (optionalEndTag) {
case 'html':
case 'head':
case 'body':
case 'colgroup':
case 'caption':
return true;
case 'li':
case 'optgroup':
case 'tr':
return tag === optionalEndTag;
case 'dt':
case 'dd':
return descriptionElements.has(tag);
case 'p':
return pBlockElements.has(tag);
case 'rb':
case 'rt':
case 'rp':
return rubyEndTagOmission.has(tag);
case 'rtc':
return rubyRtcEndTagOmission.has(tag);
case 'option':
return optionElements.has(tag);
case 'thead':
case 'tbody':
return tableContentElements.has(tag);
case 'tfoot':
return tag === 'tbody';
case 'td':
case 'th':
return cellElements.has(tag);
}
return false;
}
/**
* @param {string} tag
* @param {HTMLAttribute[]} attrs
* @param {ProcessedOptions} options
* @param {string} [tagOut] - As hooks get it
*/
function canRemoveElement(tag, attrs, options, tagOut = tag) {
// Elements with `id` attribute must never be removed—they serve as:
// - Navigation targets (skip links, URL fragments)
// - JavaScript selector targets (`getElementById`, `querySelector`)
// - CSS targets (`:target` pseudo-class, ID selectors)
// - Accessibility landmarks (ARIA references)
// - Portal mount points (React portals, etc.)
// An empty one that `removeEmptyAttributes` removes does none of this
const id = attrs.find(attr => attr.name.toLowerCase() === 'id');
const idName = id && options.name(id.name);
if (id && !(idName === 'id' && removesEmptyAttribute(tag, idName, id.value, options, tagOut, nameAttrOut(id, idName, options)))) {
return false;
}
// Nor must elements that carry accessibility semantics, take focus, or point elsewhere
// with `for` (a `label` activates a control, an `output` shows its result, a `template`
// replaces content, even with nothing), unless the value is empty
const takesFor = tag === 'label' || tag === 'output' || tag === 'template';
for (const attr of attrs) {
const name = attr.name.toLowerCase();
if ((name === 'role' || name === 'tabindex' || name.startsWith('aria-') || (takesFor && name === 'for')) && attr.value?.trim()) {
return false;
}
}
// Custom elements and `canvas` are there for scripts to fill or draw into
if (tag.includes('-')) {
return false;
}
switch (tag) {
// Without a `frameset`, browsers render what follows it
case 'canvas':
case 'frameset':
case 'textarea':
return false;
// Browsers create them anyway, but not with their attributes
case 'body':
case 'head':
case 'html':
return attrs.length === 0;
case 'audio':
case 'script':
case 'video':
if (hasAttrName('src', attrs)) {
return false;
}
break;
case 'iframe':
if (hasAttrName('src', attrs) || hasAttrName('srcdoc', attrs)) {
return false;
}
break;
case 'object':
if (hasAttrName('data', attrs)) {
return false;
}
break;
case 'applet':
if (hasAttrName('code', attrs)) {
return false;
}
break;
}
return true;
}
/**
* @param {string} str - Tag name or HTML-like element spec (e.g., `td` or `<span aria-hidden='true'>`)
* @param {ProcessedOptions} options - Options object for name normalization
* @returns {{tag: string, attrs: Object.<string, string|undefined>|null}|null} Parsed spec or null if invalid
*/
function parseElementSpec(str, options) {
const trimmed = str.trim();
if (!trimmed) {
return null;
}
// Simple tag name: `td`
if (!/[<>]/.test(trimmed)) {
return { tag: options.name(trimmed), attrs: null };
}
// HTML-like markup: `<span aria-hidden='true'>` or `<td></td>`
const match = trimmed.match(/^<([a-zA-Z][\w:-]*)((?:\s+[^>]*)?)>/);
if (!match) {
return null;
}
const tag = options.name(match[1] ?? '');
const attrString = match[2] ?? '';
if (!attrString.trim()) {
return { tag, attrs: null };
}
/** @type {{[key: string]: string | undefined}} */
const attrs = {};
const attrRegex = /([a-zA-Z][\w:-]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>/]+)))?/g;
let attrMatch;
while ((attrMatch = attrRegex.exec(attrString))) {
const attrName = options.name(attrMatch[1] ?? '');
const attrValue = attrMatch[2] ?? attrMatch[3] ?? attrMatch[4];
// Boolean attributes have no value (undefined)
attrs[attrName] = attrValue;
}
return {
tag,
attrs: Object.keys(attrs).length > 0 ? attrs : null
};
}
/**
* @param {string[]} input - Array of element specifications from `removeEmptyElementsExcept` option
* @param {ProcessedOptions} options - Options object for parsing
* @returns {Array<{tag: string, attrs: Object.<string, string|undefined>|null}>} Array of parsed element specs
*/
function parseRemoveEmptyElementsExcept(input, options) {
return /** @type {Array<{tag: string, attrs: {[x: string]: string | undefined} | null}>} */ (input.map(item => {
if (typeof item === 'string') {
const spec = parseElementSpec(item, options);
if (!spec) {
options.log('Warning: Unable to parse `removeEmptyElementsExcept` specification: “' + item + '”');
}
return spec;
}
options.log('Warning: `removeEmptyElementsExcept` specification must be a string, received: ' + typeof item);
return null;
}).filter(Boolean));
}
/**
* @param {string} tag - Element tag name
* @param {HTMLAttribute[]} attrs - Array of element attributes
* @param {Array<{tag: string, attrs: Object.<string, string|undefined>|null}>} preserveList - Parsed preserve specs
* @returns {boolean} True if the empty element should be preserved
*/
function shouldPreserveEmptyElement(tag, attrs, preserveList) {
for (const spec of preserveList) {
if (spec.tag !== tag) {
continue;
}
if (!spec.attrs) {
return true;
}
const allAttrsMatch = Object.entries(spec.attrs).every(([name, value]) => {
const attr = attrs.find(a => a.name === name);
if (!attr) {
return false; // Attribute not present
}
// Boolean attribute in spec (undefined value) matches if attribute is present
if (value === undefined) {
return true;
}
return attr.value === value;
});
if (allAttrsMatch) {
return true;
}
}
return false;
}
export {
// Tag omission
canRemoveParentTag,
isStartTagMandatory,
canRemovePrecedingTag,
// Element removal
canRemoveElement,
parseRemoveEmptyElementsExcept,
shouldPreserveEmptyElement
};