提交 5518728d 编写于 作者: fxy060608's avatar fxy060608

feat(v3): v-model

上级 62fb0d52
......@@ -8598,19 +8598,8 @@ var serviceContext = (function () {
}
});
function optimize (k, v) {
if (
k === V_IF ||
k === V_ELSE_IF ||
k === V_SHOW
) {
return v ? 1 : 0
}
return v
}
function publishHandler (eventType, args, pageIds) {
args = JSON.stringify(args, optimize);
args = JSON.stringify(args);
if (process.env.NODE_ENV !== 'production') {
console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds);
}
......@@ -9158,17 +9147,17 @@ var serviceContext = (function () {
return result
}
function initData (Vue) {
function initData(Vue) {
Vue.prototype._$s = setData;
Vue.prototype._$i = setIfData;
Vue.prototype._$f = setForData;
Vue.prototype._$e = setElseIfData;
Vue.prototype._$setData = function setData (type, data) {
Vue.prototype._$setData = function setData(type, data) {
this._$vd.push(type, this._$id, data);
};
Vue.prototype._$mounted = function mounted () {
Vue.prototype._$mounted = function mounted() {
if (!this._$vd) {
return
}
......@@ -9181,7 +9170,7 @@ var serviceContext = (function () {
}
};
Vue.prototype._$updated = function updated () {
Vue.prototype._$updated = function updated() {
if (!this._$vd) {
return
}
......@@ -9199,13 +9188,13 @@ var serviceContext = (function () {
};
Object.defineProperty(Vue.prototype, '_$vd', {
get () {
get() {
return this.$root._$vdomSync
}
});
Vue.mixin({
beforeCreate () {
beforeCreate() {
if (this.$options.mpType) {
this.mpType = this.$options.mpType;
}
......@@ -9225,7 +9214,7 @@ var serviceContext = (function () {
this._$newData = Object.create(null);
}
},
beforeUpdate () {
beforeUpdate() {
if (!this._$vd) {
return
}
......@@ -9234,7 +9223,7 @@ var serviceContext = (function () {
console.log(`[${this._$id}] beforeUpdate ` + Date.now());
this._$newData = Object.create(null);
},
beforeDestroy () {
beforeDestroy() {
if (!this._$vd) {
return
}
......@@ -9244,7 +9233,7 @@ var serviceContext = (function () {
});
}
function setData (id, name, value) {
function setData(id, name, value) {
switch (name) {
case B_CLASS:
value = this._$stringifyClass(value);
......@@ -9252,9 +9241,10 @@ var serviceContext = (function () {
case B_STYLE:
value = this._$normalizeStyleBinding(value);
break
case V_IF:
case V_IF:
case V_SHOW:
case V_ELSE_IF:
value = !!value;
value = value ? 1 : 0;
break
case V_FOR:
return setForData.call(this, id, value)
......@@ -9263,7 +9253,7 @@ var serviceContext = (function () {
return ((this._$newData[id] || (this._$newData[id] = {}))[name] = value)
}
function setForData (id, value) {
function setForData(id, value) {
const diffData = this._$newData[id] || (this._$newData[id] = {});
const vForData = diffData[V_FOR] || (diffData[V_FOR] = []);
......@@ -9284,11 +9274,11 @@ var serviceContext = (function () {
return key
}
function setIfData (id, value) {
function setIfData(id, value) {
return ((this._$newData[id] || (this._$newData[id] = {}))[V_IF] = !!value)
}
function setElseIfData (id, value) {
function setElseIfData(id, value) {
return ((this._$newData[id] || (this._$newData[id] = {}))[V_ELSE_IF] = !!value)
}
......
......@@ -141,21 +141,21 @@ describe('codegen', () => {
it('generate v-model directive', () => {
assertCodegen(
'<input v-model="test">',
`with(this){return _c('c-i',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',test)),callback:function ($$v) {},expression:"_$s(0,'v-model',test)"}})}`
`with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(test),expression:"test"}],attrs:{"_i":0},domProps:{"value":_$s(0,'v-model',(test))},on:{"input":function($event){if($event.target.composing)return;test=$event.target.value}}})}`
)
})
it('generate multiline v-model directive', () => {
assertCodegen(
'<input v-model="\n test \n">',
`with(this){return _c('c-i',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',\n test \n)),callback:function ($$v) {},expression:"_$s(0,'v-model',\\n test \\n)"}})}`
`with(this){return _c('input',{directives:[{name:"model",rawName:"v-model",value:(\n test \n),expression:"\\n test \\n"}],attrs:{"_i":0},domProps:{"value":_$s(0,'v-model',(\n test \n))},on:{"input":function($event){if($event.target.composing)return;\n test \n=$event.target.value}}})}`
)
})
it('generate multiline v-model directive on custom component', () => {
assertCodegen(
'<my-component v-model="\n test \n" />',
`with(this){return _c('my-component',{attrs:{"_i":0},model:{value:(_$s(0,'v-model',\n test \n)),callback:function ($$v) {},expression:"_$s(0,'v-model',\\n test \\n)"}})}`
`with(this){return _c('my-component',{attrs:{"_i":0},model:{value:_$s(0,'v-model',(\n test \n)),callback:function ($$v) {\n test \n=$$v},expression:"\\n test \\n"}})}`
)
})
......@@ -272,7 +272,7 @@ describe('codegen', () => {
// input + value
assertCodegen(
'<input :value="msg">',
`with(this){return _c('c-i',{attrs:{"value":_$s(0,'a-value',msg),"_i":0}})}`
`with(this){return _c('input',{attrs:{"value":_$s(0,'a-value',msg),"_i":0}})}`
)
// non input
assertCodegen(
......@@ -284,59 +284,59 @@ describe('codegen', () => {
it('generate attrs with v-bind directive', () => {
assertCodegen(
'<input :name="field1">',
`with(this){return _c('c-i',{attrs:{"name":_$s(0,'a-name',field1),"_i":0}})}`
`with(this){return _c('input',{attrs:{"name":_$s(0,'a-name',field1),"_i":0}})}`
)
})
it('generate static attrs', () => {
assertCodegen(
'<input name="field1">',
`with(this){return _c('c-i',{})}`
`with(this){return _c('input',{})}`
)
})
it('generate events with v-on directive', () => {
assertCodegen(
'<input @input="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":onInput}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":onInput}})}`
)
})
it('generate events with method call', () => {
assertCodegen(
'<input @input="onInput($event);">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event);}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event);}}})}`
)
// empty arguments
assertCodegen(
'<input @input="onInput();">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput();}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput();}}})}`
)
// without semicolon
assertCodegen(
'<input @input="onInput($event)">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event)}}})}`
)
// multiple args
assertCodegen(
'<input @input="onInput($event, \'abc\', 5);">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 'abc', 5);}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 'abc', 5);}}})}`
)
// expression in args
assertCodegen(
'<input @input="onInput($event, 2+2);">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 2+2);}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return onInput($event, 2+2);}}})}`
)
// tricky symbols in args
assertCodegen(
`<input @input="onInput(');[\\'());');">`,
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput(');[\\'());');}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput(');[\\'());');}}})}`
)
// function name including a `function` part (#9920)
// 2.6.10 暂未修复此 bug
// assertCodegen(
// '<input @input="functionName()">',
// `with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){return functionName()}}})}`
// `with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){return functionName()}}})}`
// )
})
......@@ -344,64 +344,64 @@ describe('codegen', () => {
// normal function
assertCodegen(
'<input @input="onInput1();onInput2()">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput1();onInput2()}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput1();onInput2()}}})}`
)
// function with multiple args
assertCodegen(
'<input @input="onInput1($event, \'text\');onInput2(\'text2\', $event)">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){onInput1($event, 'text');onInput2('text2', $event)}}})}`
)
})
it('generate events with keycode', () => {
assertCodegen(
'<input @input.enter="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return onInput($event)}}})}`
)
// multiple keycodes (delete)
assertCodegen(
'<input @input.delete="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
)
// multiple keycodes (esc)
assertCodegen(
'<input @input.esc="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"esc",27,$event.key,["Esc","Escape"]))return null;return onInput($event)}}})}`
)
// multiple keycodes (space)
assertCodegen(
'<input @input.space="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"space",32,$event.key,[" ","Spacebar"]))return null;return onInput($event)}}})}`
)
// multiple keycodes (chained)
assertCodegen(
'<input @keydown.enter.delete="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter")&&_k($event.keyCode,"delete",[8,46],$event.key,["Backspace","Delete","Del"]))return null;return onInput($event)}}})}`
)
// number keycode
assertCodegen(
'<input @input.13="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&$event.keyCode!==13)return null;return onInput($event)}}})}`
)
// custom keycode
assertCodegen(
'<input @input.custom="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"custom",undefined,$event.key,undefined))return null;return onInput($event)}}})}`
)
})
it('generate events with generic modifiers', () => {
assertCodegen(
'<input @input.stop="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();return onInput($event)}}})}`
)
assertCodegen(
'<input @input.prevent="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.preventDefault();return onInput($event)}}})}`
)
assertCodegen(
'<input @input.self="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
)
})
......@@ -409,81 +409,81 @@ describe('codegen', () => {
it('generate events with generic modifiers and keycode correct order', () => {
assertCodegen(
'<input @keydown.enter.prevent="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.preventDefault();return onInput($event)}}})}`
)
assertCodegen(
'<input @keydown.enter.stop="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"keydown":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;$event.stopPropagation();return onInput($event)}}})}`
)
})
it('generate events with mouse event modifiers', () => {
assertCodegen(
'<input @click.ctrl="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;return onClick($event)}}})}`
)
assertCodegen(
'<input @click.shift="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.shiftKey)return null;return onClick($event)}}})}`
)
assertCodegen(
'<input @click.alt="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.altKey)return null;return onClick($event)}}})}`
)
assertCodegen(
'<input @click.meta="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.metaKey)return null;return onClick($event)}}})}`
)
assertCodegen(
'<input @click.exact="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if($event.ctrlKey||$event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}`
)
assertCodegen(
'<input @click.ctrl.exact="onClick">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"click":function($event){if(!$event.ctrlKey)return null;if($event.shiftKey||$event.altKey||$event.metaKey)return null;return onClick($event)}}})}`
)
})
it('generate events with multiple modifiers', () => {
assertCodegen(
'<input @input.stop.prevent.self="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){$event.stopPropagation();$event.preventDefault();if($event.target !== $event.currentTarget)return null;return onInput($event)}}})}`
)
})
it('generate events with capture modifier', () => {
assertCodegen(
'<input @input.capture="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"!input":function($event){return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"!input":function($event){return onInput($event)}}})}`
)
})
it('generate events with once modifier', () => {
assertCodegen(
'<input @input.once="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"~input":function($event){return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"~input":function($event){return onInput($event)}}})}`
)
})
it('generate events with capture and once modifier', () => {
assertCodegen(
'<input @input.capture.once="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}`
)
})
it('generate events with once and capture modifier', () => {
assertCodegen(
'<input @input.once.capture="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"~!input":function($event){return onInput($event)}}})}`
)
})
it('generate events with inline statement', () => {
assertCodegen(
'<input @input="current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`
)
})
......@@ -491,42 +491,42 @@ describe('codegen', () => {
// normal function
assertCodegen(
'<input @input="function () { current++ }">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function () { current++ }}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function () { current++ }}})}`
)
// normal named function
assertCodegen(
'<input @input="function fn () { current++ }">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function fn () { current++ }}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function fn () { current++ }}})}`
)
// arrow with no args
assertCodegen(
'<input @input="()=>current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":()=>current++}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":()=>current++}})}`
)
// arrow with parens, single arg
assertCodegen(
'<input @input="(e) => current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":(e) => current++}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":(e) => current++}})}`
)
// arrow with parens, multi args
assertCodegen(
'<input @input="(a, b, c) => current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":(a, b, c) => current++}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":(a, b, c) => current++}})}`
)
// arrow with destructuring
assertCodegen(
'<input @input="({ a, b }) => current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":({ a, b }) => current++}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":({ a, b }) => current++}})}`
)
// arrow single arg no parens
assertCodegen(
'<input @input="e=>current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":e=>current++}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":e=>current++}})}`
)
// with modifiers
assertCodegen(
`<input @keyup.enter="e=>current++">`,
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"keyup":function($event){if(!$event.type.indexOf('key')&&_k($event.keyCode,"enter",13,$event.key,"Enter"))return null;return (e=>current++)($event)}}})}`
)
})
......@@ -534,14 +534,14 @@ describe('codegen', () => {
it('should not treat handler with unexpected whitespace as inline statement', () => {
assertCodegen(
'<input @input=" onInput ">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":onInput}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":onInput}})}`
)
})
it('generate unhandled events', () => {
assertCodegen(
'<input @input="current++">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`,
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":function($event){current++}}})}`,
ast => {
ast.events.input = undefined
}
......@@ -551,7 +551,7 @@ describe('codegen', () => {
it('generate multiple event handlers', () => {
assertCodegen(
'<input @input="current++" @input.stop="onInput">',
`with(this){return _c('c-i',{attrs:{"_i":0},on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}`
`with(this){return _c('input',{attrs:{"_i":0},on:{"input":[function($event){current++},function($event){$event.stopPropagation();return onInput($event)}]}})}`
)
})
......
......@@ -2,6 +2,8 @@ const compiler = require('../lib')
const res = compiler.compile(
`
<my-component v-model="\n test \n" />
`, {
resourcePath: '/User/fxy/Documents/test.wxml',
isReservedTag: function (tag) {
......@@ -19,4 +21,4 @@ const res = compiler.compile(
console.log(require('util').inspect(res, {
colors: true,
depth: null
}))
}))
......@@ -32,10 +32,12 @@ function parseBinding (el, genVar) {
el.styleBinding && (el.styleBinding = genVar('s', el.styleBinding))
}
function parseDirs (el, genVar) {
function parseDirs (el, genVar, ignoreDirs = []) {
el.directives && el.directives.forEach(dir => {
dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
if (ignoreDirs.indexOf(dir.name) === -1) {
dir.value && (dir.value = genVar('v-' + dir.name, dir.value))
dir.isDynamicArg && (dir.arg = genVar('v-' + dir.name + '-arg', dir.arg))
}
})
}
......
const {
ID,
hasOwn,
addAttr
addRawAttr
} = require('./util')
module.exports = function preTransformNode (el, options) {
module.exports = function preTransformNode (el, options) {
if (!hasOwn(options, 'nid')) {
options.nid = 0
}
addAttr(el, ID, options.nid++)
addRawAttr(el, ID, options.nid++)
if (el.attrsMap['v-for']) {
el.forId = el.attrsMap[ID]
}
......
......@@ -26,7 +26,7 @@ const {
const parseEvent = require('./parser/event-parser')
const parseBlock = require('./parser/block-parser')
const basePreTransformNode = require('./pre-transform-node')
const preTransformNode = require('./pre-transform-node')
const optimize = require('./optimizer')
......@@ -87,7 +87,7 @@ function transformNode (el, parent, state) {
parseIf(el, createGenVar)
parseBinding(el, genVar)
parseDirs(el, genVar)
parseDirs(el, genVar, ['model'])
if (!isComponent(el.tag)) {
parseAttrs(el, genVar)
......@@ -106,27 +106,30 @@ function postTransformNode (el, options) {
}
}
function parseTag (el) {
if (el.tag === 'input' || el.tag === 'textarea') {
el.tag = `c-${el.tag.substr(0, 1)}` // 返回一个自定义组件标签,保证 v-model
function genVModel (el) {
if (
(el.tag === 'input' || el.tag === 'textarea') &&
el.directives &&
el.directives.find(dir => dir.name === 'model')
) {
const prop = el.props.find(prop => prop.name === 'value')
prop.value = createGenVar(el.attrsMap[ID])('v-model', prop.value)
}
if (el.model) {
el.model.value = createGenVar(el.attrsMap[ID])('v-model', el.model.value)
}
}
function genData (el) {
delete el.$parentIterator3
if (el.model) {
el.model.callback = `function ($$v) {}`
}
genVModel(el)
return ''
}
module.exports = {
preTransformNode: function (el, options) {
parseTag(el)
return basePreTransformNode(el, options)
},
preTransformNode,
postTransformNode,
genData
}
......@@ -28,7 +28,7 @@ function isVar (str) {
return true
}
function addAttr (el, name, value) {
function addRawAttr (el, name, value) {
el.attrsMap[name] = value
el.attrsList.push({
name,
......@@ -42,7 +42,7 @@ function updateEleId (el, it, state) {
}
const id = el.attrsMap[ID]
const newId = Number.isInteger(id) ? `("${id}-"+${it})` : `(${id}+${it})`
addAttr(el, ID, newId)
addRawAttr(el, ID, newId)
const attr = el.attrs.find(attr => attr.name === ID)
attr.value = newId
el.children.forEach(child => {
......@@ -121,7 +121,7 @@ function processForKey (el) {
const keyIndex = forEl.children.indexOf(el)
el.key = `${forEl.forId}+'-${keyIndex}'+${it}`
} else { // 当 template 下只有文本节点
if (el.children && el.children.length && !el.children.find(child => child.key)) {
if (el.children && el.children.length && !el.children.find(child => child.key)) {
el.children[0].parent = el
el.children[0].key = `${forEl.forId}+'-0'+${it}`
return true
......@@ -146,17 +146,63 @@ function traverseNode (el, parent, state) {
el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => traverseNode(slot, el, state))
}
function addAttr (el, name, value, dynamic) {
const attrs = dynamic
? (el.dynamicAttrs || (el.dynamicAttrs = []))
: (el.attrs || (el.attrs = []))
attrs.push({
name,
value,
dynamic
})
el.plain = false
}
function removeRawAttr (el, name) {
delete el.attrsMap[name]
const index = el.attrsList.findIndex(attr => attr.name === name)
index !== -1 && el.attrsList.splice(index, 1)
}
function removeRawBindingAttr (el, name) {
removeRawAttr(el, ':' + name)
removeRawAttr(el, 'v-bind:' + name)
console.log(el)
throw new Error('123')
}
function addHandler (el, name, value, important) {
const events = el.events || (el.events = {})
const handlers = events[name]
const newHandler = {
value: value.trim(),
dynamic: undefined
}
if (Array.isArray(handlers)) {
important ? handlers.unshift(newHandler) : handlers.push(newHandler)
} else if (handlers) {
events[name] = important ? [newHandler, handlers] : [handlers, newHandler]
} else {
events[name] = newHandler
}
el.plain = false
}
module.exports = {
V_FOR,
V_IF,
V_ELSE_IF,
ID,
ID,
SET_DATA,
GET_DATA,
isVar,
hasOwn,
addAttr,
addRawAttr,
removeRawAttr,
removeRawBindingAttr,
getForEl,
addHandler,
processForKey,
updateForEleId,
getBindingAttr,
......
......@@ -183,12 +183,7 @@ const {
} = require('./h5')
function isComponent (tagName) {
if (
tagName === 'block' ||
tagName === 'template' ||
tagName === 'c-i' || // v3 service input => c-i
tagName === 'c-t' // v3 service textarea => c-t
) {
if (tagName === 'block' || tagName === 'template') {
return false
}
return !hasOwn(tags, getTagName(tagName.replace('v-uni-', '')))
......
......@@ -3696,6 +3696,10 @@
return
}
if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio
return
}
var typeBinding;
if (map[':type'] || map['v-bind:type']) {
typeBinding = getBindingAttr(el, 'type');
......
......@@ -3318,6 +3318,10 @@ function preTransformNode (el, options) {
return
}
if(process.env.UNI_PLATFORM !== 'h5'){ // fixed by xxxxxx 非 h5 平台 type 不会是 checkbox,radio
return
}
var typeBinding;
if (map[':type'] || map['v-bind:type']) {
typeBinding = getBindingAttr(el, 'type');
......
......@@ -213,7 +213,7 @@ module.exports = function (pagesJson, userManifestJson) {
let flexDir = false
if (manifestJson.plus.nvueCompiler && manifestJson.plus.nvueCompiler === 'uni-app') {
if (process.env.UNI_USING_NVUE_COMPILER) {
appJson.nvueCompiler = 'uni-app'
flexDir = getFlexDirection(manifestJson.plus)
} else {
......
......@@ -14,7 +14,8 @@ import {
import {
V_IF,
V_FOR,
V_FOR,
V_SHOW,
V_ELSE_IF,
B_CLASS,
B_STYLE
......@@ -110,7 +111,7 @@ export function initData (Vue) {
})
}
function setData (id, name, value) {
function setData (id, name, value) {
switch (name) {
case B_CLASS:
value = this._$stringifyClass(value)
......@@ -118,9 +119,10 @@ function setData (id, name, value) {
case B_STYLE:
value = this._$normalizeStyleBinding(value)
break
case V_IF:
case V_IF:
case V_SHOW:
case V_ELSE_IF:
value = !!value
value = value ? 1 : 0
break
case V_FOR:
return setForData.call(this, id, value)
......
import {
V_IF,
V_ELSE_IF,
V_SHOW
}
from './constants'
function optimize (k, v) {
if (
k === V_IF ||
k === V_ELSE_IF ||
k === V_SHOW
) {
return v ? 1 : 0
}
return v
}
export function publishHandler (eventType, args, pageIds) {
args = JSON.stringify(args, optimize)
args = JSON.stringify(args)
if (process.env.NODE_ENV !== 'production') {
console.log(`UNIAPP[publishHandler]:[${+new Date()}]`, eventType, args, pageIds)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册