diff --git a/dom.d b/dom.d index 0648e02..baf08f8 100644 --- a/dom.d +++ b/dom.d @@ -1450,9 +1450,9 @@ class Document : FileResource, DomParent { if(insertComments) s ~= "-->"; */ - s ~= root.toPrettyString(insertComments, indentationLevel, indentWith); + s ~= root.toPrettyStringImpl(insertComments, indentationLevel, indentWith); foreach(a; piecesAfterRoot) - s ~= a.toPrettyString(insertComments, indentationLevel, indentWith); + s ~= a.toPrettyStringImpl(insertComments, indentationLevel, indentWith); return s; } @@ -3372,7 +3372,7 @@ class Element : DomParent { } protected string toPrettyStringIndent(bool insertComments, int indentationLevel, string indentWith) const { - if(indentWith is null) + if(indentWith is null || this.children.length == 0) return null; string s; @@ -3388,8 +3388,40 @@ class Element : DomParent { /++ Writes out with formatting. Be warned: formatting changes the contents. Use ONLY for eyeball debugging. + + $(PITFALL + This function is not stable. Its interface and output may change without + notice. The only promise I make is that it will continue to make a best- + effort attempt at being useful for debugging by human eyes. + + I have used it in the past for diffing html documents, but even then, it + might change between versions. If it is useful, great, but beware; this + use is at your own risk. + ) + + History: + On November 19, 2021, I changed this to `final`. If you were overriding it, + change our override to `toPrettyStringImpl` instead. It now just calls + `toPrettyStringImpl.strip` to be an entry point for a stand-alone call. + + If you are calling it as part of another implementation, you might want to + change that call to `toPrettyStringImpl` as well. + + I am NOT considering this a breaking change since this function is documented + to only be used for eyeball debugging anyway, which means the exact format is + not specified and the override behavior can generally not be relied upon. + + (And I find it extremely unlikely anyone was subclassing anyway, but if you were, + email me, and we'll see what we can do. I'd like to know at least.) + + I reserve the right to make future changes in the future without considering + them breaking as well. +/ - string toPrettyString(bool insertComments = false, int indentationLevel = 0, string indentWith = "\t") const { + final string toPrettyString(bool insertComments = false, int indentationLevel = 0, string indentWith = "\t") const { + return toPrettyStringImpl(insertComments, indentationLevel, indentWith).strip; + } + + string toPrettyStringImpl(bool insertComments = false, int indentationLevel = 0, string indentWith = "\t") const { // first step is to concatenate any consecutive text nodes to simplify // the white space analysis. this changes the tree! but i'm allowed since @@ -3472,7 +3504,7 @@ class Element : DomParent { foreach(child; children) { assert(child !is null); - s ~= child.toPrettyString(insertComments, indentationLevel + 1, indentWith); + s ~= child.toPrettyStringImpl(insertComments, indentationLevel + 1, indentWith); } s ~= toPrettyStringIndent(insertComments, indentationLevel, indentWith); @@ -4093,10 +4125,10 @@ class DocumentFragment : Element { return this.innerHTML(where); } - override string toPrettyString(bool insertComments, int indentationLevel, string indentWith) const { + override string toPrettyStringImpl(bool insertComments, int indentationLevel, string indentWith) const { string s; foreach(child; children) - s ~= child.toPrettyString(insertComments, indentationLevel, indentWith); + s ~= child.toPrettyStringImpl(insertComments, indentationLevel, indentWith); return s; } @@ -4395,7 +4427,7 @@ class RawSource : SpecialElement { return source; } - override string toPrettyString(bool, int, string) const { + override string toPrettyStringImpl(bool, int, string) const { return source; } @@ -4429,7 +4461,7 @@ abstract class ServerSideCode : SpecialElement { return where.data[start .. $]; } - override string toPrettyString(bool, int, string) const { + override string toPrettyStringImpl(bool, int, string) const { return "<" ~ source ~ ">"; } @@ -4493,7 +4525,7 @@ class BangInstruction : SpecialElement { return where.data[start .. $]; } - override string toPrettyString(bool, int, string) const { + override string toPrettyStringImpl(bool, int, string) const { string s; s ~= ""); + assert(Element.make("a", "b").toPrettyString == "b"); + assert(Element.make("a", "b").toPrettyString(false, 0, "") == "b"); + + { + auto document = new Document("

hello world

"); + auto pretty = document.toPrettyString(false, 0, " "); + assert(pretty == +` + + +

hello world

+ +`, pretty); + } + + { + auto document = new XmlDocument("

hello world

"); + auto pretty = document.toPrettyString(false, 0, " "); + assert(pretty == +` + + +

hello + world +

+ +`, pretty); + } + + auto document = new XmlDocument("Hello\n \n \n How are you?\n \n \n Good & you?\n \n \n \n"); + assert(document.root.toPrettyString(false, 0, " ") == "\n \n Hello\n \n \n How are you?\n \n \n Good & you?\n \n \n \n"); + +} + /* Copyright: Adam D. Ruppe, 2010 - 2021 License: Boost License 1.0.