From da96069df792ddc9f917a4cea389a7f216a61683 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Wed, 16 Jun 2021 16:36:35 +0800 Subject: [PATCH] chore: add normalizeRoute --- packages/global.d.ts | 2 + packages/shims-uni-app.d.ts | 4 + packages/uni-api/src/protocols/route/route.ts | 4 +- packages/uni-core/src/helpers/index.ts | 2 +- .../src/helpers/{getRealRoute.ts => route.ts} | 31 +++--- packages/uni-h5/dist/uni-h5.cjs.js | 25 ++--- packages/uni-h5/dist/uni-h5.es.js | 40 +++---- packages/uni-h5/src/framework/setup/index.ts | 4 +- packages/uni-h5/src/platform/dom.ts | 5 +- packages/uni-h5/src/service/api/ui/tabBar.ts | 4 +- .../uni-shared/__tests__/vdom/utils.spec.ts | 7 +- packages/uni-shared/dist/uni-shared.cjs.js | 104 ++++++++---------- packages/uni-shared/dist/uni-shared.d.ts | 10 +- packages/uni-shared/dist/uni-shared.es.js | 102 ++++++++--------- packages/uni-shared/src/constants.ts | 5 + packages/uni-shared/src/utils.ts | 10 ++ packages/uni-shared/src/vdom/Node.ts | 4 +- packages/uni-shared/src/vdom/decode.ts | 25 +++++ .../src/vdom/{utils.ts => encode.ts} | 75 +++---------- packages/uni-shared/src/vdom/index.ts | 3 +- 20 files changed, 214 insertions(+), 252 deletions(-) rename packages/uni-core/src/helpers/{getRealRoute.ts => route.ts} (55%) create mode 100644 packages/uni-shared/src/vdom/decode.ts rename packages/uni-shared/src/vdom/{utils.ts => encode.ts} (52%) diff --git a/packages/global.d.ts b/packages/global.d.ts index 93be54718..212bf6beb 100644 --- a/packages/global.d.ts +++ b/packages/global.d.ts @@ -1,3 +1,5 @@ +declare var weex: any + declare var tt: any declare var qa: any declare var swan: any diff --git a/packages/shims-uni-app.d.ts b/packages/shims-uni-app.d.ts index 0168bc106..30c798bef 100644 --- a/packages/shims-uni-app.d.ts +++ b/packages/shims-uni-app.d.ts @@ -51,6 +51,10 @@ declare namespace UniApp { tabBar?: TabBarOptions subPackages?: { root: string }[] qqMapKey?: string + // app-plus + entryPagePath?: string + entryPageQuery?: string + realEntryPagePath?: string } interface UniRoute { diff --git a/packages/uni-api/src/protocols/route/route.ts b/packages/uni-api/src/protocols/route/route.ts index 022d0575e..3a6797c81 100644 --- a/packages/uni-api/src/protocols/route/route.ts +++ b/packages/uni-api/src/protocols/route/route.ts @@ -1,5 +1,5 @@ import { extend } from '@vue/shared' -import { getRealRoute } from '@dcloudio/uni-core' +import { normalizeRoute } from '@dcloudio/uni-core' import { encodeQueryString } from './encodeQueryString' const ANIMATION_IN = [ @@ -135,7 +135,7 @@ function createNormalizeUrl(type: string) { return `Missing required args: "url"` } // 格式化为绝对路径路由 - url = getRealRoute(url) + url = normalizeRoute(url) const pagePath = url.split('?')[0] // 匹配路由是否存在 const routeOptions = __uniRoutes.find( diff --git a/packages/uni-core/src/helpers/index.ts b/packages/uni-core/src/helpers/index.ts index 63d591b8b..3003fc6f3 100644 --- a/packages/uni-core/src/helpers/index.ts +++ b/packages/uni-core/src/helpers/index.ts @@ -3,6 +3,6 @@ export * from './util' export * from './icon' export * from './page' export * from './scroll' -export * from './getRealRoute' +export * from './route' export * from './callbacks' export { default as Emitter } from './TinyEmitter' diff --git a/packages/uni-core/src/helpers/getRealRoute.ts b/packages/uni-core/src/helpers/route.ts similarity index 55% rename from packages/uni-core/src/helpers/getRealRoute.ts rename to packages/uni-core/src/helpers/route.ts index 82e45ddbb..cd7a7ecc8 100644 --- a/packages/uni-core/src/helpers/getRealRoute.ts +++ b/packages/uni-core/src/helpers/route.ts @@ -1,19 +1,18 @@ -export function getRealRoute(fromRoute: string, toRoute?: string): string { - if (!toRoute) { - toRoute = fromRoute - if (toRoute.indexOf('/') === 0) { - return toRoute - } - const pages = getCurrentPages() - if (pages.length) { - fromRoute = (pages[pages.length - 1] as any).$page.route - } else { - fromRoute = '' - } - } else { - if (toRoute.indexOf('/') === 0) { - return toRoute - } +export function normalizeRoute(toRoute: string) { + if (toRoute.indexOf('/') === 0) { + return toRoute + } + let fromRoute = '' + const pages = getCurrentPages() + if (pages.length) { + fromRoute = (pages[pages.length - 1] as any).$page.route + } + return getRealRoute(fromRoute, toRoute) +} + +export function getRealRoute(fromRoute: string, toRoute: string): string { + if (toRoute.indexOf('/') === 0) { + return toRoute } if (toRoute.indexOf('./') === 0) { return getRealRoute(fromRoute, toRoute.substr(2)) diff --git a/packages/uni-h5/dist/uni-h5.cjs.js b/packages/uni-h5/dist/uni-h5.cjs.js index ae651fd22..763bea262 100644 --- a/packages/uni-h5/dist/uni-h5.cjs.js +++ b/packages/uni-h5/dist/uni-h5.cjs.js @@ -419,21 +419,8 @@ function useCurrentPageId() { return vue.getCurrentInstance().root.proxy.$page.id; } function getRealRoute(fromRoute, toRoute) { - if (!toRoute) { - toRoute = fromRoute; - if (toRoute.indexOf("/") === 0) { - return toRoute; - } - const pages = getCurrentPages(); - if (pages.length) { - fromRoute = pages[pages.length - 1].$page.route; - } else { - fromRoute = ""; - } - } else { - if (toRoute.indexOf("/") === 0) { - return toRoute; - } + if (toRoute.indexOf("/") === 0) { + return toRoute; } if (toRoute.indexOf("./") === 0) { return getRealRoute(fromRoute, toRoute.substr(2)); @@ -975,8 +962,6 @@ var index$w = /* @__PURE__ */ defineBuiltInComponent({ }; } }); -const SCHEME_RE = /^([a-z-]+:)?\/\//i; -const DATA_RE = /^data:.*,.*/; const baseUrl = __IMPORT_META_ENV_BASE_URL__; function addBase(filePath) { return baseUrl + filePath; @@ -992,7 +977,7 @@ function getRealPath(filePath) { return addBase(filePath.substr(1)); } } - if (SCHEME_RE.test(filePath) || DATA_RE.test(filePath) || filePath.indexOf("blob:") === 0) { + if (uniShared.SCHEME_RE.test(filePath) || uniShared.DATA_RE.test(filePath) || filePath.indexOf("blob:") === 0) { return filePath; } const pages = getCurrentPages(); @@ -9472,6 +9457,10 @@ function normalizeContentType(header) { return; } const contentType = header[name]; + if (name !== "Content-Type") { + header["Content-Type"] = header[name]; + delete header[name]; + } if (contentType.indexOf("application/json") === 0) { return "json"; } else if (contentType.indexOf("application/x-www-form-urlencoded") === 0) { diff --git a/packages/uni-h5/dist/uni-h5.es.js b/packages/uni-h5/dist/uni-h5.es.js index 70c3a9a8d..189d70b3c 100644 --- a/packages/uni-h5/dist/uni-h5.es.js +++ b/packages/uni-h5/dist/uni-h5.es.js @@ -1,5 +1,5 @@ import { isFunction, extend, hyphenate, isPlainObject, isString, isArray, hasOwn, isObject, capitalize, toRawType, makeMap as makeMap$1, isPromise, invokeArrayFns as invokeArrayFns$1 } from "@vue/shared"; -import { once, passive, normalizeTarget, isBuiltInComponent, initCustomDataset, invokeArrayFns, getCustomDataset, callOptions, PRIMARY_COLOR, removeLeadingSlash, getLen, debounce, NAVBAR_HEIGHT, parseQuery, ON_REACH_BOTTOM_DISTANCE, decodedQuery, updateElementStyle, addFont, scrollTo, RESPONSIVE_MIN_WIDTH, formatDateTime } from "@dcloudio/uni-shared"; +import { once, passive, normalizeTarget, isBuiltInComponent, initCustomDataset, invokeArrayFns, SCHEME_RE, DATA_RE, getCustomDataset, callOptions, PRIMARY_COLOR, removeLeadingSlash, getLen, debounce, NAVBAR_HEIGHT, parseQuery, ON_REACH_BOTTOM_DISTANCE, decodedQuery, WEB_INVOKE_APPSERVICE, updateElementStyle, addFont, scrollTo, RESPONSIVE_MIN_WIDTH, formatDateTime } from "@dcloudio/uni-shared"; import { openBlock, createBlock, mergeProps, createVNode, toDisplayString, withModifiers, getCurrentInstance, defineComponent, ref, provide, computed, watch, onUnmounted, inject, onBeforeUnmount, reactive, onActivated, onMounted, nextTick, onBeforeMount, withDirectives, vShow, shallowRef, watchEffect, isVNode, Fragment, markRaw, createTextVNode, injectHook, onBeforeActivate, onBeforeDeactivate, renderList, onDeactivated, Teleport, createApp, Transition, withCtx, KeepAlive, resolveDynamicComponent, renderSlot } from "vue"; import { initVueI18n, LOCALE_EN, LOCALE_ES, LOCALE_FR, LOCALE_ZH_HANS, LOCALE_ZH_HANT } from "@dcloudio/uni-i18n"; import { useRoute, createRouter, createWebHistory, createWebHashHistory, useRouter, isNavigationFailure, RouterView } from "vue-router"; @@ -857,22 +857,20 @@ function createScrollListener({ ticking = true; }; } +function normalizeRoute(toRoute) { + if (toRoute.indexOf("/") === 0) { + return toRoute; + } + let fromRoute = ""; + const pages = getCurrentPages(); + if (pages.length) { + fromRoute = pages[pages.length - 1].$page.route; + } + return getRealRoute(fromRoute, toRoute); +} function getRealRoute(fromRoute, toRoute) { - if (!toRoute) { - toRoute = fromRoute; - if (toRoute.indexOf("/") === 0) { - return toRoute; - } - const pages = getCurrentPages(); - if (pages.length) { - fromRoute = pages[pages.length - 1].$page.route; - } else { - fromRoute = ""; - } - } else { - if (toRoute.indexOf("/") === 0) { - return toRoute; - } + if (toRoute.indexOf("/") === 0) { + return toRoute; } if (toRoute.indexOf("./") === 0) { return getRealRoute(fromRoute, toRoute.substr(2)); @@ -1824,8 +1822,6 @@ var index$s = /* @__PURE__ */ defineBuiltInComponent({ function findElem(vm) { return vm.$el; } -const SCHEME_RE = /^([a-z-]+:)?\/\//i; -const DATA_RE = /^data:.*,.*/; const baseUrl = import.meta.env.BASE_URL; function addBase(filePath) { return baseUrl + filePath; @@ -4665,7 +4661,7 @@ function createNormalizeUrl(type) { if (!url) { return `Missing required args: "url"`; } - url = getRealRoute(url); + url = normalizeRoute(url); const pagePath = url.split("?")[0]; const routeOptions = __uniRoutes.find(({ path, alias }) => path === pagePath || alias === pagePath); if (!routeOptions) { @@ -13430,7 +13426,7 @@ function setupApp(comp) { } onMounted(() => { window.addEventListener("message", function(evt) { - if (isPlainObject(evt.data) && evt.data.type === "WEB_INVOKE_APPSERVICE") { + if (isPlainObject(evt.data) && evt.data.type === WEB_INVOKE_APPSERVICE) { UniServiceJSBridge.emit("onWebInvokeAppService", evt.data.data, evt.data.pageId); } }); @@ -17911,7 +17907,7 @@ function setProperties(item, props2, propsData) { } }); } -function normalizeRoute(index2, oldPagePath, newPagePath) { +function normalizeTabBarRoute(index2, oldPagePath, newPagePath) { const oldTabBarRoute = __uniRoutes.find((item) => item.meta.route === oldPagePath); if (oldTabBarRoute) { const { meta } = oldTabBarRoute; @@ -17941,7 +17937,7 @@ function setTabBar(type, args, resolve) { setProperties(tabBarItem, setTabBarItemProps, args); const { pagePath } = args; if (pagePath && pagePath !== oldPagePath) { - normalizeRoute(index2, oldPagePath, pagePath); + normalizeTabBarRoute(index2, oldPagePath, pagePath); } break; case API_SET_TAB_BAR_STYLE: diff --git a/packages/uni-h5/src/framework/setup/index.ts b/packages/uni-h5/src/framework/setup/index.ts index 5114847a0..3041ca52f 100644 --- a/packages/uni-h5/src/framework/setup/index.ts +++ b/packages/uni-h5/src/framework/setup/index.ts @@ -12,7 +12,7 @@ import { onBeforeMount, } from 'vue' import { useRouter } from 'vue-router' -import { decodedQuery } from '@dcloudio/uni-shared' +import { decodedQuery, WEB_INVOKE_APPSERVICE } from '@dcloudio/uni-shared' import { LayoutComponent } from '../..' import { initApp } from './app' import { initPage, onPageShow, onPageReady } from './page' @@ -134,7 +134,7 @@ export function setupApp(comp: any) { }) { if ( isPlainObject(evt.data) && - evt.data.type === 'WEB_INVOKE_APPSERVICE' + evt.data.type === WEB_INVOKE_APPSERVICE ) { UniServiceJSBridge.emit( 'onWebInvokeAppService', diff --git a/packages/uni-h5/src/platform/dom.ts b/packages/uni-h5/src/platform/dom.ts index 6fb5e6606..03052c7c7 100644 --- a/packages/uni-h5/src/platform/dom.ts +++ b/packages/uni-h5/src/platform/dom.ts @@ -1,6 +1,6 @@ import { ComponentPublicInstance } from 'vue' import { getRealRoute } from '@dcloudio/uni-core' - +import { DATA_RE, SCHEME_RE } from '@dcloudio/uni-shared' declare global { interface ImportMeta { env: { @@ -13,9 +13,6 @@ export function findElem(vm: ComponentPublicInstance) { return vm.$el } -const SCHEME_RE = /^([a-z-]+:)?\/\//i -const DATA_RE = /^data:.*,.*/ - const baseUrl = __IMPORT_META_ENV_BASE_URL__ function addBase(filePath: string) { return baseUrl + filePath diff --git a/packages/uni-h5/src/service/api/ui/tabBar.ts b/packages/uni-h5/src/service/api/ui/tabBar.ts index 9ec8ca814..3de2f1385 100644 --- a/packages/uni-h5/src/service/api/ui/tabBar.ts +++ b/packages/uni-h5/src/service/api/ui/tabBar.ts @@ -54,7 +54,7 @@ function setProperties( }) } -function normalizeRoute( +function normalizeTabBarRoute( index: number, oldPagePath: string, newPagePath: string @@ -97,7 +97,7 @@ function setTabBar( setProperties(tabBarItem, setTabBarItemProps, args) const { pagePath } = args if (pagePath && pagePath !== oldPagePath) { - normalizeRoute(index, oldPagePath, pagePath) + normalizeTabBarRoute(index, oldPagePath, pagePath) } break case API_SET_TAB_BAR_STYLE: diff --git a/packages/uni-shared/__tests__/vdom/utils.spec.ts b/packages/uni-shared/__tests__/vdom/utils.spec.ts index 9a96191bc..29e29d546 100644 --- a/packages/uni-shared/__tests__/vdom/utils.spec.ts +++ b/packages/uni-shared/__tests__/vdom/utils.spec.ts @@ -1,11 +1,10 @@ import { + ATTR_MAP, COMPONENT_MAP, encodeTag, - decodeTag, - ATTR_MAP, encodeAttr, - decodeAttr, -} from '../../src/vdom/utils' +} from '../../src/vdom/encode' +import { decodeTag, decodeAttr } from '../../src/vdom/decode' describe('encode', () => { test('tag', () => { diff --git a/packages/uni-shared/dist/uni-shared.cjs.js b/packages/uni-shared/dist/uni-shared.cjs.js index aab03dd6b..15e83c285 100644 --- a/packages/uni-shared/dist/uni-shared.cjs.js +++ b/packages/uni-shared/dist/uni-shared.cjs.js @@ -367,17 +367,25 @@ function proxyStyle(uniCssStyle) { const ATTR_MAP = { class: '.c', style: '.s', + onClick: '.e0', + onChange: '.e1', + onInput: '.e2', + onLoad: '.e3', + onError: '.e4', + onTouchstart: '.e5', + onTouchmove: '.e6', + onTouchcancel: '.e7', + onTouchend: '.e8', + onLongpress: '.e9', + onTransitionend: '.ea', + onAnimationstart: '.eb', + onAnimationiteration: '.ec', + onAnimationend: '.ed', + onTouchforcechange: '.ee', }; function encodeAttr(name) { return ATTR_MAP[name] || name; } -const ATTR_RESTORE_MAP = { - '.c': 'class', - '.s': 'style', -}; -function decodeAttr(name) { - return ATTR_RESTORE_MAP[name] || name; -} const COMPONENT_MAP = { VIEW: 1, IMAGE: 2, @@ -425,55 +433,6 @@ const COMPONENT_MAP = { }; function encodeTag(tag) { return COMPONENT_MAP[tag] || tag; -} -const COMPONENT_ARR = [ - '', - 'view', - 'image', - 'text', - '#text', - '#comment', - 'navigator', - 'form', - 'button', - 'input', - 'label', - 'radio', - 'checkbox', - 'checkbox-group', - 'ad', - 'audio', - 'camera', - 'canvas', - 'cover-image', - 'cover-view', - 'editor', - 'functional-page-navigator', - 'icon', - 'radio-group', - 'live-player', - 'live-pusher', - 'map', - 'movable-area', - 'movable-view', - 'official-account', - 'open-data', - 'picker', - 'picker-view', - 'picker-view-column', - 'progress', - 'rich-text', - 'scroll-view', - 'slider', - 'swiper', - 'swiper-item', - 'switch', - 'textarea', - 'video', - 'web-view', -]; -function decodeTag(tag) { - return COMPONENT_ARR[tag] || tag; } const NODE_TYPE_PAGE = 0; @@ -628,7 +587,7 @@ class UniBaseNode extends UniNode { removeEventListener(type, callback, options) { super.removeEventListener(type, callback, options); const normalized = normalizeEventType(type); - if (this.attributes[normalized]) { + if (this.attributes[encodeAttr(normalized)]) { this.removeAttribute(normalized); } } @@ -713,6 +672,28 @@ class UniTextNode extends UniBaseNode { } } +const DECODED_ATTR_MAP = /*#__PURE__*/ Object.keys(ATTR_MAP).reduce((map, name) => { + map[ATTR_MAP[name]] = name; + return map; +}, Object.create(null)); +function decodeAttr(name) { + return DECODED_ATTR_MAP[name] || name; +} +const DECODED_COMPONENT_ARR = /*#__PURE__*/ Object.keys(COMPONENT_MAP).reduce((arr, name) => { + arr.push(name.toLowerCase()); + return arr; +}, ['']); +function decodeTag(tag) { + return DECODED_COMPONENT_ARR[tag] || tag; +} + +const cacheStringFunction = (fn) => { + const cache = Object.create(null); + return ((str) => { + const hit = cache[str]; + return hit || (cache[str] = fn(str)); + }); +}; function getLen(str = '') { return ('' + str).replace(/[^\x00-\xff]/g, '**').length; } @@ -883,7 +864,10 @@ const UNI_SSR = '__uniSSR'; const UNI_SSR_TITLE = 'title'; const UNI_SSR_STORE = 'store'; const UNI_SSR_DATA = 'data'; -const UNI_SSR_GLOBAL_DATA = 'globalData'; +const UNI_SSR_GLOBAL_DATA = 'globalData'; +const SCHEME_RE = /^([a-z-]+:)?\/\//i; +const DATA_RE = /^data:.*,.*/; +const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE'; function getEnvLocale() { const { env } = process; @@ -895,6 +879,7 @@ exports.BUILT_IN_TAGS = BUILT_IN_TAGS; exports.COMPONENT_NAME_PREFIX = COMPONENT_NAME_PREFIX; exports.COMPONENT_PREFIX = COMPONENT_PREFIX; exports.COMPONENT_SELECTOR_PREFIX = COMPONENT_SELECTOR_PREFIX; +exports.DATA_RE = DATA_RE; exports.NAVBAR_HEIGHT = NAVBAR_HEIGHT; exports.NODE_TYPE_COMMENT = NODE_TYPE_COMMENT; exports.NODE_TYPE_ELEMENT = NODE_TYPE_ELEMENT; @@ -904,6 +889,7 @@ exports.ON_REACH_BOTTOM_DISTANCE = ON_REACH_BOTTOM_DISTANCE; exports.PLUS_RE = PLUS_RE; exports.PRIMARY_COLOR = PRIMARY_COLOR; exports.RESPONSIVE_MIN_WIDTH = RESPONSIVE_MIN_WIDTH; +exports.SCHEME_RE = SCHEME_RE; exports.TABBAR_HEIGHT = TABBAR_HEIGHT; exports.TAGS = TAGS; exports.UNI_SSR = UNI_SSR; @@ -919,7 +905,9 @@ exports.UniInputElement = UniInputElement; exports.UniNode = UniNode; exports.UniTextAreaElement = UniTextAreaElement; exports.UniTextNode = UniTextNode; +exports.WEB_INVOKE_APPSERVICE = WEB_INVOKE_APPSERVICE; exports.addFont = addFont; +exports.cacheStringFunction = cacheStringFunction; exports.callOptions = callOptions; exports.createRpx2Unit = createRpx2Unit; exports.debounce = debounce; diff --git a/packages/uni-shared/dist/uni-shared.d.ts b/packages/uni-shared/dist/uni-shared.d.ts index 791b09b19..f6c27278c 100644 --- a/packages/uni-shared/dist/uni-shared.d.ts +++ b/packages/uni-shared/dist/uni-shared.d.ts @@ -4,6 +4,8 @@ export declare function addFont(family: string, source: string, desc?: FontFaceD export declare const BUILT_IN_TAGS: string[]; +export declare const cacheStringFunction: string>(fn: T) => T; + export declare function callOptions(options: Options, errMsg: string): void; export declare function callOptions(options: Options, data: { @@ -19,6 +21,8 @@ export declare const COMPONENT_SELECTOR_PREFIX = "uni-"; export declare function createRpx2Unit(unit: string, unitRatio: number, unitPrecision: number): (val: string) => string; +export declare const DATA_RE: RegExp; + export declare function debounce(fn: Function, delay: number): { (this: any): void; cancel(): void; @@ -33,7 +37,7 @@ export declare function debounce(fn: Function, delay: number): { */ export declare function decode(text: string | number): string; -export declare function decodeAttr(name: string): string; +export declare function decodeAttr(name: string): any; export declare function decodedQuery(query?: Record): Record; @@ -144,6 +148,8 @@ export declare type Rpx2UnitOptions = typeof defaultRpx2Unit; export declare const sanitise: (val: unknown) => any; +export declare const SCHEME_RE: RegExp; + declare function scrollTo_2(scrollTop: number | string, duration: number): void; export { scrollTo_2 as scrollTo } @@ -310,4 +316,6 @@ export declare class UniTextNode extends UniBaseNode { export declare function updateElementStyle(element: HTMLElement, styles: Partial): void; +export declare const WEB_INVOKE_APPSERVICE = "WEB_INVOKE_APPSERVICE"; + export { } diff --git a/packages/uni-shared/dist/uni-shared.es.js b/packages/uni-shared/dist/uni-shared.es.js index e4b63e411..2974e6047 100644 --- a/packages/uni-shared/dist/uni-shared.es.js +++ b/packages/uni-shared/dist/uni-shared.es.js @@ -363,17 +363,25 @@ function proxyStyle(uniCssStyle) { const ATTR_MAP = { class: '.c', style: '.s', + onClick: '.e0', + onChange: '.e1', + onInput: '.e2', + onLoad: '.e3', + onError: '.e4', + onTouchstart: '.e5', + onTouchmove: '.e6', + onTouchcancel: '.e7', + onTouchend: '.e8', + onLongpress: '.e9', + onTransitionend: '.ea', + onAnimationstart: '.eb', + onAnimationiteration: '.ec', + onAnimationend: '.ed', + onTouchforcechange: '.ee', }; function encodeAttr(name) { return ATTR_MAP[name] || name; } -const ATTR_RESTORE_MAP = { - '.c': 'class', - '.s': 'style', -}; -function decodeAttr(name) { - return ATTR_RESTORE_MAP[name] || name; -} const COMPONENT_MAP = { VIEW: 1, IMAGE: 2, @@ -421,55 +429,6 @@ const COMPONENT_MAP = { }; function encodeTag(tag) { return COMPONENT_MAP[tag] || tag; -} -const COMPONENT_ARR = [ - '', - 'view', - 'image', - 'text', - '#text', - '#comment', - 'navigator', - 'form', - 'button', - 'input', - 'label', - 'radio', - 'checkbox', - 'checkbox-group', - 'ad', - 'audio', - 'camera', - 'canvas', - 'cover-image', - 'cover-view', - 'editor', - 'functional-page-navigator', - 'icon', - 'radio-group', - 'live-player', - 'live-pusher', - 'map', - 'movable-area', - 'movable-view', - 'official-account', - 'open-data', - 'picker', - 'picker-view', - 'picker-view-column', - 'progress', - 'rich-text', - 'scroll-view', - 'slider', - 'swiper', - 'swiper-item', - 'switch', - 'textarea', - 'video', - 'web-view', -]; -function decodeTag(tag) { - return COMPONENT_ARR[tag] || tag; } const NODE_TYPE_PAGE = 0; @@ -624,7 +583,7 @@ class UniBaseNode extends UniNode { removeEventListener(type, callback, options) { super.removeEventListener(type, callback, options); const normalized = normalizeEventType(type); - if (this.attributes[normalized]) { + if (this.attributes[encodeAttr(normalized)]) { this.removeAttribute(normalized); } } @@ -709,6 +668,28 @@ class UniTextNode extends UniBaseNode { } } +const DECODED_ATTR_MAP = /*#__PURE__*/ Object.keys(ATTR_MAP).reduce((map, name) => { + map[ATTR_MAP[name]] = name; + return map; +}, Object.create(null)); +function decodeAttr(name) { + return DECODED_ATTR_MAP[name] || name; +} +const DECODED_COMPONENT_ARR = /*#__PURE__*/ Object.keys(COMPONENT_MAP).reduce((arr, name) => { + arr.push(name.toLowerCase()); + return arr; +}, ['']); +function decodeTag(tag) { + return DECODED_COMPONENT_ARR[tag] || tag; +} + +const cacheStringFunction = (fn) => { + const cache = Object.create(null); + return ((str) => { + const hit = cache[str]; + return hit || (cache[str] = fn(str)); + }); +}; function getLen(str = '') { return ('' + str).replace(/[^\x00-\xff]/g, '**').length; } @@ -879,7 +860,10 @@ const UNI_SSR = '__uniSSR'; const UNI_SSR_TITLE = 'title'; const UNI_SSR_STORE = 'store'; const UNI_SSR_DATA = 'data'; -const UNI_SSR_GLOBAL_DATA = 'globalData'; +const UNI_SSR_GLOBAL_DATA = 'globalData'; +const SCHEME_RE = /^([a-z-]+:)?\/\//i; +const DATA_RE = /^data:.*,.*/; +const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE'; function getEnvLocale() { const { env } = process; @@ -887,4 +871,4 @@ function getEnvLocale() { return (lang && lang.replace(/[.:].*/, '')) || 'en'; } -export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniNode, UniTextAreaElement, UniTextNode, addFont, callOptions, createRpx2Unit, debounce, decode, decodeAttr, decodeTag, decodedQuery, defaultRpx2Unit, encodeAttr, encodeTag, formatDateTime, getCustomDataset, getEnvLocale, getLen, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle }; +export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, DATA_RE, NAVBAR_HEIGHT, NODE_TYPE_COMMENT, NODE_TYPE_ELEMENT, NODE_TYPE_PAGE, NODE_TYPE_TEXT, ON_REACH_BOTTOM_DISTANCE, PLUS_RE, PRIMARY_COLOR, RESPONSIVE_MIN_WIDTH, SCHEME_RE, TABBAR_HEIGHT, TAGS, UNI_SSR, UNI_SSR_DATA, UNI_SSR_GLOBAL_DATA, UNI_SSR_STORE, UNI_SSR_TITLE, UniBaseNode, UniCommentNode, UniElement, UniEvent, UniInputElement, UniNode, UniTextAreaElement, UniTextNode, WEB_INVOKE_APPSERVICE, addFont, cacheStringFunction, callOptions, createRpx2Unit, debounce, decode, decodeAttr, decodeTag, decodedQuery, defaultRpx2Unit, encodeAttr, encodeTag, formatDateTime, getCustomDataset, getEnvLocale, getLen, initCustomDataset, invokeArrayFns, isBuiltInComponent, isCustomElement, isNativeTag, normalizeDataset, normalizeTarget, once, parseQuery, passive, plusReady, removeLeadingSlash, sanitise, scrollTo, stringifyQuery, updateElementStyle }; diff --git a/packages/uni-shared/src/constants.ts b/packages/uni-shared/src/constants.ts index bad31a99a..386b409b9 100644 --- a/packages/uni-shared/src/constants.ts +++ b/packages/uni-shared/src/constants.ts @@ -12,3 +12,8 @@ export const UNI_SSR_TITLE = 'title' export const UNI_SSR_STORE = 'store' export const UNI_SSR_DATA = 'data' export const UNI_SSR_GLOBAL_DATA = 'globalData' + +export const SCHEME_RE = /^([a-z-]+:)?\/\//i +export const DATA_RE = /^data:.*,.*/ + +export const WEB_INVOKE_APPSERVICE = 'WEB_INVOKE_APPSERVICE' diff --git a/packages/uni-shared/src/utils.ts b/packages/uni-shared/src/utils.ts index 3af0b262f..759d46730 100644 --- a/packages/uni-shared/src/utils.ts +++ b/packages/uni-shared/src/utils.ts @@ -1,3 +1,13 @@ +export const cacheStringFunction = string>( + fn: T +): T => { + const cache: Record = Object.create(null) + return ((str: string) => { + const hit = cache[str] + return hit || (cache[str] = fn(str)) + }) as any +} + export function getLen(str = '') { return ('' + str).replace(/[^\x00-\xff]/g, '**').length } diff --git a/packages/uni-shared/src/vdom/Node.ts b/packages/uni-shared/src/vdom/Node.ts index 27967f6b6..71ee2c33c 100644 --- a/packages/uni-shared/src/vdom/Node.ts +++ b/packages/uni-shared/src/vdom/Node.ts @@ -8,7 +8,7 @@ import { UniCSSStyleDeclaration, UniCSSStyleDeclarationJSON, } from './Style' -import { encodeAttr, encodeTag } from './utils' +import { encodeAttr, encodeTag } from './encode' export const NODE_TYPE_PAGE = 0 export const NODE_TYPE_ELEMENT = 1 @@ -270,7 +270,7 @@ export class UniBaseNode extends UniNode { ) { super.removeEventListener(type, callback, options) const normalized = normalizeEventType(type) - if (this.attributes[normalized]) { + if (this.attributes[encodeAttr(normalized)]) { this.removeAttribute(normalized) } } diff --git a/packages/uni-shared/src/vdom/decode.ts b/packages/uni-shared/src/vdom/decode.ts new file mode 100644 index 000000000..70649628e --- /dev/null +++ b/packages/uni-shared/src/vdom/decode.ts @@ -0,0 +1,25 @@ +import { ATTR_MAP, COMPONENT_MAP } from './encode' + +const DECODED_ATTR_MAP = /*#__PURE__*/ Object.keys(ATTR_MAP).reduce( + (map, name) => { + map[ATTR_MAP[name as keyof typeof ATTR_MAP]] = name + return map + }, + Object.create(null) +) + +export function decodeAttr(name: string) { + return DECODED_ATTR_MAP[name as keyof typeof DECODED_ATTR_MAP] || name +} + +const DECODED_COMPONENT_ARR = /*#__PURE__*/ Object.keys(COMPONENT_MAP).reduce( + (arr, name) => { + arr.push(name.toLowerCase()) + return arr + }, + [''] +) + +export function decodeTag(tag: string | number) { + return DECODED_COMPONENT_ARR[tag as number] || tag +} diff --git a/packages/uni-shared/src/vdom/utils.ts b/packages/uni-shared/src/vdom/encode.ts similarity index 52% rename from packages/uni-shared/src/vdom/utils.ts rename to packages/uni-shared/src/vdom/encode.ts index 3c4dde4dd..c1eee9e69 100644 --- a/packages/uni-shared/src/vdom/utils.ts +++ b/packages/uni-shared/src/vdom/encode.ts @@ -1,21 +1,27 @@ export const ATTR_MAP = { class: '.c', style: '.s', + onClick: '.e0', + onChange: '.e1', + onInput: '.e2', + onLoad: '.e3', + onError: '.e4', + onTouchstart: '.e5', + onTouchmove: '.e6', + onTouchcancel: '.e7', + onTouchend: '.e8', + onLongpress: '.e9', + onTransitionend: '.ea', + onAnimationstart: '.eb', + onAnimationiteration: '.ec', + onAnimationend: '.ed', + onTouchforcechange: '.ee', } export function encodeAttr(name: string) { return ATTR_MAP[name as keyof typeof ATTR_MAP] || name } -export const ATTR_RESTORE_MAP = { - '.c': 'class', - '.s': 'style', -} - -export function decodeAttr(name: string) { - return ATTR_RESTORE_MAP[name as keyof typeof ATTR_RESTORE_MAP] || name -} - export const COMPONENT_MAP = { VIEW: 1, IMAGE: 2, @@ -65,54 +71,3 @@ export const COMPONENT_MAP = { export function encodeTag(tag: string) { return COMPONENT_MAP[tag as keyof typeof COMPONENT_MAP] || tag } - -const COMPONENT_ARR = [ - '', - 'view', - 'image', - 'text', - '#text', - '#comment', - 'navigator', - 'form', - 'button', - 'input', - 'label', - 'radio', - 'checkbox', - 'checkbox-group', - 'ad', - 'audio', - 'camera', - 'canvas', - 'cover-image', - 'cover-view', - 'editor', - 'functional-page-navigator', - 'icon', - 'radio-group', - 'live-player', - 'live-pusher', - 'map', - 'movable-area', - 'movable-view', - 'official-account', - 'open-data', - 'picker', - 'picker-view', - 'picker-view-column', - 'progress', - 'rich-text', - 'scroll-view', - 'slider', - 'swiper', - 'swiper-item', - 'switch', - 'textarea', - 'video', - 'web-view', -] - -export function decodeTag(tag: string | number) { - return COMPONENT_ARR[tag as number] || tag -} diff --git a/packages/uni-shared/src/vdom/index.ts b/packages/uni-shared/src/vdom/index.ts index fff48791c..746941b7b 100644 --- a/packages/uni-shared/src/vdom/index.ts +++ b/packages/uni-shared/src/vdom/index.ts @@ -12,4 +12,5 @@ export { IUniPageNode, } from './Node' export { UniTextNode } from './Text' -export { encodeTag, decodeTag, encodeAttr, decodeAttr } from './utils' +export { encodeAttr, encodeTag } from './encode' +export { decodeAttr, decodeTag } from './decode' -- GitLab