diff options
Diffstat (limited to 'src/html')
| -rw-r--r-- | src/html/dom.nim | 69 | ||||
| -rw-r--r-- | src/html/domexception.nim | 3 | ||||
| -rw-r--r-- | src/html/env.nim | 6 | ||||
| -rw-r--r-- | src/html/xmlhttprequest.nim | 5 |
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() |