Răsfoiți Sursa

refactor: use regex instead of startsWith

Evan You 7 ani în urmă
părinte
comite
342a9f3a03

+ 0 - 1
packages/core/README.md

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

+ 2 - 2
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

+ 5 - 10
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

+ 2 - 2
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])

+ 5 - 2
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),

+ 2 - 1
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)
       }
     }