File tree Expand file tree Collapse file tree 7 files changed +23
-15
lines changed
browser-vm/src/dynamicNode Expand file tree Collapse file tree 7 files changed +23
-15
lines changed Original file line number Diff line number Diff line change @@ -19,14 +19,14 @@ function injector(current: Function, methodName: string) {
19
19
const el = methodName === 'insertAdjacentElement'
20
20
? arguments [ 1 ]
21
21
: arguments [ 0 ] ;
22
- const sandbox = el && sandboxMap . get ( el ) ;
22
+ const sandbox = sandboxMap . get ( el ) ;
23
23
const originProcess = ( ) => current . apply ( this , arguments ) ;
24
24
25
- if ( this ?. tagName ?. toLowerCase ( ) === 'style' ) {
25
+ if ( el && this ?. tagName ?. toLowerCase ( ) === 'style' ) {
26
26
const { baseUrl, namespace } = sandbox ?. options || { } ;
27
27
const manager = new StyleManager ( el . textContent ) ;
28
- manager . setScope ( namespace ) ;
29
28
manager . correctPath ( baseUrl ) ;
29
+ manager . setAppName ( namespace ) ;
30
30
el . textContent = manager . transformCode ( manager . styleCode ) ;
31
31
return originProcess ( ) ;
32
32
}
Original file line number Diff line number Diff line change @@ -246,8 +246,8 @@ export class DynamicNodeProcessor {
246
246
else if ( this . is ( 'style' ) ) {
247
247
parentNode = this . findParentNodeInApp ( context , 'head' ) ;
248
248
const manager = new StyleManager ( this . el . textContent ) ;
249
- manager . setScope ( namespace ) ;
250
249
manager . correctPath ( baseUrl ) ;
250
+ manager . setAppName ( namespace ) ;
251
251
this . el . textContent = manager . transformCode ( manager . styleCode ) ;
252
252
convertedNode = this . el ;
253
253
this . sandbox . dynamicStyleSheetElementSet . add ( this . el ) ;
Original file line number Diff line number Diff line change @@ -552,7 +552,7 @@ export class App {
552
552
const text = node . children [ 0 ] as Text ;
553
553
if ( text ) {
554
554
const styleManager = new StyleManager ( text . content , baseUrl ) ;
555
- styleManager . setScope ( this . name ) ;
555
+ styleManager . setAppName ( this . name ) ;
556
556
return entryManager . ignoreChildNodesCreation (
557
557
styleManager . renderAsStyleElement ( ) ,
558
558
) ;
Original file line number Diff line number Diff line change @@ -8,15 +8,16 @@ export interface Options {
8
8
}
9
9
10
10
export function GarfishCssScope ( options : Options = { } ) {
11
+ const protoCache = new Set < StyleManager > ( ) ;
12
+ const codeCache = new Map < string , string > ( ) ;
13
+
11
14
const disable = ( appName : string ) => {
12
15
const { excludes } = options ;
13
16
if ( Array . isArray ( excludes ) ) return excludes . includes ( appName ) ;
14
17
if ( typeof excludes === 'function' ) return excludes ( appName ) ;
15
18
return false ;
16
19
} ;
17
20
18
- const protoCache = new Set ( ) ;
19
-
20
21
return function ( Garfish : interfaces . Garfish ) : interfaces . Plugin {
21
22
return {
22
23
name : 'css-scope' ,
@@ -28,10 +29,17 @@ export function GarfishCssScope(options: Options = {}) {
28
29
protoCache . add ( proto ) ;
29
30
30
31
proto . transformCode = function ( code : string ) {
31
- if ( ! code || ! this . scope || disable ( this . scope ) ) {
32
+ if ( ! code || ! this . appName || disable ( this . appName ) ) {
32
33
return code ;
34
+ } else {
35
+ const astNode = parse ( code , { source : this . url } ) ;
36
+ const newCode = stringify (
37
+ astNode ,
38
+ `div[id^=garfish_app_for_${ this . appName } _]` ,
39
+ ) ;
40
+ codeCache . set ( code , newCode ) ;
41
+ return newCode ;
33
42
}
34
- return '' ;
35
43
} ;
36
44
}
37
45
} ,
Original file line number Diff line number Diff line change @@ -66,7 +66,7 @@ class Compiler {
66
66
: s === 'head'
67
67
? '[__GarfishMockHead__]'
68
68
: s ;
69
- return `# ${ this . prefix } ${ s } ` ;
69
+ return `${ this . prefix } ${ s } ` ;
70
70
} ) ;
71
71
}
72
72
Original file line number Diff line number Diff line change @@ -173,7 +173,7 @@ export class Loader {
173
173
174
174
// Set css scope
175
175
if ( fileType === FileTypes . css ) {
176
- ( resourceManager as StyleManager ) . setScope ( scope ) ;
176
+ ( resourceManager as StyleManager ) . setAppName ( scope ) ;
177
177
}
178
178
179
179
// The results will be cached this time.
Original file line number Diff line number Diff line change @@ -6,12 +6,12 @@ const MATCH_CSS_URL = /url\(['"]?([^\)]+?)['"]?\)/g;
6
6
export class StyleManager {
7
7
public styleCode : string ;
8
8
public url : string | null ;
9
- public scope : string | null ;
9
+ public appName : string | null ;
10
10
11
11
private depsStack = new Set ( ) ;
12
12
13
13
constructor ( styleCode : string , url ?: string ) {
14
- this . scope = null ;
14
+ this . appName = null ;
15
15
this . url = url || null ;
16
16
this . styleCode = styleCode ;
17
17
}
@@ -37,8 +37,8 @@ export class StyleManager {
37
37
this . depsStack . add ( node ) ;
38
38
}
39
39
40
- setScope ( scope : string ) {
41
- this . scope = scope ;
40
+ setAppName ( appName : string ) {
41
+ this . appName = appName ;
42
42
}
43
43
44
44
isSameOrigin ( node : Node ) {
You can’t perform that action at this time.
0 commit comments