From f34d87bffdb1567fdf67bf8d828d018768eacafd Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 5 Jan 2025 13:57:48 +0100 Subject: dom: clean up, add some strict defs --- src/html/dom.nim | 341 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 171 insertions(+), 170 deletions(-) (limited to 'src/html') diff --git a/src/html/dom.nim b/src/html/dom.nim index 985a8b8c..31cf4ccf 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -502,6 +502,7 @@ jsDestructor(CSSStyleDeclaration) # Forward declarations func attr*(element: Element; s: StaticAtom): string func attrb*(element: Element; s: CAtom): bool +func serializeFragment(res: var string; node: Node) func value*(option: HTMLOptionElement): string proc append*(parent, node: Node) proc attr*(element: Element; name: CAtom; value: string) @@ -955,7 +956,7 @@ func attrType0(s: static string): StaticAtom {.compileTime.} = return strictParseEnum[StaticAtom](s).get template toset(ts: openArray[TagType]): set[TagType] = - var tags: system.set[TagType] + var tags: system.set[TagType] = {} for tag in ts: tags.incl(tag) tags @@ -1134,26 +1135,27 @@ func findAttrNS(element: Element; namespace, qualifiedName: CAtom): int = return i return -1 -func escapeText(s: string; attribute_mode = false): string = - var nbsp_mode = false - var nbsp_prev: char +func escapeText(s: string; attributeMode = false): string = + result = newStringOfCap(s.len) + var nbspMode = false + var nbspPrev = '\0' for c in s: - if nbsp_mode: - if c == char(0xA0): + if nbspMode: + if c == '\xA0': result &= " " else: - result &= nbsp_prev & c - nbsp_mode = false + result &= nbspPrev & c + nbspMode = false elif c == '&': result &= "&" - elif c == char(0xC2): - nbsp_mode = true - nbsp_prev = c - elif attribute_mode and c == '"': + elif c == '\xC2': + nbspMode = true + nbspPrev = c + elif attributeMode and c == '"': result &= """ - elif not attribute_mode and c == '<': + elif not attributeMode and c == '<': result &= "<" - elif not attribute_mode and c == '>': + elif not attributeMode and c == '>': result &= ">" else: result &= c @@ -2124,23 +2126,6 @@ func isSubmitButton*(element: Element): bool = return element.inputType in {itSubmit, itImage} return false -func canSubmitImplicitly*(form: HTMLFormElement): bool = - const BlocksImplicitSubmission = { - itText, itSearch, itURL, itTel, itEmail, itPassword, itDate, itMonth, - itWeek, itTime, itDatetimeLocal, itNumber - } - var found = false - for control in form.controls: - if control of HTMLInputElement: - let input = HTMLInputElement(control) - if input.inputType in BlocksImplicitSubmission: - if found: - return false - found = true - elif control.isSubmitButton(): - return false - return true - # https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-write-steps proc write(ctx: JSContext; document: Document; args: varargs[JSValue]): Err[DOMException] {.jsfunc.} = @@ -2178,12 +2163,6 @@ func head*(document: Document): HTMLElement {.jsfget.} = func body*(document: Document): HTMLElement {.jsfget.} = return document.findFirst(TAG_BODY) -func select*(option: HTMLOptionElement): HTMLSelectElement = - for anc in option.ancestors: - if anc of HTMLSelectElement: - return HTMLSelectElement(anc) - return nil - func countChildren(node: Node; nodeType: type): int = result = 0 for child in node.childList: @@ -2539,8 +2518,6 @@ func serializesAsVoid(element: Element): bool = const Extra = {TAG_BASEFONT, TAG_BGSOUND, TAG_FRAME, TAG_KEYGEN, TAG_PARAM} return element.tagType in VoidElements + Extra -func serializeFragment(res: var string; node: Node) - func serializeFragmentInner(res: var string; child: Node; parentType: TagType) = if child of Element: let element = Element(child) @@ -2597,7 +2574,7 @@ func serializeFragment*(node: Node): string = result = "" result.serializeFragment(node) -# Element attribute reflection (getters) +# Element func innerHTML(element: Element): string {.jsfget.} = #TODO xml return element.serializeFragment() @@ -2607,6 +2584,7 @@ func outerHTML(element: Element): string {.jsfget.} = result = "" result.serializeFragmentInner(element, TAG_UNKNOWN) +# HTMLElement func crossOrigin0(element: HTMLElement): CORSAttribute = if not element.attrb(satCrossorigin): return caNoCors @@ -2644,59 +2622,6 @@ proc sheets*(document: Document): seq[CSSStylesheet] = document.cachedSheetsInvalid = false return document.cachedSheets -func checked*(input: HTMLInputElement): bool {.inline.} = - return input.internalChecked - -proc setChecked*(input: HTMLInputElement; b: bool) {.jsfset: "checked".} = - if input.inputType == itRadio: - for radio in input.radiogroup: - radio.invalidDeps.incl(dtChecked) - radio.internalChecked = false - radio.setInvalid() - input.invalidDeps.incl(dtChecked) - input.internalChecked = b - input.setInvalid() - -func inputString*(input: HTMLInputElement): string = - case input.inputType - of itCheckbox, itRadio: - if input.checked: - "*" - else: - " " - of itSearch, itText, itEmail, itURL, itTel: - input.value.padToWidth(int(input.attrulgz(satSize).get(20))) - of itPassword: - '*'.repeat(input.value.len).padToWidth(int(input.attrulgz(satSize).get(20))) - of itReset: - if input.attrb(satValue): - input.value - else: - "RESET" - of itSubmit, itButton: - if input.attrb(satValue): - input.value - else: - "SUBMIT" - of itFile: - let s = if input.file != nil: input.file.name else: "" - s.padToWidth(int(input.attrulgz(satSize).get(20))) - else: input.value - -func textAreaString*(textarea: HTMLTextAreaElement): string = - result = "" - let split = textarea.value.split('\n') - let rows = int(textarea.attrul(satRows).get(1)) - for i in 0 ..< rows: - let cols = int(textarea.attrul(satCols).get(20)) - if cols > 2: - if i < split.len: - result &= '[' & split[i].padToWidth(cols - 2) & "]\n" - else: - result &= '[' & ' '.repeat(cols - 2) & "]\n" - else: - result &= "[]\n" - func isButton*(element: Element): bool = if element of HTMLButtonElement: return true @@ -2904,14 +2829,6 @@ proc hyperlinkGetProp(ctx: JSContext; element: HTMLElement; a: JSAtom; return JS_TRUE # dummy value return JS_UNINITIALIZED -# -proc href(base: HTMLBaseElement): string {.jsfget.} = - #TODO with fallback base url - let url = parseURL(base.attr(satHref)) - if url.isSome: - return $url.get - return "" - # proc getter(ctx: JSContext; this: HTMLAnchorElement; a: JSAtom; desc: ptr JSPropertyDescriptor): JSValue {.jsgetownprop.} = @@ -2940,32 +2857,39 @@ proc toString(area: HTMLAreaElement): string {.jsfunc.} = proc setRelList(area: HTMLAreaElement; s: string) {.jsfset: "relList".} = area.attr(satRel, s) -#