about summary refs log tree commit diff stats
path: root/src/css
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-10-14 18:00:59 +0200
committerbptato <nincsnevem662@gmail.com>2023-10-14 18:00:59 +0200
commit6dd80b688bbadd6d93a62cea95e7a178f3d2c22f (patch)
treefd7eb1c6db052432606aada9b896e3dbafba5f7d /src/css
parentc225ff1619c9db8a48160c4e82c82da91f133f4d (diff)
downloadchawan-6dd80b688bbadd6d93a62cea95e7a178f3d2c22f.tar.gz
css: support alphabetic list types, support <LI TYPE=...
Diffstat (limited to 'src/css')
-rw-r--r--src/css/cascade.nim9
-rw-r--r--src/css/values.nim52
2 files changed, 59 insertions, 2 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim
index 288d6a89..0cc417b7 100644
--- a/src/css/cascade.nim
+++ b/src/css/cascade.nim
@@ -177,6 +177,13 @@ func calcPresentationalHints(element: Element): CSSComputedValues =
       let i = rowspan.get
       if i <= 65534:
         set_cv "-cha-rowspan", int(i)
+  template map_list_type =
+    let ctype = element.attr("type")
+    if ctype != "":
+      let tok = CSSToken(tokenType: CSS_IDENT_TOKEN, value: ctype)
+      let x = cssListStyleType(tok)
+      if x.isSome:
+        set_cv "list-style-type", x.get
 
   case element.tagType
   of TAG_DIV:
@@ -219,6 +226,8 @@ func calcPresentationalHints(element: Element): CSSComputedValues =
     let input = HTMLInputElement(element)
     if input.inputType in InputTypeWithSize:
       map_size
