-
-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathutil.js
More file actions
220 lines (201 loc) · 5.23 KB
/
Copy pathutil.js
File metadata and controls
220 lines (201 loc) · 5.23 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { DIRTY, BITS } from './constants';
export const VOID_ELEMENTS =
/^(?:area|base|br|col|embed|hr|img|input|link|meta|param|source|track|wbr)$/;
// oxlint-disable-next-line no-control-regex
export const UNSAFE_NAME = /[\s\n\\/='"\0<>]/;
export const NAMESPACE_REPLACE_REGEX = /^(xlink|xmlns|xml)([A-Z])/;
export const HTML_LOWER_CASE =
/^(?:accessK|auto[A-Z]|cell|ch|col|cont|cross|dateT|encT|form[A-Z]|frame|hrefL|inputM|maxL|minL|noV|playsI|popoverT|readO|rowS|src[A-Z]|tabI|useM|item[A-Z])/;
export const SVG_CAMEL_CASE =
/^ac|^ali|arabic|basel|cap|clipPath$|clipRule$|color|dominant|enable|fill|flood|font|glyph[^R]|horiz|image|letter|lighting|marker[^WUH]|overline|panose|pointe|paint|rendering|shape|stop|strikethrough|stroke|text[^L]|transform|underline|unicode|units|^v[^i]|^w|^xH/;
// Boolean DOM properties that translate to enumerated ('true'/'false') attributes
export const HTML_ENUMERATED = new Set(['draggable', 'spellcheck']);
export const COMPONENT_DIRTY_BIT = 1 << 3;
export function setDirty(component) {
if (component[BITS] !== undefined) {
component[BITS] |= COMPONENT_DIRTY_BIT;
} else {
component[DIRTY] = true;
}
}
export function unsetDirty(component) {
if (component.__g !== undefined) {
component.__g &= ~COMPONENT_DIRTY_BIT;
} else {
component[DIRTY] = false;
}
}
export function isDirty(component) {
if (component.__g !== undefined) {
return (component.__g & COMPONENT_DIRTY_BIT) !== 0;
}
return component[DIRTY] === true;
}
/** @param {string} str */
export function encodeEntities(str) {
// Skip all work for strings with no entities needing encoding:
let i = 0;
if (str.length < 8) {
for (; i < str.length; i++) {
const char = str.charCodeAt(i);
if (char === 34 || char === 38 || char === 60) break;
}
if (i === str.length) return str;
} else if (
str.indexOf('"') === -1 &&
str.indexOf('&') === -1 &&
str.indexOf('<') === -1
) {
return str;
}
let last = 0,
out = '',
ch = '';
// Seek forward in str until the next entity char:
for (; i < str.length; i++) {
switch (str.charCodeAt(i)) {
case 34:
ch = '"';
break;
case 38:
ch = '&';
break;
case 60:
ch = '<';
break;
default:
continue;
}
// Append skipped/buffered characters and the encoded entity:
if (i !== last) out = out + str.slice(last, i);
out = out + ch;
// Start the next seek/buffer after the entity's offset:
last = i + 1;
}
if (i !== last) out = out + str.slice(last, i);
return out;
}
export let indent = (s, char) =>
String(s).replace(/(\n+)/g, '$1' + (char || '\t'));
export let isLargeString = (s, length, ignoreLines) =>
String(s).length > (length || 40) ||
(!ignoreLines && String(s).indexOf('\n') !== -1) ||
String(s).indexOf('<') !== -1;
const JS_TO_CSS = {};
const IS_NON_DIMENSIONAL = new Set([
'animation-iteration-count',
'border-image-outset',
'border-image-slice',
'border-image-width',
'box-flex',
'box-flex-group',
'box-ordinal-group',
'column-count',
'fill-opacity',
'flex',
'flex-grow',
'flex-negative',
'flex-order',
'flex-positive',
'flex-shrink',
'flood-opacity',
'font-weight',
'grid-column',
'grid-row',
'line-clamp',
'line-height',
'opacity',
'order',
'orphans',
'stop-opacity',
'stroke-dasharray',
'stroke-dashoffset',
'stroke-miterlimit',
'stroke-opacity',
'stroke-width',
'tab-size',
'widows',
'z-index',
'zoom'
]);
const CSS_REGEX = /[A-Z]/g;
// Convert an Object style to a CSSText string
export function styleObjToCss(s) {
let str = '';
for (let prop in s) {
let val = s[prop];
if (val != null && val !== '') {
const name =
prop[0] == '-'
? prop
: JS_TO_CSS[prop] ||
(JS_TO_CSS[prop] = prop.replace(CSS_REGEX, '-$&').toLowerCase());
let suffix = ';';
if (
typeof val === 'number' &&
// Exclude custom-attributes
!name.startsWith('--') &&
!IS_NON_DIMENSIONAL.has(name)
) {
suffix = 'px;';
}
str = str + name + ':' + val + suffix;
}
}
return str || undefined;
}
/**
* Get flattened children from the children prop
* @param {Array} accumulator
* @param {any} children A `props.children` opaque object.
* @returns {Array} accumulator
* @private
*/
export function getChildren(accumulator, children) {
if (Array.isArray(children)) {
children.reduce(getChildren, accumulator);
} else if (children != null && children !== false) {
accumulator.push(children);
}
return accumulator;
}
function markAsDirty() {
this.__d = true;
}
export function createComponent(vnode, context) {
return {
__v: vnode,
context,
props: vnode.props,
// silently drop state updates
setState: markAsDirty,
forceUpdate: markAsDirty,
__d: true,
// hooks
// oxlint-disable-next-line no-new-array
__h: new Array(0)
};
}
// Necessary for createContext api. Setting this property will pass
// the context value as `this.context` just for this component.
export function getContext(nodeName, context) {
let cxType = nodeName.contextType;
let provider = cxType && context[cxType.__c];
return cxType != null
? provider
? provider.props.value
: cxType.__
: context;
}
/**
* @template T
*/
export class Deferred {
constructor() {
/** @type {Promise<T>} */
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}