@cocreate/utils 1.10.23 → 1.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/package.json +1 -1
- package/src/{index.old.js → archive.js} +29 -0
- package/src/index.js +14 -267
- package/src/utils.js +283 -0
- package/src/encodedecode.js +0 -111
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
# [1.11.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.10.23...v1.11.0) (2022-11-25)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* add valueTypes to archive ([6fd51ff](https://github.com/CoCreate-app/CoCreate-utils/commit/6fd51ffe880699b5303be7f2452a363cc3737716))
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* utils can be used in browser or server ([f8ad791](https://github.com/CoCreate-app/CoCreate-utils/commit/f8ad7913e5d0c658ca3ef58bda09082dde8b67ef))
|
|
12
|
+
|
|
1
13
|
## [1.10.23](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.10.22...v1.10.23) (2022-11-24)
|
|
2
14
|
|
|
3
15
|
|
package/package.json
CHANGED
|
@@ -443,6 +443,35 @@ async function complexSelector(comSelector, callback) {
|
|
|
443
443
|
return objectData;
|
|
444
444
|
}
|
|
445
445
|
|
|
446
|
+
function valueTypes(data) {
|
|
447
|
+
let object = {}
|
|
448
|
+
if ( typeof data === 'object' ) {
|
|
449
|
+
// update['$set'] = {}
|
|
450
|
+
for (let [key, value] of Object.entries(data)) {
|
|
451
|
+
let val;
|
|
452
|
+
let valueType = typeof value;
|
|
453
|
+
switch(valueType) {
|
|
454
|
+
case 'string':
|
|
455
|
+
val = value
|
|
456
|
+
break;
|
|
457
|
+
case 'number':
|
|
458
|
+
val = Number(value)
|
|
459
|
+
break;
|
|
460
|
+
case 'object':
|
|
461
|
+
if (Array.isArray(value))
|
|
462
|
+
val = new Array(...value)
|
|
463
|
+
else
|
|
464
|
+
val = new Object(value)
|
|
465
|
+
break;
|
|
466
|
+
default:
|
|
467
|
+
val = value
|
|
468
|
+
}
|
|
469
|
+
object[key] = val
|
|
470
|
+
}
|
|
471
|
+
return object;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
446
475
|
|
|
447
476
|
export default {
|
|
448
477
|
getElementPath,
|
package/src/index.js
CHANGED
|
@@ -1,267 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
function
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
frameDocuments.set(frameDocument, '')
|
|
16
|
-
frameDocument.addEventListener('click', e => {
|
|
17
|
-
frameDocument.clickedElement = e.target;
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function dotNotationToObject(data, obj = {}) {
|
|
24
|
-
try {
|
|
25
|
-
for (const [key, value] of Object.entries(data)) {
|
|
26
|
-
let newObject = obj
|
|
27
|
-
let oldObject = new Object(obj)
|
|
28
|
-
let keys = key.split('.');
|
|
29
|
-
let length = keys.length - 1
|
|
30
|
-
for (let i = 0; i < keys.length; i++) {
|
|
31
|
-
if (/\[([0-9]*)\]/g.test(keys[i])){
|
|
32
|
-
let [k, index] = keys[i].split('[');
|
|
33
|
-
index = index.slice(0, -1)
|
|
34
|
-
if (length == i){
|
|
35
|
-
newObject[k] = oldObject[k] || [];
|
|
36
|
-
newObject[k][index] = value;
|
|
37
|
-
}
|
|
38
|
-
else {
|
|
39
|
-
newObject[k] = oldObject[k] || [];
|
|
40
|
-
newObject[k][index] = oldObject[k][index] || {};
|
|
41
|
-
}
|
|
42
|
-
newObject = newObject[k][index]
|
|
43
|
-
oldObject = oldObject[k][index]
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
if (length == i)
|
|
47
|
-
newObject[keys[i]] = value;
|
|
48
|
-
else
|
|
49
|
-
newObject[keys[i]] = oldObject[keys[i]] || {};
|
|
50
|
-
|
|
51
|
-
newObject = newObject[keys[i]]
|
|
52
|
-
oldObject = oldObject[keys[i]]
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return obj
|
|
57
|
-
} catch (error) {
|
|
58
|
-
console.log("Error converting dot notation to object", error);
|
|
59
|
-
return false;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function parseTextToHtml(text) {
|
|
64
|
-
let doc = new DOMParser().parseFromString(text, "text/html");
|
|
65
|
-
if (doc.head.children[0]) return doc.head.children[0];
|
|
66
|
-
else return doc.body.children[0];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function cssPath(node, container) {
|
|
70
|
-
let pathSplits = [];
|
|
71
|
-
do {
|
|
72
|
-
if (!node || !node.tagName) return false;
|
|
73
|
-
let pathSplit = node.tagName.toLowerCase();
|
|
74
|
-
// if (node.tagName == "DOM-PARSER" || node.hasAttribute('contenteditable')){
|
|
75
|
-
// pathSplit = "[contenteditable]";
|
|
76
|
-
// node = '';
|
|
77
|
-
// }
|
|
78
|
-
if (node.id){
|
|
79
|
-
pathSplit += "#" + node.id;
|
|
80
|
-
node = '';
|
|
81
|
-
}
|
|
82
|
-
else {
|
|
83
|
-
// let eid = node.getAttribute('eid');
|
|
84
|
-
// if (/{{\s*([\w\W]+)\s*}}/g.test(eid)) {
|
|
85
|
-
// eid = false;
|
|
86
|
-
// }
|
|
87
|
-
// if (eid) {
|
|
88
|
-
// pathSplit += `[eid="${eid}"]`;
|
|
89
|
-
// node = '';
|
|
90
|
-
// }
|
|
91
|
-
// else {
|
|
92
|
-
// if (node.classList.length) {
|
|
93
|
-
// node.classList.forEach((item) => {
|
|
94
|
-
// if (item.indexOf(":") === -1) pathSplit += "." + item;
|
|
95
|
-
// });
|
|
96
|
-
// }
|
|
97
|
-
|
|
98
|
-
if (node.parentNode && node.parentNode.children.length > 1) {
|
|
99
|
-
// ToDo: improve array logic so ignores javascript generated html??
|
|
100
|
-
let children = []
|
|
101
|
-
for (let child of node.parentNode.children){
|
|
102
|
-
// if (!child.matches('.mirror'))
|
|
103
|
-
// children.push(child);
|
|
104
|
-
if (child.tagName == node.tagName)
|
|
105
|
-
children.push(child);
|
|
106
|
-
}
|
|
107
|
-
let index = Array.prototype.indexOf.call(
|
|
108
|
-
children,
|
|
109
|
-
node
|
|
110
|
-
);
|
|
111
|
-
// if (children.length > 1)
|
|
112
|
-
// pathSplit += `:nth-child(${index + 1})`;
|
|
113
|
-
pathSplit += `:nth-of-type(${index + 1})`;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// pathSplits.unshift(pathSplit);
|
|
117
|
-
node = node.parentNode;
|
|
118
|
-
if (node == null || node.tagName == "HTML" || node.tagName == "DOM-PARSER" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
|
|
119
|
-
node = '';
|
|
120
|
-
}
|
|
121
|
-
// }
|
|
122
|
-
pathSplits.unshift(pathSplit);
|
|
123
|
-
} while (node);
|
|
124
|
-
let path = pathSplits.join(" > ")
|
|
125
|
-
if (path && path.includes('<')) {
|
|
126
|
-
let index = path.lastIndexOf(' >')
|
|
127
|
-
if (index != -1)
|
|
128
|
-
path = path.slice(0, index)
|
|
129
|
-
else{
|
|
130
|
-
index = path.lastIndexOf('<')
|
|
131
|
-
path = path.slice(0, index)
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return path;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export function domParser(str) {
|
|
139
|
-
try {
|
|
140
|
-
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
141
|
-
} catch (e){
|
|
142
|
-
// console.log(e, 'find position: can not find the main tag');
|
|
143
|
-
}
|
|
144
|
-
let doc;
|
|
145
|
-
switch (mainTag) {
|
|
146
|
-
case 'html':
|
|
147
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
148
|
-
return doc.documentElement;
|
|
149
|
-
case 'body':
|
|
150
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
151
|
-
return doc.body;
|
|
152
|
-
case 'head':
|
|
153
|
-
doc = new DOMParser().parseFromString(str, "text/html");
|
|
154
|
-
return doc.head;
|
|
155
|
-
|
|
156
|
-
default:
|
|
157
|
-
let con = document.createElement('dom-parser');
|
|
158
|
-
con.innerHTML = str;
|
|
159
|
-
return con;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function queryDocumentSelectorAll(selector) {
|
|
164
|
-
let elements = [];
|
|
165
|
-
|
|
166
|
-
if (selector) {
|
|
167
|
-
let selectors = [selector];
|
|
168
|
-
if (selector.indexOf(',') !== -1){
|
|
169
|
-
selectors = selector.split(',');
|
|
170
|
-
}
|
|
171
|
-
for (let selector of selectors){
|
|
172
|
-
let els;
|
|
173
|
-
if (selector.indexOf(';') !== -1) {
|
|
174
|
-
let targetDocument;
|
|
175
|
-
let [documentSelector, targetSelector] = selector.split(';');
|
|
176
|
-
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
177
|
-
targetDocument = window.parent.document;
|
|
178
|
-
else {
|
|
179
|
-
let frame = document.querySelector(documentSelector);
|
|
180
|
-
if (frame)
|
|
181
|
-
targetDocument = frame.contentDocument;
|
|
182
|
-
}
|
|
183
|
-
if (targetDocument){
|
|
184
|
-
if (targetSelector)
|
|
185
|
-
els = targetDocument.querySelectorAll(targetSelector);
|
|
186
|
-
else
|
|
187
|
-
if (targetDocument.clickedElement)
|
|
188
|
-
els = [targetDocument.clickedElement];
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
else
|
|
192
|
-
els = document.querySelectorAll(selector);
|
|
193
|
-
if (els){
|
|
194
|
-
els = Array.prototype.slice.call(els);
|
|
195
|
-
elements = elements.concat(els);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
return elements;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
export function queryDocumentSelector(selector) {
|
|
203
|
-
if (selector) {
|
|
204
|
-
let selectors = [selector];
|
|
205
|
-
if (selector.indexOf(',') !== -1){
|
|
206
|
-
selectors = selector.split(',');
|
|
207
|
-
}
|
|
208
|
-
for (let selector of selectors){
|
|
209
|
-
let el;
|
|
210
|
-
if (selector.indexOf(';') !== -1) {
|
|
211
|
-
let targetDocument;
|
|
212
|
-
let [documentSelector, targetSelector] = selector.split(';');
|
|
213
|
-
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
214
|
-
targetDocument = window.parent.document;
|
|
215
|
-
else {
|
|
216
|
-
let frame = document.querySelector(documentSelector);
|
|
217
|
-
if (frame)
|
|
218
|
-
targetDocument = frame.contentDocument;
|
|
219
|
-
}
|
|
220
|
-
if (targetDocument){
|
|
221
|
-
if (targetSelector)
|
|
222
|
-
el = targetDocument.querySelector(targetSelector);
|
|
223
|
-
else
|
|
224
|
-
if (targetDocument.clickedElement)
|
|
225
|
-
el = [targetDocument.clickedElement];
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
else
|
|
229
|
-
el = document.querySelector(selector);
|
|
230
|
-
if (el)
|
|
231
|
-
return el
|
|
232
|
-
}
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// export function computeStyles(el, properties) {
|
|
238
|
-
// let computed = window.getComputedStyle(el);
|
|
239
|
-
// let result = {};
|
|
240
|
-
// properties.forEach((property) => {
|
|
241
|
-
// result[property] = parseInt(computed[property]);
|
|
242
|
-
// });
|
|
243
|
-
// return result;
|
|
244
|
-
// }
|
|
245
|
-
|
|
246
|
-
// function checkParent(element, selectors){
|
|
247
|
-
// let parentElement;
|
|
248
|
-
// do {
|
|
249
|
-
// parentElement = element.parentElement.closest(selectors);
|
|
250
|
-
// if (parentElement) {
|
|
251
|
-
// element = parentElement;
|
|
252
|
-
// } else {
|
|
253
|
-
// return element;
|
|
254
|
-
// }
|
|
255
|
-
// } while (parentElement);
|
|
256
|
-
// }
|
|
257
|
-
|
|
258
|
-
clickedElement();
|
|
259
|
-
|
|
260
|
-
export default {
|
|
261
|
-
parseTextToHtml,
|
|
262
|
-
dotNotationToObject,
|
|
263
|
-
cssPath,
|
|
264
|
-
domParser,
|
|
265
|
-
queryDocumentSelector,
|
|
266
|
-
queryDocumentSelectorAll
|
|
267
|
-
};
|
|
1
|
+
(function (root, factory) {
|
|
2
|
+
if (typeof define === 'function' && define.amd) {
|
|
3
|
+
define(["./utils"], function(CoCreateUtils) {
|
|
4
|
+
return factory(CoCreateUtils)
|
|
5
|
+
});
|
|
6
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
+
const CoCreateUtils = require("./utils.js")
|
|
8
|
+
module.exports = factory(CoCreateUtils);
|
|
9
|
+
} else {
|
|
10
|
+
root.returnExports = factory(root["./utils.js"]);
|
|
11
|
+
}
|
|
12
|
+
}(typeof self !== 'undefined' ? self : this, function (CoCreateUtils) {
|
|
13
|
+
return CoCreateUtils;
|
|
14
|
+
}));
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
(function (root, factory) {
|
|
2
|
+
if (typeof define === 'function' && define.amd) {
|
|
3
|
+
define([], function() {
|
|
4
|
+
return factory(true)
|
|
5
|
+
});
|
|
6
|
+
} else if (typeof module === 'object' && module.exports) {
|
|
7
|
+
module.exports = factory(false);
|
|
8
|
+
} else {
|
|
9
|
+
root.returnExports = factory(true);
|
|
10
|
+
}
|
|
11
|
+
}(typeof self !== 'undefined' ? self : this, function (isBrowser) {
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/*globals DOMParser*/
|
|
15
|
+
function clickedElement() {
|
|
16
|
+
document.addEventListener('click', e => {
|
|
17
|
+
document.clickedElement = e.target;
|
|
18
|
+
});
|
|
19
|
+
let frameDocuments = window.top.frameDocuments;
|
|
20
|
+
if (!frameDocuments){
|
|
21
|
+
window.top.frameDocuments = new Map();
|
|
22
|
+
frameDocuments = window.top.frameDocuments;
|
|
23
|
+
}
|
|
24
|
+
let frames = document.querySelectorAll('iframe');
|
|
25
|
+
for (let frame of frames){
|
|
26
|
+
let frameDocument = frame.contentDocument;
|
|
27
|
+
if (!frameDocuments.has(frameDocument)){
|
|
28
|
+
frameDocuments.set(frameDocument, '')
|
|
29
|
+
frameDocument.addEventListener('click', e => {
|
|
30
|
+
frameDocument.clickedElement = e.target;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function dotNotationToObject(data, obj = {}) {
|
|
37
|
+
try {
|
|
38
|
+
for (const [key, value] of Object.entries(data)) {
|
|
39
|
+
let newObject = obj
|
|
40
|
+
let oldObject = new Object(obj)
|
|
41
|
+
let keys = key.split('.');
|
|
42
|
+
let length = keys.length - 1
|
|
43
|
+
for (let i = 0; i < keys.length; i++) {
|
|
44
|
+
if (/\[([0-9]*)\]/g.test(keys[i])){
|
|
45
|
+
let [k, index] = keys[i].split('[');
|
|
46
|
+
index = index.slice(0, -1)
|
|
47
|
+
if (length == i){
|
|
48
|
+
newObject[k] = oldObject[k] || [];
|
|
49
|
+
newObject[k][index] = value;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
newObject[k] = oldObject[k] || [];
|
|
53
|
+
newObject[k][index] = oldObject[k][index] || {};
|
|
54
|
+
}
|
|
55
|
+
newObject = newObject[k][index]
|
|
56
|
+
oldObject = oldObject[k][index]
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
if (length == i)
|
|
60
|
+
newObject[keys[i]] = value;
|
|
61
|
+
else
|
|
62
|
+
newObject[keys[i]] = oldObject[keys[i]] || {};
|
|
63
|
+
|
|
64
|
+
newObject = newObject[keys[i]]
|
|
65
|
+
oldObject = oldObject[keys[i]]
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return obj
|
|
70
|
+
} catch (error) {
|
|
71
|
+
console.log("Error converting dot notation to object", error);
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function parseTextToHtml(text) {
|
|
77
|
+
let doc = new DOMParser().parseFromString(text, "text/html");
|
|
78
|
+
if (doc.head.children[0]) return doc.head.children[0];
|
|
79
|
+
else return doc.body.children[0];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function cssPath(node, container) {
|
|
83
|
+
let pathSplits = [];
|
|
84
|
+
do {
|
|
85
|
+
if (!node || !node.tagName) return false;
|
|
86
|
+
let pathSplit = node.tagName.toLowerCase();
|
|
87
|
+
// if (node.tagName == "DOM-PARSER" || node.hasAttribute('contenteditable')){
|
|
88
|
+
// pathSplit = "[contenteditable]";
|
|
89
|
+
// node = '';
|
|
90
|
+
// }
|
|
91
|
+
if (node.id){
|
|
92
|
+
pathSplit += "#" + node.id;
|
|
93
|
+
node = '';
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// let eid = node.getAttribute('eid');
|
|
97
|
+
// if (/{{\s*([\w\W]+)\s*}}/g.test(eid)) {
|
|
98
|
+
// eid = false;
|
|
99
|
+
// }
|
|
100
|
+
// if (eid) {
|
|
101
|
+
// pathSplit += `[eid="${eid}"]`;
|
|
102
|
+
// node = '';
|
|
103
|
+
// }
|
|
104
|
+
// else {
|
|
105
|
+
// if (node.classList.length) {
|
|
106
|
+
// node.classList.forEach((item) => {
|
|
107
|
+
// if (item.indexOf(":") === -1) pathSplit += "." + item;
|
|
108
|
+
// });
|
|
109
|
+
// }
|
|
110
|
+
|
|
111
|
+
if (node.parentNode && node.parentNode.children.length > 1) {
|
|
112
|
+
// ToDo: improve array logic so ignores javascript generated html??
|
|
113
|
+
let children = []
|
|
114
|
+
for (let child of node.parentNode.children){
|
|
115
|
+
// if (!child.matches('.mirror'))
|
|
116
|
+
// children.push(child);
|
|
117
|
+
if (child.tagName == node.tagName)
|
|
118
|
+
children.push(child);
|
|
119
|
+
}
|
|
120
|
+
let index = Array.prototype.indexOf.call(
|
|
121
|
+
children,
|
|
122
|
+
node
|
|
123
|
+
);
|
|
124
|
+
// if (children.length > 1)
|
|
125
|
+
// pathSplit += `:nth-child(${index + 1})`;
|
|
126
|
+
pathSplit += `:nth-of-type(${index + 1})`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// pathSplits.unshift(pathSplit);
|
|
130
|
+
node = node.parentNode;
|
|
131
|
+
if (node == null || node.tagName == "HTML" || node.tagName == "DOM-PARSER" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
|
|
132
|
+
node = '';
|
|
133
|
+
}
|
|
134
|
+
// }
|
|
135
|
+
pathSplits.unshift(pathSplit);
|
|
136
|
+
} while (node);
|
|
137
|
+
let path = pathSplits.join(" > ")
|
|
138
|
+
if (path && path.includes('<')) {
|
|
139
|
+
let index = path.lastIndexOf(' >')
|
|
140
|
+
if (index != -1)
|
|
141
|
+
path = path.slice(0, index)
|
|
142
|
+
else{
|
|
143
|
+
index = path.lastIndexOf('<')
|
|
144
|
+
path = path.slice(0, index)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return path;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function domParser(str) {
|
|
152
|
+
try {
|
|
153
|
+
var mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
154
|
+
} catch (e){
|
|
155
|
+
// console.log(e, 'find position: can not find the main tag');
|
|
156
|
+
}
|
|
157
|
+
let doc;
|
|
158
|
+
switch (mainTag) {
|
|
159
|
+
case 'html':
|
|
160
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
161
|
+
return doc.documentElement;
|
|
162
|
+
case 'body':
|
|
163
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
164
|
+
return doc.body;
|
|
165
|
+
case 'head':
|
|
166
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
167
|
+
return doc.head;
|
|
168
|
+
|
|
169
|
+
default:
|
|
170
|
+
let con = document.createElement('dom-parser');
|
|
171
|
+
con.innerHTML = str;
|
|
172
|
+
return con;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function queryDocumentSelectorAll(selector) {
|
|
177
|
+
let elements = [];
|
|
178
|
+
|
|
179
|
+
if (selector) {
|
|
180
|
+
let selectors = [selector];
|
|
181
|
+
if (selector.indexOf(',') !== -1){
|
|
182
|
+
selectors = selector.split(',');
|
|
183
|
+
}
|
|
184
|
+
for (let selector of selectors){
|
|
185
|
+
let els;
|
|
186
|
+
if (selector.indexOf(';') !== -1) {
|
|
187
|
+
let targetDocument;
|
|
188
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
189
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
190
|
+
targetDocument = window.parent.document;
|
|
191
|
+
else {
|
|
192
|
+
let frame = document.querySelector(documentSelector);
|
|
193
|
+
if (frame)
|
|
194
|
+
targetDocument = frame.contentDocument;
|
|
195
|
+
}
|
|
196
|
+
if (targetDocument){
|
|
197
|
+
if (targetSelector)
|
|
198
|
+
els = targetDocument.querySelectorAll(targetSelector);
|
|
199
|
+
else
|
|
200
|
+
if (targetDocument.clickedElement)
|
|
201
|
+
els = [targetDocument.clickedElement];
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else
|
|
205
|
+
els = document.querySelectorAll(selector);
|
|
206
|
+
if (els){
|
|
207
|
+
els = Array.prototype.slice.call(els);
|
|
208
|
+
elements = elements.concat(els);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return elements;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
function queryDocumentSelector(selector) {
|
|
216
|
+
if (selector) {
|
|
217
|
+
let selectors = [selector];
|
|
218
|
+
if (selector.indexOf(',') !== -1){
|
|
219
|
+
selectors = selector.split(',');
|
|
220
|
+
}
|
|
221
|
+
for (let selector of selectors){
|
|
222
|
+
let el;
|
|
223
|
+
if (selector.indexOf(';') !== -1) {
|
|
224
|
+
let targetDocument;
|
|
225
|
+
let [documentSelector, targetSelector] = selector.split(';');
|
|
226
|
+
if (['parent', 'parentDocument'].includes(documentSelector))
|
|
227
|
+
targetDocument = window.parent.document;
|
|
228
|
+
else {
|
|
229
|
+
let frame = document.querySelector(documentSelector);
|
|
230
|
+
if (frame)
|
|
231
|
+
targetDocument = frame.contentDocument;
|
|
232
|
+
}
|
|
233
|
+
if (targetDocument){
|
|
234
|
+
if (targetSelector)
|
|
235
|
+
el = targetDocument.querySelector(targetSelector);
|
|
236
|
+
else
|
|
237
|
+
if (targetDocument.clickedElement)
|
|
238
|
+
el = [targetDocument.clickedElement];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
else
|
|
242
|
+
el = document.querySelector(selector);
|
|
243
|
+
if (el)
|
|
244
|
+
return el
|
|
245
|
+
}
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// function computeStyles(el, properties) {
|
|
251
|
+
// let computed = window.getComputedStyle(el);
|
|
252
|
+
// let result = {};
|
|
253
|
+
// properties.forEach((property) => {
|
|
254
|
+
// result[property] = parseInt(computed[property]);
|
|
255
|
+
// });
|
|
256
|
+
// return result;
|
|
257
|
+
// }
|
|
258
|
+
|
|
259
|
+
// function checkParent(element, selectors){
|
|
260
|
+
// let parentElement;
|
|
261
|
+
// do {
|
|
262
|
+
// parentElement = element.parentElement.closest(selectors);
|
|
263
|
+
// if (parentElement) {
|
|
264
|
+
// element = parentElement;
|
|
265
|
+
// } else {
|
|
266
|
+
// return element;
|
|
267
|
+
// }
|
|
268
|
+
// } while (parentElement);
|
|
269
|
+
// }
|
|
270
|
+
|
|
271
|
+
if (isBrowser)
|
|
272
|
+
clickedElement();
|
|
273
|
+
|
|
274
|
+
return {
|
|
275
|
+
parseTextToHtml,
|
|
276
|
+
dotNotationToObject,
|
|
277
|
+
cssPath,
|
|
278
|
+
domParser,
|
|
279
|
+
queryDocumentSelector,
|
|
280
|
+
queryDocumentSelectorAll
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
}));
|
package/src/encodedecode.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// function __mergeObject(target, source)
|
|
2
|
-
// {
|
|
3
|
-
// target = target || {};
|
|
4
|
-
// for (let key of Object.keys(source)) {
|
|
5
|
-
// if (source[key] instanceof Object) {
|
|
6
|
-
// Object.assign(source[key], __mergeObject(target[key], source[key]))
|
|
7
|
-
// }
|
|
8
|
-
// }
|
|
9
|
-
|
|
10
|
-
// Object.assign(target || {}, source)
|
|
11
|
-
// return target
|
|
12
|
-
// }
|
|
13
|
-
|
|
14
|
-
// function __createObject(data, path)
|
|
15
|
-
// {
|
|
16
|
-
// if (!path) return data;
|
|
17
|
-
|
|
18
|
-
// let keys = path.split('.')
|
|
19
|
-
// let newObject = data;
|
|
20
|
-
|
|
21
|
-
// for (var i = keys.length - 1; i >= 0; i--) {
|
|
22
|
-
// newObject = {[keys[i]]: newObject}
|
|
23
|
-
// }
|
|
24
|
-
// return newObject;
|
|
25
|
-
// }
|
|
26
|
-
|
|
27
|
-
// function __createArray(key, data)
|
|
28
|
-
// {
|
|
29
|
-
// try {
|
|
30
|
-
// let item = /([\w\W]+)\[(\d+)\]/gm.exec(key)
|
|
31
|
-
// if (item && item.length == 3) {
|
|
32
|
-
// let arrayKey = item[1];
|
|
33
|
-
// let index = parseInt(item[2]);
|
|
34
|
-
|
|
35
|
-
// if (!data[arrayKey] || !Array.isArray(data[arrayKey])) {
|
|
36
|
-
// data[arrayKey] = [];
|
|
37
|
-
// }
|
|
38
|
-
// data[arrayKey][index] = data[key];
|
|
39
|
-
// delete data[key];
|
|
40
|
-
// key = arrayKey;
|
|
41
|
-
// }
|
|
42
|
-
// } catch {
|
|
43
|
-
// console.log('create array error');
|
|
44
|
-
// }
|
|
45
|
-
// return key;
|
|
46
|
-
// }
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// function isObject(item) {
|
|
51
|
-
// return (!!item) && (item.constructor === Object);
|
|
52
|
-
// }
|
|
53
|
-
// function isArray(item) {
|
|
54
|
-
// return (!!item) && (item.constructor === Array);
|
|
55
|
-
// }
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// function decodeObject(data) {
|
|
59
|
-
// let keys = Object.keys(data)
|
|
60
|
-
// let objectData = {};
|
|
61
|
-
|
|
62
|
-
// keys.forEach((k) => {
|
|
63
|
-
// k = __createArray(k, data);
|
|
64
|
-
// if (k.split('.').length > 1) {
|
|
65
|
-
// let newData = __createObject(data[k], k);
|
|
66
|
-
// delete data[k];
|
|
67
|
-
|
|
68
|
-
// objectData = __mergeObject(objectData, newData);
|
|
69
|
-
// } else {
|
|
70
|
-
// objectData[k] = data[k];
|
|
71
|
-
// }
|
|
72
|
-
// })
|
|
73
|
-
// return objectData;
|
|
74
|
-
// }
|
|
75
|
-
|
|
76
|
-
// function encodeObject(data) {
|
|
77
|
-
// let keys = Object.keys(data);
|
|
78
|
-
// let newData = {};
|
|
79
|
-
// keys.forEach((k) => {
|
|
80
|
-
// let data_value = data[k];
|
|
81
|
-
// if (isObject(data[k])) {
|
|
82
|
-
// let new_obj = encodeObject(data[k]);
|
|
83
|
-
// let newKeys = Object.keys(new_obj);
|
|
84
|
-
// newKeys.forEach((newKey) => {
|
|
85
|
-
// let value = new_obj[newKey];
|
|
86
|
-
// if (isNaN(parseInt(newKey))) {
|
|
87
|
-
// newKey = `${k}.${newKey}`;
|
|
88
|
-
// } else {
|
|
89
|
-
// newKey = `${k}[${newKey}]`;
|
|
90
|
-
// }
|
|
91
|
-
|
|
92
|
-
// newData[newKey] = value;
|
|
93
|
-
// })
|
|
94
|
-
|
|
95
|
-
// } else if (isArray(data_value)){
|
|
96
|
-
// data_value.forEach((v, index) => {
|
|
97
|
-
// newData[`${k}[${index}]`] = v;
|
|
98
|
-
// })
|
|
99
|
-
// } else {
|
|
100
|
-
// newData[k] = data[k];
|
|
101
|
-
// }
|
|
102
|
-
// })
|
|
103
|
-
// return newData;
|
|
104
|
-
// }
|
|
105
|
-
|
|
106
|
-
module.exports = {
|
|
107
|
-
// decodeObject,
|
|
108
|
-
// encodeObject,
|
|
109
|
-
replaceArray
|
|
110
|
-
};
|
|
111
|
-
|