forked from vuejs/vue-web-component-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
171 lines (150 loc) · 4.15 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const camelizeRE = /-(\w)/g;
export const camelize = (str) => {
return str.replace(camelizeRE, (_, c) => (c ? c.toUpperCase() : ''));
};
const hyphenateRE = /\B([A-Z])/g;
export const hyphenate = (str) => {
return str.replace(hyphenateRE, '-$1').toLowerCase();
};
/**
* Checks if node is an element.
* @param {Node} node
* @returns {node is HTMLElement}
*/
export function isElement(node) {
return node.nodeType === Node.ELEMENT_NODE;
}
/**
* Gets the attribute name for the slotted
* children.
* @param {import('vue').default} wrapper
*/
export const getSlottedId = (wrapper) => {
return getScope(wrapper) + '-slot';
};
/**
* Gets the attribute name for the host.
* @param {import('vue').default} wrapper
*/
export const getHostId = (wrapper) => {
return getScope(wrapper) + '-host';
};
/**
* Gets the scope ID from the wrapper
* @param {import('vue').default} wrapper
*/
export const getScope = (wrapper) => {
if (wrapper && wrapper.$children && wrapper.$children[0]) {
return wrapper.$children[0].$options._scopeId || 'unknown';
}
return 'unknown';
};
export function getInitialProps(propsList, currProps) {
const res = {};
propsList.forEach((key) => {
res[key] = currProps[key] || undefined;
});
return res;
}
export function injectHook(options, key, hook) {
options[key] = [].concat(options[key] || []);
options[key].unshift(hook);
}
export function callHooks(vm, hook) {
if (vm) {
const hooks = vm.$options[hook] || [];
hooks.forEach((hook) => {
hook.call(vm);
});
}
}
export function createCustomEvent(name, args) {
return new CustomEvent(name, {
bubbles: true,
cancelable: true,
composed: true,
detail: args,
});
}
export function isIgnoredAttribute(attr) {
return (
[
'class',
'style',
'id',
'key',
'ref',
'slot',
'slot-scope',
'is',
].indexOf(attr) !== -1 || attr.indexOf('data-v-') !== -1
);
}
const isBoolean = (val) => /function Boolean/.test(String(val));
const isNumber = (val) => /function Number/.test(String(val));
export function convertAttributeValue(value, name, { type } = {}) {
if (isBoolean(type)) {
if (value === 'true' || value === 'false') {
return value === 'true';
}
if (value === '' || value === name) {
return true;
}
return value != null;
} else if (isNumber(type)) {
const parsed = parseFloat(value, 10);
return isNaN(parsed) ? value : parsed;
} else {
return value;
}
}
export function isShadyDom() {
return !!window.ShadyDOM;
}
export function createSlot(h, scopeId, name) {
const vnode = { attrs: { [scopeId]: '' } };
if (name) {
vnode.slot = name;
vnode.attrs.name = name;
}
let slot = h('slot', vnode);
if (isShadyDom()) {
slot = h('shady-slot', { attrs: { [scopeId]: '' }}, [slot]);
}
return slot;
}
export function toVNodes(h, children, scopeId) {
let unnamed = false;
const named = {};
for (let i = 0, l = children.length; i < l; i++) {
const childSlot =
children[i].getAttribute && children[i].getAttribute('slot');
if (childSlot && !named[childSlot]) {
named[childSlot] = createSlot(h, scopeId, childSlot);
} else if (!childSlot && !unnamed) {
unnamed = createSlot(h, scopeId);
}
}
const res = Array.from(Object.values(named));
if (unnamed) {
res.push(unnamed);
}
return res;
}
export function getNodeAttributes(node, ignoreAttributes, ignoreReserved) {
const res = {};
for (let i = 0, l = node.attributes.length; i < l; i++) {
const attr = node.attributes[i];
const name = attr.name || attr.nodeName;
const value = attr.value || attr.nodeValue;
if (
ignoreAttributes &&
ignoreAttributes.indexOf(name) !== -1 &&
(ignoreReserved && isIgnoredAttribute(name))
) {
continue;
}
res[name] = value;
}
return res;
}