@domql/element 3.2.3 → 3.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/children.js +40 -12
  2. package/create.js +40 -26
  3. package/define.js +1 -1
  4. package/dist/cjs/children.js +39 -11
  5. package/dist/cjs/create.js +41 -11
  6. package/dist/cjs/define.js +1 -1
  7. package/dist/cjs/event/animationFrame.js +96 -0
  8. package/dist/cjs/event/can.js +28 -0
  9. package/dist/cjs/event/index.js +20 -0
  10. package/dist/cjs/event/on.js +84 -0
  11. package/dist/cjs/event/store.js +27 -0
  12. package/dist/cjs/extend.js +6 -6
  13. package/dist/cjs/index.js +9 -6
  14. package/dist/cjs/iterate.js +13 -13
  15. package/dist/cjs/methods/set.js +5 -0
  16. package/dist/cjs/methods/v2.js +1 -1
  17. package/dist/cjs/mixins/attr.js +3 -2
  18. package/dist/cjs/mixins/classList.js +11 -1
  19. package/dist/cjs/mixins/content.js +1 -2
  20. package/dist/cjs/mixins/html.js +1 -2
  21. package/dist/cjs/mixins/state.js +2 -2
  22. package/dist/cjs/mixins/style.js +11 -2
  23. package/dist/cjs/mixins/text.js +5 -1
  24. package/dist/cjs/node.js +8 -5
  25. package/dist/cjs/render/append.js +72 -0
  26. package/dist/cjs/render/cache.js +80 -0
  27. package/dist/cjs/render/create.js +25 -0
  28. package/dist/cjs/render/index.js +20 -0
  29. package/dist/cjs/set.js +12 -14
  30. package/dist/cjs/update.js +80 -40
  31. package/dist/cjs/utils/applyParam.js +3 -3
  32. package/dist/cjs/utils/extendUtils.js +5 -5
  33. package/dist/cjs/utils/index.js +2 -0
  34. package/dist/cjs/utils/propEvents.js +21 -4
  35. package/dist/esm/children.js +39 -11
  36. package/dist/esm/create.js +42 -11
  37. package/dist/esm/define.js +1 -1
  38. package/dist/esm/event/animationFrame.js +76 -0
  39. package/dist/esm/event/can.js +8 -0
  40. package/dist/esm/event/index.js +3 -0
  41. package/dist/esm/event/on.js +64 -0
  42. package/dist/esm/event/store.js +7 -0
  43. package/dist/esm/extend.js +7 -7
  44. package/dist/esm/index.js +8 -6
  45. package/dist/esm/iterate.js +13 -13
  46. package/dist/esm/methods/set.js +10 -0
  47. package/dist/esm/methods/v2.js +1 -1
  48. package/dist/esm/mixins/attr.js +4 -3
  49. package/dist/esm/mixins/classList.js +11 -1
  50. package/dist/esm/mixins/content.js +1 -2
  51. package/dist/esm/mixins/html.js +1 -2
  52. package/dist/esm/mixins/state.js +2 -2
  53. package/dist/esm/mixins/style.js +12 -3
  54. package/dist/esm/mixins/text.js +6 -2
  55. package/dist/esm/node.js +8 -5
  56. package/dist/esm/render/append.js +52 -0
  57. package/dist/esm/render/cache.js +60 -0
  58. package/dist/esm/render/create.js +5 -0
  59. package/dist/esm/render/index.js +3 -0
  60. package/dist/esm/set.js +12 -14
  61. package/dist/esm/update.js +80 -42
  62. package/dist/esm/utils/applyParam.js +3 -3
  63. package/dist/esm/utils/extendUtils.js +5 -5
  64. package/dist/esm/utils/index.js +1 -0
  65. package/dist/esm/utils/propEvents.js +21 -4
  66. package/dist/iife/index.js +4718 -0
  67. package/extend.js +7 -10
  68. package/index.js +9 -6
  69. package/iterate.js +20 -13
  70. package/node.js +10 -8
  71. package/package.json +42 -18
  72. package/set.js +6 -5
  73. package/update.js +90 -52
  74. package/__tests__/checkIfOnUpdate.test.js +0 -103
  75. package/__tests__/children.test.js +0 -209
  76. package/__tests__/define.test.js +0 -75
  77. package/__tests__/inheritStateUpdates.test.js +0 -79
  78. package/__tests__/renderElement.test.js +0 -131
  79. package/__tests__/resetElement.test.js +0 -44
  80. package/__tests__/set.test.js +0 -312
  81. package/__tests__/throughExecProps.test.js +0 -86
  82. package/__tests__/throughInitialDefine.test.js +0 -104
  83. package/__tests__/throughInitialExec.test.js +0 -92
  84. package/__tests__/throughUpdatedDefine.test.js +0 -92
  85. package/__tests__/throughUpdatedExec.test.js +0 -111
  86. package/__tests__/tree.test.js +0 -15
  87. package/__tests__/update.test.js +0 -256
  88. package/dist/cjs/package.json +0 -4
  89. package/methods/set.js +0 -63
  90. package/methods/v2.js +0 -83
  91. package/mixins/attr.js +0 -32
  92. package/mixins/classList.js +0 -54
  93. package/mixins/content.js +0 -65
  94. package/mixins/data.js +0 -26
  95. package/mixins/html.js +0 -21
  96. package/mixins/index.js +0 -23
  97. package/mixins/registry.js +0 -46
  98. package/mixins/scope.js +0 -23
  99. package/mixins/state.js +0 -19
  100. package/mixins/style.js +0 -16
  101. package/mixins/text.js +0 -26
  102. package/utils/applyParam.js +0 -34
  103. package/utils/extendUtils.js +0 -149
  104. package/utils/index.js +0 -3
  105. package/utils/propEvents.js +0 -19
