@lightningjs/blits 0.3.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.
- package/.editorconfig +7 -0
- package/.eslintignore +1 -0
- package/.eslintrc.cjs +60 -0
- package/.vscode/extensions.json +6 -0
- package/.vscode/settings.json +9 -0
- package/LICENSE +202 -0
- package/NOTICE +3 -0
- package/README.md +11 -0
- package/bin/index.cjs +25 -0
- package/boilerplate/.editorconfig +7 -0
- package/boilerplate/.eslintignore +3 -0
- package/boilerplate/.eslintrc.js +38 -0
- package/boilerplate/.vscode/extensions.json +7 -0
- package/boilerplate/.vscode/launch.json +12 -0
- package/boilerplate/.vscode/settings.json +9 -0
- package/boilerplate/index.html +11 -0
- package/boilerplate/package.json +36 -0
- package/boilerplate/public/assets/logo.png +0 -0
- package/boilerplate/src/App.js +12 -0
- package/boilerplate/src/components/Loader.js +23 -0
- package/boilerplate/src/index.js +11 -0
- package/boilerplate/src/pages/Home.js +15 -0
- package/boilerplate/src/renderer.js +3 -0
- package/boilerplate/vite.config.js +23 -0
- package/docs/.nojekyll +0 -0
- package/docs/README.md +30 -0
- package/docs/_sidebar.md +9 -0
- package/docs/component_state.md +21 -0
- package/docs/components.md +27 -0
- package/docs/computed_properties.md +18 -0
- package/docs/getting_started.md +40 -0
- package/docs/index.html +47 -0
- package/docs/lifecycle_events.md +46 -0
- package/docs/props.md +44 -0
- package/docs/template_syntax.md +31 -0
- package/docs/user_input.md +64 -0
- package/docs/watchers.md +38 -0
- package/index.js +26 -0
- package/package.json +44 -0
- package/src/application.js +55 -0
- package/src/component.js +192 -0
- package/src/component.test.js +51 -0
- package/src/components/Circle.js +31 -0
- package/src/components/Image.js +33 -0
- package/src/components/RouterView.js +38 -0
- package/src/components/Sprite.js +56 -0
- package/src/components/Text.js +61 -0
- package/src/element.js +236 -0
- package/src/focus.js +65 -0
- package/src/helpers/sequence.js +26 -0
- package/src/launch.js +61 -0
- package/src/lib/codegenerator/generator.js +276 -0
- package/src/lib/codegenerator/generator.test.js +970 -0
- package/src/lib/colors/colors.js +70 -0
- package/src/lib/colors/colors.test.js +190 -0
- package/src/lib/colors/htmlColors.js +168 -0
- package/src/lib/componentId.js +27 -0
- package/src/lib/componentId.test.js +83 -0
- package/src/lib/eventListeners.js +55 -0
- package/src/lib/hooks.js +29 -0
- package/src/lib/log.js +106 -0
- package/src/lib/reactivity/effect.js +54 -0
- package/src/lib/reactivity/effect.test.js +35 -0
- package/src/lib/reactivity/reactive.js +113 -0
- package/src/lib/reactivity/reactive.test.js +27 -0
- package/src/lib/setup/base.js +218 -0
- package/src/lib/setup/computed.js +43 -0
- package/src/lib/setup/input.js +28 -0
- package/src/lib/setup/methods.js +35 -0
- package/src/lib/setup/props.js +53 -0
- package/src/lib/setup/props.test.js +205 -0
- package/src/lib/setup/routes.js +24 -0
- package/src/lib/setup/state.js +45 -0
- package/src/lib/setup/watch.js +33 -0
- package/src/lib/templateparser/parser.js +103 -0
- package/src/lib/templateparser/parser.test.js +769 -0
- package/src/router.js +67 -0
- package/src/settings.js +34 -0
- package/vite/index.js +1 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { Log } from '../log.js'
|
|
19
|
+
|
|
20
|
+
const baseProp = {
|
|
21
|
+
cast: (v) => v,
|
|
22
|
+
required: false,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export default (component, props = []) => {
|
|
26
|
+
if (props.indexOf('id') === -1) {
|
|
27
|
+
props.push('id')
|
|
28
|
+
}
|
|
29
|
+
component.___propKeys = []
|
|
30
|
+
props.forEach((prop) => {
|
|
31
|
+
prop = { ...baseProp, ...(typeof prop === 'object' ? prop : { key: prop }) }
|
|
32
|
+
component.___propKeys.push(prop.key)
|
|
33
|
+
Object.defineProperty(component.prototype, prop.key, {
|
|
34
|
+
get() {
|
|
35
|
+
const value = prop.cast(
|
|
36
|
+
this.___props && prop.key in this.___props
|
|
37
|
+
? this.___props[prop.key]
|
|
38
|
+
: prop.default || undefined
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
if (prop.required && value === undefined) {
|
|
42
|
+
Log.warn(`${prop.key} is required`)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return value
|
|
46
|
+
},
|
|
47
|
+
set(v) {
|
|
48
|
+
Log.warn(`Warning! Avoid mutating props directly (${prop.key})`)
|
|
49
|
+
this.___props[prop.key] = v
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import test from 'tape'
|
|
19
|
+
import propsFn from './props.js'
|
|
20
|
+
|
|
21
|
+
test('Type props function', (assert) => {
|
|
22
|
+
const expected = 'function'
|
|
23
|
+
const actual = typeof propsFn
|
|
24
|
+
|
|
25
|
+
assert.equal(actual, expected, 'props should be a function')
|
|
26
|
+
assert.end()
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('Create a ___propkeys on component', (assert) => {
|
|
30
|
+
const component = new Function()
|
|
31
|
+
|
|
32
|
+
/* eslint-disable */
|
|
33
|
+
assert.false(component.hasOwnProperty('___propKeys'), 'Component should not have a ___propKeys key')
|
|
34
|
+
|
|
35
|
+
propsFn(component)
|
|
36
|
+
|
|
37
|
+
/* eslint-disable */
|
|
38
|
+
assert.true(component.hasOwnProperty('___propKeys'), 'Component should have a ___propKeys key')
|
|
39
|
+
assert.end()
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('Pass props as an array', (assert) => {
|
|
43
|
+
const component = new Function()
|
|
44
|
+
const props = ['index', 'img', 'url']
|
|
45
|
+
propsFn(component, props)
|
|
46
|
+
|
|
47
|
+
assert.equal(props.length, component.___propKeys.length, 'All passed props should be stored on ___propKeys')
|
|
48
|
+
assert.equal(props.length, props.map(prop => component.___propKeys.indexOf(prop) > -1).filter(prop => prop === true).length, 'All passed props should be stored on ___propKeys')
|
|
49
|
+
|
|
50
|
+
props.forEach((prop) => {
|
|
51
|
+
assert.true(typeof Object.getOwnPropertyDescriptor(component.prototype, prop).get === 'function', `A getter should have been created for property ${prop}`)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
assert.end()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
test('Get value of props', (assert) => {
|
|
59
|
+
const component = new Function()
|
|
60
|
+
|
|
61
|
+
const componentInstance = new component
|
|
62
|
+
componentInstance.___props = {
|
|
63
|
+
index: 1,
|
|
64
|
+
img: 'lorem-ipsum.jpg',
|
|
65
|
+
url: 'http://localhost'
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const componentInstance2 = new component
|
|
69
|
+
componentInstance2.___props = {
|
|
70
|
+
index: 2,
|
|
71
|
+
img: 'bla.jpg',
|
|
72
|
+
url: 'http://127.0.0.1'
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const props = ['index', 'img', 'url']
|
|
76
|
+
propsFn(component, props)
|
|
77
|
+
|
|
78
|
+
props.forEach((prop) => {
|
|
79
|
+
assert.equal(componentInstance[prop], componentInstance.___props[prop], `The correct value of ${prop} should be returned via the getter`)
|
|
80
|
+
assert.equal(componentInstance2[prop], componentInstance2.___props[prop], `The correct value of ${prop} should be returned via the getter`)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
assert.equal(componentInstance['bla'], undefined, `Undefined should be returned if a prop doesn't exist`)
|
|
84
|
+
assert.end()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('Passing props as an object', (assert) => {
|
|
88
|
+
|
|
89
|
+
const component = new Function()
|
|
90
|
+
const props = [{key: 'index', key: 'img', key: 'url'}]
|
|
91
|
+
propsFn(component, props)
|
|
92
|
+
|
|
93
|
+
assert.equal(props.length, component.___propKeys.length, 'All passed props should be stored on ___propKeys')
|
|
94
|
+
assert.equal(props.length, props.map(prop => component.___propKeys.indexOf(prop.key) > -1).filter(prop => prop === true).length, 'All passed props should be stored on ___propKeys')
|
|
95
|
+
|
|
96
|
+
props.forEach((prop) => {
|
|
97
|
+
assert.true(typeof Object.getOwnPropertyDescriptor(component.prototype, prop.key).get === 'function', `A getter should have been created for property ${prop}`)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
assert.end()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
test('Passing props as an object mixed with single keys', (assert) => {
|
|
104
|
+
|
|
105
|
+
const component = new Function()
|
|
106
|
+
const props = [{key: 'index'}, 'img', {key: 'url'}]
|
|
107
|
+
propsFn(component, props)
|
|
108
|
+
|
|
109
|
+
assert.equal(props.length, component.___propKeys.length, 'All passed props should be stored on ___propKeys')
|
|
110
|
+
assert.equal(props.length, props.map(prop => component.___propKeys.indexOf(typeof prop === 'object' ? prop.key : prop) > -1).filter(prop => prop === true).length, 'All passed props should be stored on ___propKeys')
|
|
111
|
+
|
|
112
|
+
props.forEach((prop) => {
|
|
113
|
+
assert.true(typeof Object.getOwnPropertyDescriptor(component.prototype, typeof prop === 'object' ? prop.key : prop).get === 'function', `A getter should have been created for property ${prop}`)
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
assert.end()
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
test('Casting props to a type', (assert) => {
|
|
120
|
+
const component = new Function()
|
|
121
|
+
|
|
122
|
+
const componentInstance = new component
|
|
123
|
+
componentInstance.___props = {
|
|
124
|
+
number: '1',
|
|
125
|
+
string: 100,
|
|
126
|
+
boolean: true,
|
|
127
|
+
image: 'my_image.jpg'
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const props = [{
|
|
131
|
+
key: 'number',
|
|
132
|
+
cast: Number
|
|
133
|
+
}, {
|
|
134
|
+
key: 'string',
|
|
135
|
+
cast: String
|
|
136
|
+
}, {
|
|
137
|
+
key: 'boolean',
|
|
138
|
+
cast: Boolean
|
|
139
|
+
},{
|
|
140
|
+
key: 'image',
|
|
141
|
+
cast(v) {
|
|
142
|
+
return `http://localhost/${v}`
|
|
143
|
+
}
|
|
144
|
+
}]
|
|
145
|
+
propsFn(component, props)
|
|
146
|
+
|
|
147
|
+
assert.true(typeof componentInstance.number === 'number', 'Should cast prop value to a Number')
|
|
148
|
+
assert.true(typeof componentInstance.string === 'string', 'Should cast prop value to a String')
|
|
149
|
+
assert.true(typeof componentInstance.boolean === 'boolean', 'Should cast prop value to a Boolean')
|
|
150
|
+
assert.equal(componentInstance.image, 'http://localhost/my_image.jpg','Should cast according to a custom function')
|
|
151
|
+
|
|
152
|
+
assert.end()
|
|
153
|
+
})
|
|
154
|
+
|
|
155
|
+
test('Setting default value for undefined props', (assert) => {
|
|
156
|
+
const component = new Function()
|
|
157
|
+
|
|
158
|
+
const componentInstance = new component
|
|
159
|
+
|
|
160
|
+
const props = [{
|
|
161
|
+
key: 'missing',
|
|
162
|
+
default: 'I am missing'
|
|
163
|
+
}]
|
|
164
|
+
propsFn(component, props)
|
|
165
|
+
|
|
166
|
+
assert.equal(componentInstance.missing, 'I am missing','Should return default prop value when undefined')
|
|
167
|
+
|
|
168
|
+
assert.end()
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
test('Required props with default', (assert) => {
|
|
172
|
+
const component = new Function()
|
|
173
|
+
|
|
174
|
+
const componentInstance = new component
|
|
175
|
+
|
|
176
|
+
const props = [{
|
|
177
|
+
key: 'missing',
|
|
178
|
+
default: 'I am missing',
|
|
179
|
+
required: true
|
|
180
|
+
}]
|
|
181
|
+
propsFn(component, props)
|
|
182
|
+
|
|
183
|
+
assert.equal(componentInstance.missing, 'I am missing', 'Should return default prop value when undefined')
|
|
184
|
+
|
|
185
|
+
assert.end()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test('Required props without default', (assert) => {
|
|
189
|
+
const component = new Function()
|
|
190
|
+
|
|
191
|
+
const componentInstance = new component
|
|
192
|
+
|
|
193
|
+
const props = [{
|
|
194
|
+
key: 'missing',
|
|
195
|
+
required: true
|
|
196
|
+
}]
|
|
197
|
+
propsFn(component, props)
|
|
198
|
+
|
|
199
|
+
assert.equal(componentInstance.missing, undefined, 'Should return undefined prop value when undefined')
|
|
200
|
+
// todo: should log a warning about prop being required
|
|
201
|
+
|
|
202
|
+
assert.end()
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
// todo add test when setting a prop (should work, but should log a warning about avoiding mutating a prop)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export default (component, routes) => {
|
|
19
|
+
component.prototype.___routes = []
|
|
20
|
+
Object.keys(routes).forEach((key) => {
|
|
21
|
+
// todo: validate routes[key] for expected format etc.
|
|
22
|
+
component.prototype.___routes[key] = routes[key]
|
|
23
|
+
})
|
|
24
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { Log } from '../log.js'
|
|
19
|
+
|
|
20
|
+
export default (component, state) => {
|
|
21
|
+
component.___stateKeys = []
|
|
22
|
+
|
|
23
|
+
state = state.apply(component)
|
|
24
|
+
Object.keys(state).forEach((key) => {
|
|
25
|
+
if (component.___propKeys && component.___propKeys.indexOf(key) > -1) {
|
|
26
|
+
Log.error(`State ${key} already exists as a prop`)
|
|
27
|
+
}
|
|
28
|
+
if (component.___methodKeys && component.___methodKeys.indexOf(key) > -1) {
|
|
29
|
+
Log.error(`State ${key} already exists as a method`)
|
|
30
|
+
}
|
|
31
|
+
component.___stateKeys.push(key)
|
|
32
|
+
try {
|
|
33
|
+
Object.defineProperty(component.prototype, key, {
|
|
34
|
+
get() {
|
|
35
|
+
return this.___state && key in this.___state && this.___state[key]
|
|
36
|
+
},
|
|
37
|
+
set(v) {
|
|
38
|
+
if (this.___state) this.___state[key] = v
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
} catch (e) {
|
|
42
|
+
Log.error(e)
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export default (component, watchers) => {
|
|
19
|
+
component.prototype.___watchKeys = []
|
|
20
|
+
component.prototype.___watchers = {}
|
|
21
|
+
|
|
22
|
+
for (let watch in watchers) {
|
|
23
|
+
if (typeof watchers[watch] !== 'function') {
|
|
24
|
+
console.warn(`${watch} is not a function`)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
component.prototype.___watchKeys.push(watch)
|
|
28
|
+
|
|
29
|
+
component.prototype.___watchers[watch] = function (v, old) {
|
|
30
|
+
watchers[watch].call(this, v, old)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 Comcast Cable Communications Management, LLC
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*
|
|
15
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export default (template = '') => {
|
|
19
|
+
let cursor = 0
|
|
20
|
+
let cleaned = false
|
|
21
|
+
|
|
22
|
+
const clean = (template) => {
|
|
23
|
+
// remove all unnecessary new lines and comments
|
|
24
|
+
return template
|
|
25
|
+
.replace(/<!--.*?-->/gms, '') // remove comments
|
|
26
|
+
.replace(/\r?\n\s*\r\n/gm, ' ') // remove empty lines
|
|
27
|
+
.replace(/\r?\n\s*(\S)/gm, ' $1') // remove line endings & spacing
|
|
28
|
+
.replace(/\r?\n/g, '') // remove all line endings
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const parse = () => {
|
|
32
|
+
if (!cleaned) {
|
|
33
|
+
template = clean(template)
|
|
34
|
+
cleaned = true
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const output = { children: [] }
|
|
38
|
+
let attributeStarted = false
|
|
39
|
+
while (template[cursor]) {
|
|
40
|
+
if (template[cursor] === '"') {
|
|
41
|
+
attributeStarted = !attributeStarted
|
|
42
|
+
}
|
|
43
|
+
if (template.charCodeAt(cursor) === '<'.charCodeAt(0) && !attributeStarted) {
|
|
44
|
+
if (template.charCodeAt(cursor + 1) === '/'.charCodeAt(0)) {
|
|
45
|
+
return output
|
|
46
|
+
}
|
|
47
|
+
parseNode(output)
|
|
48
|
+
}
|
|
49
|
+
cursor++
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return output
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const parseNode = (output) => {
|
|
56
|
+
const endTagMatch = template.slice(cursor).match(/(?:[^">]*|"[^"]*")*>/)
|
|
57
|
+
const endPosition = endTagMatch && endTagMatch[0] && endTagMatch[0].length + cursor - 1
|
|
58
|
+
const tag = parseTag(template.substring(cursor + 1, endPosition))
|
|
59
|
+
const node = { ...{ type: tag.type }, ...tag.attributes }
|
|
60
|
+
// self closing tag
|
|
61
|
+
if (template.charCodeAt(endPosition - 1) === '/'.charCodeAt(0)) {
|
|
62
|
+
output.children.push(node)
|
|
63
|
+
} else {
|
|
64
|
+
cursor = endPosition
|
|
65
|
+
const nested = parse()
|
|
66
|
+
if (nested.children.length) {
|
|
67
|
+
node.children = [...nested.children]
|
|
68
|
+
}
|
|
69
|
+
// else {
|
|
70
|
+
// const content = template.substring(endPosition + 1, cursor).trim()
|
|
71
|
+
// if (content) node.slotcontent = content
|
|
72
|
+
// }
|
|
73
|
+
output.children.push(node)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const parseTag = (tag) => {
|
|
78
|
+
let match
|
|
79
|
+
const result = {
|
|
80
|
+
type: (match = tag.match(/^([A-Za-z0-9]+)(?![A-Za-z0-9=])/)) && match[0], // || 'Element' maybe default to 'Element'
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const attributes = tag.match(/[:*\w.-]+="[^"]*"/g) || []
|
|
84
|
+
if (attributes.length) {
|
|
85
|
+
result['attributes'] = attributes.reduce((obj, attr) => {
|
|
86
|
+
const match = /(.+)=["'](.+)["']/.exec(attr)
|
|
87
|
+
if (match) {
|
|
88
|
+
if (match[1].indexOf('.') > -1) {
|
|
89
|
+
const parts = match[1].split('.')
|
|
90
|
+
obj[parts[0]] = `{${parts[1]}: ${match[2]}}`
|
|
91
|
+
} else {
|
|
92
|
+
obj[match[1]] = match[2]
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return obj
|
|
96
|
+
}, {})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return result
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return parse()
|
|
103
|
+
}
|