All files / once / mod.ts

100.00% Branches 13/13
100.00% Functions 3/3
100.00% Lines 35/35
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
 
x9
 
 
 
x9
x9
x9
x9
x9
 
 
x9
x9
x9
x9
x9
x16
x9
x9
x68
x4
x4
x9
x9
x68
x68
x2
x2
x68
x68
x59
x59
x9
x68
x5
x5
x9
x68
x9
 
 
 
 
 
x9











































// Imports
import { type Cache, type Directive, Phase } from "@mizu/internal/engine"
export type * from "@mizu/internal/engine"

/** `*once` typings. */
export const typings = {
  modifiers: {
    flat: { type: Boolean },
  },
} as const

/** `*once` directive. */
export const _once = {
  name: "*once",
  phase: Phase.POSTPROCESSING,
  typings,
  init(renderer) {
    renderer.cache<Cache<typeof _once>>(this.name, new WeakSet())
  },
  setup(_, element, { cache }) {
    if (cache.has(element)) {
      return false
    }
  },
  cleanup(renderer, element, { cache }) {
    let target = element
    if ((renderer.isComment(element)) && (renderer.cache("*").has(element))) {
      target = renderer.cache("*").get(element)!
    }
    const attribute = renderer.getAttributes(target, this.name, { first: true })
    if (!attribute) {
      return
    }
    const parsed = renderer.parseAttribute(attribute, this.typings, { modifiers: true })
    if ((renderer.isHtmlElement(element)) && (parsed.modifiers.flat)) {
      renderer.replaceElementWithChildNodes(element, element).forEach((element) => cache.add(element))
    }
    cache.add(element)
  },
} as const satisfies Directive<{
  Cache: WeakSet<HTMLElement | Comment>
  Typings: typeof typings
}>

/** Default exports. */
export default _once