-
Notifications
You must be signed in to change notification settings - Fork 10
Open
Description
function avalon() {
}
//单例HTML标签,多例自定义标签
var aslice = Array.prototype.slice
avalon.createElement = function (type, props, children) {
var obj = {
type: type,
props: props,
children: aslice.call(arguments, 2)
}
if (obj.children[2] == null) {
if (props && Array.isArray(props.children)) {
obj.children = props.children
}
}
return obj
}
function genCode(array) {
this.nodes = this.genChildren(array)
}
var pp = genCode.prototype
pp.genFn = function () {
console.log(this.nodes)
return new Function('return ' + this.nodes)
}
//如果一个元素没有JSX,那么它
pp.genTag = function (el) {
var children = this.genChildren(el.children, el)
return 'avalon.createElement(' + JSON.stringify(el.type) +
',' + this.genProps(el.props, el) +
',' + children + ')'
}
pp.genChildren = function (children, obj, join) {
if (obj) {
if (obj.isVoidTag||!obj.children.length){
obj.static = true
return 'null'
}
}
obj.static = true
var static = true
var ret = []
for (var i = 0, el; el = children[i++]; ) {
if (el.type === '#js') {
static = false
if (Array.isArray(el.nodeValue)) {
ret[ret.length] = this.genChildren(el.nodeValue, null, ' ')
} else {
ret[ret.length] = el.nodeValue
}
} else if (el.type === '#text') {
ret[ret.length] = JSON.stringify(el.nodeValue)
} else if (el) {
ret[ret.length] = this.genTag(el)
if(!el.static){
static = false
}
}
}
if(static === false)
delete obj.static
return ret.join(join || ',')
}
var staticID = 0
function getStaticID(){
return '$'+(++staticID)
}
pp.genProps = function (props, el) {
if (!props) {
if(el.static)
el.static = true
return '{$staticID:"'+ getStaticID() +'"}'
}
var peg = {index: 0}
var ret = '{'
for (var i in props) {
ret += JSON.stringify(i) + ':' + this.genPropValue(props[i], peg) + ',\n'
}
if(!peg.index && el.static){
el.static = true
ret += '$staticID' + ':' + getStaticID() + ',\n'
} else{
delete el.static
}
return ret.replace(/\,\n$/, '') + '}'
}
pp.genPropValue = function (val, peg) {
if (typeof val === 'string') {
return JSON.stringify(val)
}
if (val) {
++peg.index
if (Array.isArray(val.nodeValue)) {
return this.genChildren(val.nodeValue)
}
if (val) {
return val.nodeValue
}
}
}
//return <div ><span></span></div>
// React.createElement('div', {$staicID: "$3434"})Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels