jj 0.1.0 → 2.0.0

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 (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -7
  3. package/lib/ComponentFiles.d.ts +35 -0
  4. package/lib/ComponentFiles.js +116 -0
  5. package/lib/ComponentFiles.js.map +1 -0
  6. package/lib/WC.d.ts +33 -0
  7. package/lib/WC.js +160 -0
  8. package/lib/WC.js.map +1 -0
  9. package/lib/WDF.d.ts +11 -0
  10. package/lib/WDF.js +31 -0
  11. package/lib/WDF.js.map +1 -0
  12. package/lib/WE.d.ts +42 -0
  13. package/lib/WE.js +132 -0
  14. package/lib/WE.js.map +1 -0
  15. package/lib/WF.d.ts +14 -0
  16. package/lib/WF.js +44 -0
  17. package/lib/WF.js.map +1 -0
  18. package/lib/WHE.d.ts +21 -0
  19. package/lib/WHE.js +75 -0
  20. package/lib/WHE.js.map +1 -0
  21. package/lib/WN-mixin.d.ts +9 -0
  22. package/lib/WN-mixin.js +59 -0
  23. package/lib/WN-mixin.js.map +1 -0
  24. package/lib/WN.d.ts +34 -0
  25. package/lib/WN.js +145 -0
  26. package/lib/WN.js.map +1 -0
  27. package/lib/WS.d.ts +11 -0
  28. package/lib/WS.js +32 -0
  29. package/lib/WS.js.map +1 -0
  30. package/lib/WSH.d.ts +11 -0
  31. package/lib/WSH.js +29 -0
  32. package/lib/WSH.js.map +1 -0
  33. package/lib/WT.d.ts +12 -0
  34. package/lib/WT.js +39 -0
  35. package/lib/WT.js.map +1 -0
  36. package/lib/Welem.d.ts +49 -0
  37. package/lib/Welem.js +173 -0
  38. package/lib/Welem.js.map +1 -0
  39. package/lib/Wfrag.d.ts +15 -0
  40. package/lib/Wfrag.js +56 -0
  41. package/lib/Wfrag.js.map +1 -0
  42. package/lib/Whelem.d.ts +17 -0
  43. package/lib/Whelem.js +69 -0
  44. package/lib/Whelem.js.map +1 -0
  45. package/lib/Wnode.d.ts +6 -0
  46. package/lib/Wnode.js +33 -0
  47. package/lib/Wnode.js.map +1 -0
  48. package/lib/Wshad.d.ts +9 -0
  49. package/lib/Wshad.js +31 -0
  50. package/lib/Wshad.js.map +1 -0
  51. package/lib/bundle.js +737 -0
  52. package/lib/bundle.js.map +7 -0
  53. package/lib/bundle.min.js +3 -0
  54. package/lib/case.d.ts +3 -0
  55. package/lib/case.js +34 -0
  56. package/lib/case.js.map +1 -0
  57. package/lib/case.test.d.ts +1 -0
  58. package/lib/case.test.js +79 -0
  59. package/lib/case.test.js.map +1 -0
  60. package/lib/h.d.ts +3 -0
  61. package/lib/h.js +9 -0
  62. package/lib/h.js.map +1 -0
  63. package/lib/index.d.ts +11 -0
  64. package/lib/index.js +12 -0
  65. package/lib/index.js.map +1 -0
  66. package/lib/util.d.ts +5 -0
  67. package/lib/util.js +19 -0
  68. package/lib/util.js.map +1 -0
  69. package/lib/wrap-unwrap.d.ts +22 -0
  70. package/lib/wrap-unwrap.js +68 -0
  71. package/lib/wrap-unwrap.js.map +1 -0
  72. package/package.json +56 -26
  73. package/CSS.js +0 -145
  74. package/Router.js +0 -89
  75. package/Selector.js +0 -175
  76. package/Tag.js +0 -827
  77. package/control.js +0 -74
  78. package/events.js +0 -16
  79. package/index.js +0 -26
  80. package/observer.js +0 -27
  81. package/rollup.config.js +0 -25
  82. package/unit.js +0 -78
  83. package/util.js +0 -153
  84. package/win.js +0 -11
package/Tag.js DELETED
@@ -1,827 +0,0 @@
1
- import { mapKeyVal, alias, isArr, arrFrom, isObj, isStr, isComponent, nl, rnd, doc, isFn } from './util.js'
2
- import { control } from './control.js'
3
- import { events } from './events.js'
4
- import { sel, isSel } from './Selector.js'
5
- import { objToRulesArr } from './CSS.js'
6
- import { router } from './Router.js'
7
-
8
- const HTTP_WWW_W3_ORG = 'http://www.w3.org/'
9
- const SVG_NS = HTTP_WWW_W3_ORG + '2000/svg'
10
- const MATH_NS = HTTP_WWW_W3_ORG + '1998/mathml'
11
- const jaTag = Symbol('jaTag')
12
-
13
- const STYLE = 'style'
14
- const HIDDEN = 'hidden'
15
- const MOUSE = 'mouse'
16
-
17
- class Node {
18
- constructor(el) {
19
- if (isObj(el) && Node.validNodeTypes.includes(el.nodeType)) {
20
- this.el = el
21
- } else {
22
- throw new TypeError(`Invalid parameter to Node constructor: ${el}`)
23
- }
24
- }
25
-
26
- mount(rootTagOrEl) {
27
- const root = toTag(rootTagOrEl)
28
- return root.setChild(this)
29
- }
30
-
31
- addTo(targetElement) {
32
- toTag(targetElement).append(this)
33
- return this
34
- }
35
-
36
- addToHead() {
37
- return this.appendTo(document.head)
38
- }
39
-
40
- addToBody() {
41
- return this.appendTo(document.body)
42
- }
43
-
44
- preTo(targetElement) {
45
- toTag(targetElement).prepend(this)
46
- return this
47
- }
48
-
49
- addComment(str) {
50
- this.append(doc.comm(str))
51
- return this.norm()
52
- }
53
-
54
- preComment(str) {
55
- this.prepend(doc.comm(str))
56
- return this.norm()
57
- }
58
-
59
- //#region child
60
- addChildren(...children) {
61
- if (children.length) {
62
- this.el.append(toNativeFrag(...children))
63
- }
64
- return this
65
- }
66
-
67
- preChildren(...children) {
68
- if (children.length) {
69
- this.el.prepend(toNativeFrag(...children))
70
- }
71
- return this
72
- }
73
-
74
- // TODO: Clean data and events
75
- // https://github.com/jquery/jquery/blob/438b1a3e8a52d3e4efd8aba45498477038849c97/src/manipulation.js#L355
76
- // https://github.com/jquery/jquery/blob/438b1a3e8a52d3e4efd8aba45498477038849c97/src/manipulation.js#L265
77
- rmChildren(...children) {
78
- if (children.length) {
79
- children.map(child => toNativeEl(child)).forEach(nativeChild => this.el.removeChild(nativeChild))
80
- } else {
81
- while(this.el.firstChild){
82
- this.el.removeChild(this.el.firstChild);
83
- }
84
- }
85
- return this
86
- }
87
-
88
- setChildren(...children) {
89
- this.empty().addChildren(...children)
90
- return this
91
- }
92
-
93
- getChildren() {
94
- return arrFrom(this.el.childNodes).map(childNode => toTag(childNode))
95
- }
96
-
97
- hasChildren(...children) {
98
- if (children.length === 0) {
99
- return this.el.hasChildren
100
- }
101
- return children.every(child => this.el.contains(toNativeEl(child)))
102
- }
103
- //#endregion child
104
-
105
- //#region text
106
- addText(...strings) {
107
- strings.flat().forEach(str => this.append(doc.text(str)))
108
- return this.norm()
109
- }
110
-
111
- preText(...strings) {
112
- strings.flat().forEach(str => this.prepend(doc.text(str)))
113
- return this.norm()
114
- }
115
-
116
- rmText() {
117
- this.childNodes.filter(c => c instanceof Text).forEach(c => c.remove())
118
- return this
119
- }
120
-
121
- setText(...strings) {
122
- return this.rmText().addText(...strings)
123
- }
124
-
125
- json(obj) {
126
- return this.setText(JSON.stringify(obj, null, 2))
127
- }
128
-
129
- getText() {
130
- return this.childNodes.map(c => c instanceof Text ? c.wholeText : '').join('')
131
- }
132
-
133
- hasText() {
134
- return this.childNodes.some(c => c instanceof Text)
135
- }
136
-
137
- normText() {
138
- this.el.normalize()
139
- return this
140
- }
141
- //#endregion text
142
-
143
- //#region query
144
- queryId(id) {
145
- const result = this.el.getElementById(id)
146
- if (result) {
147
- return toTag(result)
148
- }
149
- }
150
-
151
- query(selector) {
152
- const result = this.el.querySelector(selector)
153
- if (result) {
154
- return toTag(result)
155
- }
156
- }
157
-
158
- queryAll(selector) {
159
- const result = arrFrom(this.el.querySelectorAll(selector))
160
- if (result) {
161
- return result.map(r => toTag(r))
162
- }
163
- }
164
- //#endregion query
165
-
166
- toString() {
167
- return this.el.outerHTML
168
- }
169
- }
170
-
171
- alias(Node, {
172
- addTo: ['appendTo'],
173
- addToHead: 'appendToHead',
174
- addToBody: 'appendToBody',
175
- addComment: 'comment',
176
- preTo: ['prependTo'],
177
- setChildren: ['setChild'],
178
- addChildren: ['append', 'children', 'child', 'addChild'],
179
- preChildren: ['prepend', 'prependChildren', 'prependChild'],
180
- addText: ['text'],
181
- normText: ['norm'],
182
- rmChildren: ['empty', 'clear', 'rmChild'],
183
- toString: 'stringify',
184
- })
185
-
186
- class Frag extends Node {
187
- constructor() {
188
- super(doc.frag())
189
- }
190
-
191
- clone(deep) {
192
- return new Frag(this.el.cloneNode(deep))
193
- }
194
- }
195
-
196
- export function frag(...children) {
197
- const ret = new Frag()
198
- ret.append(children)
199
- return ret
200
- }
201
-
202
- // See: https://developer.mozilla.org/en-US/docs/Web/API/Node
203
- Node.validNodeTypes = [1, 9, 11]
204
-
205
- class Tag extends Node {
206
- constructor(x) {
207
- // TODO if x is a Tag, we're essentially creating two tag objects pointing to the same native element!
208
- super(x instanceof Tag ? x.el : x)
209
- }
210
-
211
- clone(deep) {
212
- return new Tag(this.el.cloneNode(deep))
213
- }
214
-
215
- get parent() {
216
- return toTag(this.el.parentElement)
217
- }
218
-
219
- get ns() {
220
- return this.el.namespaceURI
221
- }
222
-
223
- get classList() {
224
- return this.el.classList
225
- }
226
-
227
- get childNodes() {
228
- return arrFrom(this.el.childNodes)
229
- }
230
-
231
- style(styles) {
232
- if (isObj(styles)) {
233
- mapKeyVal(styles, (k, v) => this.el.style[k] = v)
234
- } else {
235
- this.rmProp(STYLE)
236
- }
237
- return this
238
- }
239
-
240
- //#region class
241
- setClasses(...classNames) {
242
- this.el.className = classNames.join(' ')
243
- }
244
-
245
- getClass() {
246
- return this.el.className
247
- }
248
-
249
- getClasses() {
250
- return arrFrom(this.el.className)
251
- }
252
-
253
- hasClasses(...classNames) {
254
- return classNames.every(className => this.classList.contains(className))
255
- }
256
-
257
- addClasses(...classNames) {
258
- this.classList.add(...classNames)
259
- return this
260
- }
261
-
262
- rmClasses(...classNames) {
263
- this.classList.remove(...classNames)
264
- return this
265
- }
266
-
267
- mvClass(oldClassName, newClassName) {
268
- this.classList.replace(oldClassName, newClassName)
269
- return this
270
- }
271
-
272
- mvClasses(classNamesDic) {
273
- mapKeyVal(classNamesDic, (oldClassName, newClassName) => this.mvClass(oldClassName, newClassName))
274
- return this
275
- }
276
-
277
- togClass(className, force) {
278
- this.classList.toggle(className, force)
279
- return this
280
- }
281
-
282
- togClassObj(classNameDic) {
283
- mapKeyVal(classNameDic, (className, force) => this.togClass(className, force))
284
- return this
285
- }
286
-
287
- togClasses(...classNames) {
288
- classNames.forEach(className => this.togClass(className))
289
- return this
290
- }
291
- //#endregion class
292
-
293
- //#region attr
294
- setAttr(attrName, attrValue) {
295
- if (attrName.toLowerCase() === STYLE) {
296
- return this.style(attrValue)
297
- }
298
- if (attrValue === undefined) {
299
- attrValue = ''
300
- }
301
- this.el.setAttribute(attrName, attrValue)
302
- return this
303
- }
304
-
305
- setAttrs(attrsDic) {
306
- if (!isObj(attrsDic)) {
307
- throw new TypeError(`Expected an object but got ${attrsDic}`)
308
- }
309
- mapKeyVal(attrsDic, (attrName, attrValue) => this.setAttr(attrName, attrValue))
310
- return this
311
- }
312
-
313
- getAttr(attrName) {
314
- return this.el.getAttribute(attrName)
315
- }
316
-
317
- getAttrs(...attrNames) {
318
- return Object.fromEntries(
319
- attrNames.map(attrName => [attrName, this.getAttr(attrName)])
320
- )
321
- }
322
-
323
- hasAttrs(...attrNames) {
324
- return attrNames.every(attrName => this.this.el.hasAttribute(attrName))
325
- }
326
-
327
- rmAttrs(...attrNames) {
328
- attrNames.forEach((attrName) => {
329
- this.el.removeAttribute(attrName)
330
- })
331
- return this
332
- }
333
-
334
- togAttr(attrName, force) {
335
- this.el.toggleAttribute(attrName, force)
336
- return this
337
- }
338
- //#endregion attr
339
-
340
- //#region prop
341
- setProp(propName, propValue) {
342
- if (propName.toLowerCase() === STYLE) {
343
- return this.style(propValue)
344
- }
345
- this.el[propName] = propValue
346
- return this
347
- }
348
-
349
- setPropsObj(propsDic) {
350
- mapKeyVal(propsDic, (propName, propValue) => this.setProp(propName, propValue))
351
- return this
352
- }
353
-
354
- getProp(propName) {
355
- return this.el[propName]
356
- }
357
-
358
- getPropsObj(...propNames) {
359
- return Object.fromEntries(
360
- propNames.map(propName => [propName, this.getProp(propName)])
361
- )
362
- }
363
-
364
- hasProps(...propNames) {
365
- return propNames.every(propName => propName in this.el)
366
- }
367
-
368
- rmProps(...propNames) {
369
- propNames.forEach((propName) => {
370
- try {
371
- delete this.el[propName]
372
- }
373
- catch (e) {
374
- this.el[propName] = undefined
375
- }
376
- })
377
- return this
378
- }
379
-
380
- togProp(propName, force = false) {
381
- if (arguments.length === 2) {
382
- this.setProp(propName, !!force)
383
- } else {
384
- this.setProp(propName, !!this.getProp(propName))
385
- }
386
- return this
387
- }
388
- //#endregion prop
389
-
390
- //#region data
391
- setData(name, value) {
392
- this.el.dataset[name] = value
393
- return this
394
- }
395
-
396
- setDataObj(dataDic) {
397
- mapKeyVal(dataDic, (name, value) => this.setData(name, value))
398
- return this
399
- }
400
-
401
- getData(name) {
402
- return this.el.dataset[name]
403
- }
404
-
405
- getDataObj(...names) {
406
- return Object.fromEntries(names.map(name => [name, this.getData(name)]))
407
- }
408
-
409
- hasData(name) {
410
- return name in this.el.dataset
411
- }
412
-
413
- rmData(name) {
414
- return delete this.el.dataset[name]
415
- }
416
- //#endregion data
417
-
418
- //#region visibility
419
- tog(show) {
420
- if (arguments.length === 1) {
421
- return this.togProp(HIDDEN, !show)
422
- }
423
- return this.togProp(HIDDEN)
424
- }
425
-
426
- hide() {
427
- return this.setProp(HIDDEN, false)
428
- }
429
-
430
- show() {
431
- return this.setProp(HIDDEN, true)
432
- }
433
- //#endregion visibility
434
-
435
- //#region query
436
- closest(selector) {
437
- return this.el.closest(selector) || undefined
438
- }
439
-
440
- matches(selector) {
441
- return this.el.matches(selector)
442
- }
443
- //#endregion query
444
-
445
- //#reguin animation
446
- animFrame(fn, ...extraParams) {
447
- // TODO we should throw if a loop is already running (or merge loops)
448
- const runFn = () => {
449
- if (this.isAttached()) {
450
- this.run(fn, ...extraParams)
451
- }
452
- scheduleCall()
453
- }
454
-
455
- const scheduleCall = () => {
456
- this.animFrameRequestId = window.requestAnimationFrame(runFn)
457
- }
458
-
459
- scheduleCall()
460
- return this
461
- }
462
-
463
- clearAnimFrame() {
464
- window.requestAnimationFrame(this.animFrameRequestId)
465
- return this
466
- }
467
- //#endregion animation
468
-
469
- //#region routing
470
- route(regex, fn) {
471
- router(this).addRoute(regex, fn)
472
- return this
473
- }
474
-
475
- defRoute(fn) {
476
- router(this).addDefRoute(fn)
477
- return this
478
- }
479
- //#endregion routing
480
-
481
- css(cssRef, prefix, styles) {
482
- if (styles === undefined) {
483
- styles = prefix
484
- prefix = undefined
485
- }
486
- if (!(cssRef instanceof StyleTag)) {
487
- throw new Error(`Expected an instance of StyleTag but got ${cssRef}`)
488
- }
489
- const rndClassName = rnd(prefix)
490
- this.addClass(rndClassName)
491
- cssRef.append(
492
- sel().class(rndClassName).css(styles)
493
- )
494
- return this
495
- }
496
-
497
- aria(noun, value) {
498
- return this.setAttr(`aria-${noun}`, value)
499
- }
500
-
501
- wrap(wrapper) {
502
- wrapper = toTag(wrapper)
503
- return wrapper.append(this)
504
- }
505
-
506
- swap(otherTag) {
507
- this.el.replaceWith(otherTag.el)
508
- return otherTag
509
- }
510
-
511
- isAttached() {
512
- return document.documentElement.contains(this.el)
513
- }
514
-
515
- queryId(id) {
516
- const result = this.el.querySelector('#' + id)
517
- if (result) {
518
- return toTag(result)
519
- }
520
- }
521
-
522
- toString() {
523
- return this.el.outerHTML
524
- }
525
- }
526
-
527
- Object.assign(Tag.prototype, control, events)
528
-
529
- const attrShorthands = [
530
- 'alt',
531
- 'aria-role',
532
- 'checked',
533
- 'content',
534
- 'height',
535
- HIDDEN,
536
- 'href',
537
- 'htmlFor',
538
- 'id',
539
- 'is',
540
- 'label',
541
- 'lang',
542
- 'name',
543
- 'placeholder',
544
- 'readonly',
545
- 'rel',
546
- 'required',
547
- 'role',
548
- 'selected',
549
- 'src',
550
- 'title',
551
- 'type',
552
- 'value',
553
- 'width',
554
- ]
555
-
556
- attrShorthands.forEach(attrName => {
557
- Tag.prototype[attrName] = function (attrValue) {
558
- return this.setAttr(attrName, attrValue)
559
- }
560
- })
561
-
562
- const eventShorthands = [
563
- 'attached',
564
- 'blur',
565
- 'change',
566
- 'click',
567
- 'contextmenu',
568
- 'dblclick',
569
- 'detached',
570
- 'error',
571
- 'focus',
572
- 'focusout',
573
- 'hover',
574
- 'keydown',
575
- 'keydpress',
576
- 'keydup',
577
- 'load',
578
- MOUSE + 'down',
579
- MOUSE + 'enter',
580
- MOUSE + 'leave',
581
- MOUSE + 'move',
582
- MOUSE + 'out',
583
- MOUSE + 'over',
584
- MOUSE + 'up',
585
- 'resize',
586
- 'scroll',
587
- 'select',
588
- 'submit',
589
- 'unload',
590
- ]
591
-
592
- eventShorthands.forEach(eventName => {
593
- Tag.prototype[eventName] = function (...handlers) {
594
- this.on(eventName, ...handlers)
595
- return this
596
- }
597
- })
598
-
599
- alias(Tag, {
600
- addClasses: ['addClass', 'class', 'classes'],
601
- class: 'className',
602
- hasClasses: 'hasClass',
603
- rmClasses: 'rmClass',
604
- setClasses: 'setClass',
605
- togClass: 'toggleClass',
606
-
607
- hasAttrs: 'hasAttr',
608
- rmAttrs: 'rmAttr',
609
- setAttr: 'attr',
610
- setAttrs: 'attrs',
611
- togAttr: 'toggleAttr',
612
-
613
- hasProps: 'hasProp',
614
- rmProps: 'rmProp',
615
- setProp: 'prop',
616
- setProps: 'props',
617
-
618
- tog: 'toggle',
619
- swap: ['swapWith', 'replaceWith', 'replace'],
620
-
621
- value: 'val',
622
- htmlFor: 'for',
623
- })
624
-
625
- class StyleTag {
626
- constructor(...rulesetObjArr) {
627
- this.rulesetObjArr = rulesetObjArr
628
- }
629
-
630
- get isAttached() {
631
- return this._root && this._root.isAttached()
632
- }
633
-
634
- get root() {
635
- if (!this._root) {
636
- this._root = html(STYLE)
637
- .type('text/css')
638
- .rel('stylesheet')
639
- .on('attached', () => this.update())
640
- }
641
- return this._root
642
- }
643
-
644
- appendToHead() {
645
- if (!document.head.contains(this.root.el)) {
646
- this.root.appendToHead()
647
- }
648
- return this
649
- }
650
-
651
- append(...rulesetObjArr) {
652
- if (rulesetObjArr.length) {
653
- rulesetObjArr.flat().forEach(r => this.rulesetObjArr.push(r))
654
- if (this.isAttached) {
655
- return this.update()
656
- }
657
- }
658
- return this
659
- }
660
-
661
- get disabled() {
662
- return this.root.el.disabled
663
- }
664
-
665
- disable(isDisabled = true) {
666
- this.root.el.disabled = isDisabled
667
- return this
668
- }
669
-
670
- enable(isEnabled = true) {
671
- return this.disable(!isEnabled)
672
- }
673
-
674
- title(tagTitle) {
675
- this.root.el.title = tagTitle
676
- return this
677
- }
678
-
679
- rm() {
680
- this.root.rm()
681
- this._root = undefined
682
- return this
683
- }
684
-
685
- minified(val) {
686
- this.minified = val
687
- return this
688
- }
689
-
690
- toString(minified = this.minified) {
691
- const indentation = minified ? -1 : 0
692
- const separator = nl(indentation)
693
- return this.rulesetObjArr
694
- .map(rule => objToRulesArr(rule)
695
- .map(rule => rule.toString(indentation))
696
- .join(separator)
697
- )
698
- .join(separator)
699
- }
700
-
701
- update() {
702
- const content = this.toString()
703
- // Only if successful, empty it
704
- this.root.setText(content)
705
- return this
706
- }
707
- }
708
-
709
- export function css(...rulesetObjArr) {
710
- return new StyleTag(...rulesetObjArr)
711
- }
712
-
713
- export function toTag(x) {
714
- if (x instanceof Element) {
715
- return x[jaTag] || new Tag(x)
716
- }
717
- if (x instanceof Tag) {
718
- return x
719
- }
720
- if (isComponent(x)) {
721
- return toTag(x.root)
722
- }
723
- if (isTagObj(x)) {
724
- return objToTag(x)
725
- }
726
- throw new Error(`Invalid Tag or Element instance: ${x}`)
727
- }
728
-
729
- function isTagObj(x) {
730
- return isObj(x) && isStr(x.name)
731
- }
732
-
733
- function objToTag(x) {
734
- const { name, ns } = x
735
- const el = doc.el(name, ns)
736
- const ret = new Tag(el)
737
- mapKeyVal(x, (method, param) => {
738
- if (method !== 'name' && method !== 'ns' && isFn(ret[method])) {
739
- if (isArr(param)) {
740
- ret[method](...param)
741
- } else {
742
- ret[method](param)
743
- }
744
- }
745
- })
746
- return ret
747
- }
748
-
749
- function toNativeFrag(...xArr) {
750
- const ret = doc.frag()
751
- xArr.forEach(x => x !== undefined && ret.appendChild(toNative(x)))
752
- return ret
753
- }
754
-
755
- function toNativeEl(x) {
756
- if (arguments.length === 0 || x === undefined) {
757
- throw new TypeError(`Cannot convert ${x} to native DOM`)
758
- }
759
- if (x instanceof Element || x instanceof Text || x instanceof DocumentFragment || x instanceof Comment) {
760
- return x
761
- }
762
- if (x instanceof Tag || x instanceof Frag) {
763
- return x.el
764
- }
765
- if (isComponent(x)) {
766
- return toNativeEl(x.root)
767
- }
768
- if (isTagObj(x)) {
769
- return objToTag(x).el
770
- }
771
- // Whatever else will convert to a text node
772
- return doc.text(x)
773
- }
774
-
775
- function toNative(x) {
776
- if (isArr(x)) {
777
- return toNativeFrag(...x)
778
- }
779
- return toNativeEl(x)
780
- }
781
-
782
- export function h(tagName, attrs, children) {
783
- return html(tagName).setAttrs(attrs).append(children)
784
- }
785
-
786
- function htmlTag(tagName = 'html') {
787
- return toTag(isStr(tagName) ? doc.el(tagName) : tagName)
788
- }
789
-
790
- export function svgTag(tagName = 'svg') {
791
- return toTag(isStr(tagName) ? doc.el(tagName, SVG_NS) : tagName)
792
- }
793
-
794
- export function mathMlTag(tagName = 'math') {
795
- return toTag(isStr(tagName) ? doc.el(tagName, MATH_NS) : tagName)
796
- }
797
-
798
- const sugarProxy = {
799
- get(target, tagName) {
800
- const tag = target(tagName)
801
- return function proxiedTag(...children) {
802
- return tag.append(...children)
803
- }
804
- }
805
- }
806
-
807
- export const html = new Proxy(htmlTag, sugarProxy)
808
- export const svg = new Proxy(svgTag, sugarProxy)
809
- export const mathMl = new Proxy(mathMlTag, sugarProxy)
810
-
811
- export function queryId(elementId) {
812
- return toTag(document.getElementById(elementId))
813
- }
814
-
815
- export function query(selector, root = document.body) {
816
- if (isSel(selector)) {
817
- selector = selector.selector
818
- }
819
- return toTag(root).query(selector)
820
- }
821
-
822
- export function queryAll(selector, root = document.body) {
823
- if (isSel(selector)) {
824
- selector = selector.selector
825
- }
826
- return toTag(root).queryAll(selector)
827
- }