From 25f067544fb40c058d841fd2917de34092342bb1 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Fri, 3 Dec 2021 10:28:09 -0500 Subject: [PATCH] treat empty text nodes as still empty for selectors and indentation purposes --- dom.d | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/dom.d b/dom.d index a693f4b..2ad083b 100644 --- a/dom.d +++ b/dom.d @@ -3372,8 +3372,31 @@ class Element : DomParent { return writeToAppender(); } + /++ + Returns if the node would be printed to string as `` or ``. In other words, if it has no non-empty text nodes and no element nodes. Please note that whitespace text nodes are NOT considered empty; `Html(" ").isEmpty == false`. + + + The value is undefined if there are comment or processing instruction nodes. The current implementation returns false if it sees those, assuming the nodes haven't been stripped out during parsing. But I'm not married to the current implementation and reserve the right to change it without notice. + + History: + Added December 3, 2021 (dub v10.5) + + +/ + public bool isEmpty() const { + foreach(child; this.children) { + // any non-text node is of course not empty since that's a tag + if(child.nodeType != NodeType.Text) + return false; + // or a text node is empty if it is is a null or empty string, so this length check fixes that + if(child.nodeValue.length) + return false; + } + + return true; + } + protected string toPrettyStringIndent(bool insertComments, int indentationLevel, string indentWith) const { - if(indentWith is null || this.children.length == 0) + if(indentWith is null || this.isEmpty()) return null; string s; @@ -5840,7 +5863,7 @@ int intFromHex(string hex) { } +/ if(emptyElement) { - if(e.children.length) + if(e.isEmpty()) return false; } if(whitespaceOnly) { @@ -7979,6 +8002,7 @@ unittest { // toPrettyString is not stable, but these are some best-effort attempts // despite these being in a test, I might change these anyway! assert(Element.make("a").toPrettyString == ""); + assert(Element.make("a", "").toPrettyString(false, 0, " ") == ""); assert(Element.make("a", "b").toPrettyString == "b"); assert(Element.make("a", "b").toPrettyString(false, 0, "") == "b");