aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2025-01-06 17:45:09 +0100
committerbptato <nincsnevem662@gmail.com>2025-01-06 17:45:09 +0100
commite0f3655cb0dc5a45206d4e53f0243c7d96452279 (patch)
tree4ad9975b27169d8b9a7312fcee307afe467acd40 /src/html
parentc1ededa968960bea8dc4ba9d608809772797e324 (diff)
downloadchawan-e0f3655cb0dc5a45206d4e53f0243c7d96452279.tar.gz
env: add media query matching
In lite mode, it uses default window attributes. Incidentally, this also untangles media query matching from cascade, saving us a forward declaration hack (yay!)
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim22
-rw-r--r--src/html/env.nim22
2 files changed, 29 insertions, 15 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index b5760b4f..7cb6c5b4 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -82,6 +82,9 @@ type
Window* = ref object of EventTarget
attrsp*: ptr WindowAttributes
+ # In app mode, attrsp == scriptAttrsp.
+ # In lite mode, scriptAttrsp == addr dummyAttrs.
+ scriptAttrsp*: ptr WindowAttributes
internalConsole*: Console
navigator* {.jsget.}: Navigator
screen* {.jsget.}: Screen
@@ -519,9 +522,6 @@ proc remove*(node: Node)
proc setInvalid*(element: Element)
# Forward declaration hacks
-# set in css/cascade
-var appliesImpl*: proc(mqlist: MediaQueryList; window: Window): bool
- {.nimcall, noSideEffect.}
# set in css/match
var matchesImpl*: proc(element: Element; cxsels: seq[ComplexSelector]): bool
{.nimcall, noSideEffect.} = nil
@@ -3682,21 +3682,13 @@ proc getter(ctx: JSContext; this: CSSStyleDeclaration; atom: JSAtom):
return ctx.toJS(this.getPropertyValue(s))
return JS_UNINITIALIZED
-template dummyWindow(): WindowAttributes = WindowAttributes(
- width: 80,
- height: 24,
- ppc: 9,
- ppl: 18,
- widthPx: 80 * 9,
- heightPx: 24 * 18
-)
-
proc setValue(this: CSSStyleDeclaration; i: int; cvals: seq[CSSComponentValue]):
Err[void] =
if i notin 0 .. this.decls.high:
return err()
+ # dummyAttrs can be safely used because the result is discarded.
var dummy: seq[CSSComputedEntry]
- ?parseComputedValues(dummy, this.decls[i].name, cvals, dummyWindow())
+ ?parseComputedValues(dummy, this.decls[i].name, cvals, dummyAttrs)
this.decls[i].value = cvals
return ok()
@@ -3731,7 +3723,7 @@ proc setProperty(this: CSSStyleDeclaration; name, value: string):
return ok()
else:
var dummy: seq[CSSComputedEntry]
- let val0 = parseComputedValues(dummy, name, cvals, dummyWindow())
+ let val0 = parseComputedValues(dummy, name, cvals, dummyAttrs)
if val0.isNone:
return ok()
this.decls.add(CSSDeclaration(name: name, value: cvals))
@@ -3824,7 +3816,7 @@ proc loadResource(window: Window; link: HTMLLinkElement) =
if media != "":
let cvals = parseComponentValues(media)
let media = parseMediaQueryList(cvals, window.attrsp)
- applies = media.appliesImpl(window)
+ applies = media.applies(window.settings.scripting, window.attrsp)
window.loadSheet(link, url, applies)
proc getImageId(window: Window): int =
diff --git a/src/html/env.nim b/src/html/env.nim
index 65126d07..2db9a294 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -1,5 +1,7 @@
import std/tables
+import css/cssparser
+import css/mediaquery
import html/catom
import html/chadombuilder
import html/dom
@@ -290,6 +292,21 @@ proc getComputedStyle(window: Window; element: Element;
# Maybe it works.
return element.style
+type MediaQueryList = ref object
+ media: string
+ matches: bool
+ #TODO onchange
+
+jsDestructor(MediaQueryList)
+
+proc matchMedia(window: Window; s: string): MediaQueryList {.jsfunc.} =
+ let cvals = parseComponentValues(s)
+ let mqlist = parseMediaQueryList(cvals, window.scriptAttrsp)
+ return MediaQueryList(
+ matches: mqlist.applies(window.settings.scripting, window.scriptAttrsp),
+ media: s #TODO this should be serialized from mqlist
+ )
+
proc postMessage(window: Window) {.jsfunc.} =
window.console.log("postMessage: Stub")
@@ -306,6 +323,7 @@ proc addWindowModule*(ctx: JSContext) =
ctx.addEventModule()
let eventTargetCID = ctx.getClass("EventTarget")
ctx.registerType(Window, parent = eventTargetCID, asglobal = true)
+ ctx.registerType(MediaQueryList)
proc addWindowModule2*(ctx: JSContext) =
ctx.addEventModule()
@@ -329,6 +347,10 @@ proc addScripting*(window: Window) =
window.jsctx = ctx
window.importMapsAllowed = true
window.timeouts = newTimeoutState(ctx, evalJSFree, window)
+ if window.settings.scripting == smApp:
+ window.scriptAttrsp = window.attrsp
+ else:
+ window.scriptAttrsp = unsafeAddr dummyAttrs
ctx.addWindowModule()
ctx.setGlobal(window)
ctx.addDOMExceptionModule()