about summary refs log tree commit diff stats
path: root/src/types
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-17 13:00:20 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-17 13:00:20 +0200
commitba2d0b1d8dae67b6f8d3e815a61d26c7c0b1ce2f (patch)
tree31e702713bf5514956dda362c9125eec78f8b6a4 /src/types
parentee2ab29090201c2bf08dcb46e7f6ebd35d9bc23a (diff)
downloadchawan-ba2d0b1d8dae67b6f8d3e815a61d26c7c0b1ce2f.tar.gz
javascript: static methods, URL.canParse
Diffstat (limited to 'src/types')
-rw-r--r--src/types/url.nim17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/types/url.nim b/src/types/url.nim
index 456dd16d..10100ee1 100644
--- a/src/types/url.nim
+++ b/src/types/url.nim
@@ -956,8 +956,7 @@ proc set*(params: URLSearchParams, name: string, value: string) {.jsfunc.} =
         first = false
         params.list[i][1] = value
 
-proc newURL*(s: string, base: Option[string] = none(string)):
-    JSResult[URL] {.jsctor.} =
+proc parseAPIURL(s: string, base: Option[string]): JSResult[URL] =
   let baseURL = if base.isSome:
     let x = parseURL(base.get)
     if x.isNone:
@@ -968,11 +967,16 @@ proc newURL*(s: string, base: Option[string] = none(string)):
   let url = parseURL(s, baseURL)
   if url.isNone:
     return err(newTypeError(s & " is not a valid URL"))
-  url.get.searchParams = newURLSearchParams()
-  url.get.searchParams.url = url
-  url.get.searchParams.initURLSearchParams(url.get.query.get(""))
   return ok(url.get)
 
+proc newURL*(s: string, base: Option[string] = none(string)):
+    JSResult[URL] {.jsctor.} =
+  let url = ?parseAPIURL(s, base)
+  url.searchParams = newURLSearchParams()
+  url.searchParams.url = some(url)
+  url.searchParams.initURLSearchParams(url.query.get(""))
+  return ok(url)
+
 proc origin0*(url: URL): Origin =
   case url.scheme
   of "blob":
@@ -1097,6 +1101,9 @@ proc setHash*(url: URL, s: string) {.jsfset: "hash".} =
   url.fragment = some("")
   discard basicParseURL(s, url = url, stateOverride = some(FRAGMENT_STATE))
 
+proc canParse(url: string, base: Option[string]): bool {.jsstfunc: "URL".} =
+  return parseAPIURL(url, base).isSome
+
 proc addURLModule*(ctx: JSContext) =
   ctx.registerType(URL)
   ctx.registerType(URLSearchParams)