about summary refs log tree commit diff stats
path: root/src/css
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-11-22 23:57:50 +0100
committerbptato <nincsnevem662@gmail.com>2022-11-22 23:57:50 +0100
commitee930b0f5a587768d340c4204cf1f2e9fb818c89 (patch)
treedda0aeddce19b4e28a4c341d308d0cd87652125b /src/css
parent1da72ab345a13ee61b1f06b30fdc6c57c97bf524 (diff)
downloadchawan-ee930b0f5a587768d340c4204cf1f2e9fb818c89.tar.gz
Table improvements
Diffstat (limited to 'src/css')
-rw-r--r--src/css/values.nim23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/css/values.nim b/src/css/values.nim
index 1d28484c..67200532 100644
--- a/src/css/values.nim
+++ b/src/css/values.nim
@@ -322,6 +322,29 @@ func cssLength(val: float64, unit: string): CSSLength =
   else:
     raise newException(CSSValueError, "Invalid unit")
 
+func parseDimensionValues*(s: string): Option[CSSLength] =
+  if s == "": return
+  var i = 0
+  while s[i] in AsciiWhitespace: inc i
+  if i >= s.len or s[i] notin AsciiDigit: return
+  var n: float64
+  while s[i] in AsciiDigit:
+    n *= 10
+    n += float64(decValue(s[i]))
+    inc i
+  if i >= s.len: return some(CSSLength(num: n, unit: UNIT_PX))
+  if s[i] == '.':
+    inc i
+    if i >= s.len: return some(CSSLength(num: n, unit: UNIT_PX))
+    var d = 1
+    while i < s.len and s[i] in AsciiDigit:
+      n += float64(decValue(s[i])) / float64(d)
+      inc d
+      inc i
+  if i >= s.len: return some(CSSLength(num: n, unit: UNIT_PX))
+  if s[i] == '%': return some(CSSLength(num: n, unit: UNIT_PERC))
+  return some(CSSLength(num: n, unit: UNIT_PX))
+
 func color(r, g, b: int): CSSColor =
   return CSSColor(rgba: rgba(r, g, b, 256))