solarite 0.1.0 → 0.1.1

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.
@@ -1,17 +1,208 @@
1
1
  // noinspection DuplicatedCode
2
2
 
3
3
  import {camelToDashes, htmlContext} from "../src/solarite/Util.js";
4
- import {watchGet, watchSet} from "../src/solarite/watch.js";
5
- import Testimony, {assert} from './Testimony.js';
6
- Testimony.enableJsDom();
4
+ //import {watchGet, watchSet} from "../src/solarite/watch.js";
7
5
 
8
- import {Solarite, r} from '../src/solarite/Solarite.js';
9
- //import {Red, r, Perf} from '../dist/Solarite.min.js'; // This will help the Benchmark test warm up.
6
+ import {Solarite, r, getArg} from '../src/solarite/Solarite.js';
7
+ //import {Solarite, r, getArg} from '../dist/Solarite.min.js'; // This will help the Benchmark test warm up.
10
8
  import {watch} from "../src/solarite/watch2.js";
9
+ import watch3 from "../src/solarite/watch3.js";
10
+ import NodeGroupManager from "../src/solarite/NodeGroupManager.js";
11
+ import NodeGroup from "../src/solarite/NodeGroup.js";
12
+ import Template from "../src/solarite/Template.js";
13
+ import Shell from "../src/solarite/Shell.js";
14
+
15
+ import Testimony, {assert} from './Testimony.js';
16
+
17
+
18
+ // This function is used by the various tests.
19
+ window.getHtml = (item, includeComments=false) => {
20
+ if (!item)
21
+ return item;
22
+
23
+ if (item.fragment)
24
+ item = item.fragment; // Shell
25
+ if (item instanceof DocumentFragment)
26
+ item = [...item.childNodes]
27
+
28
+ else if (item.getNodes)
29
+ item = item.getNodes()
30
+
31
+ let result;
32
+ if (Array.isArray(item)) {
33
+ if (!includeComments)
34
+ item = item.filter(n => n.nodeType !==8)
35
+
36
+ result = item.map(n => n.nodeType === 8 ? `<!--${n.textContent}-->` : n.outerHTML || n.textContent).join('|')
37
+ }
38
+ else
39
+ result = item.outerHTML || item.textContent
40
+
41
+ if (!includeComments)
42
+ result = result.replace(/(<)!--(.*?)-->/g, '')
43
+
44
+ // Remove whitespace between tags, so we can write simpler tests.
45
+ return result.replace(/^\s+</g, '<').replace(/>\s+</g, '><').replace(/>\s+$/g, '>');
46
+ }
47
+
48
+
49
+ Testimony.test('Solarite.Shell.empty', () => {
50
+ let shell = new Shell(['', ''])
51
+ assert.eq(getHtml(shell, true), '<!--PathStart:0-->|<!--PathEnd:0-->')
52
+ });
53
+
54
+ Testimony.test('Solarite.Shell.paragraph', () => {
55
+ let shell = new Shell(['<p>', '</p>'])
56
+ assert.eq(getHtml(shell, true), '<p><!--PathStart:0--><!--PathEnd:0--></p>')
57
+ });
58
+
59
+ Testimony.test('Solarite.Shell.nodeBefore', () => {
60
+ let shell = new Shell(['a', ''])
61
+ assert.eq(getHtml(shell, true), 'a|<!--PathEnd:0-->')
62
+ });
63
+
64
+ Testimony.test('Solarite.Shell.nodeAfter', () => {
65
+ let shell = new Shell(['', 'b'])
66
+ assert.eq(getHtml(shell, true), '<!--PathStart:0-->|b')
67
+ });
68
+
69
+ Testimony.test('Solarite.Shell.nodeBeforeAfter', () => {
70
+ let shell = new Shell(['a', 'b'])
71
+ assert.eq(getHtml(shell, true), 'a|b')
72
+ });
73
+
74
+ Testimony.test('Solarite.Shell.emptyTwoPaths', () => {
75
+ let shell = new Shell(['', '', ''])
76
+ assert.eq(getHtml(shell, true), '<!--PathStart:0-->|<!--PathEnd:0-->|<!--PathEnd:1-->')
77
+ });
78
+
79
+ Testimony.test('Solarite.Shell.nodeBetweenPaths', () => {
80
+ let shell = new Shell(['', 'a', ''])
81
+ assert.eq(getHtml(shell, true), '<!--PathStart:0-->|a|<!--PathEnd:1-->')
82
+ });
83
+
84
+ Testimony.test('Solarite.Shell.nodesAroundPaths', () => {
85
+ let shell = new Shell(['a', 'b', 'c'])
86
+ assert.eq(getHtml(shell, true), 'a|b|c')
87
+ });
88
+
89
+ Testimony.test('Solarite.Shell.emptyTwoPathsNested', () => {
90
+ let shell = new Shell(['<p>', '', '</p>'])
91
+ assert.eq(getHtml(shell, true), '<p><!--PathStart:0--><!--PathEnd:0--><!--PathEnd:1--></p>')
92
+ });
93
+
94
+ Testimony.test('Solarite.Shell.nodesAroundPathsNested', () => {
95
+ let shell = new Shell(['<p>a', 'b', 'c</p>'])
96
+ assert.eq(getHtml(shell, true), '<p>abc</p>')
97
+ });
11
98
 
