-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathattributes.py
More file actions
722 lines (593 loc) · 21.1 KB
/
attributes.py
File metadata and controls
722 lines (593 loc) · 21.1 KB
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
from __future__ import annotations
import json
import re
from collections.abc import Iterable, Iterator, Mapping
from itertools import chain
from typing import TYPE_CHECKING, Literal, TypeAlias
if TYPE_CHECKING:
from typing import Self
__all__ = ["attribute_generator"]
JSEvent = Literal[
"abort",
"afterprint",
"animationend",
"animationiteration",
"animationstart",
"beforeprint",
"beforeunload",
"blur",
"canplay",
"canplaythrough",
"change",
"click",
"contextmenu",
"copy",
"cut",
"dblclick",
"drag",
"dragend",
"dragenter",
"dragleave",
"dragover",
"dragstart",
"drop",
"durationchange",
"ended",
"error",
"focus",
"focusin",
"focusout",
"fullscreenchange",
"fullscreenerror",
"hashchange",
"input",
"invalid",
"keydown",
"keypress",
"keyup",
"load",
"loadeddata",
"loadedmetadata",
"loadstart",
"message",
"mousedown",
"mouseenter",
"mouseleave",
"mousemove",
"mouseover",
"mouseout",
"mouseup",
"mousewheel",
"offline",
"online",
"open",
"pagehide",
"pageshow",
"paste",
"pause",
"play",
"playing",
"popstate",
"progress",
"ratechange",
"resize",
"reset",
"scroll",
"search",
"seeked",
"seeking",
"select",
"show",
"stalled",
"storage",
"submit",
"suspend",
"timeupdate",
"toggle",
"touchcancel",
"touchend",
"touchmove",
"touchstart",
"transitionend",
"unload",
"volumechange",
"waiting",
"wheel",
]
SignalValue: TypeAlias = (
str | int | float | bool | dict[str, "SignalValue"] | list["SignalValue"] | None
)
class AttributeGenerator:
def __init__(self, alias: str = "data-") -> None:
"""A helper which can generate all the Datastar attributes.
:param alias: The prefix for all attributes. Defaults to `data-`.
"""
self._alias: str = alias
def signals(
self,
signals_dict: Mapping[str, SignalValue] | None = None,
/,
*,
expressions_: bool = False,
**signals: SignalValue,
) -> SignalsAttr:
"""Patch one or more signals into the existing signals.
:param signals_dict: A dictionary of signals to patch.
:param expressions_: If True, the values of the signals will be evaluated as expressions
rather than literals.
"""
signals = {**(signals_dict or {}), **signals}
val = _js_object(signals) if expressions_ else json.dumps(signals)
return SignalsAttr(value=val, alias=self._alias)
def computed(self, computed_dict: Mapping | None = None, /, **computed: str) -> BaseAttr:
"""Create signals that are computed based on an expression."""
computed = {**(computed_dict or {}), **computed}
first, *rest = (
BaseAttr("computed", key=sig, value=expr, alias=self._alias)
for sig, expr in computed.items()
)
first._other_attrs = rest
return first
def effect(self, expression: str) -> BaseAttr:
"""Execute an expression when any referenced signals change."""
return BaseAttr("effect", value=expression, alias=self._alias)
@property
def ignore(self) -> IgnoreAttr:
"""Tell Datastar to ignore data-* attributes on the element."""
return IgnoreAttr(alias=self._alias)
def attr(self, attr_dict: Mapping | None = None, /, **attrs: str) -> BaseAttr:
"""Set the value of any HTML attributes to expressions, and keep them in sync."""
attrs = {**(attr_dict or {}), **attrs}
return BaseAttr("attr", value=_js_object(attrs), alias=self._alias)
def bind(self, signal_name: str) -> BaseAttr:
"""Set up two-way data binding between a signal and an element's value."""
return BaseAttr("bind", value=signal_name, alias=self._alias)
def class_(self, class_dict: Mapping | None = None, /, **classes: str) -> BaseAttr:
"""Add or removes classes to or from an element based on expressions."""
classes = {**(class_dict or {}), **classes}
return BaseAttr("class", value=_js_object(classes), alias=self._alias)
def init(self, expression: str) -> InitAttr:
"""Execute an expression when the element is loaded into the DOM."""
return InitAttr(value=expression, alias=self._alias)
def on(self, event: JSEvent | str, expression: str) -> OnAttr:
"""Execute an expression when an event occurs."""
return OnAttr(key=event, value=expression, alias=self._alias)
def on_interval(self, expression: str) -> OnIntervalAttr:
"""Execute an expression at a regular interval."""
return OnIntervalAttr(value=expression, alias=self._alias)
def on_intersect(self, expression: str) -> OnIntersectAttr:
"""Execute an expression when the element intersects with the viewport."""
return OnIntersectAttr(value=expression, alias=self._alias)
def on_raf(self, expression: str) -> OnRafAttr:
"""(PRO) Execute an expression on every requestAnimationFrame event."""
return OnRafAttr(value=expression, alias=self._alias)
def on_signal_patch(
self, expression: str, include: str | None = None, exclude: str | None = None
) -> OnSignalPatchAttr:
"""Execute an expression when a signal patch takes place."""
attr = OnSignalPatchAttr(value=expression, alias=self._alias)
if include or exclude:
attr.filter(include, exclude)
return attr
def on_resize(self, expression: str) -> OnResizeAttr:
"""(PRO) Execute an expression each time the element's dimensions change."""
return OnResizeAttr(value=expression, alias=self._alias)
@property
def persist(self) -> PersistAttr:
"""(PRO) Persist signals in local storage."""
return PersistAttr(alias=self._alias)
def ref(self, signal_name: str) -> BaseAttr:
"""Create a signal which references the element on which the attribute is placed."""
return BaseAttr("ref", value=signal_name, alias=self._alias)
def replace_url(self, url_expression: str) -> BaseAttr:
"""(PRO) Replace the URL in the browser without replacing the page."""
return BaseAttr("replace-url", value=url_expression, alias=self._alias)
def show(self, expression: str) -> BaseAttr:
"""Show or hides an element based on whether an expression evaluates to true or false."""
return BaseAttr("show", value=expression, alias=self._alias)
def style(self, style_dict: Mapping | None = None, /, **styles: str) -> BaseAttr:
"""Set the value of inline CSS styles on an element based on an expression, and keeps them in sync."""
styles = {**(style_dict or {}), **styles}
return BaseAttr("style", value=_js_object(styles), alias=self._alias)
def text(self, expression: str) -> BaseAttr:
"""Bind the text content of an element to an expression."""
return BaseAttr("text", value=expression, alias=self._alias)
def indicator(self, signal_name: str) -> BaseAttr:
"""Create a signal whose value is true while an SSE request is in flight."""
return BaseAttr("indicator", value=signal_name, alias=self._alias)
def custom_validity(self, expression: str) -> BaseAttr:
"""(PRO) Set the validity message for an element based on an expression."""
return BaseAttr("custom-validity", value=expression, alias=self._alias)
@property
def scroll_into_view(self) -> ScrollIntoViewAttr:
"""(PRO) Scrolls the element into view."""
return ScrollIntoViewAttr(alias=self._alias)
def view_transition(self, expression: str) -> BaseAttr:
"""(PRO) Set the view-transition-name style attribute explicitly."""
return BaseAttr("view-transition", value=expression, alias=self._alias)
@property
def json_signals(self) -> BaseAttr:
"""Create a signal that contains the JSON representation of the signals."""
return JsonSignalsAttr(alias=self._alias)
@property
def ignore_morph(self) -> BaseAttr:
"""Do not overwrite this element or its children when morphing."""
return BaseAttr("ignore-morph", alias=self._alias)
def preserve_attr(self, attrs: str | Iterable[str]) -> BaseAttr:
"""Preserve the client side state for specified attribute(s) when morphing."""
value = attrs if isinstance(attrs, str) else " ".join(attrs)
return BaseAttr("preserve-attrs", value=value, alias=self._alias)
@property
def query_string(self) -> QueryStringAttr:
"""(PRO) Sync the query string with signal values."""
return QueryStringAttr(alias=self._alias)
class BaseAttr(Mapping):
_attr: str
def __init__(
self,
attr: str | None = None,
/,
*,
key: str | None = None,
value: str | Literal[True] = True,
alias: str = "data-",
) -> None:
if attr:
self._attr: str = attr
self._key: str | None = None
self._mods: dict[str, list[str]] = {}
self._other_attrs: list[BaseAttr] = []
self._value: str | Literal[True] = value
self._alias: str = alias
if key:
self._to_kebab_key(key)
def __call__(self) -> Self:
# Because some attributes and modifiers do not need to be called,
# allow calling them anyway so that all attributes allow parens.
return self
def _full_key(self) -> str:
key = f"{self._alias}{self._attr}"
if self._key:
key += f":{self._key}"
for mod, values in self._mods.items():
key += f"__{mod}"
if values:
key += f".{'.'.join(values)}"
return key
def _to_kebab_key(self, key_name: str) -> None:
if "__" in key_name:
# _ are allowed in attributes, the only time we need to convert is if there are multiple underscores
kebab_name, from_case = key_name.lower().replace("_", "-"), "snake"
elif key_name[0].isupper():
kebab_name, from_case = (
re.sub(r"((?<!\.)[A-Z])", r"-\1", key_name).lstrip("-").lower(),
"pascal",
)
elif key_name.lower() != key_name:
kebab_name, from_case = (
re.sub(r"([A-Z])", r"-\1", key_name).lower(),
"camel",
)
else:
# kebab case means the raw name from the attribute will be passed through
kebab_name, from_case = key_name, "kebab"
self._key = kebab_name
if from_case:
self._mods["case"] = [from_case]
def __getitem__(self, key: str, /) -> str | Literal[True]:
if key == self._full_key():
return self._value
for attr in self._other_attrs:
if key == attr._full_key():
return attr._value
raise KeyError(key)
def __len__(self) -> int:
return len(self._other_attrs) + 1
def __iter__(self) -> Iterator[str]:
return chain((self._full_key(),), *self._other_attrs)
def __str__(self) -> str:
r = _escape(self._full_key())
if isinstance(self._value, str):
r += f'="{_escape(self._value)}"'
if self._other_attrs:
other = " ".join(str(o) for o in self._other_attrs)
r += f" {other}"
return r
__html__ = __str__
class TimingMod:
def debounce(
self: Self,
wait: int | str,
*,
leading: bool = False,
notrailing: bool = False,
) -> Self:
"""Debounce the event listener.
:param wait: The minimum interval between events.
:param leading: If True, the event listener will be called on the leading edge of the
wait time.
:param notrailing: If True, the event listener will not be called on the trailing edge of the
wait time.
"""
self._mods["debounce"] = [str(wait)]
if leading:
self._mods["debounce"].append("leading")
if notrailing:
self._mods["debounce"].append("notrailing")
return self
def throttle(
self: Self,
wait: int | str,
*,
noleading: bool = False,
trailing: bool = False,
) -> Self:
"""Throttle the event listener.
:param wait: The minimum interval between events.
:param noleading: If true, the event listener will not be called on the leading edge of the
wait time.
:param trailing: If true, the event listener will be called on the trailing edge of the
wait time.
"""
self._mods["throttle"] = [str(wait)]
if noleading:
self._mods["throttle"].append("noleading")
if trailing:
self._mods["throttle"].append("trailing")
return self
class DelayMod:
def delay(
self: Self,
wait: int | str,
) -> Self:
"""Delay the event listener.
:param wait: The delay time.
"""
self._mods["delay"] = [str(wait)]
return self
class ViewtransitionMod:
@property
def viewtransition(self: Self) -> Self:
"""Wrap the expression in document.startViewTransition()."""
self._mods["view-transition"] = []
return self
class SignalsAttr(BaseAttr):
_attr = "signals"
@property
def ifmissing(self) -> Self:
"""Only set signals that do not already exist."""
self._mods["ifmissing"] = []
return self
class IgnoreAttr(BaseAttr):
_attr = "ignore"
@property
def self(self) -> Self:
"""Only ignore the element itself, not its descendants."""
self._mods["self"] = []
return self
class OnAttr(BaseAttr, TimingMod, DelayMod, ViewtransitionMod):
_attr = "on"
@property
def once(self) -> Self:
"""Only trigger the event listener once."""
self._mods["once"] = []
return self
@property
def passive(self) -> Self:
"""Do not call preventDefault on the event listener."""
self._mods["passive"] = []
return self
@property
def capture(self) -> Self:
"""Use a capture event listener."""
self._mods["capture"] = []
return self
@property
def window(self) -> Self:
"""Attach the event listener to the window element."""
self._mods["window"] = []
return self
@property
def outside(self) -> Self:
"""Trigger when the event is outside the element."""
self._mods["outside"] = []
return self
@property
def prevent(self) -> Self:
"""Call preventDefault on the event listener."""
self._mods["prevent"] = []
return self
@property
def stop(self) -> Self:
"""Call stopPropagation on the event listener."""
self._mods["stop"] = []
return self
@property
def trust(self) -> Self:
"""Run even when isTrusted property on the event is false."""
self._mods["trust"] = []
return self
class PersistAttr(BaseAttr):
_attr = "persist"
def __call__(
self,
storage_key: str | None = None,
include: str | None = None,
exclude: str | None = None,
) -> Self:
if storage_key:
self._key = storage_key
if include or exclude:
self._value = json.dumps(_filter_dict(include=include, exclude=exclude))
return self
@property
def session(self) -> Self:
"""Persist signals in session storage."""
self._mods["session"] = []
return self
class JsonSignalsAttr(BaseAttr):
_attr = "json-signals"
def __call__(self, include: str | None = None, exclude: str | None = None) -> Self:
if include or exclude:
self._value = json.dumps(_filter_dict(include=include, exclude=exclude))
return self
@property
def terse(self) -> Self:
"""Output without extra whitespace."""
self._mods["terse"] = []
return self
class ScrollIntoViewAttr(BaseAttr):
_attr = "scroll-into-view"
@property
def smooth(self) -> Self:
"""Animate scrolling smoothly."""
self._mods["smooth"] = []
return self
@property
def instant(self) -> Self:
"""Scroll instantly."""
self._mods["instant"] = []
return self
@property
def auto(self) -> Self:
"""Let scrolling be determined by the computed scroll-behavior CSS property."""
self._mods["auto"] = []
return self
@property
def hstart(self) -> Self:
"""Scroll to the left of the element."""
self._mods["hstart"] = []
return self
@property
def hcenter(self) -> Self:
"""Scroll to the horizontal center of the element."""
self._mods["hcenter"] = []
return self
@property
def hend(self) -> Self:
"""Scroll to the right of the element."""
self._mods["hend"] = []
return self
@property
def hnearest(self) -> Self:
"""Scroll to the nearest horizontal edge of the element."""
self._mods["hnearest"] = []
return self
@property
def vstart(self) -> Self:
"""Scroll to the top of the element."""
self._mods["vstart"] = []
return self
@property
def vcenter(self) -> Self:
"""Scroll to the vertical center of the element."""
self._mods["vcenter"] = []
return self
@property
def vend(self) -> Self:
"""Scroll to the bottom of the element."""
self._mods["vend"] = []
return self
@property
def vnearest(self) -> Self:
"""Scroll to the nearest vertical edge of the element."""
self._mods["vnearest"] = []
return self
@property
def focus(self) -> Self:
"""Focus the element after scrolling."""
self._mods["focus"] = []
return self
class OnIntersectAttr(BaseAttr, TimingMod, DelayMod, ViewtransitionMod):
_attr = "on-intersect"
@property
def once(self) -> Self:
"""Only trigger the event listener once."""
self._mods["once"] = []
return self
@property
def half(self) -> Self:
"""Trigger the event listener when half the element enters the viewport."""
self._mods["half"] = []
return self
@property
def full(self) -> Self:
"""Trigger the event listener when the full element is visible."""
self._mods["full"] = []
return self
@property
def exit(self) -> Self:
"""Trigger the event listener when the element exits the viewport."""
self._mods["exit"] = []
return self
def threshold(self, threshold: int) -> Self:
"""Trigger the event listener when the element enters the viewport at the specified percentage."""
self._mods["threshold"] = [str(threshold)]
return self
class OnIntervalAttr(BaseAttr, ViewtransitionMod):
_attr = "on-interval"
def duration(self, duration: float | str, *, leading: bool = False) -> Self:
"""Set the interval duration."""
self._mods["duration"] = [str(duration)]
if leading:
self._mods["duration"].append("leading")
return self
class InitAttr(BaseAttr, ViewtransitionMod, DelayMod):
_attr = "init"
@property
def once(self) -> Self:
"""Only trigger the event listener once."""
self._mods["once"] = []
return self
class OnRafAttr(BaseAttr, TimingMod):
_attr = "on-raf"
class OnSignalPatchAttr(BaseAttr, TimingMod, DelayMod):
_attr = "on-signal-patch"
def filter(self, include: str | None = None, exclude: str | None = None) -> Self:
"""Filter the signal patch events."""
if include or exclude:
self._other_attrs = [
BaseAttr(
"on-signal-patch-filter",
value=json.dumps(_filter_dict(include=include, exclude=exclude)),
)
]
return self
class OnResizeAttr(BaseAttr, TimingMod):
_attr = "on-resize"
class QueryStringAttr(BaseAttr):
_attr = "query-string"
def __call__(self, include: str | None = None, exclude: str | None = None) -> Self:
if include or exclude:
self._value = json.dumps(_filter_dict(include=include, exclude=exclude))
return self
@property
def history(self) -> Self:
self._mods["history"] = []
return self
def _escape(s: str) -> str:
return (
s.replace("&", "&")
.replace("'", "'")
.replace('"', """)
.replace(">", ">")
.replace("<", "<")
)
def _filter_dict(include: str | None = None, exclude: str | None = None) -> dict:
filter_dict = {}
if include:
filter_dict["include"] = include
if exclude:
filter_dict["exclude"] = exclude
return filter_dict
def _js_object(obj: dict) -> str:
"""Create a JS object where the values are expressions rather than strings."""
return (
"{"
+ ", ".join(
f"{json.dumps(k)}: {_js_object(v) if isinstance(v, dict) else v}"
for k, v in obj.items()
)
+ "}"
)
attribute_generator = AttributeGenerator()