File: __parseTriggerSpecs.js

package info (click to toggle)
htmx 4.0.0-alpha6-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 117,328 kB
  • sloc: javascript: 49,403; sh: 29; makefile: 7
file content (128 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download | duplicates (2)
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
describe('__parseTriggerSpecs unit tests', function() {

    it('parses single event', function () {
        let specs = htmx.__parseTriggerSpecs('click')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
    })

    it('parses multiple events with comma', function () {
        let specs = htmx.__parseTriggerSpecs('click, change')
        assert.equal(specs.length, 2)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[1].name, 'change')
    })

    it('parses event with modifier value', function () {
        let specs = htmx.__parseTriggerSpecs('click delay:100ms')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].delay, '100ms')
    })

    it('parses event with boolean modifier', function () {
        let specs = htmx.__parseTriggerSpecs('click once')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].once, true)
    })

    it('parses event with multiple modifiers', function () {
        let specs = htmx.__parseTriggerSpecs('click delay:100ms throttle:200ms once')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].delay, '100ms')
        assert.equal(specs[0].throttle, '200ms')
        assert.equal(specs[0].once, true)
    })

    it('parses event with from modifier', function () {
        let specs = htmx.__parseTriggerSpecs('click from:#other')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].from, '#other')
    })

    it('parses event with target modifier', function () {
        let specs = htmx.__parseTriggerSpecs('click target:.item')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].target, '.item')
    })

    it('parses event with consume modifier', function () {
        let specs = htmx.__parseTriggerSpecs('click consume')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].consume, true)
    })

    it('parses event with changed modifier', function () {
        let specs = htmx.__parseTriggerSpecs('change changed')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'change')
        assert.equal(specs[0].changed, true)
    })

    it('parses event with filter', function () {
        let specs = htmx.__parseTriggerSpecs('click[ctrlKey]')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click[ctrlKey]')
    })

    it('parses event with filter containing spaces', function () {
        let specs = htmx.__parseTriggerSpecs('click[event.detail > 5]')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click[event.detail > 5]')
    })

    it('preserves whitespace in string literals in filters', function () {
        let specs = htmx.__parseTriggerSpecs('click[event.detail === "hello world"]')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click[event.detail === "hello world"]')
    })

    it('parses multiple events with different modifiers', function () {
        let specs = htmx.__parseTriggerSpecs('click once, change delay:100ms')
        assert.equal(specs.length, 2)
        assert.equal(specs[0].name, 'click')
        assert.equal(specs[0].once, true)
        assert.equal(specs[1].name, 'change')
        assert.equal(specs[1].delay, '100ms')
    })

    it('parses every trigger', function () {
        let specs = htmx.__parseTriggerSpecs('every 1s')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'every')
        assert.equal(specs[0]['1s'], true)
    })

    it('handles empty string', function () {
        let specs = htmx.__parseTriggerSpecs('')
        assert.equal(specs.length, 0)
    })

    it('handles whitespace only', function () {
        let specs = htmx.__parseTriggerSpecs('   ')
        assert.equal(specs.length, 0)
    })

    it('throws on unterminated filter', function () {
        assert.throws(() => {
            htmx.__parseTriggerSpecs('click[ctrlKey')
        })
    })

    it('parses complex trigger spec', function () {
        let specs = htmx.__parseTriggerSpecs('click[ctrlKey] delay:100ms throttle:200ms from:body target:.item once')
        assert.equal(specs.length, 1)
        assert.equal(specs[0].name, 'click[ctrlKey]')
        assert.equal(specs[0].delay, '100ms')
        assert.equal(specs[0].throttle, '200ms')
        assert.equal(specs[0].from, 'body')
        assert.equal(specs[0].target, '.item')
        assert.equal(specs[0].once, true)
    })

});