From bc882c587f62a25f24d1a4ffa739bd505c09ce8d Mon Sep 17 00:00:00 2001 From: bptato Date: Tue, 30 Jul 2024 19:50:06 +0200 Subject: env, dom: add History stub, basic LocalStorage * History: doesn't really do anything, just adding it to fix some pages * LocalStorage: kind of works, but does lookups with linear search, and the quota limitation is on the number of entries not their size. plus it doesn't actually store anything on disk yet (like cookies). --- src/html/dom.nim | 10 ++++++++ src/html/env.nim | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/js/window.html | 8 +++++++ 3 files changed, 86 insertions(+) diff --git a/src/html/dom.nim b/src/html/dom.nim index 802d2eb2..950bdd7b 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -83,6 +83,9 @@ type internalConsole*: Console navigator* {.jsget.}: Navigator screen* {.jsget.}: Screen + history* {.jsget.}: History + localStorage* {.jsget.}: Storage + sessionStorage* {.jsget.}: Storage settings*: EnvironmentSettings loader*: FileLoader location* {.jsget.}: Location @@ -110,6 +113,11 @@ type Screen* = object + History* = object + + Storage* = object + map*: seq[tuple[key, value: string]] + NamedNodeMap = ref object element: Element attrlist: seq[Attr] @@ -387,6 +395,8 @@ jsDestructor(Navigator) jsDestructor(PluginArray) jsDestructor(MimeTypeArray) jsDestructor(Screen) +jsDestructor(History) +jsDestructor(Storage) jsDestructor(Element) jsDestructor(HTMLElement) diff --git a/src/html/env.nim b/src/html/env.nim index 7117a9b0..fed27096 100644 --- a/src/html/env.nim +++ b/src/html/env.nim @@ -1,4 +1,5 @@ import std/selectors +import std/tables import html/catom import html/chadombuilder @@ -21,6 +22,7 @@ import loader/request import loader/response import monoucha/javascript import monoucha/jserror +import monoucha/jspropenumlist import monoucha/jstypes import monoucha/quickjs import monoucha/tojs @@ -92,11 +94,77 @@ proc height(screen: var Screen): int64 {.jsfget.} = proc colorDepth(screen: var Screen): int64 {.jsfget.} = 24 proc pixelDepth(screen: var Screen): int64 {.jsfget.} = screen.colorDepth +# History +func length(history: var History): uint32 {.jsfget.} = 1 +func state(history: var History): JSValue {.jsfget.} = JS_NULL +func go(history: var History) {.jsfunc.} = discard +func back(history: var History) {.jsfunc.} = discard +func forward(history: var History) {.jsfunc.} = discard + +# Storage +func find(this: Storage; key: string): int = + for i in 0 ..< this.map.len: + if this.map[i].key == key: + return i + return -1 + +func length(this: var Storage): uint32 {.jsfget.} = + return uint32(this.map.len) + +func key(this: var Storage; i: uint32): Option[string] {.jsfunc.} = + if int(i) < this.map.len: + return some(this.map[int(i)].value) + return none(string) + +func getItem(this: var Storage; s: string): Option[string] + {.jsfunc.} = + let i = this.find(s) + if i != -1: + return some(this.map[i].value) + return none(string) + +func setItem(this: var Storage; key, value: string): + Err[DOMException] {.jsfunc.} = + let i = this.find(key) + if i != -1: + this.map[i].value = value + else: + if this.map.len >= 64: + return errDOMException("Quota exceeded", "QuotaExceededError") + this.map.add((key, value)) + ok() + +func removeItem(this: var Storage; key: string) {.jsfunc.} = + let i = this.find(key) + if i != -1: + this.map.del(i) + +func names(ctx: JSContext; this: var Storage): JSPropertyEnumList + {.jspropnames.} = + var list = newJSPropertyEnumList(ctx, uint32(this.map.len)) + for it in this.map: + list.add(it.key) + return list + +func getter(this: var Storage; s: string): Option[string] + {.jsgetprop.} = + return this.getItem(s) + +func setter(this: var Storage; k, v: string): Err[DOMException] + {.jssetprop.} = + return this.setItem(k, v) + +func delete(this: var Storage; k: string): bool {.jsdelprop.} = + this.removeItem(k) + return true + proc addNavigatorModule*(ctx: JSContext) = ctx.registerType(Navigator) ctx.registerType(PluginArray) ctx.registerType(MimeTypeArray) ctx.registerType(Screen) + ctx.registerType(History) + ctx.registerType(Storage) proc fetch(window: Window; input: JSValue; init = none(RequestInit)): JSResult[FetchPromise] {.jsfunc.} = diff --git a/test/js/window.html b/test/js/window.html index 5aef5186..07b76145 100644 --- a/test/js/window.html +++ b/test/js/window.html @@ -7,5 +7,13 @@ assert_equals(window.toString(), "[object Window]"); const desc = Object.getOwnPropertyDescriptor(window, "window"); assert(desc.enumerable, "window must be enumerable"); assert(!desc.configurable, "window must not be configurable"); +assert_equals(window.history.state, null); +window.localStorage.setItem("a", "x"); +window.localStorage["b"] = "y"; +assert_equals(window.localStorage["b"], "y") +delete window.localStorage.b; +assert_equals(window.localStorage["b"], undefined); +window.localStorage["b"] = "y"; +assert(window.sessionStorage instanceof Storage); document.getElementById("x").textContent = "Success"; -- cgit