@@ -9,10 +9,10 @@ import './ng_dev_mode';
99
1010import { assertEqual } from './assert' ;
1111import { LContext , MONKEY_PATCH_KEY_NAME } from './interfaces/context' ;
12- import { LElementNode , TNode , TNodeFlags } from './interfaces/node' ;
12+ import { TNode , TNodeFlags } from './interfaces/node' ;
1313import { RElement } from './interfaces/renderer' ;
1414import { CONTEXT , HEADER_OFFSET , HOST , LViewData , TVIEW } from './interfaces/view' ;
15- import { getComponentViewByIndex , readElementValue , readPatchedData } from './util' ;
15+ import { getComponentViewByIndex , getNativeByTNode , readElementValue , readPatchedData } from './util' ;
1616
1717
1818/** Returns the matching `LContext` data for a given DOM node, directive or component instance.
@@ -66,11 +66,11 @@ export function getContext(target: any): LContext|null {
6666 // are expensive. Instead, only the target data (the element, compontent or
6767 // directive details) are filled into the context. If called multiple times
6868 // with different target values then the missing target data will be filled in.
69- const lNode = getLNodeFromViewData ( lViewData , nodeIndex ) ! ;
70- const existingCtx = readPatchedData ( lNode . native ) ;
69+ const native = readElementValue ( lViewData [ nodeIndex ] ) ;
70+ const existingCtx = readPatchedData ( native ) ;
7171 const context : LContext = ( existingCtx && ! Array . isArray ( existingCtx ) ) ?
7272 existingCtx :
73- createLContext ( lViewData , nodeIndex , lNode . native ) ;
73+ createLContext ( lViewData , nodeIndex , native ) ;
7474
7575 // only when the component has been discovered then update the monkey-patch
7676 if ( component && context . component === undefined ) {
@@ -114,9 +114,9 @@ export function getContext(target: any): LContext|null {
114114
115115 const index = findViaNativeElement ( lViewData , rElement ) ;
116116 if ( index >= 0 ) {
117- const lNode = getLNodeFromViewData ( lViewData , index ) ! ;
118- const context = createLContext ( lViewData , index , lNode . native ) ;
119- attachPatchData ( lNode . native , context ) ;
117+ const native = readElementValue ( lViewData [ index ] ) ;
118+ const context = createLContext ( lViewData , index , native ) ;
119+ attachPatchData ( native , context ) ;
120120 mpValue = context ;
121121 break ;
122122 }
@@ -129,10 +129,10 @@ export function getContext(target: any): LContext|null {
129129/**
130130 * Creates an empty instance of a `LContext` context
131131 */
132- function createLContext ( lViewData : LViewData , lNodeIndex : number , native : RElement ) : LContext {
132+ function createLContext ( lViewData : LViewData , nodeIndex : number , native : RElement ) : LContext {
133133 return {
134134 lViewData,
135- nodeIndex : lNodeIndex , native,
135+ nodeIndex : nodeIndex , native,
136136 component : undefined ,
137137 directives : undefined ,
138138 localRefs : undefined ,
@@ -150,9 +150,9 @@ export function getComponentViewByInstance(componentInstance: {}): LViewData {
150150 let view : LViewData ;
151151
152152 if ( Array . isArray ( lViewData ) ) {
153- const lNodeIndex = findViaComponent ( lViewData , componentInstance ) ;
154- view = getComponentViewByIndex ( lNodeIndex , lViewData ) ;
155- const context = createLContext ( lViewData , lNodeIndex , ( view [ HOST ] as LElementNode ) . native ) ;
153+ const nodeIndex = findViaComponent ( lViewData , componentInstance ) ;
154+ view = getComponentViewByIndex ( nodeIndex , lViewData ) ;
155+ const context = createLContext ( lViewData , nodeIndex , view [ HOST ] as RElement ) ;
156156 context . component = componentInstance ;
157157 attachPatchData ( componentInstance , context ) ;
158158 attachPatchData ( context . native , context ) ;
@@ -182,11 +182,11 @@ export function isDirectiveInstance(instance: any): boolean {
182182/**
183183 * Locates the element within the given LViewData and returns the matching index
184184 */
185- function findViaNativeElement ( lViewData : LViewData , native : RElement ) : number {
185+ function findViaNativeElement ( lViewData : LViewData , target : RElement ) : number {
186186 let tNode = lViewData [ TVIEW ] . firstChild ;
187187 while ( tNode ) {
188- const lNode = getLNodeFromViewData ( lViewData , tNode . index ) ! ;
189- if ( lNode . native === native ) {
188+ const native = getNativeByTNode ( tNode , lViewData ) ! ;
189+ if ( native === target ) {
190190 return tNode . index ;
191191 }
192192 tNode = traverseNextElement ( tNode ) ;
@@ -261,18 +261,6 @@ function assertDomElement(element: any) {
261261 assertEqual ( element . nodeType , 1 , 'The provided value must be an instance of an HTMLElement' ) ;
262262}
263263
264- /**
265- * Retruns the instance of the LElementNode at the given index in the LViewData.
266- *
267- * This function will also unwrap the inner value incase it's stuffed into an
268- * array (which is what happens when [style] and [class] bindings are present
269- * in the view instructions for the element being returned).
270- */
271- function getLNodeFromViewData ( lViewData : LViewData , lElementIndex : number ) : LElementNode | null {
272- const value = lViewData [ lElementIndex ] ;
273- return value ? readElementValue ( value ) : null ;
274- }
275-
276264/**
277265 * Returns a list of directives extracted from the given view based on the
278266 * provided list of directive index values.
@@ -294,17 +282,16 @@ export function discoverDirectives(
294282 * Returns a map of local references (local reference name => element or directive instance) that
295283 * exist on a given element.
296284 */
297- export function discoverLocalRefs ( lViewData : LViewData , lNodeIndex : number ) : { [ key : string ] : any } |
285+ export function discoverLocalRefs ( lViewData : LViewData , nodeIndex : number ) : { [ key : string ] : any } |
298286 null {
299- const tNode = lViewData [ TVIEW ] . data [ lNodeIndex ] as TNode ;
287+ const tNode = lViewData [ TVIEW ] . data [ nodeIndex ] as TNode ;
300288 if ( tNode && tNode . localNames ) {
301289 const result : { [ key : string ] : any } = { } ;
302290 for ( let i = 0 ; i < tNode . localNames . length ; i += 2 ) {
303291 const localRefName = tNode . localNames [ i ] ;
304292 const directiveIndex = tNode . localNames [ i + 1 ] as number ;
305- result [ localRefName ] = directiveIndex === - 1 ?
306- getLNodeFromViewData ( lViewData , lNodeIndex ) ! . native :
307- lViewData [ directiveIndex ] ;
293+ result [ localRefName ] =
294+ directiveIndex === - 1 ? getNativeByTNode ( tNode , lViewData ) ! : lViewData [ directiveIndex ] ;
308295 }
309296 return result ;
310297 }
0 commit comments