Skip to content

Commit 25c5088

Browse files
committed
feat: cssPath and domParser
1 parent e68882f commit 25c5088

File tree

1 file changed

+72
-23
lines changed

1 file changed

+72
-23
lines changed

src/index.js

+72-23
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,82 @@ export function parseTextToHtml(text) {
1212
else return doc.body.children[0];
1313
}
1414

15-
export function cssPath(node) {
16-
let pathSplits = [];
17-
do {
18-
if (!node || !node.tagName) return false;
19-
let pathSplit = node.tagName.toLowerCase();
20-
if (node.id && node.tagName !== "BODY") pathSplit += "#" + node.id;
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;
2121

22-
if (node.classList.length && node.tagName !== "BODY") {
23-
node.classList.forEach((item) => {
24-
if (item.indexOf(":") === -1) pathSplit += "." + item;
25-
});
26-
}
22+
if (node.classList.length) {
23+
node.classList.forEach((item) => {
24+
if (item.indexOf(":") === -1) pathSplit += "." + item;
25+
});
26+
}
2727

28-
if (node.tagName !== "BODY" && node.parentNode) {
29-
let index = Array.prototype.indexOf.call(
30-
node.parentNode.children,
31-
node
32-
);
33-
pathSplit += `:nth-child(${index + 1})`;
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');
3548

36-
pathSplits.unshift(pathSplit);
37-
node = node.parentNode;
38-
} while (node.tagName !== "HTML");
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;
3960

40-
return pathSplits.join(" > ");
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+
}
4178
}
4279

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+
}
4391
// export function computeStyles(el, properties) {
4492
// let computed = window.getComputedStyle(el);
4593
// let result = {};
@@ -64,5 +112,6 @@ export function cssPath(node) {
64112
export default {
65113
parseTextToHtml,
66114
getAttributes,
67-
cssPath
115+
cssPath,
116+
domParser
68117
};

0 commit comments

Comments
 (0)