From a556b7dc0190155752b1bfbfef5be40b1c56dda1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 16 Sep 2024 12:04:52 +0200 Subject: [PATCH 1/3] Add `.tsbuildinfo` to `.gitignore` --- .gitignore | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 40205b1..b5761b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,8 @@ -.DS_Store +coverage/ +node_modules/ *.d.ts.map *.d.ts *.log -coverage/ -node_modules/ +*.tsbuildinfo +.DS_Store yarn.lock From 1c938b92d411076077e72bcdb8fc1bf8134cb117 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 16 Sep 2024 12:04:57 +0200 Subject: [PATCH 2/3] Fix `head` opening tag omission w/o title While typically a `title` is needed, there are cases where `` can be empty, in which case, the start tag can be omitted. Closes GH-43. --- lib/omission/opening.js | 22 ++++++++++++---------- test/omission-closing-head.js | 8 ++++---- test/omission-opening-head.js | 9 ++++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/omission/opening.js b/lib/omission/opening.js index edc878c..54e3520 100644 --- a/lib/omission/opening.js +++ b/lib/omission/opening.js @@ -37,23 +37,25 @@ function html(node) { * Whether the opening tag can be omitted. */ function head(node) { - const children = node.children - /** @type {Array} */ - const seen = [] - let index = -1 + /** @type {Set} */ + const seen = new Set() - while (++index < children.length) { - const child = children[index] + // Whether `srcdoc` or not, + // make sure the content model at least doesn’t have too many `base`s/`title`s. + for (const child of node.children) { if ( child.type === 'element' && - (child.tagName === 'title' || child.tagName === 'base') + (child.tagName === 'base' || child.tagName === 'title') ) { - if (seen.includes(child.tagName)) return false - seen.push(child.tagName) + if (seen.has(child.tagName)) return false + seen.add(child.tagName) } } - return children.length > 0 + // “May be omitted if the element is empty, + // or if the first thing inside the head element is an element.” + const child = node.children[0] + return !child || child.type === 'element' } /** diff --git a/test/omission-closing-head.js b/test/omission-closing-head.js index 3460d52..aae6efc 100644 --- a/test/omission-closing-head.js +++ b/test/omission-closing-head.js @@ -6,7 +6,7 @@ import {u} from 'unist-builder' test('`head` (closing)', async function (t) { await t.test('should omit tag without following', async function () { - assert.deepEqual(toHtml(h('head'), {omitOptionalTags: true}), '') + assert.deepEqual(toHtml(h('head'), {omitOptionalTags: true}), '') }) await t.test( @@ -16,7 +16,7 @@ test('`head` (closing)', async function (t) { toHtml(h('html', [h('head'), u('comment', 'alpha')]), { omitOptionalTags: true }), - '' + '' ) } ) @@ -26,7 +26,7 @@ test('`head` (closing)', async function (t) { async function () { assert.deepEqual( toHtml(h('html', [h('head'), ' alpha']), {omitOptionalTags: true}), - ' alpha' + ' alpha' ) } ) @@ -38,7 +38,7 @@ test('`head` (closing)', async function (t) { toHtml(h('html', [h('head'), u('text', 'alpha')]), { omitOptionalTags: true }), - 'alpha' + 'alpha' ) } ) diff --git a/test/omission-opening-head.js b/test/omission-opening-head.js index c53c799..0932964 100644 --- a/test/omission-opening-head.js +++ b/test/omission-opening-head.js @@ -11,9 +11,12 @@ test('`head` (opening)', async function (t) { ) }) - await t.test('should not omit tag without children', async function () { - assert.deepEqual(toHtml(h('head'), {omitOptionalTags: true}), '') - }) + await t.test( + 'should omit tag without children (could be fine in `srcdoc`)', + async function () { + assert.deepEqual(toHtml(h('head'), {omitOptionalTags: true}), '') + } + ) await t.test('should omit tag with `title`', async function () { assert.deepEqual( From fb74df81557311c06d613ea671d15a6f64f10e26 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Mon, 16 Sep 2024 12:09:05 +0200 Subject: [PATCH 3/3] 9.0.3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cdbb819..c96fc04 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "hast-util-to-html", - "version": "9.0.2", + "version": "9.0.3", "description": "hast utility to serialize to HTML", "license": "MIT", "keywords": [