Skip to content

Commit afd647a

Browse files
Default value for templates added, new grammar for class and alias
1 parent ddf91ad commit afd647a

File tree

4 files changed

+33
-21
lines changed

4 files changed

+33
-21
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eft-parser",
3-
"version": "0.1.5",
3+
"version": "0.2.0",
44
"description": "ef.js template parser",
55
"main": "dist/eft-parser.min.js",
66
"module": "src/eft-parser.js",

src/eft-parser.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ESCAPE from './escape-parser.js'
22

33
const typeSymbols = '>#%@.-+'.split('')
4-
const reserved = 'attached data element methods subscribe unsubscribe update'.split(' ').map(i => `$${i}`)
4+
const reserved = 'attached data element nodes methods subscribe unsubscribe update'.split(' ').map(i => `$${i}`)
55
const fullMustache = /^\{\{.*\}\}$/
66
const mustache = /\{\{.+?\}\}/g
77

@@ -22,11 +22,30 @@ const resolveDepth = (ast, depth) => {
2222
return currentNode
2323
}
2424

25+
const parseTag = (string) => {
26+
const [content, ...alias] = string.split('#')
27+
const [tag, ...classes] = content.split('.')
28+
return {
29+
tag,
30+
alias: alias.join('#'),
31+
class: classes.join(' ')
32+
}
33+
}
34+
35+
const splitDefault = (string) => {
36+
string = string.substr(2, string.length - 4)
37+
const [_path, ..._default] = string.split('=')
38+
const pathArr = _path.trim().split('.')
39+
const defaultVal = ESCAPE(_default.join('=').trim())
40+
if (defaultVal) pathArr.push([defaultVal])
41+
return pathArr
42+
}
43+
2544
const parseNodeProps = (string) => {
2645
const splited = string.split('=')
2746
const name = splited.shift().trim()
28-
let value = splited.join('=').trim()
29-
if (fullMustache.test(value)) return { name, value: value.substr(2, value.length - 4).split('.') }
47+
const value = splited.join('=').trim()
48+
if (fullMustache.test(value)) return { name, value: splitDefault(value) }
3049
return { name, value }
3150
}
3251

@@ -37,9 +56,7 @@ const parseText = (string) => {
3756
const texts = string.split(mustache)
3857
for (let i = 0; i < texts.length; i++) {
3958
if (texts[i]) parts.push(ESCAPE(texts[i]))
40-
if (mustaches[i]) parts.push(mustaches[i].substr(2, mustaches[i].length - 4)
41-
.trim()
42-
.split('.'))
59+
if (mustaches[i]) parts.push(splitDefault(mustaches[i]))
4360
}
4461
} else parts.push(string)
4562
return parts
@@ -76,12 +93,15 @@ const eftParser = (template) => {
7693
minDepth = depth
7794
}
7895
prevType = 'tag'
96+
const info = parseTag(content)
7997
const newNode = [{
80-
tag: content,
98+
tag: info.tag,
8199
attr: {},
82100
prop: {},
83101
event: {}
84102
}]
103+
if (info.class) newNode[0].attr.class = info.class
104+
if (info.alias) newNode[0].alias = info.alias
85105
currentNode.push(newNode)
86106
currentNode = newNode
87107
break

test/ef.min.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/test.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,29 @@ var ast = [
9191
]
9292

9393
var template = 'this is a comment\n' +
94-
'>div\n' +
95-
' #class = {{class}}\n' +
94+
'>div.box.test#root\n' +
9695
' #style = {{attr.style}}\n' +
9796
' #id = testdiv\n' +
9897
' #some-attr = some text\n' +
9998
' #content =\n' +
10099
' %title = {{name}}\n' +
101100
' %anotherProperty = text\n' +
102-
' %contentEditable = {{edit}}\n' +
103-
' .Name: {{name}}&nJob: {{job}}\n' +
101+
' .Name: {{name = some name}}&nJob: {{job = some job}}\n' +
104102
' >br\n' +
105103
' -node1\n' +
106-
' >p\n' +
104+
' >p#info\n' +
107105
' #class = some class name\n' +
108106
' @click = alertNotice\n' +
109107
' /@mousedown = setState\n' +
110108
' >span\n' +
111-
' .Notice: {{notice}}\n' +
109+
' .Notice: {{notice = N/A}}\n' +
112110
' . test\n' +
113111
' -node2\n' +
114112
' +list1'
115113

116114
var data1 = {
117115
$data: {
118-
class: 'box test class',
119-
name: 'Bob',
120116
job: 'Assit Alice',
121-
notice: 'ooooooops'
122117
},
123118
$methods: {
124119
alertNotice(state) {
@@ -145,10 +140,8 @@ state4.$data.text = 'box'
145140

146141
// state.$data.root.text = 'component 1'
147142
state2.$data.root.text = 'component 2'
148-
state3.$data.class = 'box'
149143
state3.$data.name = 'Alice'
150144
state3.$data.job = 'Developer'
151-
state3.$data.notice = 'N/A'
152145
state4.$data.job = 'Assiting Alice'
153146

154147
var data2 = {

0 commit comments

Comments
 (0)