UNPKG

1.21 kBPlain TextView Raw
1import {IsolateModule} from './IsolateModule';
2import {Scope} from './isolate';
3import {isEqualNamespace} from './utils';
4
5export class ScopeChecker {
6 public readonly _namespace: Array<Scope>;
7 constructor(
8 public readonly namespace: Array<Scope>,
9 private isolateModule: IsolateModule
10 ) {
11 this._namespace = namespace.filter(n => n.type !== 'selector');
12 }
13
14 /**
15 * Checks whether the given element is *directly* in the scope of this
16 * scope checker. Being contained *indirectly* through other scopes
17 * is not valid. This is crucial for implementing parent-child isolation,
18 * so that the parent selectors don't search inside a child scope.
19 */
20 public isDirectlyInScope(leaf: Element): boolean {
21 const namespace = this.isolateModule.getNamespace(leaf);
22 if (!namespace) {
23 return false;
24 }
25
26 if (
27 this._namespace.length > namespace.length ||
28 !isEqualNamespace(
29 this._namespace,
30 namespace.slice(0, this._namespace.length)
31 )
32 ) {
33 return false;
34 }
35 for (let i = this._namespace.length; i < namespace.length; i++) {
36 if (namespace[i].type === 'total') {
37 return false;
38 }
39 }
40 return true;
41 }
42}