-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathutils.js
More file actions
144 lines (123 loc) · 3.53 KB
/
Copy pathutils.js
File metadata and controls
144 lines (123 loc) · 3.53 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
import * as React from 'react';
const noBooleanTransformName = 'reactive-elements-no-boolean-transform';
function getAllProperties(obj) {
const props = {};
while (obj && obj !== React.Component.prototype && obj !== Object.prototype) {
var propNames = Object.getOwnPropertyNames(obj);
for (var i = 0; i < propNames.length; i++) {
props[propNames[i]] = null;
}
obj = Object.getPrototypeOf(obj);
}
delete props.constructor;
return Object.keys(props);
}
function extend(extensible, extending) {
const props = getAllProperties(extending);
for (var i = 0; i < props.length; i++) {
var prop = props[i];
if (!(prop in extensible)) {
var val = extending[prop];
extensible[prop] = val;
}
}
}
function elementHasNoBooleanTransformAttribute(el) {
let foundAttribute = false;
for (var i = 0; i < el.attributes.length; i++) {
var attribute = el.attributes[i];
if (attribute.name === noBooleanTransformName) {
foundAttribute = true;
break;
}
}
return foundAttribute;
}
function getProps(el) {
const props = {};
const noBooleanTransforms = elementHasNoBooleanTransformAttribute(el);
for (var i = 0; i < el.attributes.length; i++) {
var attribute = el.attributes[i];
if (attribute.name === noBooleanTransformName) continue;
var name = attributeNameToPropertyName(attribute.name);
props[name] = parseAttributeValue(attribute.value, {
noBooleanTransforms: noBooleanTransforms,
});
}
props.container = el;
return props;
}
function getterSetter(
variableParent,
variableName,
getterFunction,
setterFunction
) {
if (Object.defineProperty) {
Object.defineProperty(variableParent, variableName, {
get: getterFunction,
set: setterFunction,
});
} else if (document.__defineGetter__) {
variableParent.__defineGetter__(variableName, getterFunction);
variableParent.__defineSetter__(variableName, setterFunction);
}
variableParent['get' + variableName] = getterFunction;
variableParent['set' + variableName] = setterFunction;
}
function attributeNameToPropertyName(attributeName) {
return attributeName
.replace(/^(x|data)[-_:]/i, '')
.replace(/[-_:](.)/g, function(x, chr) {
return chr.toUpperCase();
});
}
function parseAttributeValue(value, transformOptions) {
if (value == undefined) {
return null;
}
if (!transformOptions) {
transformOptions = {};
}
// Support attribute values with newlines
value = value.replace(/[\n\r]/g, '');
var pointerRegexp = /^{.*?}$/i,
jsonRegexp = /^{{2}.*}{2}$/,
jsonArrayRegexp = /^{\[.*\]}$/;
var pointerMatches = value.match(pointerRegexp),
jsonMatches = value.match(jsonRegexp) || value.match(jsonArrayRegexp);
if (jsonMatches) {
value = JSON.parse(jsonMatches[0].replace(/^{|}$/g, ''));
} else if (pointerMatches) {
value = eval(pointerMatches[0].replace(/[{}]/g, ''));
} else if (
(value === 'true' || value === 'false') &&
!transformOptions.noBooleanTransforms
) {
// convert the value to its actual boolean
value = value === 'true';
}
return value;
}
function getChildren(el) {
const fragment = document.createDocumentFragment();
while (el.childNodes.length) {
fragment.appendChild(el.childNodes[0]);
}
return fragment;
}
function shallowCopy(a, b) {
for (const key in b) a[key] = b[key];
return a;
}
export {
getAllProperties,
extend,
elementHasNoBooleanTransformAttribute,
getProps,
getterSetter,
attributeNameToPropertyName,
parseAttributeValue,
getChildren,
shallowCopy,
};