about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-27 18:45:17 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-27 18:45:17 +0100
commit0f2c08006a8787fc1a8a67ecf125fdffadd245ff (patch)
tree77db5c898bf8f55835466efa175342fa42a32f01 /src
parent5c978a3441cec24f2ffbd11b6e6bb3e2f429aadf (diff)
downloadchawan-0f2c08006a8787fc1a8a67ecf125fdffadd245ff.tar.gz
misc refactorings
* rename buffer enums
* fix isAscii for char 0x80
* remove dead code from URL
Diffstat (limited to 'src')
-rw-r--r--src/css/cssparser.nim4
-rw-r--r--src/local/container.nim10
-rw-r--r--src/server/buffer.nim37
-rw-r--r--src/types/url.nim75
-rw-r--r--src/utils/twtstr.nim14
5 files changed, 45 insertions, 95 deletions
diff --git a/src/css/cssparser.nim b/src/css/cssparser.nim
index 88b41eab..57f46015 100644
--- a/src/css/cssparser.nim
+++ b/src/css/cssparser.nim
@@ -162,8 +162,8 @@ proc consume(state: var CSSTokenizerState): char =
 proc consumeRChar(state: var CSSTokenizerState): char =
   var r: Rune
   fastRuneAt(state.buf, state.at, r)
-  if r.isAscii():
-    return cast[char](r)
+  if int32(r) < 0x80:
+    return char(r)
   return char(128)
 
 proc reconsume(state: var CSSTokenizerState) =
diff --git a/src/local/container.nim b/src/local/container.nim
index c4427ec6..7641b6d9 100644
--- a/src/local/container.nim
+++ b/src/local/container.nim
@@ -443,7 +443,7 @@ func getHoverText*(container: Container): string =
       return container.hovertext[t]
 
 func isHoverURL*(container: Container, url: URL): bool =
-  let hoverurl = parseURL(container.hovertext[HOVER_LINK])
+  let hoverurl = parseURL(container.hovertext[htLink])
   return hoverurl.isSome and url.host == hoverurl.get.host
 
 proc triggerEvent(container: Container, event: ContainerEvent) =
