From f490b2ecee69ff8a3b472b88824d00057e23ccdc Mon Sep 17 00:00:00 2001 From: Katja Potensky <785327-hesxenon@users.noreply.gitlab.com> Date: Sun, 8 Dec 2024 13:06:53 +0100 Subject: [PATCH 1/2] exempt string values from being treated as known booleans unless empty or equal to name --- lib/handle/element.js | 4 ++-- test/attribute.js | 44 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/handle/element.js b/lib/handle/element.js index 17b4713..f20a58b 100644 --- a/lib/handle/element.js +++ b/lib/handle/element.js @@ -173,8 +173,8 @@ function serializeAttribute(state, key, value) { if (info.overloadedBoolean && (value === info.attribute || value === '')) { value = true } else if ( - info.boolean || - (info.overloadedBoolean && typeof value !== 'string') + (typeof value !== 'string' || value === '' || value === info.attribute) && + (info.boolean || info.overloadedBoolean) ) { value = Boolean(value) } diff --git a/test/attribute.js b/test/attribute.js index 99e2a5e..4659203 100644 --- a/test/attribute.js +++ b/test/attribute.js @@ -140,6 +140,7 @@ test('`element` attributes', async (t) => { } ) + // TODO: why? "hidden" is a valid value for the `hidden` attribute await t.test( 'should serialize known booleans set to their name without value', async function () { @@ -161,6 +162,49 @@ test('`element` attributes', async (t) => { ) } ) + + await t.test( + 'should serialize known booleans set to arbitrary strings with value', + async function () { + assert.deepEqual( + toHtml( + h('div', { + selected: 'some string value for a well known boolean attribute' + }) + ), + '
' + ) + } + ) + + await t.test( + 'should serialize known booleans set to an empty string without value', + async function () { + assert.deepEqual( + toHtml( + h('div', { + selected: '' + }) + ), + '
' + ) + } + ) + + await t.test( + 'should serialize known overloaded booleans set to arbitrary strings with value', + async function () { + assert.deepEqual( + toHtml( + h('div', { + download: + 'some string value for a well known overloaded boolean attribute' + }) + ), + '
' + ) + } + ) }) await t.test('should support known overloaded booleans', async function (t) { From b3726a4e326390941e26bdf10e99a966f9e3d342 Mon Sep 17 00:00:00 2001 From: Katja Potensky <785327-hesxenon@users.noreply.gitlab.com> Date: Fri, 13 Dec 2024 12:47:43 +0100 Subject: [PATCH 2/2] remove todo comment, question has been satisfied --- test/attribute.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/attribute.js b/test/attribute.js index 4659203..5180737 100644 --- a/test/attribute.js +++ b/test/attribute.js @@ -140,7 +140,6 @@ test('`element` attributes', async (t) => { } ) - // TODO: why? "hidden" is a valid value for the `hidden` attribute await t.test( 'should serialize known booleans set to their name without value', async function () {