@@ -1,111 +0,0 @@
1
- import { throughUpdatedExec } from '../iterate'
2
-
3
- describe('throughUpdatedExec', () => {
4
- let element, ref
5
-
6
- beforeEach(() => {
7
- ref = {
8
- __exec: {},
9
- __defineCache: {}
10
- }
11
- element = {
12
- __ref: ref,
13
- state: { testState: true },
14
- context: { testContext: true }
15
- }
16
- })
17
-
18
- it('should skip execution for params in __defineCache', async () => {
19
- ref.__defineCache.cachedParam = true
20
- ref.__exec.cachedParam = () => 'should not execute'
21
- element.cachedParam = 'original'
22
-
23
- const changes = await throughUpdatedExec(element)
24
-
25
- expect(element.cachedParam).toBe('original')
26
- expect(changes).toEqual({})
27
- })
28
-
29
- it('should update element properties when newExec differs from current value', async () => {
30
- ref.__exec.testParam = () => 'new value'
31
- element.testParam = 'old value'
32
-
33
- const changes = await throughUpdatedExec(element)
34
-
35
- expect(element.testParam).toBe('new value')
36
- expect(changes).toEqual({ testParam: 'old value' })
37
- })
38
-
39
- it('should overwrite text property for nodes when exec returns string or number', async () => {
40
- const node = { node: true, text: 'old text' }
41
- ref.__exec.testParam = () => 'new text'
42
- element.testParam = node
43
-
44
- const changes = await throughUpdatedExec(element)
45
-
46
- expect(element.testParam.text).toBe('new text')
47
- expect(changes).toEqual({})
48
- })
49
-
50
- it('should handle component naming matches and overwrite with context component', async () => {
51
- const contextComponent = { extends: 'Component', prop: 'value' }
52
- element.context.components = { TestComponent: contextComponent }
53
- ref.__exec.TestComponent = () => contextComponent
54
- element.TestComponent = { oldProp: 'oldValue' }
55
-
56
- const changes = await throughUpdatedExec(element)
57
-
58
- expect(element.TestComponent).toEqual({
59
- oldProp: 'oldValue',
60
- prop: 'value'
61
- })
62
- expect(changes).toEqual({})
63
- })
64
-
65
- it('should not update element properties when newExec matches current value', async () => {
66
- ref.__exec.testParam = () => 'same value'
67
- element.testParam = 'same value'
68
-
69
- const changes = await throughUpdatedExec(element)
70
-
71
- expect(element.testParam).toBe('same value')
72
- expect(changes).toEqual({})
73
- })
74
-
75
- it('should handle non-string/non-number exec returns for non-node properties', async () => {
76
- const newValue = { complex: 'object' }
77
- ref.__exec.testParam = () => newValue
78
- element.testParam = 'old value'
79
-
80
- const changes = await throughUpdatedExec(element)
81
-
82
- expect(element.testParam).toBe(newValue)
83
- expect(changes).toEqual({ testParam: 'old value' })
84
- })
85
-
86
- it('should return an empty changes object when no updates occur', async () => {
87
- ref.__exec.testParam = () => 'same value'
88
- element.testParam = 'same value'
89
-
90
- const changes = await throughUpdatedExec(element)
91
-
92
- expect(changes).toEqual({})
93
- })
94
-
95
- it('should handle multiple properties and return correct changes', async () => {
96
- ref.__exec.param1 = () => 'new value 1'
97
- ref.__exec.param2 = () => 'new value 2'
98
- element.param1 = 'old value 1'
99
- element.param2 = 'old value 2'
100
-
101
- const changes = await throughUpdatedExec(element)
102
-
103
- console.log(element)
104
- expect(element.param1).toBe('new value 1')
105
- expect(element.param2).toBe('new value 2')
106
- expect(changes).toEqual({
107
- param1: 'old value 1',
108
- param2: 'old value 2'
109
- })
110
- })
111
- })
@@ -1,15 +0,0 @@
1
- import { ROOT, TREE } from '../tree'
2
-
3
- describe('ROOT/TREE initialization and report()', () => {
4
- // Test 1: ROOT object structure
5
- it('initializes ROOT with :root key and document.body node', () => {
6
- expect(ROOT.key).toBe(':root')
7
- expect(ROOT.node).toBe(document.body) // Works in JSDOM
8
- })
9
-
10
- // Test 2: TREE === ROOT
11
- it('assigns TREE to reference ROOT', () => {
12
- expect(TREE).toBe(ROOT)
13
- expect(TREE.node).toBe(document.body)
14
- })
15
- })
@@ -1,256 +0,0 @@
1
- import { update } from '../update'
2
-
3
- describe('update()', () => {
4
- let element, params, opts
5
-
6
- beforeEach(() => {
7
- element = {
8
- __ref: {
9
- __if: true,
10
- __execProps: {},
11
- __exec: {},
12
- __defineCache: {},
13
- __propsStack: [],
14
- __props: [],
15
- __state: 'state'
16
- },
17
- state: 'string',
18
- props: {},
19
- parent: {
20
- props: {}
21
- },
22
- context: {},
23
- define: {},
24
- node: document.createElement('div'),
25
- key: 'testElement',
26
- on: {},
27
- update
28
- }
29
- opts = {
30
- preventUpdate: [],
31
- preventDefineUpdate: [],
32
- preventBeforeStateUpdateListener: false,
33
- preventListeners: false,
34
- preventStateUpdateListener: false
35
- }
36
- params = {}
37
- })
38
-
39
- it('does not modify opts when params and opts are empty', () => {
40
- element.update({}, opts)
41
- expect(opts).toEqual({
42
- calleeElement: false,
43
- cleanExec: true,
44
- currentSnapshot: false,
45
- exclude: [],
46
- preventRecursive: false,
47
- stackChanges: false,
48
- preventUpdate: [],
49
- preventDefineUpdate: [],
50
- preventBeforeStateUpdateListener: false,
51
- preventListeners: false,
52
- preventStateUpdateListener: false
53
- })
54
- })
55
-
56
- it('initializes options with UPDATE_DEFAULT_OPTIONS when opts is empty', () => {
57
- element.update({}, opts)
58
- expect(opts.calleeElement).toBe(false) // Ensure opts is not mutated
59
- expect(element.__ref).toBeDefined() // Ensure __ref is initialized
60
- })
61
-
62
- it('merges opts with UPDATE_DEFAULT_OPTIONS using deepMerge', () => {
63
- opts.customOption = true
64
- element.update({}, opts)
65
- expect(opts.customOption).toBe(true) // Ensure custom options are preserved
66
- })
67
-
68
- it('converts string params to { text: params }', () => {
69
- element.update('testString', opts)
70
- expect(element.text).toBe('testString')
71
- })
72
-
73
- it('converts number params to { text: params }', () => {
74
- element.update(123, opts)
75
- expect(element.text).toBe(123)
76
- })
77
-
78
- it('returns early if preventInheritAtCurrentState matches element', () => {
79
- opts.preventInheritAtCurrentState = { __element: element }
80
- element.update({}, opts)
81
- expect(element.__ref.__currentSnapshot).toBe(6) // No snapshot update
82
- })
83
-
84
- it('initializes __ref if not present', () => {
85
- delete element.__ref
86
- element.update({}, opts)
87
- expect(element.__ref).toBeDefined()
88
- })
89
-
90
- it('merges options with UPDATE_DEFAULT_OPTIONS when exclude is missing', () => {
91
- element.update({}, opts)
92
- expect(opts.exclude).toBeDefined() // Ensure exclude is added
93
- })
94
-
95
- it('does not throw or modify opts when params is undefined', () => {
96
- element.update(undefined, opts)
97
- expect(opts).toEqual({
98
- calleeElement: false,
99
- cleanExec: true,
100
- currentSnapshot: false,
101
- exclude: [],
102
- preventRecursive: false,
103
- preventUpdate: [],
104
- preventDefineUpdate: [],
105
- stackChanges: false,
106
- preventBeforeStateUpdateListener: false,
107
- preventListeners: false,
108
- preventStateUpdateListener: false
109
- })
110
- })
111
-
112
- it('does not throw when opts is undefined', () => {
113
- element.update({}, undefined)
114
- expect(element.__ref).toBeDefined() // Ensure __ref is initialized
115
- })
116
-
117
- it('does not throw when opts is null', () => {
118
- element.update({}, null)
119
- expect(element.__ref).toBeDefined() // Ensure __ref is initialized
120
- })
121
-
122
- it('does not modify the params object', () => {
123
- params = { key: 'value' }
124
- element.update(params, opts)
125
- expect(params).toEqual({ key: 'value' })
126
- })
127
-
128
- it('does modify opts when params is an empty object', () => {
129
- element.update({}, opts)
130
- expect(opts).toEqual({
131
- calleeElement: false,
132
- cleanExec: true,
133
- currentSnapshot: false,
134
- exclude: [],
135
- preventRecursive: false,
136
- stackChanges: false,
137
- preventUpdate: [],
138
- preventDefineUpdate: [],
139
- preventBeforeStateUpdateListener: false,
140
- preventListeners: false,
141
- preventStateUpdateListener: false
142
- })
143
- })
144
-
145
- it('moves regular properties to element.props', () => {
146
- params = { props: { title: 'Test', description: 'Content' } }
147
- element.update(params, opts)
148
- expect(element.props).toEqual({
149
- title: 'Test',
150
- description: 'Content'
151
- })
152
- expect(element.title).toBeUndefined()
153
- })
154
-
155
- it('keeps element-rooted properties', () => {
156
- params = { Header: {}, Footer: {}, 0: 'index' }
157
- element.update(params, opts)
158
- expect(element.Header).toBeDefined()
159
- expect(element.Footer).toBeDefined()
160
- expect(element['0']).toBe('index')
161
- expect(element.props).toEqual({})
162
- })
163
-
164
- it('preserves built-in properties on element', () => {
165
- params = { props: { className: 'container', hidden: true } }
166
- element.update(params, opts)
167
- expect(element.props.className).toBe('container')
168
- expect(element.props.hidden).toBe(true)
169
- expect(element.props).toEqual({ className: 'container', hidden: true })
170
- })
171
-
172
- it('moves element-like properties from props to root', () => {
173
- params = { props: { Header: {} } }
174
- element.update(params, opts)
175
- expect(element.Header).toBeDefined()
176
- expect(element.props.Header).toBeUndefined()
177
- })
178
-
179
- it('exits early when inheritStateUpdates returns false', () => {
180
- // Simulate inheritStateUpdates failure
181
- element.__ref.__stateBlocked = true
182
- element.update({ props: { shouldChange: true } }, opts)
183
-
184
- expect(element.props.shouldChange).toBe(true)
185
- expect(element.__ref.__stateBlocked).toBe(true) // State remains blocked
186
- })
187
-
188
- it('exits early when checkIfOnUpdate fails', () => {
189
- // Force checkIfOnUpdate failure
190
- element.parent.props.ifCondition = false
191
- element.update({ state: { newState: true } }, opts)
192
-
193
- expect(element.state.newState).toBe(true)
194
- })
195
-
196
- it('updates props from parent key match', () => {
197
- element.parent.props.testKey = { inherited: true }
198
- element.update({}, opts)
199
- expect(element.props.inherited).toBeUndefined()
200
- })
201
-
202
- it('updates props when functions exist in __props', () => {
203
- element.__ref.__props.push(() => 'dynamic')
204
- element.update({}, opts)
205
- expect(element.props).toEqual(expect.any(Object)) // Props were processed
206
- })
207
-
208
- it('skips props update when preventPropsUpdate=true', () => {
209
- opts.preventPropsUpdate = true
210
- opts.preventUpdateAfter = true
211
- element.parent.props.testKey = { shouldExist: true }
212
- element.update({}, opts)
213
- expect(element.props.shouldExist).toBeUndefined()
214
- })
215
-
216
- it('should not skips props update when preventPropsUpdate=false', () => {
217
- opts.preventPropsUpdate = false
218
- opts.lazyLoad = true
219
- opts.onEachUpdate = () => {
220
- return true
221
- }
222
- element.parent.props.testKey = { shouldExist: true }
223
- element.__ref.__propsStack = []
224
- element.__ref.__if = true
225
- element.off = { text: 'off' }
226
- element.update({}, opts)
227
- expect(element.props.shouldExist).toBeUndefined()
228
- })
229
-
230
- it('should set preventUpdateAfterCount to 1 when is not a number', () => {
231
- opts.preventPropsUpdate = true
232
- opts.preventUpdateAfter = 2
233
- opts.preventUpdateAfterCount = undefined
234
- element.parent.props.testKey = { shouldExist: true }
235
- element.update({}, opts)
236
- expect(element.props.shouldExist).toBeUndefined()
237
- })
238
-
239
- it('processes parent.childProps', () => {
240
- element.parent.props.childProps = { global: true }
241
- element.update({}, opts)
242
- expect(element.props.global).toBe(true)
243
- })
244
-
245
- it('processes function props', () => {
246
- element.update({ props: { calc: () => 42 } }, opts)
247
- expect(element.props.calc()).toBe(42)
248
- })
249
-
250
- it('returns element when beforeUpdate rejects', () => {
251
- // Simulate beforeUpdate rejection
252
- element.on.beforeUpdate = () => false
253
- const result = element.update({}, opts)
254
- expect(result).toBe(element)
255
- })
256
- })
@@ -1,4 +0,0 @@
1
- {
2
- "type": "commonjs",
3
- "main": "index.js"
4
- }
package/methods/set.js DELETED
@@ -1,63 +0,0 @@
1
- 'use strict'
2
-
3
- import { merge, overwrite } from '@domql/utils'
4
-
5
- import { set, reset, updateContent, removeContent } from '../set.js'
6
- import { update } from '../update.js'
7
-
8
- import {
9
- call,
10
- error,
11
- getPath,
12
- getRef,
13
- keys,
14
- log,
15
- lookdown,
16
- lookdownAll,
17
- lookup,
18
- nextElement,
19
- parse,
20
- parseDeep,
21
- previousElement,
22
- remove,
23
- setNodeStyles,
24
- setProps,
25
- spotByPath,
26
- variables,
27
- verbose,
28
- warn
29
- } from '@domql/utils/methods'
30
-
31
- export const addMethods = (element, parent, options = {}) => {
32
- const proto = {
33
- set,
34
- reset,
35
- update,
36
- variables,
37
- remove,
38
- updateContent,
39
- removeContent,
40
- setProps,
41
- lookup,
42
- lookdown,
43
- lookdownAll,
44
- getRef,
45
- getPath,
46
- setNodeStyles,
47
- spotByPath,
48
- parse,
49
- parseDeep,
50
- keys,
51
- nextElement,
52
- previousElement,
53
- log,
54
- verbose,
55
- warn,
56
- error,
57
- call
58
- }
59
- if (element.context.methods) {
60
- ;(options.strict ? merge : overwrite)(proto, element.context.methods)
61
- }
62
- Object.setPrototypeOf(element, proto)
63
- }
package/methods/v2.js DELETED
@@ -1,83 +0,0 @@
1
- 'use strict'
2
-
3
- import { isDefined, isFunction, isObjectLike } from '@domql/utils'
4
-
5
- export const defineSetter = (element, key, get, set) =>
6
- Object.defineProperty(element, key, { get, set })
7
-
8
- export const keys = function () {
9
- const element = this
10
- const keys = []
11
- for (const param in element) {
12
- // if (REGISTRY[param] && !parseFilters.elementKeys.includes(param)) { continue }
13
- keys.push(param)
14
- }
15
- return keys
16
- }
17
-
18
- export const parse = function (excl = []) {
19
- const element = this
20
- const obj = {}
21
- const keyList = keys.call(element)
22
- keyList.forEach(v => {
23
- if (excl.includes(v)) return
24
- let val = element[v]
25
- if (v === 'state') {
26
- if (element.__ref && element.__ref.__hasRootState) return
27
- if (isFunction(val && val.parse)) val = val.parse()
28
- } else if (v === 'props') {
29
- const { __element, update, ...props } = element[v]
30
- obj[v] = props
31
- } else if (isDefined(val)) obj[v] = val
32
- })
33
- return obj
34
- }
35
-
36
- export const parseDeep = function (excl = []) {
37
- const element = this
38
- const obj = parse.call(element, excl)
39
- for (const v in obj) {
40
- if (excl.includes(v)) return
41
- if (isObjectLike(obj[v])) {
42
- obj[v] = parseDeep.call(obj[v], excl)
43
- }
44
- }
45
- return obj
46
- }
47
-
48
- export const log = function (...args) {
49
- const element = this
50
- const { __ref } = element
51
- console.group(element.key)
52
- if (args.length) {
53
- args.forEach(v => console.log(`%c${v}:\n`, 'font-weight: bold', element[v]))
54
- } else {
55
- console.log(__ref?.path)
56
- const keys = element.keys()
57
- keys.forEach(v => console.log(`%c${v}:\n`, 'font-weight: bold', element[v]))
58
- }
59
- console.groupEnd(element.key)
60
- return element
61
- }
62
-
63
- export const nextElement = function () {
64
- const element = this
65
- const { key, parent } = element
66
- const { __children } = parent.__ref
67
-
68
- const currentIndex = __children.indexOf(key)
69
- const nextChild = __children[currentIndex + 1]
70
-
71
- return parent[nextChild]
72
- }
73
-
74
- export const previousElement = function (el) {
75
- const element = el || this
76
- const { key, parent } = element
77
- const { __children } = parent.__ref
78
-
79
- if (!__children) return
80
-
81
- const currentIndex = __children.indexOf(key)
82
- return parent[__children[currentIndex - 1]]
83
- }
package/mixins/attr.js DELETED
@@ -1,32 +0,0 @@
1
- 'use strict'
2
-
3
- import { deepMerge, exec, isNot, isNull, isUndefined } from '@domql/utils'
4
- import { report } from '@domql/report'
5
-
6
- /**
7
- * Recursively add attributes to a DOM node
8
- */
9
- export function attr (params, element, node) {
10
- const { __ref: ref, props } = element
11
- const { __attr } = ref
12
- if (isNot('object')) report('HTMLInvalidAttr', params)
13
- if (params) {
14
- const attrs = exec(params, element)
15
- if (props.attr) deepMerge(attrs, props.attr)
16
- for (const attr in attrs) {
17
- const val = exec(attrs[attr], element)
18
- // if (__attr[attr] === val) return
19
- if (
20
- val !== false &&
21
- !isUndefined(val) &&
22
- !isNull(val) &&
23
- node.setAttribute
24
- ) {
25
- node.setAttribute(attr, val)
26
- } else if (node.removeAttribute) node.removeAttribute(attr)
27
- __attr[attr] = val
28
- }
29
- }
30
- }
31
-
32
- export default attr
@@ -1,54 +0,0 @@
1
- 'use strict'
2
-
3
- import { exec, isObject, isString } from '@domql/utils'
4
-
5
- export const assignKeyAsClassname = element => {
6
- const { key } = element
7
- if (element.classlist === true) element.classlist = key
8
- else if (
9
- !element.classlist &&
10
- typeof key === 'string' &&
11
- key.charAt(0) === '_' &&
12
- key.charAt(1) !== '_'
13
- ) {
14
- element.classlist = key.slice(1)
15
- }
16
- }
17
-
18
- // stringifies class object
19
- export const classify = (obj, element) => {
20
- let className = ''
21
- for (const item in obj) {
22
- const param = obj[item]
23
- if (typeof param === 'boolean' && param) className += ` ${item}`
24
- else if (typeof param === 'string') className += ` ${param}`
25
- else if (typeof param === 'function') {
26
- className += ` ${exec(param, element)}`
27
- }
28
- }
29
- return className
30
- }
31
-
32
- export const classList = (params, element) => {
33
- if (!params) return
34
- const { key } = element
35
- if (params === true) params = element.classlist = { key }
36
- if (isString(params)) params = element.classlist = { default: params }
37
- if (isObject(params)) params = classify(params, element)
38
- // TODO: fails on string
39
- const className = params.replace(/\s+/g, ' ').trim()
40
- return className
41
- }
42
-
43
- // LEGACY (still needed in old domql)
44
- export const applyClassListOnNode = (params, element, node) => {
45
- const className = classList(params, element)
46
- node.classList = className
47
- return className
48
- }
49
-
50
- export function applyClasslist (params, element, node) {
51
- applyClassListOnNode(params, element, node)
52
- }
53
-
54
- export default applyClasslist
package/mixins/content.js DELETED
@@ -1,65 +0,0 @@
1
- 'use strict'
2
-
3
- import { isFunction, setContentKey } from '@domql/utils'
4
- import { set } from '../set.js'
5
-
6
- export const updateContent = function (params, options) {
7
- const element = this
8
- const ref = element.__ref
9
-
10
- const contentKey = ref.contentElementKey
11
-
12
- if (!element[contentKey]) return
13
- if (element[contentKey].update) element[contentKey].update(params, options)
14
- }
15
-
16
- export const removeContent = function (el, opts = {}) {
17
- const element = el || this
18
- const { __ref: ref } = element
19
- const contentElementKey = setContentKey(element, opts)
20
-
21
- if (opts.contentElementKey !== 'content') opts.contentElementKey = 'content'
22
- if (element[contentElementKey]) {
23
- if (element[contentElementKey].node && element.node) {
24
- if (element[contentElementKey].tag === 'fragment')
25
- element.node.innerHTML = ''
26
- else {
27
- const contentNode = element[contentElementKey].node
28
- if (contentNode.parentNode === element.node)
29
- element.node.removeChild(element[contentElementKey].node)
30
- }
31
- }
32
-
33
- const { __cached } = ref
34
- if (__cached && __cached[contentElementKey]) {
35
- if (__cached[contentElementKey].tag === 'fragment')
36
- __cached[contentElementKey].parent.node.innerHTML = ''
37
- else if (
38
- __cached[contentElementKey] &&
39
- isFunction(__cached[contentElementKey].remove)
40
- )
41
- __cached[contentElementKey].remove()
42
- }
43
-
44
- ref.__children.splice(ref.__children.indexOf(element[contentElementKey]), 1)
45
-
46
- delete element[contentElementKey]
47
- }
48
- }
49
-
50
- /**
51
- * Appends anything as content
52
- * an original one as a child
53
- */
54
- export function setContent (param, element, node, opts) {
55
- const contentElementKey = setContentKey(element, opts)
56
- if (param && element) {
57
- if (element[contentElementKey]?.update) {
58
- element[contentElementKey].update({}, opts)
59
- } else {
60
- set.call(element, param, opts)
61
- }
62
- }
63
- }
64
-
65
- export default setContent