@@ -499,9 +499,9 @@ proc sendCursorPosition*(container: Container) =
   container.iface.updateHover(container.cursorx, container.cursory)
       .then(proc(res: UpdateHoverResult) =
     if res.link.isSome:
-      container.hovertext[HOVER_LINK] = res.link.get
+      container.hovertext[htLink] = res.link.get
     if res.title.isSome:
-      container.hovertext[HOVER_TITLE] = res.title.get
+      container.hovertext[htTitle] = res.title.get
     if res.link.isSome or res.title.isSome:
       container.triggerEvent(STATUS)
     if res.repaint:
@@ -1541,10 +1541,10 @@ proc peekCursor(container: Container) {.jsfunc.} =
     container.lastpeek = low(HoverType)
 
 func hoverLink(container: Container): string {.jsfget.} =
-  return container.hovertext[HOVER_LINK]
+  return container.hovertext[htLink]
 
 func hoverTitle(container: Container): string {.jsfget.} =
-  return container.hovertext[HOVER_TITLE]
+  return container.hovertext[htTitle]
 
 proc handleCommand(container: Container) =
   var packetid, len: int
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 0b312517..e72b41c9 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -1,4 +1,4 @@
-from std/strutils import split
+from std/strutils import split, toUpperAscii
 
 import std/macros
 import std/nativesockets
@@ -59,18 +59,19 @@ import chame/tags
 
 type
   BufferCommand* = enum
-    LOAD, FORCE_RENDER, WINDOW_CHANGE, FIND_ANCHOR, READ_SUCCESS, READ_CANCELED,
-    CLICK, FIND_NEXT_LINK, FIND_PREV_LINK, FIND_NTH_LINK, FIND_REV_NTH_LINK,
-    FIND_NEXT_MATCH, FIND_PREV_MATCH, GET_LINES, UPDATE_HOVER, CONNECT,
-    CONNECT2, GOTO_ANCHOR, CANCEL, GET_TITLE, SELECT, REDIRECT_TO_FD,
-    READ_FROM_FD, CLONE, FIND_PREV_PARAGRAPH, FIND_NEXT_PARAGRAPH
+    bcLoad, bcForceRender, bcWindowChange, bcFindAnchor, bcReadSuccess,
+    bcReadCanceled, bcClick, bcFindNextLink, bcFindPrevLink, bcFindNthLink,
+    bcFindRevNthLink, bcFindNextMatch, bcFindPrevMatch, bcGetLines,
+    bcUpdateHover, bcConnect, bcConnect2, bcGotoAnchor, bcCancel, bcGetTitle,
+    bcSelect, bcRedirectToFd, bcReadFromFd, bcClone, bcFindPrevParagraph,
+    bcFindNextParagraph
 
   BufferState = enum
     bsConnecting, bsLoadingPage, bsLoadingResources, bsLoaded
 
   HoverType* = enum
-    HOVER_TITLE = "TITLE"
-    HOVER_LINK = "URL"
+    htTitle = "TITLE"
+    htLink = "URL"
 
   BufferMatch* = object
     success*: bool
@@ -165,9 +166,7 @@ proc hasPromises*(iface: BufferInterface): bool =
 # get enum identifier of proxy function
 func getFunId(fun: NimNode): string =
   let name = fun[0] # sym
-  result = name.strVal.toScreamingSnakeCase()
-  if result[^1] == '=':
-    result = "SET_" & result[0..^2]
+  return "bc" & name.strVal[0].toUpperAscii() & name.strVal.substr(1)
 
 proc buildInterfaceProc(fun: NimNode, funid: string): tuple[fun, name: NimNode] =
   let name = fun[0] # sym
@@ -768,13 +767,13 @@ proc updateHover*(buffer: Buffer, cursorx, cursory: int): UpdateHoverResult {.pr
           result.repaint = true
 
     let title = thisnode.getTitleAttr()
-    if buffer.hovertext[HOVER_TITLE] != title:
+    if buffer.hovertext[htTitle] != title:
       result.title = some(title)
-      buffer.hovertext[HOVER_TITLE] = title
+      buffer.hovertext[htTitle] = title
     let click = thisnode.getClickHover()
-    if buffer.hovertext[HOVER_LINK] != click:
+    if buffer.hovertext[htLink] != click:
       result.link = some(click)
-      buffer.hovertext[HOVER_LINK] = click
+      buffer.hovertext[htLink] = click
 
     for styledNode in prevnode.branch:
       if styledNode.t == STYLED_ELEMENT and styledNode.node != nil:
@@ -1166,7 +1165,7 @@ proc onload(buffer: Buffer) =
   of bsConnecting:
     assert false
   of bsLoadingResources, bsLoaded:
-    buffer.resolveTask(LOAD, -1)
+    buffer.resolveTask(bcLoad, -1)
     return
   of bsLoadingPage:
     discard
@@ -1193,17 +1192,17 @@ proc onload(buffer: Buffer) =
           buffer.state = bsLoaded
           buffer.document.readyState = rsComplete
           buffer.dispatchLoadEvent()
-          buffer.resolveTask(LOAD, -1)
+          buffer.resolveTask(bcLoad, -1)
         )
         return # skip incr render
-      buffer.resolveTask(LOAD, buffer.bytesRead)
+      buffer.resolveTask(bcLoad, buffer.bytesRead)
     except ErrorAgain:
       break
   # incremental rendering: only if we cannot read the entire stream in one
   # pass
   #TODO this could be improved
   buffer.do_reshape()
-  buffer.resolveTask(LOAD, -2)
+  buffer.resolveTask(bcLoad, -2)
 
 proc getTitle*(buffer: Buffer): string {.proxy.} =
   if buffer.document != nil:
diff --git a/src/types/url.nim b/src/types/url.nim
index 180c41d1..2d9433c8 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -270,9 +270,6 @@ func getIdnaTableStatus(r: Rune): IDNATableStatus =
       return IDNA_IGNORED
     if u in DisallowedLow:
       return IDNA_DISALLOWED
-    for item in Deviation:
-      if item[0] == u:
-        return IDNA_DEVIATION
     if DisallowedRangesLow.isInRange(u):
       return IDNA_DISALLOWED
     if MappedMapLow.isInMap(u):
@@ -298,14 +295,13 @@ func getIdnaMapped(r: Rune): string =
   let n = MappedMapHigh.searchInMap(i)
   return $MappedMapHigh[n].mapped
 
-func getDeviationMapped(r: Rune): string =
-  for item in Deviation:
-    if item[0] == uint16(r):
-      return $item[1]
-  return ""
-
-func processIdna(str: string, checkhyphens, checkbidi, checkjoiners,
-    transitionalprocessing: bool): Option[string] =
+func processIdna(str: string, beStrict: bool): Option[string] =
+  # CheckHyphens = false
+  # CheckBidi = true
+  # CheckJoiners = true
+  # UseSTD3ASCIIRules = beStrict (but STD3 is not implemented)
+  # Transitional_Processing = false
+  # VerifyDnsLength = beStrict
   var mapped: seq[Rune]
   for r in str.runes():
     let status = getIdnaTableStatus(r)
@@ -313,11 +309,7 @@ func processIdna(str: string, checkhyphens, checkbidi, checkjoiners,
     of IDNA_DISALLOWED: return none(string) #error
     of IDNA_IGNORED: discard
     of IDNA_MAPPED: mapped &= getIdnaMapped(r).toRunes()
-    of IDNA_DEVIATION:
-      if transitionalprocessing:
-        mapped &= getDeviationMapped(r).toRunes()
-      else:
-        mapped &= r
+    of IDNA_DEVIATION: mapped &= r
     of IDNA_VALID: mapped &= r
   if mapped.len == 0: return
   mapped.mnormalize()
@@ -335,11 +327,7 @@ func processIdna(str: string, checkhyphens, checkbidi, checkjoiners,
         let x1 = normalize(x0)
         if x0 != x1:
           return none(string) #error
-        if checkhyphens:
-          if s.len >= 4 and s[2] == '-' and s[3] == '-':
-            return none(string) #error
-          if s.len > 0 and s[0] == '-' and s[^1] == '-':
-            return none(string) #error
+        # CheckHyphens is false
         if x0.len > 0:
           let cps = cast[ptr UncheckedArray[u32pair]](cr.points)
           let c = cast[uint32](x0[0])
@@ -349,13 +337,8 @@ func processIdna(str: string, checkhyphens, checkbidi, checkjoiners,
           if r == Rune('.'):
             return none(string) #error
           let status = getIdnaTableStatus(r)
-          case status
-          of IDNA_DISALLOWED, IDNA_IGNORED, IDNA_MAPPED:
+          if status in {IDNA_DISALLOWED, IDNA_IGNORED, IDNA_MAPPED}:
             return none(string) #error
-          of IDNA_DEVIATION:
-            if transitionalprocessing:
-              return none(string) #error
-          of IDNA_VALID: discard
           #TODO check joiners
           #TODO check bidi
         labels.add(s)
@@ -366,10 +349,8 @@ func processIdna(str: string, checkhyphens, checkbidi, checkjoiners,
   cr_free(addr cr)
   return some(labels.join('.'))
 
-func unicodeToAscii(s: string, checkhyphens, checkbidi, checkjoiners,
-    transitionalprocessing, verifydnslength: bool): Option[string] =
-  let processed = s.processIdna(checkhyphens, checkbidi, checkjoiners,
-    transitionalprocessing)
+func unicodeToAscii(s: string, beStrict: bool): Option[string] =
+  let processed = s.processIdna(beStrict)
   if processed.isNone:
     return none(string) #error
   var labels: seq[string]
@@ -383,44 +364,26 @@ func unicodeToAscii(s: string, checkhyphens, checkbidi, checkjoiners,
         return none(string) #error
     else:
       labels.add(label)
-    if verifydnslength:
+    if beStrict: # VerifyDnsLength
       let rl = labels[^1].runeLen()
       if rl notin 1..63:
         return none(string)
       all += rl
-  if verifydnslength:
+  if beStrict: # VerifyDnsLength
     if all notin 1..253:
       return none(string) #error
   return some(labels.join('.'))
 
-func domainToAscii*(domain: string, bestrict = false): Option[string] =
+func domainToAscii(domain: string, bestrict = false): Option[string] =
   var needsprocessing = false
   for s in domain.split('.'):
-    var i = 0
-    var xn = 0
-    while i < s.len:
-      if s[i] notin Ascii:
-        needsprocessing = true
-        break
-      case i
-      of 0:
-        if s[i] == 'x': inc xn
-      of 1:
-        if s[i] == 'n': inc xn
-      of 2:
-        if s[i] == '-': inc xn
-      of 3:
-        if s[i] == '-' and xn == 3:
-          needsprocessing = true
-          break
-      else: discard
-      inc i
-    if needsprocessing:
+    if s.startsWith("xn--") or not s.isAscii():
+      needsprocessing = true
       break
   if bestrict or needsprocessing:
     #Note: we don't implement STD3 separately, it's always true
-    result = domain.unicodeToAscii(false, true, true, false, bestrict)
-    if result.isNone or result.get == "":
+    let res = domain.unicodeToAscii(bestrict)
+    if res.isNone or res.get == "":
       return none(string)
     return result
   else:
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim
index 1916fff5..765ec24a 100644
--- a/src/utils/twtstr.nim
+++ b/src/utils/twtstr.nim
@@ -61,15 +61,6 @@ func toHeaderCase*(str: string): string =
       c = c.toUpperAscii()
     flip = c == '-'
 
-func toScreamingSnakeCase*(str: string): string = # input is camel case
-  if str.len >= 1: result &= str[0].toUpperAscii()
-  for c in str.toOpenArray(1, str.high):
-    if c in AsciiUpperAlpha:
-      result &= '_'
-      result &= c
-    else:
-      result &= c.toUpperAscii()
-
 func snakeToKebabCase*(str: string): string =
   result = str
   for c in result.mitems:
@@ -93,9 +84,6 @@ func camelToKebabCase*(s: string): string =
     else:
       result &= c
 
-func isAscii*(r: Rune): bool =
-  return uint32(r) < 128
-
 func startsWithNoCase*(str, prefix: string): bool =
   if str.len < prefix.len: return false
   # prefix.len is always lower
@@ -129,7 +117,7 @@ func decValue*(c: char): int =
 
 func isAscii*(s: string): bool =
   for c in s:
-    if c > char(0x80):
+    if c >= char(0x80):
       return false
   return true