-
-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impure pipes support +
.get
, .findInstance
can find them in…
… fixtures closes #240
- Loading branch information
Showing
17 changed files
with
590 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
export default (node: DebugNode): DebugNode => | ||
node.nativeNode.nodeName === '#text' && node.parent ? node.parent : node; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import { Type } from '../common/core.types'; | ||
|
||
import { Node } from './func.get-from-node'; | ||
|
||
export default <T>(result: T[], node: DebugNode & Node, proto: Type<T>): void => { | ||
try { | ||
const instance = node.injector.get(proto); | ||
if (result.indexOf(instance) === -1) { | ||
result.push(instance); | ||
} | ||
} catch (error) { | ||
// nothing to do | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import funcGetFromNodeIvy from './func.get-from-node-ivy'; | ||
|
||
describe('func.get-from-node-ivy', () => { | ||
class Proto {} | ||
|
||
it('finds parent context', () => { | ||
const result: any[] = []; | ||
const proto = new Proto(); | ||
const node: any = { | ||
nativeNode: {}, | ||
parent: { | ||
nativeNode: { | ||
__ngContext__: [proto], | ||
}, | ||
}, | ||
}; | ||
|
||
funcGetFromNodeIvy(result, node, Proto); | ||
|
||
expect(result).toEqual([proto]); | ||
}); | ||
|
||
it('handles lView context', () => { | ||
const result: any[] = []; | ||
const proto = new Proto(); | ||
const node: any = { | ||
nativeNode: {}, | ||
parent: { | ||
nativeNode: { | ||
__ngContext__: { | ||
lView: [proto], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
funcGetFromNodeIvy(result, node, Proto); | ||
|
||
expect(result).toEqual([proto]); | ||
}); | ||
|
||
it('handles empty context', () => { | ||
const result: any[] = []; | ||
const node: any = { | ||
nativeNode: {}, | ||
parent: { | ||
nativeNode: {}, | ||
}, | ||
}; | ||
|
||
funcGetFromNodeIvy(result, node, Proto); | ||
|
||
expect(result).toEqual([]); | ||
}); | ||
|
||
it('scans nested arrays', () => { | ||
const result: any[] = []; | ||
const proto = new Proto(); | ||
const node: any = { | ||
nativeNode: {}, | ||
parent: { | ||
nativeNode: { | ||
__ngContext__: [[[proto]]], | ||
}, | ||
}, | ||
}; | ||
|
||
funcGetFromNodeIvy(result, node, Proto); | ||
expect(result).toEqual([proto]); | ||
}); | ||
|
||
it('skips node with _debugContext', () => { | ||
const result: any[] = []; | ||
const proto = new Proto(); | ||
const node: any = { | ||
_debugContext: {}, | ||
nativeNode: {}, | ||
parent: { | ||
nativeNode: { | ||
__ngContext__: [[[proto]]], | ||
}, | ||
}, | ||
}; | ||
|
||
funcGetFromNodeIvy(result, node, Proto); | ||
expect(result).toEqual([]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import { Type } from '../common/core.types'; | ||
|
||
import { Node } from './func.get-from-node'; | ||
import funcGetFromNodeElement from './func.get-from-node-element'; | ||
import funcGetFromNodeScan from './func.get-from-node-scan'; | ||
|
||
const detectContext = (node: DebugNode): any => { | ||
let current = node; | ||
let context = current.nativeNode.__ngContext__; | ||
while (!context && current.parent) { | ||
current = current.parent; | ||
context = current.nativeNode.__ngContext__; | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
const contextToNodes = (context: any): any => (Array.isArray(context) ? context : context?.lView); | ||
|
||
export default <T>(result: T[], node: DebugNode & Node, proto: Type<T>): void => { | ||
if (!node || node._debugContext) { | ||
return; | ||
} | ||
|
||
const el = funcGetFromNodeElement(node); | ||
|
||
funcGetFromNodeScan( | ||
{ | ||
el, | ||
nodes: contextToNodes(detectContext(node)) || [], | ||
normalize: item => item, | ||
proto, | ||
result, | ||
}, | ||
true, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import { Type } from '../common/core.types'; | ||
|
||
const detectGatherFlag = (gather: boolean, el: DebugNode | null, node: any): boolean => { | ||
if (!el || !node.nodeName) { | ||
return gather; | ||
} | ||
|
||
// checking if a textNode belongs to the current element. | ||
if (node.nodeName === '#text') { | ||
return node.parentNode === el.nativeNode; | ||
} | ||
|
||
// checking if an injectedNode belongs to the current element. | ||
return node === el.nativeNode; | ||
}; | ||
|
||
const scan = <T>( | ||
{ | ||
result, | ||
el, | ||
nodes, | ||
normalize, | ||
proto, | ||
}: { | ||
el: DebugNode | null; | ||
nodes: any[]; | ||
normalize: (item: any) => any; | ||
proto: Type<T>; | ||
result: T[]; | ||
}, | ||
gatherDefault: boolean, | ||
scanned: any[] = [], | ||
): void => { | ||
scanned.push(nodes); | ||
let gather = gatherDefault; | ||
|
||
for (const raw of nodes) { | ||
const node = normalize(raw); | ||
if (!node || typeof node !== 'object') { | ||
continue; | ||
} | ||
|
||
if (scanned.indexOf(node) === -1 && Array.isArray(node)) { | ||
scan({ result, el, nodes: node, normalize, proto }, gather, scanned); | ||
} | ||
|
||
gather = detectGatherFlag(gather, el, node); | ||
if (!gather) { | ||
continue; | ||
} | ||
|
||
if (result.indexOf(node) === -1 && node instanceof proto) { | ||
result.push(node); | ||
} | ||
} | ||
}; | ||
|
||
export default (() => scan)(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import { Type } from '../common/core.types'; | ||
|
||
import { Node } from './func.get-from-node'; | ||
import funcGetFromNodeElement from './func.get-from-node-element'; | ||
import funcGetFromNodeScan from './func.get-from-node-scan'; | ||
|
||
const normalize = (item: any): any => { | ||
if (!item || typeof item !== 'object') { | ||
return item; | ||
} | ||
|
||
for (const key of ['renderElement', 'renderText', 'instance']) { | ||
if (item[key]) { | ||
return item[key]; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export default <T>(result: T[], node: DebugNode & Node, proto: Type<T>): void => { | ||
if (!node || !node._debugContext) { | ||
return; | ||
} | ||
|
||
const el = funcGetFromNodeElement(node); | ||
|
||
funcGetFromNodeScan( | ||
{ | ||
el, | ||
nodes: node._debugContext.view.nodes, | ||
normalize, | ||
proto, | ||
result, | ||
}, | ||
true, | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { DebugNode } from '@angular/core'; | ||
|
||
import { Type } from '../common/core.types'; | ||
|
||
import funcGetFromNodeInjector from './func.get-from-node-injector'; | ||
import funcGetFromNodeIvy from './func.get-from-node-ivy'; | ||
import funcGetFromNodeStandard from './func.get-from-node-standard'; | ||
|
||
export interface Node { | ||
_debugContext?: { | ||
elDef: { | ||
nodeIndex: number; | ||
}; | ||
nodeDef: { | ||
nodeIndex: number; | ||
}; | ||
nodeIndex: number; | ||
view: { | ||
nodes: Array<{ | ||
instance?: any; | ||
renderElement?: any; | ||
renderText?: any; | ||
value?: any; | ||
}>; | ||
}; | ||
}; | ||
parent?: (DebugNode & Node) | null; | ||
} | ||
|
||
export default <T>(result: T[], node: DebugNode & Node, proto: Type<T>): T[] => { | ||
funcGetFromNodeInjector(result, node, proto); | ||
funcGetFromNodeStandard(result, node, proto); | ||
funcGetFromNodeIvy(result, node, proto); | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.