From a0a884f5bbbd791c98a3edcd146a489a7f73d87d Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 6 Nov 2011 23:32:38 -0500 Subject: [PATCH] more convenience --- dom.d | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/dom.d b/dom.d index bd694b0..fc44c49 100644 --- a/dom.d +++ b/dom.d @@ -394,6 +394,12 @@ class Element { return e; } + /// . + void appendChildren(Element[] children) { + foreach(ele; children) + appendChild(ele); + } + /// Inserts the second element to this node, right before the first param Element insertBefore(Element where, Element what) in { @@ -520,6 +526,21 @@ class Element { return appendChild(e); } + /// Convenience function to append text intermixed with other children. + /// For example: div.addChildren("You can visit my website by ", new Link("mysite.com", "clicking here"), "."); + /// or div.addChildren("Hello, ", user.name, "!"); + + /// See also: appendHtml. This might be a bit simpler though because you don't have to think about escaping. + void addChildren(T...)(T t) { + foreach(item; t) { + static if(is(item : Element)) + appendChild(item); + else static if (is(isSomeString!(item))) + appendText(to!string(item)); + else static assert(0, "Cannot pass " ~ typeof(item).stringof ~ " to addChildren"); + } + } + ///. Element addChild(string tagName, Element firstChild) in { @@ -3400,6 +3421,8 @@ string camelCase(string a) { } ///. + +// FIXME: use the better parser from html.d class CssStyle { ///. this(string rule, string content) {