-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
75 lines (65 loc) · 2.12 KB
/
util.js
File metadata and controls
75 lines (65 loc) · 2.12 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
// DOM properties that should NOT have "px" added when numeric
export const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|ows|mnc|ntw|ine[ch]|zoo|^ord/i;
export const objectKeys = Object.keys || (obj => {
let keys = [];
for (let i in obj) if (obj.hasOwnProperty(i)) keys.push(i);
return keys;
});
export let encodeEntities = s => String(s)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"');
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 = {};
// 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) {
if (str) str += ' ';
// str += jsToCss(prop);
str += JS_TO_CSS[prop] || (JS_TO_CSS[prop] = prop.replace(/([A-Z])/g,'-$1').toLowerCase());
str += ': ';
str += val;
if (typeof val==='number' && IS_NON_DIMENSIONAL.test(prop)===false) {
str += 'px';
}
str += ';';
}
}
return str || undefined;
}
/**
* Copy all properties from `props` onto `obj`.
* @param {object} obj Object onto which properties should be copied.
* @param {object} props Object from which to copy properties.
* @returns {object}
* @private
*/
export function assign(obj, props) {
for (let i in props) obj[i] = props[i];
return obj;
}
/**
* Reconstruct Component-style `props` from a VNode.
* Ensures default/fallback values from `defaultProps`:
* Own-properties of `defaultProps` not present in `vnode.attributes` are added.
* @param {import('preact').VNode} vnode The VNode to get props for
* @returns {object} The props to use for this VNode
*/
export function getNodeProps(vnode) {
let props = assign({}, vnode.attributes);
props.children = vnode.children;
let defaultProps = vnode.nodeName.defaultProps;
if (defaultProps!==undefined) {
for (let i in defaultProps) {
if (props[i]===undefined) {
props[i] = defaultProps[i];
}
}
}
return props;
}