about summary refs log tree commit diff stats
path: root/src/css/mediaquery.nim
blob: f92ecfb213433bd9a1a70b3132a251b86abf0cf1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import strutils
import tables

import css/cssparser
import css/values
import types/opt
import utils/twtstr

type
  MediaQueryParser = object
    at: int
    cvals: seq[CSSComponentValue]

  MediaType* = enum
    MEDIA_TYPE_UNKNOWN = "unknown"
    MEDIA_TYPE_ALL = "all"
    MEDIA_TYPE_PRINT = "print"
    MEDIA_TYPE_SCREEN = "screen"
    MEDIA_TYPE_SPEECH = "speech"
    MEDIA_TYPE_TTY = "tty"

  MediaConditionType* = enum
    CONDITION_NOT, CONDITION_AND, CONDITION_OR, CONDITION_FEATURE,
    CONDITION_MEDIA

  MediaFeatureType* = enum
    FEATURE_COLOR, FEATURE_GRID, FEATURE_HOVER, FEATURE_PREFERS_COLOR_SCHEME,
    FEATURE_WIDTH, FEATURE_HEIGHT

  MediaFeature* = object
    case t*: MediaFeatureType
    of FEATURE_COLOR:
      range*: Slice[int]
    of FEATURE_GRID, FEATURE_HOVER, FEATURE_PREFERS_COLOR_SCHEME:
      b*: bool
    of FEATURE_WIDTH, FEATURE_HEIGHT:
      lengthrange*: Slice[CSSLength]
      lengthaeq*: bool
      lengthbeq*: bool

  MediaQuery* = ref object
    case t*: MediaConditionType
    of CONDITION_MEDIA:
      media*: MediaType
    of CONDITION_FEATURE:
      feature*: MediaFeature
    of CONDITION_NOT:
      n*: MediaQuery
    of CONDITION_OR:
      ora*: MediaQuery
      orb*: MediaQuery
    of CONDITION_AND:
      anda*: MediaQuery
      andb*: MediaQuery

  MediaQueryList* = seq[MediaQuery]

  MediaQueryComparison = enum
    COMPARISON_EQ, COMPARISON_GT, COMPARISON_LT, COMPARISON_GE, COMPARISON_LE

# for debugging
func `$`*(mf: MediaFeature): string =
  case mf.t
  of FEATURE_COLOR:
    return "color: " & $mf.range.a & ".." & $mf.range.b
  of FEATURE_GRID:
    return "grid: " & $mf.b
  of FEATURE_HOVER:
    return "hover: " & $mf.b
  of FEATURE_PREFERS_COLOR_SCHEME:
    return "prefers-color-scheme: " & $mf.b
  of FEATURE_WIDTH:
    result &= $mf.lengthrange.a
    result &= " <"
    if mf.lengthaeq:
      result &= "="
    result &= " width <"
    if mf.lengthbeq:
      result &= "="
    result &= " "
    result &= $mf.lengthrange.b
  of FEATURE_HEIGHT:
    result &= $mf.lengthrange.a
    result &= " <"
    if mf.lengthaeq:
      result &= "="
    result &= " width "
    result &= "<"
    if mf.lengthbeq:
      result &= "="
    result &= " "
    result &= $mf.lengthrange.b

func `$`*(mq: MediaQuery): string =
  case mq.t
  of CONDITION_MEDIA: return $mq.media
  of CONDITION_FEATURE: return $mq.feature
  of CONDITION_NOT: return "not (" & $mq.n
  of CONDITION_OR: return "(" & $mq.ora & ") or (" & $mq.orb & ")"
  of CONDITION_AND: return "(" & $mq.anda & ") or (" & $mq.andb & ")"

const MediaTypes = {
  "all": MEDIA_TYPE_ALL,
  "print": MEDIA_TYPE_PRINT,
  "screen": MEDIA_TYPE_SCREEN,
  "speech": MEDIA_TYPE_SPEECH,
  "tty": MEDIA_TYPE_TTY
}.toTable()

const RangeFeatures = {FEATURE_COLOR, FEATURE_WIDTH, FEATURE_HEIGHT}

proc has(parser: MediaQueryParser, i = 0): bool {.inline.} =
  return parser.cvals.len > parser.at + i

