diff options
author | bptato <nincsnevem662@gmail.com> | 2023-08-23 23:50:14 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-08-23 23:52:05 +0200 |
commit | e50186dd1112e24cbf74acda0ea2bd4c63ee2ed9 (patch) | |
tree | 756fc3b3a749716b5f188696b03820b0d529936d | |
parent | 65fe1de341be4c6f8a7f1a0ec6d954be463bba29 (diff) | |
download | chawan-e50186dd1112e24cbf74acda0ea2bd4c63ee2ed9.tar.gz |
dom: add some null checks for window
Now that we have established that window *can* be nil. (Though the document.location window null check is probably unnecessary, because it is only called from scripts... but better safe than sorry.)
-rw-r--r-- | src/html/dom.nim | 12 | ||||
-rw-r--r-- | src/html/env.nim | 2 |
2 files changed, 9 insertions, 5 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index b33cbd19..a5dc8212 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -1158,6 +1158,8 @@ proc newLocation*(window: Window): Location = return location func location(document: Document): Location {.jsfget.} = + if document.window == nil: + return nil return document.window.location func document(location: Location): Document = @@ -1169,8 +1171,10 @@ func url(location: Location): URL = return document.url return newURL("about:blank").get -proc setLocation*(document: Document, s: string): Err[DOMException] +proc setLocation*(document: Document, s: string): Err[JSError] {.jsfset: "location".} = + if document.location == nil: + return err(newTypeError("document.location is not an object")) let url = parseURL(s) if url.isNone: return err(newDOMException("Invalid URL", "SyntaxError")) @@ -1182,16 +1186,16 @@ proc setLocation*(document: Document, s: string): Err[DOMException] func href(location: Location): string {.jsuffget.} = return location.url.serialize() -proc setHref(location: Location, s: string): Err[DOMException] +proc setHref(location: Location, s: string): Err[JSError] {.jsfset: "href".} = if location.document == nil: return ok() return location.document.setLocation(s) -proc assign(location: Location, s: string): Err[DOMException] {.jsuffunc.} = +proc assign(location: Location, s: string): Err[JSError] {.jsuffunc.} = location.setHref(s) -proc replace(location: Location, s: string): Err[DOMException] {.jsuffunc.} = +proc replace(location: Location, s: string): Err[JSError] {.jsuffunc.} = location.setHref(s) proc reload(location: Location) {.jsuffunc.} = diff --git a/src/html/env.nim b/src/html/env.nim index c38852d5..6e81e614 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -89,7 +89,7 @@ proc screenTop(window: Window): int64 {.jsfget.} = 0 #TODO outerWidth, outerHeight proc devicePixelRatio(window: Window): float64 {.jsfget.} = 1 -proc setLocation(window: Window, s: string): Err[DOMException] +proc setLocation(window: Window, s: string): Err[JSError] {.jsfset: "location".} = window.document.setLocation(s) |