@cocreate/utils 1.1.36 → 1.2.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 +7 -0
- package/package.json +1 -1
- package/src/index.js +72 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.2.0](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.36...v1.2.0) (2021-10-29)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* cssPath and domParser ([25c5088](https://github.com/CoCreate-app/CoCreate-utils/commit/25c508855c663f5e2f64596549d1eccb14eb4292))
|
|
7
|
+
|
|
1
8
|
## [1.1.36](https://github.com/CoCreate-app/CoCreate-utils/compare/v1.1.35...v1.1.36) (2021-10-17)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -12,34 +12,82 @@ export function parseTextToHtml(text) {
|
|
|
12
12
|
else return doc.body.children[0];
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export function cssPath(node) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
export function cssPath(node, container) {
|
|
16
|
+
let pathSplits = [];
|
|
17
|
+
do {
|
|
18
|
+
if (!node || !node.tagName) return false;
|
|
19
|
+
let pathSplit = node.tagName.toLowerCase();
|
|
20
|
+
if (node.id) pathSplit += "#" + node.id;
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
if (node.classList.length) {
|
|
23
|
+
node.classList.forEach((item) => {
|
|
24
|
+
if (item.indexOf(":") === -1) pathSplit += "." + item;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
28
|
+
if (node.parentNode) {
|
|
29
|
+
let index = Array.prototype.indexOf.call(
|
|
30
|
+
node.parentNode.children,
|
|
31
|
+
node
|
|
32
|
+
);
|
|
33
|
+
pathSplit += `:nth-child(${index + 1})`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pathSplits.unshift(pathSplit);
|
|
37
|
+
node = node.parentNode;
|
|
38
|
+
if (node.tagName == "HTML" || node.nodeName == "#document" || node.hasAttribute('contenteditable'))
|
|
39
|
+
node = '';
|
|
40
|
+
} while (node);
|
|
41
|
+
return pathSplits.join(" > ");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function domParser(str) {
|
|
45
|
+
let mainTag = str.match(/\<(?<tag>[a-z0-9]+)(.*?)?\>/).groups.tag;
|
|
46
|
+
if (!mainTag)
|
|
47
|
+
throw new Error('find position: can not find the main tag');
|
|
35
48
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
let doc;
|
|
50
|
+
switch (mainTag) {
|
|
51
|
+
case 'html':
|
|
52
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
53
|
+
return doc.documentElement;
|
|
54
|
+
case 'body':
|
|
55
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
56
|
+
return doc.body;
|
|
57
|
+
case 'head':
|
|
58
|
+
doc = new DOMParser().parseFromString(str, "text/html");
|
|
59
|
+
return doc.head;
|
|
39
60
|
|
|
40
|
-
|
|
61
|
+
default:
|
|
62
|
+
let con = document.createElement('div');
|
|
63
|
+
con.innerHTML = str;
|
|
64
|
+
return con;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function queryFrameSelector(selector) {
|
|
69
|
+
if(selector.indexOf(';') !== -1) {
|
|
70
|
+
let [frameSelector, target] = selector.split(';');
|
|
71
|
+
let frame = document.querySelector(frameSelector);
|
|
72
|
+
if (frame) {
|
|
73
|
+
let Document = frame.contentDocument;
|
|
74
|
+
let elements = Document.querySelectorAll(target);
|
|
75
|
+
return elements;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
41
78
|
}
|
|
42
79
|
|
|
80
|
+
export function queryFrameSelectorAll(selector) {
|
|
81
|
+
if(selector.indexOf(';') !== -1) {
|
|
82
|
+
let [frameSelector, target] = selector.split(';');
|
|
83
|
+
let frame = document.querySelector(frameSelector);
|
|
84
|
+
if (frame) {
|
|
85
|
+
let Document = frame.contentDocument;
|
|
86
|
+
let element = Document.querySelector(target);
|
|
87
|
+
return element;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
43
91
|
// export function computeStyles(el, properties) {
|
|
44
92
|
// let computed = window.getComputedStyle(el);
|
|
45
93
|
// let result = {};
|
|
@@ -64,5 +112,6 @@ export function cssPath(node) {
|
|
|
64
112
|
export default {
|
|
65
113
|
parseTextToHtml,
|
|
66
114
|
getAttributes,
|
|
67
|
-
cssPath
|
|
115
|
+
cssPath,
|
|
116
|
+
domParser
|
|
68
117
|
};
|