@@ -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
- 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 ;
21
21
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
+ }
27
27
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 - z 0 - 9 ] + ) ( .* ?) ? \> / ) . groups . tag ;
46
+ if ( ! mainTag )
47
+ throw new Error ( 'find position: can not find the main tag' ) ;
35
48
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 ;
39
60
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
+ }
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
} ;
0 commit comments