@dcloudio/uni-shared
Advanced tools
Comparing version 3.0.0-alpha-3000020210528002 to 3.0.0-alpha-3000020210611002
@@ -218,2 +218,500 @@ 'use strict'; | ||
class DOMException extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'DOMException'; | ||
} | ||
} | ||
function normalizeEventType(type) { | ||
return `on${shared.capitalize(shared.camelize(type))}`; | ||
} | ||
class UniEvent { | ||
constructor(type, opts) { | ||
this.defaultPrevented = false; | ||
this.timeStamp = Date.now(); | ||
this._stop = false; | ||
this._end = false; | ||
this.type = type.toLowerCase(); | ||
this.bubbles = !!opts.bubbles; | ||
this.cancelable = !!opts.cancelable; | ||
} | ||
preventDefault() { | ||
this.defaultPrevented = true; | ||
} | ||
stopImmediatePropagation() { | ||
this._end = this._stop = true; | ||
} | ||
stopPropagation() { | ||
this._stop = true; | ||
} | ||
} | ||
class UniEventTarget { | ||
constructor() { | ||
this._listeners = {}; | ||
} | ||
dispatchEvent(evt) { | ||
const listeners = this._listeners[evt.type]; | ||
if (!listeners) { | ||
return false; | ||
} | ||
const len = listeners.length; | ||
for (let i = 0; i < len; i++) { | ||
listeners[i].call(this, evt); | ||
if (evt._end) { | ||
break; | ||
} | ||
} | ||
return evt.cancelable && evt.defaultPrevented; | ||
} | ||
addEventListener(type, listener, options) { | ||
const isOnce = options && options.once; | ||
if (isOnce) { | ||
const wrapper = function (evt) { | ||
listener.apply(this, [evt]); | ||
this.removeEventListener(type, wrapper, options); | ||
}; | ||
return this.addEventListener(type, wrapper, shared.extend(options, { once: false })); | ||
} | ||
(this._listeners[type] || (this._listeners[type] = [])).push(listener); | ||
} | ||
removeEventListener(type, callback, options) { | ||
const listeners = this._listeners[type.toLowerCase()]; | ||
if (!listeners) { | ||
return; | ||
} | ||
const index = listeners.indexOf(callback); | ||
if (index > -1) { | ||
listeners.splice(index, 1); | ||
} | ||
} | ||
} | ||
class UniCSSStyleDeclaration { | ||
constructor() { | ||
this._cssText = null; | ||
this._value = null; | ||
} | ||
setProperty(property, value) { | ||
if (value === null || value === '') { | ||
this.removeProperty(property); | ||
} | ||
else { | ||
if (!this._value) { | ||
this._value = {}; | ||
} | ||
this._value[property] = value; | ||
} | ||
} | ||
getPropertyValue(property) { | ||
if (!this._value) { | ||
return ''; | ||
} | ||
return this._value[property] || ''; | ||
} | ||
removeProperty(property) { | ||
if (!this._value) { | ||
return ''; | ||
} | ||
const value = this._value[property]; | ||
delete this._value[property]; | ||
return value; | ||
} | ||
get cssText() { | ||
return this._cssText || ''; | ||
} | ||
set cssText(cssText) { | ||
this._cssText = cssText; | ||
} | ||
toJSON() { | ||
const { _cssText, _value } = this; | ||
const hasCssText = _cssText !== null; | ||
const hasValue = _value !== null; | ||
if (hasCssText && hasValue) { | ||
return [_cssText, _value]; | ||
} | ||
if (hasCssText) { | ||
return _cssText; | ||
} | ||
if (hasValue) { | ||
return _value; | ||
} | ||
} | ||
} | ||
const STYLE_PROPS = [ | ||
'_value', | ||
'_cssText', | ||
'cssText', | ||
'getPropertyValue', | ||
'setProperty', | ||
'removeProperty', | ||
'toJSON', | ||
]; | ||
function proxyStyle(uniCssStyle) { | ||
return new Proxy(uniCssStyle, { | ||
get(target, key, receiver) { | ||
if (STYLE_PROPS.indexOf(key) === -1) { | ||
return target.getPropertyValue(key); | ||
} | ||
return Reflect.get(target, key, receiver); | ||
}, | ||
set(target, key, value, receiver) { | ||
if (STYLE_PROPS.indexOf(key) === -1) { | ||
target.setProperty(key, value); | ||
return true; | ||
} | ||
return Reflect.set(target, key, value, receiver); | ||
}, | ||
}); | ||
} | ||
const ATTR_MAP = { | ||
class: '.c', | ||
style: '.s', | ||
}; | ||
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, | ||
TEXT: 3, | ||
'#text': 4, | ||
'#comment': 5, | ||
NAVIGATOR: 6, | ||
FORM: 7, | ||
BUTTON: 8, | ||
INPUT: 9, | ||
LABEL: 10, | ||
RADIO: 11, | ||
CHECKBOX: 12, | ||
'CHECKBOX-GROUP': 13, | ||
AD: 14, | ||
AUDIO: 15, | ||
CAMERA: 16, | ||
CANVAS: 17, | ||
'COVER-IMAGE': 18, | ||
'COVER-VIEW': 19, | ||
EDITOR: 20, | ||
'FUNCTIONAL-PAGE-NAVIGATOR': 21, | ||
ICON: 22, | ||
'RADIO-GROUP': 23, | ||
'LIVE-PLAYER': 24, | ||
'LIVE-PUSHER': 25, | ||
MAP: 26, | ||
'MOVABLE-AREA': 27, | ||
'MOVABLE-VIEW': 28, | ||
'OFFICIAL-ACCOUNT': 29, | ||
'OPEN-DATA': 30, | ||
PICKER: 31, | ||
'PICKER-VIEW': 32, | ||
'PICKER-VIEW-COLUMN': 33, | ||
PROGRESS: 34, | ||
'RICH-TEXT': 35, | ||
'SCROLL-VIEW': 36, | ||
SLIDER: 37, | ||
SWIPER: 38, | ||
'SWIPER-ITEM': 39, | ||
SWITCH: 40, | ||
TEXTAREA: 41, | ||
VIDEO: 42, | ||
'WEB-VIEW': 43, | ||
}; | ||
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; | ||
const NODE_TYPE_ELEMENT = 1; | ||
const NODE_TYPE_TEXT = 3; | ||
const NODE_TYPE_COMMENT = 8; | ||
function sibling(node, type) { | ||
const { parentNode } = node; | ||
if (!parentNode) { | ||
return null; | ||
} | ||
const { childNodes } = parentNode; | ||
return childNodes[childNodes.indexOf(node) + (type === 'n' ? 1 : -1)] || null; | ||
} | ||
function removeNode(node) { | ||
const { parentNode } = node; | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
} | ||
function checkNodeId(node) { | ||
if (!node.nodeId) { | ||
node.nodeId = node.pageNode.genId(); | ||
} | ||
} | ||
// 为优化性能,各平台不使用proxy来实现node的操作拦截,而是直接通过pageNode定制 | ||
class UniNode extends UniEventTarget { | ||
constructor(nodeType, nodeName, container) { | ||
super(); | ||
this.pageNode = null; | ||
this.parentNode = null; | ||
this._text = null; | ||
if (container) { | ||
const { pageNode } = container; | ||
this.pageNode = pageNode; | ||
this.nodeId = pageNode.genId(); | ||
pageNode.onCreate(this, encodeTag(nodeName)); | ||
} | ||
this.nodeType = nodeType; | ||
this.nodeName = nodeName; | ||
this.childNodes = []; | ||
} | ||
get firstChild() { | ||
return this.childNodes[0] || null; | ||
} | ||
get lastChild() { | ||
const { childNodes } = this; | ||
const length = childNodes.length; | ||
return length ? childNodes[length - 1] : null; | ||
} | ||
get nextSibling() { | ||
return sibling(this, 'n'); | ||
} | ||
get nodeValue() { | ||
return null; | ||
} | ||
set nodeValue(_val) { } | ||
get textContent() { | ||
return this._text || ''; | ||
} | ||
set textContent(text) { | ||
this._text = text; | ||
if (this.pageNode) { | ||
this.pageNode.onTextContent(this, text); | ||
} | ||
} | ||
get parentElement() { | ||
const { parentNode } = this; | ||
if (parentNode && parentNode.nodeType === NODE_TYPE_ELEMENT) { | ||
return parentNode; | ||
} | ||
return null; | ||
} | ||
get previousSibling() { | ||
return sibling(this, 'p'); | ||
} | ||
appendChild(newChild) { | ||
return this.insertBefore(newChild, null); | ||
} | ||
cloneNode(deep) { | ||
const cloned = shared.extend(Object.create(Object.getPrototypeOf(this)), this); | ||
const { attributes } = cloned; | ||
if (attributes) { | ||
cloned.attributes = shared.extend({}, attributes); | ||
} | ||
if (deep) { | ||
cloned.childNodes = cloned.childNodes.map((childNode) => childNode.cloneNode(true)); | ||
} | ||
return cloned; | ||
} | ||
insertBefore(newChild, refChild) { | ||
removeNode(newChild); | ||
newChild.pageNode = this.pageNode; | ||
newChild.parentNode = this; | ||
checkNodeId(newChild); | ||
const { childNodes } = this; | ||
let index; | ||
if (refChild) { | ||
index = childNodes.indexOf(refChild); | ||
if (index === -1) { | ||
throw new DOMException(`Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.`); | ||
} | ||
childNodes.splice(index, 0, newChild); | ||
} | ||
else { | ||
index = childNodes.length; | ||
childNodes.push(newChild); | ||
} | ||
return this.pageNode | ||
? this.pageNode.onInsertBefore(this, newChild, index) | ||
: newChild; | ||
} | ||
removeChild(oldChild) { | ||
const { childNodes } = this; | ||
const index = childNodes.indexOf(oldChild); | ||
if (index === -1) { | ||
throw new DOMException(`Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.`); | ||
} | ||
oldChild.parentNode = null; | ||
childNodes.splice(index, 1); | ||
return this.pageNode | ||
? this.pageNode.onRemoveChild(this, oldChild) | ||
: oldChild; | ||
} | ||
} | ||
class UniBaseNode extends UniNode { | ||
constructor(nodeType, nodeName, container) { | ||
super(nodeType, nodeName, container); | ||
this.attributes = Object.create(null); | ||
this._html = null; | ||
this.style = proxyStyle(new UniCSSStyleDeclaration()); | ||
} | ||
get className() { | ||
return (this.attributes['class'] || ''); | ||
} | ||
set className(val) { | ||
this.setAttribute('class', val); | ||
} | ||
get innerHTML() { | ||
return ''; | ||
} | ||
set innerHTML(html) { | ||
this._html = html; | ||
} | ||
addEventListener(type, listener, options) { | ||
super.addEventListener(type, listener, options); | ||
const normalized = normalizeEventType(type); | ||
if (!this.attributes[normalized]) { | ||
this.setAttribute(normalized, 1); | ||
} | ||
} | ||
removeEventListener(type, callback, options) { | ||
super.removeEventListener(type, callback, options); | ||
const normalized = normalizeEventType(type); | ||
if (this.attributes[normalized]) { | ||
this.removeAttribute(normalized); | ||
} | ||
} | ||
getAttribute(qualifiedName) { | ||
return this.attributes[encodeAttr(qualifiedName)]; | ||
} | ||
removeAttribute(qualifiedName) { | ||
qualifiedName = encodeAttr(qualifiedName); | ||
delete this.attributes[qualifiedName]; | ||
if (this.pageNode) { | ||
this.pageNode.onRemoveAttribute(this, qualifiedName); | ||
} | ||
} | ||
setAttribute(qualifiedName, value) { | ||
qualifiedName = encodeAttr(qualifiedName); | ||
this.attributes[qualifiedName] = value; | ||
if (this.pageNode) { | ||
this.pageNode.onSetAttribute(this, qualifiedName, value); | ||
} | ||
} | ||
toJSON(opts = {}) { | ||
const res = { | ||
a: this.attributes, | ||
s: this.style.toJSON(), | ||
}; | ||
if (!opts.attr) { | ||
res.i = this.nodeId; | ||
res.n = encodeTag(this.nodeName); | ||
} | ||
if (this._text !== null) { | ||
res.t = this._text; | ||
} | ||
return res; | ||
} | ||
} | ||
class UniCommentNode extends UniNode { | ||
constructor(text, container) { | ||
super(NODE_TYPE_COMMENT, '#comment', container); | ||
this._text = text; | ||
} | ||
toJSON(opts = {}) { | ||
return opts.attr | ||
? { t: this._text } | ||
: { | ||
i: this.nodeId, | ||
t: this._text, | ||
}; | ||
} | ||
} | ||
class UniElement extends UniBaseNode { | ||
constructor(nodeName, container) { | ||
super(NODE_TYPE_ELEMENT, nodeName.toUpperCase(), container); | ||
this.tagName = this.nodeName; | ||
} | ||
} | ||
class UniInputElement extends UniElement { | ||
get value() { | ||
return this.getAttribute('value'); | ||
} | ||
set value(val) { | ||
this.setAttribute('value', val); | ||
} | ||
} | ||
class UniTextAreaElement extends UniInputElement { | ||
} | ||
class UniTextNode extends UniBaseNode { | ||
constructor(text, container) { | ||
super(NODE_TYPE_TEXT, '#text', container); | ||
this._text = text; | ||
} | ||
get nodeValue() { | ||
return this._text || ''; | ||
} | ||
set nodeValue(text) { | ||
this._text = text; | ||
if (this.pageNode) { | ||
this.pageNode.onNodeValue(this, text); | ||
} | ||
} | ||
} | ||
function getLen(str = '') { | ||
@@ -402,2 +900,6 @@ return ('' + str).replace(/[^\x00-\xff]/g, '**').length; | ||
exports.NAVBAR_HEIGHT = NAVBAR_HEIGHT; | ||
exports.NODE_TYPE_COMMENT = NODE_TYPE_COMMENT; | ||
exports.NODE_TYPE_ELEMENT = NODE_TYPE_ELEMENT; | ||
exports.NODE_TYPE_PAGE = NODE_TYPE_PAGE; | ||
exports.NODE_TYPE_TEXT = NODE_TYPE_TEXT; | ||
exports.ON_REACH_BOTTOM_DISTANCE = ON_REACH_BOTTOM_DISTANCE; | ||
@@ -414,2 +916,10 @@ exports.PLUS_RE = PLUS_RE; | ||
exports.UNI_SSR_TITLE = UNI_SSR_TITLE; | ||
exports.UniBaseNode = UniBaseNode; | ||
exports.UniCommentNode = UniCommentNode; | ||
exports.UniElement = UniElement; | ||
exports.UniEvent = UniEvent; | ||
exports.UniInputElement = UniInputElement; | ||
exports.UniNode = UniNode; | ||
exports.UniTextAreaElement = UniTextAreaElement; | ||
exports.UniTextNode = UniTextNode; | ||
exports.addFont = addFont; | ||
@@ -420,4 +930,8 @@ exports.callOptions = callOptions; | ||
exports.decode = decode; | ||
exports.decodeAttr = decodeAttr; | ||
exports.decodeTag = decodeTag; | ||
exports.decodedQuery = decodedQuery; | ||
exports.defaultRpx2Unit = defaultRpx2Unit; | ||
exports.encodeAttr = encodeAttr; | ||
exports.encodeTag = encodeTag; | ||
exports.formatDateTime = formatDateTime; | ||
@@ -424,0 +938,0 @@ exports.getCustomDataset = getCustomDataset; |
@@ -36,4 +36,8 @@ import { FontFaceDescriptors } from 'css-font-loading-module'; | ||
export declare function decodeAttr(name: string): string; | ||
export declare function decodedQuery(query?: Record<string, any>): Record<string, string>; | ||
export declare function decodeTag(tag: string | number): string | number; | ||
export declare const defaultRpx2Unit: { | ||
@@ -45,2 +49,6 @@ unit: string; | ||
export declare function encodeAttr(name: string): string; | ||
export declare function encodeTag(tag: string): string | number; | ||
export declare function formatDateTime({ date, mode }: { | ||
@@ -71,4 +79,26 @@ date?: Date | undefined; | ||
export declare interface IUniPageNode { | ||
pageId: number; | ||
pageNode: IUniPageNode | null; | ||
genId: () => number; | ||
push: (...args: any[]) => void; | ||
onCreate: (thisNode: UniNode, nodeName: string | number) => UniNode; | ||
onInsertBefore: (thisNode: UniNode, newChild: UniNode, index: number) => UniNode; | ||
onRemoveChild: (thisNode: UniNode, oldChild: UniNode) => UniNode; | ||
onSetAttribute: (thisNode: UniNode, qualifiedName: string, value: unknown) => void; | ||
onRemoveAttribute: (thisNode: UniNode, qualifiedName: string) => void; | ||
onTextContent: (thisNode: UniNode, text: string) => void; | ||
onNodeValue: (thisNode: UniNode, val: string | null) => void; | ||
} | ||
export declare const NAVBAR_HEIGHT = 44; | ||
export declare const NODE_TYPE_COMMENT = 8; | ||
export declare const NODE_TYPE_ELEMENT = 1; | ||
export declare const NODE_TYPE_PAGE = 0; | ||
export declare const NODE_TYPE_TEXT = 3; | ||
export declare function normalizeDataset(el: Element): any; | ||
@@ -139,4 +169,149 @@ | ||
export declare class UniBaseNode extends UniNode { | ||
attributes: Record<string, unknown>; | ||
style: UniCSSStyleDeclaration; | ||
protected _html: string | null; | ||
constructor(nodeType: UniNodeType, nodeName: string, container: UniElement | IUniPageNode); | ||
get className(): string; | ||
set className(val: string); | ||
get innerHTML(): string; | ||
set innerHTML(html: string); | ||
addEventListener(type: string, listener: UniEventListener, options?: AddEventListenerOptions): void; | ||
removeEventListener(type: string, callback: UniEventListener, options?: EventListenerOptions): void; | ||
getAttribute(qualifiedName: string): unknown; | ||
removeAttribute(qualifiedName: string): void; | ||
setAttribute(qualifiedName: string, value: unknown): void; | ||
toJSON(opts?: { | ||
attr?: boolean; | ||
children?: boolean; | ||
}): Partial<UniNodeJSON>; | ||
} | ||
export declare class UniCommentNode extends UniNode { | ||
constructor(text: string, container: UniElement | IUniPageNode); | ||
toJSON(opts?: { | ||
attr?: boolean; | ||
}): { | ||
t: string; | ||
i?: undefined; | ||
} | { | ||
i: number; | ||
t: string; | ||
}; | ||
} | ||
declare class UniCSSStyleDeclaration { | ||
[name: string]: string | unknown; | ||
private _cssText; | ||
private _value; | ||
setProperty(property: string, value: string | null): void; | ||
getPropertyValue(property: string): string | string[]; | ||
removeProperty(property: string): string; | ||
get cssText(): string; | ||
set cssText(cssText: string); | ||
toJSON(): UniCSSStyleDeclarationJSON | undefined; | ||
} | ||
declare type UniCSSStyleDeclarationJSON = string | null | Record<string, string | string[]> | [string, Record<string, string | string[]>]; | ||
export declare class UniElement extends UniBaseNode { | ||
tagName: string; | ||
constructor(nodeName: string, container: UniElement | IUniPageNode); | ||
} | ||
export declare class UniEvent { | ||
type: string; | ||
bubbles: boolean; | ||
cancelable: boolean; | ||
defaultPrevented: boolean; | ||
timeStamp: number; | ||
_stop: boolean; | ||
_end: boolean; | ||
constructor(type: string, opts: UniEventOptions); | ||
preventDefault(): void; | ||
stopImmediatePropagation(): void; | ||
stopPropagation(): void; | ||
} | ||
export declare interface UniEventListener { | ||
(evt: UniEvent): void; | ||
} | ||
declare interface UniEventOptions { | ||
bubbles: boolean; | ||
cancelable: boolean; | ||
} | ||
declare class UniEventTarget { | ||
private _listeners; | ||
dispatchEvent(evt: UniEvent): boolean; | ||
addEventListener(type: string, listener: UniEventListener, options?: AddEventListenerOptions): void; | ||
removeEventListener(type: string, callback: UniEventListener, options?: EventListenerOptions): void; | ||
} | ||
export declare class UniInputElement extends UniElement { | ||
get value(): string | number; | ||
set value(val: string | number); | ||
} | ||
export declare class UniNode extends UniEventTarget { | ||
nodeId?: number; | ||
nodeType: UniNodeType; | ||
nodeName: string; | ||
childNodes: UniNode[]; | ||
pageNode: IUniPageNode | null; | ||
parentNode: UniNode | null; | ||
protected _text: string | null; | ||
constructor(nodeType: UniNodeType, nodeName: string, container: UniElement | IUniPageNode); | ||
get firstChild(): UniNode | null; | ||
get lastChild(): UniNode | null; | ||
get nextSibling(): UniNode | null; | ||
get nodeValue(): string | null; | ||
set nodeValue(_val: string | null); | ||
get textContent(): string; | ||
set textContent(text: string); | ||
get parentElement(): UniElement | null; | ||
get previousSibling(): UniNode | null; | ||
appendChild(newChild: UniNode): UniNode; | ||
cloneNode(deep?: boolean): UniNode; | ||
insertBefore(newChild: UniNode, refChild: UniNode | null): UniNode; | ||
removeChild(oldChild: UniNode): UniNode; | ||
} | ||
export declare interface UniNodeJSON { | ||
/** | ||
* nodeId | ||
*/ | ||
i: number; | ||
/** | ||
* nodeName | ||
*/ | ||
n: string | number; | ||
/** | ||
* attributes | ||
*/ | ||
a: Record<string, unknown>; | ||
/** | ||
* style | ||
*/ | ||
s?: UniCSSStyleDeclarationJSON; | ||
/** | ||
* text | ||
*/ | ||
t?: string; | ||
} | ||
declare type UniNodeType = typeof NODE_TYPE_PAGE | typeof NODE_TYPE_ELEMENT | typeof NODE_TYPE_TEXT | typeof NODE_TYPE_COMMENT; | ||
export declare class UniTextAreaElement extends UniInputElement { | ||
} | ||
export declare class UniTextNode extends UniBaseNode { | ||
constructor(text: string, container: UniElement | IUniPageNode); | ||
get nodeValue(): string; | ||
set nodeValue(text: string); | ||
} | ||
export declare function updateElementStyle(element: HTMLElement, styles: Partial<CSSStyleDeclaration>): void; | ||
export { } |
@@ -1,2 +0,2 @@ | ||
import { camelize, extend, isString, isHTMLTag, isSVGTag, isPlainObject, isArray } from '@vue/shared'; | ||
import { camelize, extend, isString, isHTMLTag, isSVGTag, capitalize, isPlainObject, isArray } from '@vue/shared'; | ||
@@ -214,2 +214,500 @@ function formatKey(key) { | ||
class DOMException extends Error { | ||
constructor(message) { | ||
super(message); | ||
this.name = 'DOMException'; | ||
} | ||
} | ||
function normalizeEventType(type) { | ||
return `on${capitalize(camelize(type))}`; | ||
} | ||
class UniEvent { | ||
constructor(type, opts) { | ||
this.defaultPrevented = false; | ||
this.timeStamp = Date.now(); | ||
this._stop = false; | ||
this._end = false; | ||
this.type = type.toLowerCase(); | ||
this.bubbles = !!opts.bubbles; | ||
this.cancelable = !!opts.cancelable; | ||
} | ||
preventDefault() { | ||
this.defaultPrevented = true; | ||
} | ||
stopImmediatePropagation() { | ||
this._end = this._stop = true; | ||
} | ||
stopPropagation() { | ||
this._stop = true; | ||
} | ||
} | ||
class UniEventTarget { | ||
constructor() { | ||
this._listeners = {}; | ||
} | ||
dispatchEvent(evt) { | ||
const listeners = this._listeners[evt.type]; | ||
if (!listeners) { | ||
return false; | ||
} | ||
const len = listeners.length; | ||
for (let i = 0; i < len; i++) { | ||
listeners[i].call(this, evt); | ||
if (evt._end) { | ||
break; | ||
} | ||
} | ||
return evt.cancelable && evt.defaultPrevented; | ||
} | ||
addEventListener(type, listener, options) { | ||
const isOnce = options && options.once; | ||
if (isOnce) { | ||
const wrapper = function (evt) { | ||
listener.apply(this, [evt]); | ||
this.removeEventListener(type, wrapper, options); | ||
}; | ||
return this.addEventListener(type, wrapper, extend(options, { once: false })); | ||
} | ||
(this._listeners[type] || (this._listeners[type] = [])).push(listener); | ||
} | ||
removeEventListener(type, callback, options) { | ||
const listeners = this._listeners[type.toLowerCase()]; | ||
if (!listeners) { | ||
return; | ||
} | ||
const index = listeners.indexOf(callback); | ||
if (index > -1) { | ||
listeners.splice(index, 1); | ||
} | ||
} | ||
} | ||
class UniCSSStyleDeclaration { | ||
constructor() { | ||
this._cssText = null; | ||
this._value = null; | ||
} | ||
setProperty(property, value) { | ||
if (value === null || value === '') { | ||
this.removeProperty(property); | ||
} | ||
else { | ||
if (!this._value) { | ||
this._value = {}; | ||
} | ||
this._value[property] = value; | ||
} | ||
} | ||
getPropertyValue(property) { | ||
if (!this._value) { | ||
return ''; | ||
} | ||
return this._value[property] || ''; | ||
} | ||
removeProperty(property) { | ||
if (!this._value) { | ||
return ''; | ||
} | ||
const value = this._value[property]; | ||
delete this._value[property]; | ||
return value; | ||
} | ||
get cssText() { | ||
return this._cssText || ''; | ||
} | ||
set cssText(cssText) { | ||
this._cssText = cssText; | ||
} | ||
toJSON() { | ||
const { _cssText, _value } = this; | ||
const hasCssText = _cssText !== null; | ||
const hasValue = _value !== null; | ||
if (hasCssText && hasValue) { | ||
return [_cssText, _value]; | ||
} | ||
if (hasCssText) { | ||
return _cssText; | ||
} | ||
if (hasValue) { | ||
return _value; | ||
} | ||
} | ||
} | ||
const STYLE_PROPS = [ | ||
'_value', | ||
'_cssText', | ||
'cssText', | ||
'getPropertyValue', | ||
'setProperty', | ||
'removeProperty', | ||
'toJSON', | ||
]; | ||
function proxyStyle(uniCssStyle) { | ||
return new Proxy(uniCssStyle, { | ||
get(target, key, receiver) { | ||
if (STYLE_PROPS.indexOf(key) === -1) { | ||
return target.getPropertyValue(key); | ||
} | ||
return Reflect.get(target, key, receiver); | ||
}, | ||
set(target, key, value, receiver) { | ||
if (STYLE_PROPS.indexOf(key) === -1) { | ||
target.setProperty(key, value); | ||
return true; | ||
} | ||
return Reflect.set(target, key, value, receiver); | ||
}, | ||
}); | ||
} | ||
const ATTR_MAP = { | ||
class: '.c', | ||
style: '.s', | ||
}; | ||
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, | ||
TEXT: 3, | ||
'#text': 4, | ||
'#comment': 5, | ||
NAVIGATOR: 6, | ||
FORM: 7, | ||
BUTTON: 8, | ||
INPUT: 9, | ||
LABEL: 10, | ||
RADIO: 11, | ||
CHECKBOX: 12, | ||
'CHECKBOX-GROUP': 13, | ||
AD: 14, | ||
AUDIO: 15, | ||
CAMERA: 16, | ||
CANVAS: 17, | ||
'COVER-IMAGE': 18, | ||
'COVER-VIEW': 19, | ||
EDITOR: 20, | ||
'FUNCTIONAL-PAGE-NAVIGATOR': 21, | ||
ICON: 22, | ||
'RADIO-GROUP': 23, | ||
'LIVE-PLAYER': 24, | ||
'LIVE-PUSHER': 25, | ||
MAP: 26, | ||
'MOVABLE-AREA': 27, | ||
'MOVABLE-VIEW': 28, | ||
'OFFICIAL-ACCOUNT': 29, | ||
'OPEN-DATA': 30, | ||
PICKER: 31, | ||
'PICKER-VIEW': 32, | ||
'PICKER-VIEW-COLUMN': 33, | ||
PROGRESS: 34, | ||
'RICH-TEXT': 35, | ||
'SCROLL-VIEW': 36, | ||
SLIDER: 37, | ||
SWIPER: 38, | ||
'SWIPER-ITEM': 39, | ||
SWITCH: 40, | ||
TEXTAREA: 41, | ||
VIDEO: 42, | ||
'WEB-VIEW': 43, | ||
}; | ||
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; | ||
const NODE_TYPE_ELEMENT = 1; | ||
const NODE_TYPE_TEXT = 3; | ||
const NODE_TYPE_COMMENT = 8; | ||
function sibling(node, type) { | ||
const { parentNode } = node; | ||
if (!parentNode) { | ||
return null; | ||
} | ||
const { childNodes } = parentNode; | ||
return childNodes[childNodes.indexOf(node) + (type === 'n' ? 1 : -1)] || null; | ||
} | ||
function removeNode(node) { | ||
const { parentNode } = node; | ||
if (parentNode) { | ||
parentNode.removeChild(node); | ||
} | ||
} | ||
function checkNodeId(node) { | ||
if (!node.nodeId) { | ||
node.nodeId = node.pageNode.genId(); | ||
} | ||
} | ||
// 为优化性能,各平台不使用proxy来实现node的操作拦截,而是直接通过pageNode定制 | ||
class UniNode extends UniEventTarget { | ||
constructor(nodeType, nodeName, container) { | ||
super(); | ||
this.pageNode = null; | ||
this.parentNode = null; | ||
this._text = null; | ||
if (container) { | ||
const { pageNode } = container; | ||
this.pageNode = pageNode; | ||
this.nodeId = pageNode.genId(); | ||
pageNode.onCreate(this, encodeTag(nodeName)); | ||
} | ||
this.nodeType = nodeType; | ||
this.nodeName = nodeName; | ||
this.childNodes = []; | ||
} | ||
get firstChild() { | ||
return this.childNodes[0] || null; | ||
} | ||
get lastChild() { | ||
const { childNodes } = this; | ||
const length = childNodes.length; | ||
return length ? childNodes[length - 1] : null; | ||
} | ||
get nextSibling() { | ||
return sibling(this, 'n'); | ||
} | ||
get nodeValue() { | ||
return null; | ||
} | ||
set nodeValue(_val) { } | ||
get textContent() { | ||
return this._text || ''; | ||
} | ||
set textContent(text) { | ||
this._text = text; | ||
if (this.pageNode) { | ||
this.pageNode.onTextContent(this, text); | ||
} | ||
} | ||
get parentElement() { | ||
const { parentNode } = this; | ||
if (parentNode && parentNode.nodeType === NODE_TYPE_ELEMENT) { | ||
return parentNode; | ||
} | ||
return null; | ||
} | ||
get previousSibling() { | ||
return sibling(this, 'p'); | ||
} | ||
appendChild(newChild) { | ||
return this.insertBefore(newChild, null); | ||
} | ||
cloneNode(deep) { | ||
const cloned = extend(Object.create(Object.getPrototypeOf(this)), this); | ||
const { attributes } = cloned; | ||
if (attributes) { | ||
cloned.attributes = extend({}, attributes); | ||
} | ||
if (deep) { | ||
cloned.childNodes = cloned.childNodes.map((childNode) => childNode.cloneNode(true)); | ||
} | ||
return cloned; | ||
} | ||
insertBefore(newChild, refChild) { | ||
removeNode(newChild); | ||
newChild.pageNode = this.pageNode; | ||
newChild.parentNode = this; | ||
checkNodeId(newChild); | ||
const { childNodes } = this; | ||
let index; | ||
if (refChild) { | ||
index = childNodes.indexOf(refChild); | ||
if (index === -1) { | ||
throw new DOMException(`Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.`); | ||
} | ||
childNodes.splice(index, 0, newChild); | ||
} | ||
else { | ||
index = childNodes.length; | ||
childNodes.push(newChild); | ||
} | ||
return this.pageNode | ||
? this.pageNode.onInsertBefore(this, newChild, index) | ||
: newChild; | ||
} | ||
removeChild(oldChild) { | ||
const { childNodes } = this; | ||
const index = childNodes.indexOf(oldChild); | ||
if (index === -1) { | ||
throw new DOMException(`Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.`); | ||
} | ||
oldChild.parentNode = null; | ||
childNodes.splice(index, 1); | ||
return this.pageNode | ||
? this.pageNode.onRemoveChild(this, oldChild) | ||
: oldChild; | ||
} | ||
} | ||
class UniBaseNode extends UniNode { | ||
constructor(nodeType, nodeName, container) { | ||
super(nodeType, nodeName, container); | ||
this.attributes = Object.create(null); | ||
this._html = null; | ||
this.style = proxyStyle(new UniCSSStyleDeclaration()); | ||
} | ||
get className() { | ||
return (this.attributes['class'] || ''); | ||
} | ||
set className(val) { | ||
this.setAttribute('class', val); | ||
} | ||
get innerHTML() { | ||
return ''; | ||
} | ||
set innerHTML(html) { | ||
this._html = html; | ||
} | ||
addEventListener(type, listener, options) { | ||
super.addEventListener(type, listener, options); | ||
const normalized = normalizeEventType(type); | ||
if (!this.attributes[normalized]) { | ||
this.setAttribute(normalized, 1); | ||
} | ||
} | ||
removeEventListener(type, callback, options) { | ||
super.removeEventListener(type, callback, options); | ||
const normalized = normalizeEventType(type); | ||
if (this.attributes[normalized]) { | ||
this.removeAttribute(normalized); | ||
} | ||
} | ||
getAttribute(qualifiedName) { | ||
return this.attributes[encodeAttr(qualifiedName)]; | ||
} | ||
removeAttribute(qualifiedName) { | ||
qualifiedName = encodeAttr(qualifiedName); | ||
delete this.attributes[qualifiedName]; | ||
if (this.pageNode) { | ||
this.pageNode.onRemoveAttribute(this, qualifiedName); | ||
} | ||
} | ||
setAttribute(qualifiedName, value) { | ||
qualifiedName = encodeAttr(qualifiedName); | ||
this.attributes[qualifiedName] = value; | ||
if (this.pageNode) { | ||
this.pageNode.onSetAttribute(this, qualifiedName, value); | ||
} | ||
} | ||
toJSON(opts = {}) { | ||
const res = { | ||
a: this.attributes, | ||
s: this.style.toJSON(), | ||
}; | ||
if (!opts.attr) { | ||
res.i = this.nodeId; | ||
res.n = encodeTag(this.nodeName); | ||
} | ||
if (this._text !== null) { | ||
res.t = this._text; | ||
} | ||
return res; | ||
} | ||
} | ||
class UniCommentNode extends UniNode { | ||
constructor(text, container) { | ||
super(NODE_TYPE_COMMENT, '#comment', container); | ||
this._text = text; | ||
} | ||
toJSON(opts = {}) { | ||
return opts.attr | ||
? { t: this._text } | ||
: { | ||
i: this.nodeId, | ||
t: this._text, | ||
}; | ||
} | ||
} | ||
class UniElement extends UniBaseNode { | ||
constructor(nodeName, container) { | ||
super(NODE_TYPE_ELEMENT, nodeName.toUpperCase(), container); | ||
this.tagName = this.nodeName; | ||
} | ||
} | ||
class UniInputElement extends UniElement { | ||
get value() { | ||
return this.getAttribute('value'); | ||
} | ||
set value(val) { | ||
this.setAttribute('value', val); | ||
} | ||
} | ||
class UniTextAreaElement extends UniInputElement { | ||
} | ||
class UniTextNode extends UniBaseNode { | ||
constructor(text, container) { | ||
super(NODE_TYPE_TEXT, '#text', container); | ||
this._text = text; | ||
} | ||
get nodeValue() { | ||
return this._text || ''; | ||
} | ||
set nodeValue(text) { | ||
this._text = text; | ||
if (this.pageNode) { | ||
this.pageNode.onNodeValue(this, text); | ||
} | ||
} | ||
} | ||
function getLen(str = '') { | ||
@@ -393,2 +891,2 @@ return ('' + str).replace(/[^\x00-\xff]/g, '**').length; | ||
export { BUILT_IN_TAGS, COMPONENT_NAME_PREFIX, COMPONENT_PREFIX, COMPONENT_SELECTOR_PREFIX, NAVBAR_HEIGHT, 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, addFont, callOptions, createRpx2Unit, debounce, decode, decodedQuery, defaultRpx2Unit, 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, 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 }; |
{ | ||
"name": "@dcloudio/uni-shared", | ||
"version": "3.0.0-alpha-3000020210528002", | ||
"version": "3.0.0-alpha-3000020210611002", | ||
"description": "@dcloudio/uni-shared", | ||
@@ -21,3 +21,3 @@ "main": "./dist/uni-shared.cjs.js", | ||
}, | ||
"gitHead": "38f5411fbc1238e0fa41a911a7f06e22ee57541a" | ||
"gitHead": "eaf82397053bd73b217d5ca769c9d6a90a62695b" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74478
2046