From abb95994e4cd00ede801d5c973aa7c4e77288854 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Fri, 7 Jun 2024 10:56:49 -0300 Subject: html: onlyIfSiblings --- src/util/html.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/util/html.js b/src/util/html.js index bd9f4eb7..310e5a76 100644 --- a/src/util/html.js +++ b/src/util/html.js @@ -53,11 +53,18 @@ export const attributeSpec = { }, }; -// Pass to tag() as an attributes key to make tag() return a 8lank string if the +// Pass to tag() as an attributes key to make tag() return a 8lank tag if the // provided content is empty. Useful for when you'll only 8e showing an element // according to the presence of content that would 8elong there. export const onlyIfContent = Symbol(); +// Pass to tag() as an attributes key to make tag() return a blank tag if +// this tag doesn't get shown beside any siblings! (I.e, siblings who don't +// also have the [html.onlyIfSiblings] attribute.) Since they'd just be blank, +// tags with [html.onlyIfSiblings] never make the difference in counting as +// content for [html.onlyIfContent]. Useful for and such. +export const onlyIfSiblings = Symbol(); + // Pass to tag() as an attributes key to make children be joined together by the // provided string. This is handy, for example, for joining lines by
tags, // or putting some other divider between each child. Note this will only have an @@ -124,13 +131,18 @@ function isBlankArrayHelper(content) { // content of tags marked onlyIfContent) into one array, // and templates into another. And if there's anything // else, that's a non-blank condition we'll detect now. + // We'll flat-out skip items marked onlyIfSiblings, + // since they could never count as content alone + // (some other item will have to count). const arrayContent = []; const templateContent = []; for (const item of nonStringContent) { if (item instanceof Tag) { - if (item.onlyIfContent || item.contentOnly) { + if (item.onlyIfSiblings) { + continue; + } else if (item.onlyIfContent || item.contentOnly) { arrayContent.push(item.content); } else { return false; @@ -416,6 +428,10 @@ export class Tag { } get blank() { + // Tags don't have a reference to their parent, so this only evinces + // something about this tag's own content or attributes. It does *not* + // account for [html.onlyIfSiblings]! + if (this.onlyIfContent && isBlank(this.content)) { return true; } @@ -477,6 +493,14 @@ export class Tag { return this.#getAttributeFlag(onlyIfContent); } + set onlyIfSiblings(value) { + this.#setAttributeFlag(onlyIfSiblings, value); + } + + get onlyIfSiblings() { + return this.#getAttributeFlag(onlyIfSiblings); + } + set joinChildren(value) { this.#setAttributeString(joinChildren, value); } -- cgit 1.3.0-6-gf8a5