From b8f8f97634bbc424cefa7d3718e62a3663645bcd Mon Sep 17 00:00:00 2001 From: gazer Date: Fri, 13 Oct 2017 07:59:45 +0300 Subject: [PATCH] sanitize incoming border property --- src/dlangui/widgets/styles.d | 37 +++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/dlangui/widgets/styles.d b/src/dlangui/widgets/styles.d index 31e1af9c..b074663f 100644 --- a/src/dlangui/widgets/styles.d +++ b/src/dlangui/widgets/styles.d @@ -1501,6 +1501,32 @@ int decodeLayoutDimension(string s) { return decodeDimension(s); } +/// remove superfluous space characters from a border property +string sanitizeBorderProperty(string s) pure { + string[] parts = s.split(','); + foreach (ref part; parts) + part = part.strip(); + string joined = parts.join(','); + + char[] res; + // replace repeating space characters with one space + import std.ascii : isWhite; + bool isSpace; + foreach (c; joined) { + if (isWhite(c)) { + if (!isSpace) { + res ~= ' '; + isSpace = true; + } + } else { + res ~= c; + isSpace = false; + } + } + + return cast(string)res; +} + /// load style attributes from XML element bool loadStyleAttributes(Style style, Element elem, bool allowStates) { //Log.d("Theme: loadStyleAttributes ", style.id, " ", elem.tag.attr); @@ -1515,7 +1541,7 @@ bool loadStyleAttributes(Style style, Element elem, bool allowStates) { if ("padding" in elem.tag.attr) style.padding = decodeRect(elem.tag.attr["padding"]); if ("border" in elem.tag.attr) - style.border = elem.tag.attr["border"]; + style.border = sanitizeBorderProperty(elem.tag.attr["border"]); if ("align" in elem.tag.attr) style.alignment = decodeAlignment(elem.tag.attr["align"]); if ("minWidth" in elem.tag.attr) @@ -1714,3 +1740,12 @@ string overrideCustomDrawableId(string id) { shared static ~this() { currentTheme = null; } + + + + +unittest { + assert(sanitizeBorderProperty(" #aaa, 2 ") == "#aaa,2"); + assert(sanitizeBorderProperty(" #aaa, 2, 2, 2, 4") == "#aaa,2,2,2,4"); + assert(sanitizeBorderProperty(" #a aa , 2 4 ") == "#a aa,2 4"); +}