aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-10 21:00:53 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-10 21:28:58 +0100
commit8ce8a12bef5b820b5cd7a3e95291a1c0a4328d84 (patch)
tree4590eefd0ab916d755b514b5f15655b839fd4b0a /src/html
parentf6200928a08767d5336d090fa818010b189c5e44 (diff)
downloadchawan-8ce8a12bef5b820b5cd7a3e95291a1c0a4328d84.tar.gz
dom: more select interfaces
this should be all of them
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim69
-rw-r--r--src/html/domexception.nim3
-rw-r--r--src/html/env.nim6
-rw-r--r--src/html/xmlhttprequest.nim5
4 files changed, 71 insertions, 12 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 10d6ee4b..e792fdbe 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -3092,6 +3092,50 @@ proc getter(ctx: JSContext; this: HTMLOptionsCollection; atom: JSAtom): JSValue
{.jsgetownprop.} =
return ctx.getter(HTMLCollection(this), atom)
+proc add(ctx: JSContext; this: HTMLOptionsCollection; element: Element;
+ before = JS_NULL): JSValue {.jsfunc.} =
+ if not (element of HTMLOptionElement or element of HTMLOptGroupElement):
+ return JS_ThrowTypeError(ctx, "Expected option or optgroup element")
+ var beforeEl: HTMLElement = nil
+ var beforeIdx = -1
+ if not JS_IsNull(before) and ctx.fromJS(before, beforeEl).isNone and
+ ctx.fromJS(before, beforeIdx).isNone:
+ return JS_EXCEPTION
+ for it in this.root.ancestors:
+ if element == it:
+ return JS_ThrowDOMException(ctx, "Can't add ancestor of select",
+ "HierarchyRequestError")
+ if beforeEl != nil and this.root notin beforeEl:
+ return JS_ThrowDOMException(ctx, "select is not a descendant of before",
+ "NotFoundError")
+ if element != beforeEl:
+ if beforeEl == nil:
+ let it = this.item(uint32(beforeIdx))
+ if it of HTMLElement:
+ beforeEl = HTMLElement(it)
+ let parent = if beforeEl != nil: beforeEl.parentNode else: this.root
+ let res = ctx.toJS(parent.insertBefore(element, beforeEl))
+ if JS_IsException(res):
+ return res
+ return JS_UNDEFINED
+
+proc remove(this: HTMLOptionsCollection; i: int32) {.jsfunc.} =
+ let element = this.item(uint32(i))
+ if element != nil:
+ element.remove()
+
+proc setLength(this: HTMLOptionsCollection; n: uint32) {.jsfset: "length".} =
+ let len = uint32(this.getLength())
+ if n > len:
+ if n <= 100_000: # LOL
+ let parent = this.root
+ let document = parent.document
+ for i in 0 ..< n - len:
+ parent.append(document.newHTMLElement(TAG_OPTION))
+ else:
+ for i in 0 ..< len - n:
+ this.item(uint32(i)).remove()
+
func jsOptions(this: HTMLSelectElement): HTMLOptionsCollection
{.jsfget: "options".} =
if this.cachedOptions == nil:
@@ -3107,7 +3151,8 @@ func jsOptions(this: HTMLSelectElement): HTMLOptionsCollection
proc length(this: HTMLSelectElement): int {.jsfget.} =
return this.jsOptions.getLength()
-#TODO length setter
+proc setLength(this: HTMLSelectElement; n: uint32) {.jsfset: "length".} =
+ this.jsOptions.setLength(n)
proc getter(ctx: JSContext; this: HTMLSelectElement; u: JSAtom): JSValue
{.jsgetownprop.} =
@@ -3141,6 +3186,9 @@ proc selectedIndex*(this: HTMLSelectElement): int {.jsfget.} =
inc i
return -1
+proc selectedIndex(this: HTMLOptionsCollection): int {.jsfget.} =
+ return HTMLSelectElement(this.root).selectedIndex
+
proc setSelectedIndex*(this: HTMLSelectElement; n: int)
{.jsfset: "selectedIndex".} =
var i = 0
@@ -3178,7 +3226,19 @@ proc showPicker(this: HTMLSelectElement): Err[DOMException] {.jsfunc.} =
# be app mode only.
return errDOMException("not allowed", "NotAllowedError")
-#TODO add, remove
+proc add(ctx: JSContext; this: HTMLSelectElement; element: Element;
+ before = JS_NULL): JSValue {.jsfunc.} =
+ return ctx.add(this.jsOptions, element, before)
+
+proc remove(ctx: JSContext; this: HTMLSelectElement; idx: varargs[JSValue]):
+ Opt[void] {.jsfunc.} =
+ if idx.len > 0:
+ var i: int32
+ ?ctx.fromJS(idx[0], i)
+ this.jsOptions.remove(i)
+ else:
+ this.remove()
+ ok()
# <table>
func caption(this: HTMLTableElement): Element {.jsfget.} =
@@ -5646,14 +5706,13 @@ proc innerHTML(element: Element; s: string) {.jsfset.} =
element
ctx.replaceAll(fragment)
-proc outerHTML(element: Element; s: string): Err[DOMException] {.jsfset.} =
+proc outerHTML(element: Element; s: string): DOMResult[void] {.jsfset.} =
let parent0 = element.parentNode
if parent0 == nil:
return ok()
if parent0 of Document:
- let ex = newDOMException("outerHTML is disallowed for Document children",
+ return errDOMException("outerHTML is disallowed for Document children",
"NoModificationAllowedError")
- return err(ex)
let parent: Element = if parent0 of DocumentFragment:
element.document.newHTMLElement(TAG_BODY)
else:
diff --git a/src/html/domexception.nim b/src/html/domexception.nim
index 75787045..ba0c787c 100644
--- a/src/html/domexception.nim
+++ b/src/html/domexception.nim
@@ -77,6 +77,9 @@ proc newDOMException*(message = ""; name = "Error"): DOMException {.jsctor.} =
template errDOMException*(message, name: string): untyped =
err(newDOMException(message, name))
+proc JS_ThrowDOMException*(ctx: JSContext; message, name: string): JSValue =
+ return JS_Throw(ctx, ctx.toJS(newDOMException(message, name)))
+
func message0(this: DOMException): string {.jsfget: "message".} =
return this.message
diff --git a/src/html/env.nim b/src/html/env.nim
index 8d59803d..54499b1a 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -250,8 +250,7 @@ func getParent(window: Window): Window {.jsfget: "parent".} =
proc atob(ctx: JSContext; window: Window; data: string): JSValue {.jsfunc.} =
var s = ""
if (let r = s.atob(data); r.isNone):
- let ex = newDOMException($r.error, "InvalidCharacterError")
- return JS_Throw(ctx, ctx.toJS(ex))
+ return JS_ThrowDOMException(ctx, $r.error, "InvalidCharacterError")
return ctx.toJS(NarrowString(s))
proc btoa(ctx: JSContext; window: Window; data: JSValue): JSValue
@@ -262,9 +261,8 @@ proc btoa(ctx: JSContext; window: Window; data: JSValue): JSValue
doAssert JS_IsString(data)
if JS_IsStringWideChar(data):
JS_FreeValue(ctx, data)
- let ex = newDOMException("Invalid character in string",
+ return JS_ThrowDOMException(ctx, "Invalid character in string",
"InvalidCharacterError")
- return JS_Throw(ctx, ctx.toJS(ex))
let len = int(JS_GetStringLength(data))
if len == 0:
JS_FreeValue(ctx, data)
diff --git a/src/html/xmlhttprequest.nim b/src/html/xmlhttprequest.nim
index 6e6ca940..5f85aee6 100644
--- a/src/html/xmlhttprequest.nim
+++ b/src/html/xmlhttprequest.nim
@@ -382,9 +382,8 @@ func getCharset(this: XMLHttpRequest): Charset =
proc responseText(ctx: JSContext; this: XMLHttpRequest): JSValue {.jsfget.} =
if this.responseType notin {xhrtUnknown, xhrtText}:
- let ex = newDOMException("response type was expected to be '' or 'text'",
- "InvalidStateError")
- return JS_Throw(ctx, ctx.toJS(ex))
+ return JS_ThrowDOMException(ctx,
+ "Response type was expected to be '' or 'text'", "InvalidStateError")
if this.readyState notin {xhrsLoading, xhrsDone}:
return ctx.toJS("")
let charset = this.getCharset()