提交 da96069d 编写于 作者: fxy060608's avatar fxy060608

chore: add normalizeRoute

上级 32305c76
declare var weex: any
declare var tt: any
declare var qa: any
declare var swan: any
......
......@@ -51,6 +51,10 @@ declare namespace UniApp {
tabBar?: TabBarOptions
subPackages?: { root: string }[]
qqMapKey?: string
// app-plus
entryPagePath?: string
entryPageQuery?: string
realEntryPagePath?: string
}
interface UniRoute {
......
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(
......
......@@ -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'
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))
......
......@@ -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) {
......
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:
......
......@@ -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',
......
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
......
......@@ -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:
......
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', () => {
......
......@@ -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;
......
......@@ -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: <T extends (str: string) => 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<string, any>): Record<string, string>;
......@@ -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<CSSStyleDeclaration>): void;
export declare const WEB_INVOKE_APPSERVICE = "WEB_INVOKE_APPSERVICE";
export { }
......@@ -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 };
......@@ -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'
export const cacheStringFunction = <T extends (str: string) => string>(
fn: T
): T => {
const cache: Record<string, string> = 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
}
......
......@@ -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)
}
}
......
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
}
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
}
......@@ -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'
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册