12
99
 
100
+ // Testimony.test('Shell.emptySpacer', () => {
101
+ // let expr = 1;
102
+ // let shell = new Shell(['<input ', '', 'onclick=','>'])
103
+ // console.log(getHtml(shell, true))
104
+ // });
105
+
106
+
107
+
108
+ Testimony.test('Solarite.NodeGroup.empty', () => {
109
+ let ngm = new NodeGroupManager(document.body);
110
+
111
+ let ng = new NodeGroup(new Template([``], []), ngm)
112
+ assert.eq(getHtml(ng), '')
113
+
114
+ ng.applyExprs([])
115
+ assert.eq(getHtml(ng), '')
116
+ });
117
+
118
+
119
+ Testimony.test('Solarite.NodeGroup.oneExpr', () => {
120
+ let ngm = new NodeGroupManager(document.body);
121
+
122
+ let ng = new NodeGroup(new Template([``, ``], ['1']), ngm)
123
+ assert.eq(getHtml(ng), '1')
124
+
125
+ ng.applyExprs([2])
126
+ assert.eq(getHtml(ng), '2')
127
+
128
+ ng.applyExprs([[3, 4, 5]])
129
+ assert.eq(getHtml(ng), '3|4|5')
130
+ });
131
+
132
+ Testimony.test('Solarite.NodeGroup.emptyAdjacent', () => {
133
+ let ngm = new NodeGroupManager(document.body);
134
+ let ng = new NodeGroup(new Template([``, ``, ``], ['1', '2']), ngm)
135
+ ng.verify();
136
+
137
+ assert.eq(getHtml(ng), '1|2')
138
+
139
+ ng.applyExprs([3, 4])
140
+ assert.eq(getHtml(ng), '3|4')
141
+
142
+ ng.applyExprs([[1, 2, 3], [4, 5, 6]])
143
+ assert.eq(getHtml(ng), '1|2|3|4|5|6')
144
+ });
145
+
146
+
147
+ Testimony.test('Solarite.NodeGroup.paragraph', () => {
148
+ let ngm = new NodeGroupManager(document.body);
149
+ let ng = new NodeGroup(new Template(['<p>', '</p>'], ['1']), ngm)
150
+ assert.eq(getHtml(ng), '<p>1</p>')
151
+
152
+ ng.applyExprs([2])
153
+ assert.eq(getHtml(ng), '<p>2</p>')
154
+
155
+ ng.applyExprs([[3, 4, 5]])
156
+ assert.eq(getHtml(ng), '<p>345</p>')
157
+ });
158
+
159
+ Testimony.test('Solarite.NodeGroup.node', () => {
160
+ let ngm = new NodeGroupManager(document.body);
161
+ let a = r('<p>a</p>')
162
+ let b = r('<p>b</p>')
163
+ let ng = new NodeGroup(new Template(['<div>', '</div>'], [a]), ngm)
164
+
165
+ assert.eq(getHtml(ng), '<div><p>a</p></div>')
166
+
167
+ ng.applyExprs([b]);
168
+ assert.eq(getHtml(ng), '<div><p>b</p></div>')
169
+
170
+ ng.applyExprs([1]);
171
+ assert.eq(getHtml(ng), '<div>1</div>')
172
+
173
+ ng.applyExprs([a]);
174
+ assert.eq(getHtml(ng), '<div><p>a</p></div>')
175
+ });
176
+
177
+ Testimony.test('Solarite.NodeGroup._nodeSwap', () => {
178
+ let ngm = new NodeGroupManager(document.body);
179
+ let a = r('<p>a</p>')
180
+ let b = r('<p>b</p>')
181
+ let ng = new NodeGroup(new Template(['<div>', '', '</div>'], [a, b]), ngm)
182
+ document.body.append(ng.startNode)
183
+
184
+ assert.eq(getHtml(ng), '<div><p>a</p><p>b</p></div>')
185
+
186
+ ng.applyExprs([b, a]);
187
+ assert.eq(getHtml(ng), '<div><p>b</p><p>a</p></div>');
188
+ });
189
+
13
190
 
