@@ -4,13 +4,12 @@ import {
4
4
RendererStyleFlags2 , ViewEncapsulation ,
5
5
} from "@angular/core" ;
6
6
7
- import { isBlank } from "./lang-facade" ;
8
7
import { View } from "ui/core/view" ;
9
8
import { addCss } from "application" ;
10
9
import { topmost } from "ui/frame" ;
11
- import { escapeRegexSymbols } from "utils/utils" ;
12
10
import { Device } from "platform" ;
13
11
12
+ import { isBlank } from "./lang-facade" ;
14
13
import { ViewUtil } from "./view-util" ;
15
14
import { APP_ROOT_VIEW , DEVICE , getRootPage } from "./platform-providers" ;
16
15
import { NgView } from "./element-registry" ;
@@ -21,7 +20,6 @@ const COMPONENT_REGEX = /%COMP%/g;
21
20
export const COMPONENT_VARIABLE = "%COMP%" ;
22
21
export const HOST_ATTR = `_nghost-${ COMPONENT_VARIABLE } ` ;
23
22
export const CONTENT_ATTR = `_ngcontent-${ COMPONENT_VARIABLE } ` ;
24
- const ATTR_REPLACER = new RegExp ( escapeRegexSymbols ( CONTENT_ATTR ) , "g" ) ;
25
23
const ATTR_SANITIZER = / - / g;
26
24
27
25
@Injectable ( )
@@ -152,6 +150,7 @@ export class NativeScriptRenderer extends Renderer2 {
152
150
// Seems to be called on component dispose only (router outlet)
153
151
// TODO: handle this when we resolve routing and navigation.
154
152
}
153
+
155
154
setAttribute ( view : NgView , name : string , value : string ) {
156
155
traceLog ( `NativeScriptRenderer.setAttribute ${ view } : ${ name } = ${ value } ` ) ;
157
156
return this . setProperty ( view , name , value ) ;
@@ -229,58 +228,45 @@ class EmulatedRenderer extends NativeScriptRenderer {
229
228
private hostAttr : string ;
230
229
231
230
constructor (
232
- private component : RendererType2 ,
231
+ component : RendererType2 ,
233
232
rootView : NgView ,
234
233
zone : NgZone ,
235
234
viewUtil : ViewUtil ,
236
235
) {
237
236
super ( rootView , zone , viewUtil ) ;
238
237
239
- this . addStyles ( ) ;
240
- this . contentAttr = shimContentAttribute ( component . id ) ;
241
- this . hostAttr = shimHostAttribute ( component . id ) ;
238
+ const componentId = component . id . replace ( ATTR_SANITIZER , "_" ) ;
239
+ this . contentAttr = replaceNgAttribute ( CONTENT_ATTR , componentId ) ;
240
+ this . hostAttr = replaceNgAttribute ( HOST_ATTR , componentId ) ;
241
+ this . addStyles ( component . styles , componentId ) ;
242
242
}
243
243
244
244
applyToHost ( view : NgView ) {
245
245
super . setAttribute ( view , this . hostAttr , "" ) ;
246
246
}
247
247
248
248
appendChild ( parent : any , newChild : NgView ) : void {
249
- // Set an attribute to the view to scope component-specific css.
250
- // The property name is pre-generated by Angular.
251
- const cssAttribute = this . replaceNgAttribute ( CONTENT_ATTR ) ;
252
- newChild [ cssAttribute ] = true ;
253
-
254
249
super . appendChild ( parent , newChild ) ;
255
250
}
256
251
257
252
createElement ( parent : any , name : string ) : NgView {
258
253
const view = super . createElement ( parent , name ) ;
254
+
255
+ // Set an attribute to the view to scope component-specific css.
256
+ // The property name is pre-generated by Angular.
259
257
super . setAttribute ( view , this . contentAttr , "" ) ;
260
258
261
259
return view ;
262
260
}
263
261
264
- private addStyles ( ) {
265
- this . component . styles
266
- . map ( s => s . toString ( ) )
267
- . map ( s => this . replaceNgAttribute ( s ) )
262
+ private addStyles ( styles : ( string | any [ ] ) [ ] , componentId : string ) {
263
+ styles . map ( s => s . toString ( ) )
264
+ . map ( s => replaceNgAttribute ( s , componentId ) )
268
265
. forEach ( addCss ) ;
269
266
}
270
267
271
- private replaceNgAttribute ( input : string ) : string {
272
- return input . replace ( ATTR_REPLACER , `_ng_content_${ this . componentId } ` ) ;
273
- }
274
-
275
- private get componentId ( ) : string {
276
- return this . component . id . replace ( ATTR_SANITIZER , "_" ) ;
277
- }
278
- }
279
-
280
- function shimContentAttribute ( componentShortId : string ) : string {
281
- return CONTENT_ATTR . replace ( COMPONENT_REGEX , componentShortId ) ;
282
268
}
283
269
284
- function shimHostAttribute ( componentShortId : string ) : string {
285
- return HOST_ATTR . replace ( COMPONENT_REGEX , componentShortId ) ;
270
+ function replaceNgAttribute ( input : string , componentId : string ) : string {
271
+ return input . replace ( COMPONENT_REGEX , componentId ) ;
286
272
}
0 commit comments