All files / for / empty / mod.ts

100.00% Branches 21/21
100.00% Functions 1/1
100.00% Lines 42/42
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
 
x8
x8
x8
 
 
 
x8
x8
x8
x8
x8
 
 
x8
x8
x8
x8
x8
x17
x17
x17
x17
x17
 
x59
x1
x1
 
 
x59
x4
x4
 
 
x59
x15
x15
x15
x1
x1
x14
x15
x15
x15
x15
x43
x43
x3
x3
x17
x8
 
 
 
 
 
x8























































// Imports
import { type Cache, type Directive, Phase } from "@mizu/internal/engine"
import { _for, _id } from "@mizu/for"
import { _if } from "@mizu/if"
export type * from "@mizu/internal/engine"

/** Typings. */
export const typings = {
  modifiers: {
    not: { type: Boolean },
  },
} as const

/** `*empty` directive. */
export const _empty = {
  name: "*empty",
  phase: Phase.TOGGLE,
  typings,
  execute(renderer, element, { attributes: [attribute] }) {
    const parsed = renderer.parseAttribute(attribute, this.typings, { modifiers: true })
    const cache = renderer.cache<Cache<typeof _for>>(_for.name)
    const seen = [] as HTMLElement[]
    let previous = element.previousSibling as HTMLElement
    while (previous) {
      // Break on non-empty text nodes
      if ((previous.nodeType === renderer.window.Node.TEXT_NODE) && (previous.textContent?.trim())) {
        break
      }

      // Break on element node that was not created by a for directive or without an empty directive
      if ((renderer.isHtmlElement(previous)) && (!renderer.getAttributes(previous, [_empty.name, _id.name], { first: true }))) {
        seen.push(previous)
      }

      // Execute directive on first for loop found
      if ((renderer.isComment(previous)) && (cache?.has(previous))) {
        const items = [...cache.get(previous)!.items.values()]
        const $generated = items.length
        if (seen.some((item) => !items.includes(item))) {
          break
        }
        return {
          ..._if.execute(renderer, element, { ...arguments[2], _directive: { directive: this.name, expression: attribute.value, value: `!${parsed.modifiers.not ? "!" : ""}${$generated}` } }),
          state: { $generated },
        }
      }
      previous = (previous as HTMLElement).previousSibling as HTMLElement
    }
    renderer.warn(`[${this.name}] must be immediately preceded by a [${_for.name}] or [${_empty.name}] directive, ignoring`, element)
    return { state: { $generated: NaN } }
  },
} as const satisfies Directive<{
  Name: string
  Typings: typeof typings
}>

/** Default exports. */
export default [_for, _empty]