14
- Testimony.test('Util.htmlContext', () => {
191
+ Testimony.test('Solarite.NodeGroup.arrayReverse', () => {
192
+ let ngm = new NodeGroupManager(document.body);
193
+ let list = [r('a'), r('b')];
194
+
195
+ let ng = new NodeGroup(new Template(['<div>', '</div>'], [list]), ngm)
196
+ ng.verify();
197
+ assert(getHtml(ng), '<div>ab</div')
198
+
199
+ list.reverse();
200
+ ng.applyExprs([list])
201
+ assert(getHtml(ng), '<div>ba</div')
202
+ });
203
+
204
+
205
+ Testimony.test('Solarite.Util.htmlContext', () => {
15
206
  assert.eq(htmlContext('<div class="test'), htmlContext.Attribute)
16
207
  assert.eq(htmlContext('">hello '), htmlContext.Text);
17
208
  assert.eq(htmlContext('<span data-attr="hi > there"'), htmlContext.Tag);
@@ -24,7 +215,7 @@ Testimony.test('Util.htmlContext', () => {
24
215
  assert.eq(htmlContext('>'), htmlContext.Text);
25
216
  });
26
217
 
27
- Testimony.test('Util.camelToDashes', () => {
218
+ Testimony.test('Solarite.Util.camelToDashes', () => {
28
219
  assert.eq(camelToDashes('ProperName'), 'proper-name');
29
220
  assert.eq(camelToDashes('HTMLElement'), 'html-element');
30
221
  assert.eq(camelToDashes('BigUI'), 'big-ui');
@@ -1623,7 +1814,7 @@ Testimony.test('Solarite.ids', () => {
1623
1814
  class R500 extends Solarite {
1624
1815
  one;
1625
1816
  render() {
1626
- this.html = r`<div data-id="one"></div>`;
1817
+ r(this)`<div data-id="one"></div>`;
1627
1818
  }
1628
1819
  }
1629
1820
 
@@ -1641,8 +1832,6 @@ Testimony.test('Solarite.r._createElement', () => {
1641
1832
  })
1642
1833
 
1643
1834
 
1644
-
1645
-
1646
1835
  Testimony.test('Solarite.r.element', () => {
1647
1836
  let adjective = 'better'
1648
1837
  let button = r()`<button>I'm a <b>${adjective}</b> button</button>`;
@@ -1654,6 +1843,7 @@ Testimony.test('Solarite.r.element', () => {
1654
1843
 
1655
1844
  Testimony.test('Solarite.r.lightweightComponent', () => {
1656
1845
  function betterButton(count=0) {
1846
+ // Pass a function to r() and it becomes the render() function.
1657
1847
  let result = r(
1658
1848
  () => r`<button onclick=${(ev, self)=>{count++; self.render()}}>I'm a ${count}X better button</button>`
1659
1849
  );
@@ -1672,23 +1862,29 @@ Testimony.test('Solarite.r.lightweightComponent', () => {
1672
1862
  })
1673
1863
 
1674
1864
  Testimony.test('Solarite.r.lightweightComponent2', () => {
1675
- function betterButton(count=0) {
1676
- let result = r(
1677
- () => r`<button onclick=${(ev, self)=>{count++; self.render()}}>I'm a ${count}X better button</button>`
1678
- );
1679
- result.inc = () => {
1680
- count++;
1681
- result.render();
1865
+
1866
+ // Provide a function plus an object of other methods.
1867
+ let count = 0;
1868
+ let button = r(
1869
+ () => r`
1870
+ <button onclick=${(ev, self) => {
1871
+ count++;
1872
+ self.render();
1873
+ }}>I've been clicked ${count} times.</button>`,
1874
+ {
1875
+ inc() {
1876
+ count++;
1877
+ this.render();
1878
+ }
1682
1879
  }
1683
- return result;
1684
- }
1880
+ );
1685
1881
 
1686
- let button = betterButton(3);
1687
- assert.eq(getHtml(button), `<button onclick="">I'm a 3X better button</button>`)
1882
+ assert.eq(getHtml(button), `<button onclick="">I've been clicked 0 times.</button>`)
1688
1883
 
1689
1884
  button.inc();
1690
- assert.eq(getHtml(button), `<button onclick="">I'm a 4X better button</button>`)
1691
- })
1885
+ assert.eq(getHtml(button), `<button onclick="">I've been clicked 1 times.</button>`)
1886
+ });
1887
+
1692
1888
 
1693
1889
 
1694
1890
 
@@ -1773,17 +1969,13 @@ Testimony.test('Solarite.component.dynamicAttribs', 'Attribs specified via ${...
1773
1969
  });
1774
1970
 
1775
1971
  Testimony.test('Solarite.component.getArg', 'Attribs specified html when not nested in another Solarite component.', () => {
1776
-
1777
- Solarite.getArgs = function(el) {
1778
- debugger;
1779
- return {};
1780
- }
1781
-
1972
+
1782
1973
  class B514 extends Solarite {
1783
1974
  constructor({name, userid}={}) {
1784
1975
  super();
1785
- this.name = this.getArg('name', name);
1786
- this.userId = this.getArg('userid', userid);
1976
+
1977
+ this.name = getArg(this, 'name', name);
1978
+ this.userId = getArg(this, 'userid', userid);
1787
1979
  this.render();
1788
1980
  }
1789
1981
 
@@ -1796,7 +1988,7 @@ Testimony.test('Solarite.component.getArg', 'Attribs specified html when not nes
1796
1988
 
1797
1989
  let div = document.createElement('div');
1798
1990
  div.innerHTML = `<b-514 name="User" userid="2"></b-514>`
1799
-
1991
+
1800
1992
  assert.eq(div.outerHTML, `<div><b-514 name="User" userid="2"><!--PathStart:0-->User | 2<!--PathEnd:1--></b-514></div>`)
1801
1993
 
1802
1994
  });
@@ -2837,10 +3029,65 @@ Testimony.test('Solarite.watch2._forEachSpliceInsert', () => {
2837
3029
 
2838
3030
 
2839
3031
 
3032
+ Testimony.test('Solarite.watch3.primitive', () => {
2840
3033
 
3034
+ class W100 extends HTMLElement {
3035
+ name = 'Fred';
2841
3036
 
3037
+ constructor() {
3038
+ super();
3039
+ watch3(this, 'name');
3040
+ this.render();
3041
+ }
3042
+
3043
+ render() {
3044
+ r(this)`<w-100>${() => this.name + '!'}</w-100>`;
3045
+ }
3046
+ }
3047
+ customElements.define('w-100', W100);
3048
+
3049
+ let a = new W100();
3050
+ document.body.append(a);
3051
+ assert(a.outerHTML, `<w-100>Fred!</w-100>`);
3052
+
3053
+ a.name = 'Jim';
3054
+ assert(a.outerHTML, `<w-100>Jim!</w-100>`);
3055
+
3056
+ a.remove();
3057
+ });
3058
+
3059
+
3060
+ Testimony.test('Solarite.watch3.nodes', () => {
3061
+
3062
+ class W110 extends HTMLElement {
3063
+ name = 'Fred';
3064
+ name2 = 'White';
2842
3065
 
3066
+ constructor() {
3067
+ super();
3068
+ watch3(this, 'name');
3069
+ this.render();
3070
+ }
3071
+
3072
+ render() {
3073
+ r(this)`<w-110>${() => r`<div>${this.name + '!'}</div>`} ${this.name2}</w-110>`;
3074
+ }
3075
+ }
3076
+ customElements.define('w-110', W110);
2843
3077
 
3078
+ let a = new W110();
3079
+ document.body.append(a);
3080
+ assert(a.outerHTML, `<w-110><div>Fred!</div> White</w-110>`);
3081
+
3082
+ a.name = 'Jim';
3083
+ assert(a.outerHTML, `<w-110><div>Jim!</div> White</w-110>`);
3084
+
3085
+ a.name2 = 'Brown'
3086
+ a.render();
3087
+ assert(a.outerHTML, `<w-110><div>Jim!</div> Brown</w-110>`);
3088
+
3089
+ a.remove();
3090
+ });
2844
3091
 
2845
3092
 
2846
3093
 
@@ -2866,13 +3113,13 @@ Testimony.test('Solarite.full.treeItems', () => {
2866
3113
  }
2867
3114
 
2868
3115
  render() {
2869
- this.html = r`
3116
+ r(this)`
2870
3117
  <tree-item>
2871
3118
  <style>
2872
3119
  :host #childItems { padding-left: 20px }
2873
3120
  </style>
2874
3121
  <div onclick="${this.toggleChildren}">${this.titleText}</div>
2875
- <div id="childItems" hidden="${!this.showChildren}">
3122
+ <div hidden="${!this.showChildren}">
2876
3123
  ${this.childItems}
2877
3124
  </div>
2878
3125
  </tree-item>`;