Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对于原生事件做白名单 #76

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.4.9 (waiting)
* `F` 修复了`onUnload`中直接赋值导致赋值失效的BUG。
* `A` 添加了`wepy-compiler-babel`的sourceMap支持。
* `A` 事件绑定不管是自定义事件还是原生事件都可以直接使用 `@eventName` 的方式,如果说 `eventName` 是小程序已知的事件的话就是通过 `bind` 或者 `catch` 绑定原生事件,否则的话就是走自定义事件绑定。对于小程序升级新增了一些原始事件的话,此时用户可以在 `wepy.config.js` 中增加 `nativeEvents: ['xx']` 即可。


## 1.4.8 (2017-03-16)
Expand Down
117 changes: 106 additions & 11 deletions packages/wepy-cli/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ colors.setTheme({
});


export default {
const utils = {

seqPromise(promises) {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -215,22 +215,115 @@ export default {
* xml replace
*/
attrReplace(content) {
return content.replace(/<[\w-\_]*\s[^>]*>/ig, (tag) => {
const config = utils.getConfig();
const nativeEvents = [
// touch tap
'touchstart',
'touchmove',
'touchend',
'touchcancel',
'tap',
'longtap',

// 表单相关
'change',
'submit',
'reset',
'input',
'focus',
'blur',
'confirm',
'linechange',

// 音频 视频 等
'load',
'error',
'play',
'pause',
'timeupdate',
'ended',

// 地图
'markertap',
'controltap',
'regionchange',

// scroll 容器类
'scrolltoupper',
'scrolltolower',
'scroll'
];
const knownTags = [
// 视图容器
'view',
'scroll-view',
'swiper',
'swiper-item',

// 基础内容
'icon',
'text',
'progress',

// 表单组件
'button',
'checkbox-group',
'checkbox',
'form',
'input',
'label',
'picker',
'picker-view',
'picker-view-column',
'radio-group',
'radio',
'slider',
'switch',
'textarea',

// 导航
'navigator',

// 媒体组件
'audio',
'image',
'video',

// 地图
'map',

// 画布
'canvas',

// 客服会话
'contact-button'
];
const configEvents = config.nativeEvents;
if (configEvents && Array.isArray(configEvents)) {
// 增加可配置的 nativeEvents
// 以防止由于小程序升级 上边的默认事件不完全导致的问题
// 如果说小程序升级的话 在 wepy 没升级的情况下
// 用户可以通过临时加入 nativeEvents 的方法兼容
nativeEvents.push.apply(nativeEvents, configEvents);
}
const configTags = config.knownTags;
if (configTags && Array.isArray(configTags)) {
// 增加可配置的 knownTags
knownTags.push.apply(knownTags, configTags);
}
return content.replace(/<([\w-\_]*)\s[^>]*>/ig, (tag, tagName) => {
tagName = tagName.toLowerCase();
const isKnownTag = knownTags.indexOf(tagName) >= 0;
return tag.replace(/\s+:([\w-_]*)([\.\w]*)\s*=/ig, (attr, name, type) => { // replace :param.sync => v-bind:param.sync
if (type === '.once' || type === '.sync') {
}
else
type = '.once';
return ` v-bind:${name}${type}=`;
}).replace(/\s+\@([\w-_]*)([\.\w]*)\s*=/ig, (attr, name, type) => { // replace @change => v-on:change
let prefix = '';
if (type === '.stop') {
prefix = 'catch';
} else if (type === '.user') {
prefix = 'v-on:'
} else {
prefix = 'bind';
}
// 对于已知 tag 做原生事件名判断
const isNavtiveEvents = type !== '.user' && nativeEvents.indexOf(name) >= 0 && isKnownTag;
const prefix = isNavtiveEvents ? type === '.stop' ? 'catch' : 'bind' : 'v-on:';
return ` ${prefix}${name}=`;
});
});
Expand Down Expand Up @@ -420,4 +513,6 @@ export default {
}
this.log(flag + ': ' + path.relative(this.currentDir, file), type);
}
}
}

export default utils
8 changes: 6 additions & 2 deletions packages/wepy-cli/template/src/pages/index.wpy
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

<text class="testcounter">计数组件1: </text>
<view class="counterview">
<counter1 />
<counter1 @index-emit="counterEmit" />
</view>

<text class="testcounter">计数组件2: </text>
Expand Down Expand Up @@ -195,13 +195,17 @@
}
});
}
},
counterEmit (...args) {
let $event = args[args.length - 1];
console.log(`${this.$name} receive ${$event.name} from ${$event.source.$name}`);
}
};

events = {
'index-emit': (...args) => {
let $event = args[args.length - 1];
console.log(`${this.$name} receive ${$event.name} from ${$event.source.name}`);
console.log(`${this.$name} receive ${$event.name} from ${$event.source.$name}`);
}
};
onLoad() {
Expand Down