proc consume(parser: var MediaQueryParser): CSSComponentValue {.inline.} =
  result = parser.cvals[parser.at]
  inc parser.at

proc reconsume(parser: var MediaQueryParser) {.inline.} =
  dec parser.at

proc peek(parser: MediaQueryParser, i = 0): CSSComponentValue {.inline.} =
  return parser.cvals[parser.at + i]

proc skipBlanks(parser: var MediaQueryParser) {.inline.} =
  while parser.has():
    let cval = parser.peek()
    if cval of CSSToken and CSSToken(cval).tokenType == CSS_WHITESPACE_TOKEN:
      inc parser.at
    else:
      break

proc getBoolFeature(feature: MediaFeatureType): MediaQuery =
  result = MediaQuery(t: CONDITION_FEATURE)
  case feature
  of FEATURE_GRID, FEATURE_HOVER, FEATURE_PREFERS_COLOR_SCHEME:
    result.feature = MediaFeature(t: feature, b: true)
  of FEATURE_COLOR:
    result.feature = MediaFeature(t: feature, range: 1..high(int))
  else:
    return nil

template skip_has(): bool =
  parser.skipBlanks()
  parser.has()

template get_tok(tok: untyped) =
  if not (cval of CSSToken): return nil
  tok = CSSToken(cval)

template get_idtok(tok: untyped) =
  get_tok(tok)
  if tok.tokenType != CSS_IDENT_TOKEN: return nil

template consume_token(): CSSToken =
  let cval = parser.consume()
  if not (cval of CSSToken): return nil
  CSSToken(cval)

template skip_consume(): CSSToken =
  parser.skipBlanks()
  consume_token()

template expect_int(i: var int) =
  let cval = parser.consume()
  if not (cval of CSSToken): return nil
  let tok = CSSToken(cval)
  if tok.tokenType == CSS_NUMBER_TOKEN and tok.tflagb == TFLAGB_INTEGER:
    i = int(tok.nvalue)
  else:
    return nil

template expect_mq_int(b: bool, ifalse: int, itrue: int) =
  var i: int
  expect_int(i)
  if i == ifalse: b = false
  elif i == itrue: b = true
  else: return nil

template expect_bool(b: bool, sfalse: string, strue: string) =
  let tok = consume_token()
  if tok.tokenType != CSS_IDENT_TOKEN: return nil
  let s = tok.value
  case s
  of strue: b = true
  of sfalse: b = false
  else: return nil

template expect_comparison(comparison: var MediaQueryComparison) =
  let tok = consume_token()
  if tok != CSS_DELIM_TOKEN: return nil
  let c = tok.cvalue
  if c notin {'=', '<', '>'}: return nil
  block parse:
    case c
    of '<':
      if parser.has():
        let tok = skip_consume()
        if tok == CSS_DELIM_TOKEN and tok.cvalue == '=':
          comparison = COMPARISON_LE
          break parse
        parser.reconsume()
      comparison = COMPARISON_LT
    of '>':
      if parser.has():
        let tok = skip_consume()
        if tok == CSS_DELIM_TOKEN and tok.cvalue == '=':
          comparison = COMPARISON_GE
          break parse
        parser.reconsume()
      comparison = COMPARISON_GT
    of '=':
      comparison = COMPARISON_EQ
    else: return nil

template expect_int_range(range: var Slice[int], ismin, ismax: bool) =
  if ismin:
    expect_int(range.a)
  elif ismax:
    expect_int(range.b)
  else:
    let tok = consume_token
    parser.reconsume()
    if tok.tokenType == CSS_DELIM_TOKEN:
      var comparison: MediaQueryComparison
      expect_comparison(comparison)
      if not skip_has: return nil
      case comparison
      of COMPARISON_EQ:
        expect_int(range.a) #TODO should be >= 0 (for color at least)
        range.b = range.a
      of COMPARISON_GT:
        expect_int(range.a)
        range.b = high(int)
      of COMPARISON_GE:
        expect_int(range.a)
        range.b = high(int)
      of COMPARISON_LT:
        expect_int(range.b)
      of COMPARISON_LE:
        expect_int(range.b)
    else:
      return nil

template expect_length(length: var CSSLength) =
  let cval = parser.consume()
  let r = cssLength(cval)
  if r.isErr:
    return nil
  length = r.get