+  of TAG_OL, TAG_UL:
+    map_list_type
   else: discard
 
 proc applyDeclarations(styledNode: StyledNode, parent: CSSComputedValues,
diff --git a/src/css/values.nim b/src/css/values.nim
index 8d377134..61140fd8 100644
--- a/src/css/values.nim
+++ b/src/css/values.nim
@@ -1,7 +1,8 @@
-import tables
 import macros
 import options
 import strutils
+import tables
+import unicode
 
 import css/cssparser
 import css/selectorparser
@@ -81,6 +82,10 @@ type
     LIST_STYLE_TYPE_NONE, LIST_STYLE_TYPE_DISC, LIST_STYLE_TYPE_CIRCLE,
     LIST_STYLE_TYPE_SQUARE, LIST_STYLE_TYPE_DECIMAL,
     LIST_STYLE_TYPE_LOWER_ROMAN, LIST_STYLE_TYPE_UPPER_ROMAN,
+    LIST_STYLE_TYPE_LOWER_ALPHA, LIST_STYLE_TYPE_UPPER_ALPHA,
+    LIST_STYLE_TYPE_LOWER_GREEK,
+    LIST_STYLE_TYPE_HIRAGANA, LIST_STYLE_TYPE_HIRAGANA_IROHA,
+    LIST_STYLE_TYPE_KATAKANA, LIST_STYLE_TYPE_KATAKANA_IROHA,
     LIST_STYLE_TYPE_JAPANESE_INFORMAL
 
   CSSVerticalAlign2* = enum
@@ -422,6 +427,31 @@ func px*(l: CSSLength, window: WindowAttributes, p: LayoutUnit): LayoutUnit {.in
   of UNIT_VMAX:
     toLayoutUnit(max(window.width_px, window.width_px) / 100 * l.num)
 
+const UpperAlphaMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toRunes()
+const LowerAlphaMap = "abcdefghijklmnopqrstuvwxyz".toRunes()
+const LowerGreekMap = "αβγδεζηθικλμνξοπρστυφχψω".toRunes()
+const HiraganaMap = ("あいうえおかきくけこさしすせそたちつてとなにぬねの" &
+  "はひふへほまみむめもやゆよらりるれろわゐゑをん").toRunes()
+const HiraganaIrohaMap = ("いろはにほへとちりぬるをわかよたれそつねならむ" &
+  "うゐのおくやまけふこえてあさきゆめみしゑひもせす").toRunes()
+const KatakanaMap = ("アイウエオカキクケコサシスセソタチツテトナニヌネノ" &
+  "ハヒフヘホマミムメモヤユヨラリルレロワヰヱヲン").toRunes()
+const KatakanaIrohaMap = ("イロハニホヘトチリヌルヲワカヨタレソツネナラム" &
+  "ウヰノオクヤマケフコエテアサキユメミシヱヒモセス").toRunes()
+
+func numToBase(n: int, map: openArray[Rune]): string =
+  if n <= 0:
+    return $n
+  var tmp: seq[Rune]
+  var n = n
+  while n != 0:
+    n -= 1
+    tmp &= map[n mod map.len]
+    n = n div map.len
+  result = ""
+  for i in countdown(tmp.high, 0):
+    result &= $tmp[i]
+
 func listMarker*(t: CSSListStyleType, i: int): string =
   case t
   of LIST_STYLE_TYPE_NONE: return ""
@@ -431,6 +461,15 @@ func listMarker*(t: CSSListStyleType, i: int): string =
   of LIST_STYLE_TYPE_DECIMAL: return $i & ". "
   of LIST_STYLE_TYPE_UPPER_ROMAN: return romanNumber(i) & ". "
   of LIST_STYLE_TYPE_LOWER_ROMAN: return romanNumber_lower(i) & ". "
+  of LIST_STYLE_TYPE_UPPER_ALPHA: return numToBase(i, UpperAlphaMap) & ". "
+  of LIST_STYLE_TYPE_LOWER_ALPHA: return numToBase(i, LowerAlphaMap) & ". "
+  of LIST_STYLE_TYPE_LOWER_GREEK: return numToBase(i, LowerGreekMap) & ". "
+  of LIST_STYLE_TYPE_HIRAGANA: return numToBase(i, HiraganaMap) & "、"
+  of LIST_STYLE_TYPE_HIRAGANA_IROHA:
+    return numToBase(i, HiraganaIrohaMap) & "、"
+  of LIST_STYLE_TYPE_KATAKANA: return numToBase(i, KatakanaMap) & "、"
+  of LIST_STYLE_TYPE_KATAKANA_IROHA:
+    return numToBase(i, KatakanaIrohaMap) & "、"
   of LIST_STYLE_TYPE_JAPANESE_INFORMAL: return japaneseNumber(i) & "、"
 
 #TODO this should change by language
@@ -788,7 +827,7 @@ func cssWordBreak(cval: CSSComponentValue): Result[CSSWordBreak, string] =
   }
   return cssIdent(WordBreakMap, cval)
 
-func cssListStyleType(cval: CSSComponentValue):
+func cssListStyleType*(cval: CSSComponentValue):
     Result[CSSListStyleType, string] =
   const ListStyleMap = {
     "none": LIST_STYLE_TYPE_NONE,
@@ -798,6 +837,15 @@ func cssListStyleType(cval: CSSComponentValue):
     "decimal": LIST_STYLE_TYPE_DECIMAL,
     "upper-roman": LIST_STYLE_TYPE_UPPER_ROMAN,
     "lower-roman": LIST_STYLE_TYPE_LOWER_ROMAN,
+    "upper-latin": LIST_STYLE_TYPE_UPPER_ALPHA,
+    "lower-latin": LIST_STYLE_TYPE_LOWER_ALPHA,
+    "upper-alpha": LIST_STYLE_TYPE_UPPER_ALPHA,
+    "lower-alpha": LIST_STYLE_TYPE_UPPER_ALPHA,
+    "lower-greek": LIST_STYLE_TYPE_LOWER_GREEK,
+    "hiragana": LIST_STYLE_TYPE_HIRAGANA,
+    "hiragana-iroha": LIST_STYLE_TYPE_HIRAGANA_IROHA,
+    "katakana": LIST_STYLE_TYPE_KATAKANA,
+    "katakana-iroha": LIST_STYLE_TYPE_KATAKANA_IROHA,
     "japanese-informal": LIST_STYLE_TYPE_JAPANESE_INFORMAL
   }
   return cssIdent(ListStyleMap, cval)