refactor: use regex instead of startsWith

This commit is contained in:
Evan You 2018-09-25 12:06:19 -04:00
parent 296164c207
commit 342a9f3a03
6 changed files with 16 additions and 18 deletions

View File

@ -6,7 +6,6 @@
import { createRenderer, h } from '@vue/core' import { createRenderer, h } from '@vue/core'
const { render } = createRenderer({ const { render } = createRenderer({
queueJob,
nodeOps, nodeOps,
patchData, patchData,
teardownVNode teardownVNode

View File

@ -1,4 +1,4 @@
import { EMPTY_OBJ } from './utils' import { EMPTY_OBJ, nativeOnRE } from './utils'
import { import {
Component, Component,
ComponentClass, ComponentClass,
@ -100,7 +100,7 @@ export function resolveProps(
if ( if (
key === 'class' || key === 'class' ||
key === 'style' || key === 'style' ||
(isNativeOn = key.startsWith('nativeOn')) || (isNativeOn = nativeOnRE.test(key)) ||
(hasDeclaredProps && !options.hasOwnProperty(key)) (hasDeclaredProps && !options.hasOwnProperty(key))
) { ) {
const newKey = isNativeOn ? 'on' + key.slice(8) : key const newKey = isNativeOn ? 'on' + key.slice(8) : key

View File

@ -2,16 +2,11 @@ export const EMPTY_OBJ: { readonly [key: string]: any } = Object.freeze({})
export const NOOP = () => {} export const NOOP = () => {}
export const isReservedProp = (key: string): boolean => { export const onRE = /^on/
switch (key) { export const nativeOnRE = /^nativeOn/
case 'key':
case 'ref': const reserveRE = /^(?:key|ref|slots)$|^nativeOn/
case 'slots': export const isReservedProp = (key: string): boolean => reserveRE.test(key)
return true
default:
return key.startsWith('nativeOn')
}
}
export function normalizeStyle( export function normalizeStyle(
value: any value: any

View File

@ -5,7 +5,7 @@ import {
} from './component' } from './component'
import { VNodeFlags, ChildrenFlags } from './flags' import { VNodeFlags, ChildrenFlags } from './flags'
import { createComponentClassFromOptions } from './componentUtils' 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. // Vue core is platform agnostic, so we are not using Element for "DOM" nodes.
export interface RenderNode { export interface RenderNode {
@ -270,7 +270,7 @@ export function cloneVNode(vnode: VNode, extraData?: VNodeData): VNode {
clonedData.class = normalizeClass([clonedData.class, extraData.class]) clonedData.class = normalizeClass([clonedData.class, extraData.class])
} else if (key === 'style') { } else if (key === 'style') {
clonedData.style = normalizeStyle([clonedData.style, extraData.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] const existing = clonedData[key]
clonedData[key] = existing clonedData[key] = existing
? [].concat(existing, extraData[key]) ? [].concat(existing, extraData[key])

View File

@ -5,6 +5,9 @@ import { patchAttr } from './modules/attrs'
import { patchDOMProp } from './modules/props' import { patchDOMProp } from './modules/props'
import { patchEvent } from './modules/events' import { patchEvent } from './modules/events'
export const onRE = /^on/
const domPropsRE = /^domProps/
export function patchData( export function patchData(
el: Element, el: Element,
key: string, key: string,
@ -24,9 +27,9 @@ export function patchData(
patchStyle(el, prevValue, nextValue, nextVNode.data) patchStyle(el, prevValue, nextValue, nextVNode.data)
break break
default: default:
if (key.startsWith('on')) { if (onRE.test(key)) {
patchEvent(el, key.toLowerCase().slice(2), prevValue, nextValue) patchEvent(el, key.toLowerCase().slice(2), prevValue, nextValue)
} else if (key.startsWith('domProps')) { } else if (domPropsRE.test(key)) {
patchDOMProp( patchDOMProp(
el, el,
key[8].toLowerCase() + key.slice(9), key[8].toLowerCase() + key.slice(9),

View File

@ -1,11 +1,12 @@
import { VNode } from '@vue/core' import { VNode } from '@vue/core'
import { handleDelegatedEvent } from './modules/events' import { handleDelegatedEvent } from './modules/events'
import { onRE } from './patchData'
export function teardownVNode(vnode: VNode) { export function teardownVNode(vnode: VNode) {
const { el, data } = vnode const { el, data } = vnode
if (data != null) { if (data != null) {
for (const key in data) { for (const key in data) {
if (key.startsWith('on')) { if (onRE.test(key)) {
handleDelegatedEvent(el, key.toLowerCase().slice(2), null) handleDelegatedEvent(el, key.toLowerCase().slice(2), null)
} }
} }