template expect_length_range(range: var Slice[CSSLength], lengthaeq, lengthbeq:
    var bool, ismin, ismax: bool) =
  if ismin:
    expect_length(range.a)
    range.b = CSSLength(num: Inf, unit: UNIT_PX)
    lengthaeq = true
  elif ismax:
    range.a = CSSLength(num: 0, unit: UNIT_PX)
    expect_length(range.b)
    lengthbeq = true
  else:
    let tok = consume_token
    parser.reconsume()
    if tok.tokenType == CSS_DELIM_TOKEN:
      var comparison: MediaQueryComparison
      expect_comparison(comparison)
      if not skip_has: return nil
      expect_length(range.a)
      if not skip_has: return nil
      expect_length(range.b)
      case comparison
      of COMPARISON_EQ:
        expect_length(range.a)
        range.b = range.a
        lengthaeq = true
        lengthbeq = true
      of COMPARISON_GT:
        expect_length(range.a)
        range.b = CSSLength(num: Inf, unit: UNIT_PX)
      of COMPARISON_GE:
        expect_length(range.a)
        range.b = CSSLength(num: Inf, unit: UNIT_PX)
        lengthaeq = true
      of COMPARISON_LT:
        range.a = CSSLength(num: 0, unit: UNIT_PX)
        expect_length(range.b)
      of COMPARISON_LE:
        range.a = CSSLength(num: 0, unit: UNIT_PX)
        expect_length(range.b)
        lengthbeq = true
    else:
      return nil

proc parseFeature(parser: var MediaQueryParser, t: MediaFeatureType,
    ismin, ismax: bool): MediaQuery =
  if not parser.has(): return getBoolFeature(t)
  let cval = parser.consume()
  var tok: CSSToken
  get_tok(tok)
  if tok.tokenType != CSS_COLON_TOKEN: return nil
  parser.skipBlanks()
  if (ismin or ismax) and t notin RangeFeatures:
    return nil
  if not parser.has(): return nil
  let feature = case t
  of FEATURE_GRID:
    var b: bool
    expect_mq_int(b, 0, 1)
    MediaFeature(t: t, b: b)
  of FEATURE_HOVER:
    var b: bool
    expect_bool(b, "none", "hover")
    MediaFeature(t: t, b: b)
  of FEATURE_PREFERS_COLOR_SCHEME:
    var b: bool
    expect_bool(b, "light", "dark")
    MediaFeature(t: t, b: b)
  of FEATURE_COLOR:
    var range: Slice[int]
    expect_int_range(range, ismin, ismax)
    MediaFeature(t: t, range: range)
  of FEATURE_WIDTH, FEATURE_HEIGHT:
    var range: Slice[CSSLength]
    var lengthaeq: bool
    var lengthbeq: bool
    expect_length_range(range, lengthaeq, lengthbeq, ismin, ismax)
    MediaFeature(
      t: t,
      lengthrange: range,
      lengthaeq: lengthaeq,
      lengthbeq: lengthbeq
    )
  parser.skipBlanks()
  if parser.has():
    return nil
  return MediaQuery(t: CONDITION_FEATURE, feature: feature)

proc parseMediaCondition(parser: var MediaQueryParser, non = false, noor = false): MediaQuery

proc parseMediaInParens(parser: var MediaQueryParser): MediaQuery =
  var fparser: MediaQueryParser
  block:
    let cval = parser.consume()
    if not (cval of CSSSimpleBlock): return nil

    let sb = CSSSimpleBlock(cval)
    if sb.token.tokenType != CSS_LPAREN_TOKEN: return nil

    fparser.cvals = sb.value
    fparser.skipBlanks()

  block:
    let cval = fparser.consume()
    var tok: CSSToken
    get_tok(tok)
    fparser.skipBlanks()
    if tok.tokenType == CSS_IDENT_TOKEN:
      var tokval = tok.value
      let ismin = tokval.startsWith("min-")
      let ismax = tokval.startsWith("max-")
      if ismin or ismax:
        tokval = tokval.substr(4)
      case tokval
      of "not":
        return fparser.parseMediaCondition(true)
      of "color":
        return fparser.parseFeature(FEATURE_COLOR, ismin, ismax)
      of "width":
        return fparser.parseFeature(FEATURE_WIDTH, ismin, ismax)
      of "grid":
        return fparser.parseFeature(FEATURE_GRID, ismin, ismax)
      of "hover":
        return fparser.parseFeature(FEATURE_HOVER, ismin, ismax)
      of "prefers-color-scheme":
        return fparser.parseFeature(FEATURE_PREFERS_COLOR_SCHEME, ismin, ismax)
      else: discard
  return nil

