From 273304a4d3b6d4d964e1fb125bc15132c53e6599 Mon Sep 17 00:00:00 2001 From: Jens Oliver Meiert Date: Tue, 24 Nov 2026 15:17:50 +0100 Subject: [PATCH] fix: preserve spaces after removed inlines MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Keep whitespace when `removeEmptyElements` removes an inline element after an element that doesn’t render, such as a non-empty `script`. Add regression coverage and document the fix in the changelog. (This commit message was AI-generated.) --- CHANGELOG.md | 6 ++++++ src/htmlminifier.js | 7 ++++--- test/html.test.js | 3 +++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index af3af12c..6363f260 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ As of version 5.0.0, all notable changes to HTML Minifier (HM) are documented in The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and the project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +* Fixed `removeEmptyElements` with `collapseWhitespace` dropping the space after an empty inline element it removes right after an element that doesn’t render, like a non-empty `script` (e.g., `

a b

` became `

ab

`) + ## [5.4.1] - 2026-11-22 ### Fixed diff --git a/src/htmlminifier.js b/src/htmlminifier.js index e061299f..047a6d8c 100644 --- a/src/htmlminifier.js +++ b/src/htmlminifier.js @@ -1046,7 +1046,8 @@ async function minifyHTML(value, options, partialMarkup) { // Entries changed while a start tag trims, with what they were, while recording /** @type {Array<[number, string]> | null} */ let trimsRecorded = null; - // Whether the last element is a removed block, which text after it reads past + // Whether the last element is a removed block (or one removed after an element + // that doesn’t render), which text after it reads past let followsRemovedBlock = false; // End tags not written, for the start tag that comes next (`text` is empty where one can’t be) /** @type {Array<{tag: string, text: string}>} */ @@ -2914,8 +2915,8 @@ async function minifyHTML(value, options, partialMarkup) { } if (openElement.before.gapIndex !== -1) transparentGapIndex = openElement.before.gapIndex; } - // @@ Keep space by removed inline elements near `script` (`a c`) - followsRemovedBlock = !inlineElements.has(lowerTag || tag) && !inlineElementsTransparent.has(lowerTag || tag); + // Text after it reads past it if it’s a block or follows an element that doesn’t render + followsRemovedBlock = !inlineElements.has(lowerTag || tag) && !inlineElementsTransparent.has(lowerTag || tag) || inlineElementsTransparentAfter.has(charsPrevTag); // What it made optional is up to what comes next now if (openElement?.omittedEndTag) { bufferPush(openElement.omittedEndTag.text, true); diff --git a/test/html.test.js b/test/html.test.js index 77b23779..6662b1de 100644 --- a/test/html.test.js +++ b/test/html.test.js @@ -5583,6 +5583,9 @@ describe('HTML', () => { assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeComments: true }), '

a b

'); assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeComments: true }), '

a b

'); assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

'); + assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

'); + assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

'); + assert.strictEqual(await minify('

ab

', { collapseWhitespace: true, removeEmptyElements: true }), '

ab

'); assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

'); assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

'); assert.strictEqual(await minify('

a b

', { collapseWhitespace: true, removeEmptyElements: true }), '

a b

');