From 342a9f3a039ed3700af0006834bbd48654f8dffd Mon Sep 17 00:00:00 2001 From: Evan You Date: Tue, 25 Sep 2018 12:06:19 -0400 Subject: [PATCH] refactor: use regex instead of startsWith --- packages/core/README.md | 1 - packages/core/src/componentProps.ts | 4 ++-- packages/core/src/utils.ts | 15 +++++---------- packages/core/src/vdom.ts | 4 ++-- packages/renderer-dom/src/patchData.ts | 7 +++++-- packages/renderer-dom/src/teardownVNode.ts | 3 ++- 6 files changed, 16 insertions(+), 18 deletions(-) diff --git a/packages/core/README.md b/packages/core/README.md index d5ff7d87..ba699e90 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -6,7 +6,6 @@ import { createRenderer, h } from '@vue/core' const { render } = createRenderer({ - queueJob, nodeOps, patchData, teardownVNode diff --git a/packages/core/src/componentProps.ts b/packages/core/src/componentProps.ts index bd508b93..375a7155 100644 --- a/packages/core/src/componentProps.ts +++ b/packages/core/src/componentProps.ts @@ -1,4 +1,4 @@ -import { EMPTY_OBJ } from './utils' +import { EMPTY_OBJ, nativeOnRE } from './utils' import { Component, ComponentClass, @@ -100,7 +100,7 @@ export function resolveProps( if ( key === 'class' || key === 'style' || - (isNativeOn = key.startsWith('nativeOn')) || + (isNativeOn = nativeOnRE.test(key)) || (hasDeclaredProps && !options.hasOwnProperty(key)) ) { const newKey = isNativeOn ? 'on' + key.slice(8) : key diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 7b5011de..51eb0ec6 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -2,16 +2,11 @@ export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({}) export const NOOP = () => {} -export const isReservedProp = (key: string): boolean => { - switch (key) { - case 'key': - case 'ref': - case 'slots': - return true - default: - return key.startsWith('nativeOn') - } -} +export const onRE = /^on/ +export const nativeOnRE = /^nativeOn/ + +const reserveRE = /^(?:key|ref|slots)$|^nativeOn/ +export const isReservedProp = (key: string): boolean => reserveRE.test(key) export function normalizeStyle( value: any diff --git a/packages/core/src/vdom.ts b/packages/core/src/vdom.ts index c3481d9d..c5ef61af 100644 --- a/packages/core/src/vdom.ts +++ b/packages/core/src/vdom.ts @@ -5,7 +5,7 @@ import { } from './component' import { VNodeFlags, ChildrenFlags } from './flags' import { createComponentClassFromOptions } from './componentUtils' -import { normalizeClass, normalizeStyle } from './utils' +import { normalizeClass, normalizeStyle, onRE, nativeOnRE } from './utils' // Vue core is platform agnostic, so we are not using Element for "DOM" nodes. export interface RenderNode { @@ -270,7 +270,7 @@ export function cloneVNode(vnode: VNode, extraData?: VNodeData): VNode { clonedData.class = normalizeClass([clonedData.class, extraData.class]) } else if (key === 'style') { clonedData.style = normalizeStyle([clonedData.style, extraData.style]) - } else if (key.startsWith('on')) { + } else if (onRE.test(key) || nativeOnRE.test(key)) { const existing = clonedData[key] clonedData[key] = existing ? [].concat(existing, extraData[key]) diff --git a/packages/renderer-dom/src/patchData.ts b/packages/renderer-dom/src/patchData.ts index 884d5c43..803cfee1 100644 --- a/packages/renderer-dom/src/patchData.ts +++ b/packages/renderer-dom/src/patchData.ts @@ -5,6 +5,9 @@ import { patchAttr } from './modules/attrs' import { patchDOMProp } from './modules/props' import { patchEvent } from './modules/events' +export const onRE = /^on/ +const domPropsRE = /^domProps/ + export function patchData( el: Element, key: string, @@ -24,9 +27,9 @@ export function patchData( patchStyle(el, prevValue, nextValue, nextVNode.data) break default: - if (key.startsWith('on')) { + if (onRE.test(key)) { patchEvent(el, key.toLowerCase().slice(2), prevValue, nextValue) - } else if (key.startsWith('domProps')) { + } else if (domPropsRE.test(key)) { patchDOMProp( el, key[8].toLowerCase() + key.slice(9), diff --git a/packages/renderer-dom/src/teardownVNode.ts b/packages/renderer-dom/src/teardownVNode.ts index e1302c5e..89d2fec6 100644 --- a/packages/renderer-dom/src/teardownVNode.ts +++ b/packages/renderer-dom/src/teardownVNode.ts @@ -1,11 +1,12 @@ import { VNode } from '@vue/core' import { handleDelegatedEvent } from './modules/events' +import { onRE } from './patchData' export function teardownVNode(vnode: VNode) { const { el, data } = vnode if (data != null) { for (const key in data) { - if (key.startsWith('on')) { + if (onRE.test(key)) { handleDelegatedEvent(el, key.toLowerCase().slice(2), null) } }