aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-30 19:28:33 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-30 19:28:33 +0100
commitbc3b11ebd3c2554f4f30e9afb4d46328c748e9be (patch)
tree619bb135cce1e9af3fa058c9fc6c7bd4ee017773 /src/html
parentde759debe31d67776a7a70d3d4dc41a89591cdfc (diff)
downloadchawan-bc3b11ebd3c2554f4f30e9afb4d46328c748e9be.tar.gz
dom, cssvalues: add getComputedStyle
Only available in "app" mode.
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim28
-rw-r--r--src/html/env.nim8
2 files changed, 31 insertions, 5 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 759191a8..61b406b4 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -260,10 +260,13 @@ type
attrs*: seq[AttrData] # sorted by int(qualifiedName)
cachedAttributes: NamedNodeMap
cachedStyle*: CSSStyleDeclaration
+ cachedComputedStyle*: CSSStyleDeclaration
+ computed*: CSSValues
AttrDummyElement = ref object of Element
CSSStyleDeclaration* = ref object
+ computed: bool
decls*: seq[CSSDeclaration]
element: Element
@@ -3300,7 +3303,10 @@ func find(this: CSSStyleDeclaration; s: string): int =
proc getPropertyValue(this: CSSStyleDeclaration; s: string): string {.jsfunc.} =
if (let i = this.find(s); i != -1):
- return $this.decls[i].value
+ var s = ""
+ for it in this.decls[i].value:
+ s &= $it
+ return move(s)
return ""
# https://drafts.csswg.org/cssom/#idl-attribute-to-css-property
@@ -3347,7 +3353,10 @@ proc setValue(this: CSSStyleDeclaration; i: int; cvals: seq[CSSComponentValue]):
return ok()
proc setter(ctx: JSContext; this: CSSStyleDeclaration; atom: JSAtom;
- value: string): Opt[void] {.jssetprop.} =
+ value: string): DOMResult[void] {.jssetprop.} =
+ if this.computed:
+ return errDOMException("Cannot modify computed value",
+ "NoModificationAllowedError")
let cvals = parseComponentValues(value)
var u: uint32
if ctx.fromJS(atom, u).isSome:
@@ -3374,6 +3383,21 @@ proc style*(element: Element): CSSStyleDeclaration {.jsfget.} =
element.cachedStyle = CSSStyleDeclaration(element: element)
return element.cachedStyle
+proc getComputedStyle*(element: Element): CSSStyleDeclaration =
+ if element.cachedComputedStyle == nil:
+ var s = ""
+ for p in CSSPropertyType:
+ if p != cptNone:
+ s &= $p & ':'
+ if p.isBit:
+ s &= element.computed.bits[p].serialize(valueType(p))
+ else:
+ s &= element.computed.objs[p].serialize()
+ s &= ';'
+ element.cachedComputedStyle = newCSSStyleDeclaration(element, move(s))
+ element.cachedComputedStyle.computed = true
+ return element.cachedComputedStyle
+
proc corsFetch(window: Window; input: Request): FetchPromise =
if not window.images and input.url.scheme.startsWith("img-codec+"):
return newResolvedPromise(JSResult[Response].err(newFetchTypeError()))
diff --git a/src/html/env.nim b/src/html/env.nim
index 588aa897..91e82f30 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -283,9 +283,11 @@ callback(new Event("").timeStamp);
return ctx.toJS(window.setTimeout(handler, 0, callback))
proc getComputedStyle(window: Window; element: Element;
- pseudoElt = none(string)): JSResult[CSSStyleDeclaration] {.jsfunc.} =
- #TODO implement this properly
- return ok(element.style)
+ pseudoElt = none(string)): CSSStyleDeclaration {.jsfunc.} =
+ if window.settings.scripting == smApp:
+ return element.getComputedStyle()
+ # Maybe it works.
+ return element.style
proc postMessage(window: Window) {.jsfunc.} =
window.console.log("postMessage: Stub")