Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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., `<p>a<script>1</script><span></span> b</p>` became `<p>a<script>1</script>b</p>`)

## [5.4.1] - 2026-11-22

### Fixed
Expand Down
7 changes: 4 additions & 3 deletions src/htmlminifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}>} */
Expand Down Expand Up @@ -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<script>1</script><b></b> 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);
Expand Down
3 changes: 3 additions & 0 deletions test/html.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5583,6 +5583,9 @@ describe('HTML', () => {
assert.strictEqual(await minify('<p>a<script></script> <!--c--><!--d--><!--e--><script></script>b</p>', { collapseWhitespace: true, removeComments: true }), '<p>a<script></script> <script></script>b</p>');
assert.strictEqual(await minify('<p>a<script></script> <!--c--><!--d--> <script></script>b</p>', { collapseWhitespace: true, removeComments: true }), '<p>a<script></script> <script></script>b</p>');
assert.strictEqual(await minify('<p>a<script></script><span></span> b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a b</p>');
assert.strictEqual(await minify('<p>a<script>1</script><span></span> b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a<script>1</script> b</p>');
assert.strictEqual(await minify('<p>a<script>1</script><span></span><b> </b> <i>b</i></p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a<script>1</script> <i>b</i></p>');
assert.strictEqual(await minify('<p>a<script>1</script><span></span>b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a<script>1</script>b</p>');
assert.strictEqual(await minify('<p>a<link rel="x"><span></span> b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a<link rel="x"> b</p>');
assert.strictEqual(await minify('<p>a<link rel="x"> <span></span> <link rel="y">b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a<link rel="x"> <link rel="y">b</p>');
assert.strictEqual(await minify('<p>a<script></script> <script></script> <script></script>b</p>', { collapseWhitespace: true, removeEmptyElements: true }), '<p>a b</p>');
Expand Down