-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetChildFromSinglePath.ts
35 lines (33 loc) · 1.28 KB
/
getChildFromSinglePath.ts
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
import {getChildren} from './getChildren.js';
export function getChildFromSinglePath(el: HTMLElement, token: string){
let idx = 0;
let nonIndexedToken = token;
if(token === '..'){
// this.$0 = this.$0.parentElement;
// return this.$0;
return el.parentNode !== null ? el.parentNode : (<any>el).host;
}
if(token.endsWith(']')){
const posOfOpen = token.indexOf('[');
const indxString = token.substring(posOfOpen + 1, token.length - 1);
idx = parseInt(indxString);
nonIndexedToken = token.substring(0, posOfOpen);
}
//const children = this.$0.querySelectorAll(':scope > ' + nonIndexedToken);
const matchingNodes = [];
getChildren(el).forEach((child : HTMLElement) =>{
if(child.matches){
const iPosStart =nonIndexedToken.indexOf('#"');
if(iPosStart !== -1){
const iPosEnd = nonIndexedToken.indexOf('"', iPosStart + 2);
const id = nonIndexedToken.substring(iPosStart + 2, iPosEnd);
if(child.id === id){
matchingNodes.push(child);
}
}else if(child.matches(nonIndexedToken)){
matchingNodes.push(child);
}
}
});
return matchingNodes[idx] as HTMLElement;
}