proc parseMediaOr(parser: var MediaQueryParser, left: MediaQuery): MediaQuery =
  let right = parser.parseMediaCondition()
  if right != nil:
    return MediaQuery(t: CONDITION_OR, ora: left, orb: right)
  return nil

proc parseMediaAnd(parser: var MediaQueryParser, left: MediaQuery): MediaQuery =
  let right = parser.parseMediaCondition()
  if right != nil:
    return MediaQuery(t: CONDITION_AND, anda: left, andb: right)
  return nil

proc parseMediaCondition(parser: var MediaQueryParser, non = false, noor = false): MediaQuery =
  var non = non
  if not non:
    let cval = parser.consume()
    if cval of CSSToken and CSSToken(cval).tokenType == CSS_IDENT_TOKEN:
      if CSSToken(cval).value == "not":
        non = true
    else:
      parser.reconsume()

  parser.skipBlanks()
  if not parser.has():
    return nil

  result = parser.parseMediaInParens()

  if result == nil:
    return nil

  if non:
    result = MediaQuery(t: CONDITION_NOT, n: result)

  parser.skipBlanks()
  if not parser.has():
    return result

  let cval = parser.consume()
  var tok: CSSToken
  get_idtok(tok)
  parser.skipBlanks()
  let tokval = tok.value
  case tokval
  of "and":
    return parser.parseMediaAnd(result)
  of "or":
    if noor:
      return nil
    return parser.parseMediaOr(result)
  else: discard

proc parseMediaQuery(parser: var MediaQueryParser): MediaQuery =
  parser.skipBlanks()
  if not parser.has():
    return nil
  var non = false
  block:
    let cval = parser.consume()
    if cval of CSSToken:
      let tok = CSSToken(cval)
      if tok.tokenType == CSS_IDENT_TOKEN:
        let tokval = tok.value
        case tokval
        of "not":
          non = true
        of "only":
          discard
        elif tokval in MediaTypes:
          result = MediaQuery(t: CONDITION_MEDIA, media: MediaTypes[tokval])
        else:
          return nil
      else:
        return nil
    else:
      parser.reconsume()
      return parser.parseMediaCondition()

  parser.skipBlanks()
  if not parser.has():
    return result

  block:
    let cval = parser.consume()
    if cval of CSSToken:
      let tok = CSSToken(cval)
      if tok.tokenType == CSS_IDENT_TOKEN:
        let tokval = tok.value
        if result == nil:
          if tokval in MediaTypes:
            let mq = MediaQuery(t: CONDITION_MEDIA, media: MediaTypes[tokval])
            if non:
              result = MediaQuery(t: CONDITION_NOT, n: mq)
            else:
              result = mq
          else:
            return nil
        else:
          if tokval == "and":
            parser.reconsume()
            return parser.parseMediaAnd(result)
          else:
            return nil
      else:
        return nil
    else:
      parser.reconsume()
      return parser.parseMediaCondition(non)

  parser.skipBlanks()
  if not parser.has():
    return result

  block:
    let cval = parser.consume()
    if cval of CSSToken:
      let tok = CSSToken(cval)
      if tok.tokenType == CSS_IDENT_TOKEN:
        let tokval = tok.value
        if tokval != "and":
          return nil
      else:
        return nil

    parser.skipBlanks()
    if not parser.has():
      return nil

    parser.reconsume()
    return parser.parseMediaAnd(result)

proc parseMediaQueryList*(cvals: seq[CSSComponentValue]): MediaQueryList =
  let cseplist = cvals.parseCommaSeparatedListOfComponentValues()
  for list in cseplist:
    var parser: MediaQueryParser
    parser.cvals = list
    let query = parser.parseMediaQuery()
    if query != nil:
      result.add(query)