diff options
| author | bptato <nincsnevem662@gmail.com> | 2025-01-06 17:45:09 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2025-01-06 17:45:09 +0100 |
| commit | e0f3655cb0dc5a45206d4e53f0243c7d96452279 (patch) | |
| tree | 4ad9975b27169d8b9a7312fcee307afe467acd40 /src | |
| parent | c1ededa968960bea8dc4ba9d608809772797e324 (diff) | |
| download | chawan-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')
| -rw-r--r-- | src/css/cascade.nim | 53 | ||||
| -rw-r--r-- | src/css/mediaquery.nim | 58 | ||||
| -rw-r--r-- | src/html/dom.nim | 22 | ||||
| -rw-r--r-- | src/html/env.nim | 22 | ||||
| -rw-r--r-- | src/types/winattrs.nim | 10 |
5 files changed, 98 insertions, 67 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim index e8c14bc8..7940548c 100644 --- a/src/css/cascade.nim +++ b/src/css/cascade.nim @@ -28,54 +28,6 @@ type user: RuleList author: seq[RuleList] -func appliesLR(feature: MediaFeature; window: Window; n: float64): bool = - let a = feature.lengthrange.s.a.num - let b = feature.lengthrange.s.b.num - return (feature.lengthrange.aeq and a == n or a < n) and - (feature.lengthrange.beq and b == n or n < b) - -func applies(feature: MediaFeature; window: Window): bool = - case feature.t - of mftColor: - return 8 in feature.range - of mftGrid: - return feature.b - of mftHover: - return feature.b - of mftPrefersColorScheme: - return feature.b == window.attrsp.prefersDark - of mftWidth: - return feature.appliesLR(window, float64(window.attrsp.widthPx)) - of mftHeight: - return feature.appliesLR(window, float64(window.attrsp.heightPx)) - of mftScripting: - return feature.b == (window.settings.scripting != smFalse) - -func applies(mq: MediaQuery; window: Window): bool = - case mq.t - of mctMedia: - case mq.media - of mtAll: return true - of mtPrint: return false - of mtScreen: return true - of mtSpeech: return false - of mtTty: return true - of mctNot: - return not mq.n.applies(window) - of mctAnd: - return mq.left.applies(window) and mq.right.applies(window) - of mctOr: - return mq.left.applies(window) or mq.right.applies(window) - of mctFeature: - return mq.feature.applies(window) - -func applies*(mqlist: MediaQueryList; window: Window): bool = - for mq in mqlist: - if mq.applies(window): - return true - return false - -type ToSorts = array[PseudoElem, seq[(int, CSSRuleDef)]] proc calcRule(tosorts: var ToSorts; element: Element; @@ -339,7 +291,7 @@ func applyMediaQuery(ss: CSSStylesheet; window: Window): CSSStylesheet = var res = CSSStylesheet() res[] = ss[] for mq in ss.mqList: - if mq.query.applies(window): + if mq.query.applies(window.settings.scripting, window.attrsp): res.add(mq.children.applyMediaQuery(window)) return res @@ -606,6 +558,3 @@ proc applyStylesheets*(document: Document; uass, userss: CSSStylesheet; let uass = uass.applyMediaQuery(document.window) let userss = userss.applyMediaQuery(document.window) return document.applyRules(uass, userss, previousStyled) - -# Forward declaration hack -appliesImpl = applies diff --git a/src/css/mediaquery.nim b/src/css/mediaquery.nim index 5cb1ad98..83b8fd64 100644 --- a/src/css/mediaquery.nim +++ b/src/css/mediaquery.nim @@ -2,6 +2,7 @@ import std/options import css/cssparser import css/cssvalues +import html/script import types/opt import types/winattrs import utils/twtstr @@ -411,3 +412,60 @@ proc parseMediaQueryList*(cvals: seq[CSSComponentValue]; let query = parser.parseMediaQuery() if query.isSome: result.add(query.get) + +type + MediaApplyContext = object + scripting: ScriptingMode + attrsp: ptr WindowAttributes + +func appliesLR(feature: MediaFeature; n: float64): bool = + let a = feature.lengthrange.s.a.num + let b = feature.lengthrange.s.b.num + return (feature.lengthrange.aeq and a == n or a < n) and + (feature.lengthrange.beq and b == n or n < b) + +func applies(ctx: MediaApplyContext; feature: MediaFeature): bool = + case feature.t + of mftColor: + return 8 in feature.range + of mftGrid: + return feature.b + of mftHover: + return feature.b + of mftPrefersColorScheme: + return feature.b == ctx.attrsp.prefersDark + of mftWidth: + return feature.appliesLR(float64(ctx.attrsp.widthPx)) + of mftHeight: + return feature.appliesLR(float64(ctx.attrsp.heightPx)) + of mftScripting: + return feature.b == (ctx.scripting != smFalse) + +func applies(ctx: MediaApplyContext; mq: MediaQuery): bool = + case mq.t + of mctMedia: + case mq.media + of mtAll: return true + of mtPrint: return false + of mtScreen: return true + of mtSpeech: return false + of mtTty: return true + of mctNot: + return not ctx.applies(mq.n) + of mctAnd: + return ctx.applies(mq.left) and ctx.applies(mq.right) + of mctOr: + return ctx.applies(mq.left) or ctx.applies(mq.right) + of mctFeature: + return ctx.applies(mq.feature) + +func applies(ctx: MediaApplyContext; mqlist: MediaQueryList): bool = + for mq in mqlist: + if ctx.applies(mq): + return true + return false + +func applies*(mqlist: MediaQueryList; scripting: ScriptingMode; + attrsp: ptr WindowAttributes): bool = + let ctx = MediaApplyContext(scripting: scripting, attrsp: attrsp) + return ctx.applies(mqlist) 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() diff --git a/src/types/winattrs.nim b/src/types/winattrs.nim index e29daa9d..511e4699 100644 --- a/src/types/winattrs.nim +++ b/src/types/winattrs.nim @@ -6,3 +6,13 @@ type WindowAttributes* = object widthPx*: int heightPx*: int prefersDark*: bool # prefers-color-scheme accepts "dark" (not "light") + +let dummyAttrs* {.global.} = WindowAttributes( + width: 80, + height: 24, + ppc: 9, + ppl: 18, + widthPx: 80 * 9, + heightPx: 24 * 18, + prefersDark: true +) |