UNPKG

1.01 kBPlain TextView Raw
1import {ScopeChecker} from './ScopeChecker';
2import {getSelectors} from './utils';
3import {Scope} from './isolate';
4import {IsolateModule} from './IsolateModule';
5
6function toElArray(input: any): Array<Element> {
7 return Array.prototype.slice.call(input) as Array<Element>;
8}
9
10export class ElementFinder {
11 constructor(
12 public namespace: Array<Scope>,
13 public isolateModule: IsolateModule
14 ) {}
15
16 public call(): Array<Element> {
17 const namespace = this.namespace;
18 const selector = getSelectors(namespace);
19
20 const scopeChecker = new ScopeChecker(namespace, this.isolateModule);
21 const topNode = this.isolateModule.getElement(
22 namespace.filter(n => n.type !== 'selector')
23 );
24
25 if (topNode === undefined) {
26 return [];
27 }
28
29 if (selector === '') {
30 return [topNode];
31 }
32
33 return toElArray(topNode.querySelectorAll(selector))
34 .filter(scopeChecker.isDirectlyInScope, scopeChecker)
35 .concat(topNode.matches(selector) ? [topNode] : []);
36 }
37}