@@ -5,37 +5,36 @@ import {
5
5
6
6
import * as helper from './tree-helper' ;
7
7
8
- const jexl = new Jexl ( ) ;
9
8
const PARSE_OPTS = { sourceCodeLocationInfo : true } ;
10
9
11
- export function generate ( htmlTemplate : string , context : any ) : string {
10
+ export function generate ( htmlTemplate : string , context : any , jexl : any = new Jexl ( ) ) : string {
12
11
const startNode = parse ( htmlTemplate , PARSE_OPTS ) ;
13
- processChildNodes ( startNode , context ) ;
12
+ processChildNodes ( startNode , context , jexl ) ;
14
13
return serialize ( startNode ) ;
15
14
}
16
15
17
- function processChildNodes ( parentNode : Node , context : any ) {
16
+ function processChildNodes ( parentNode : Node , context : any , jexl : any ) {
18
17
const cloneChildNodes = [ ...helper . getChildNodes ( parentNode ) ] ;
19
18
20
19
if ( cloneChildNodes ) {
21
20
for ( let i = 0 , cnLength = cloneChildNodes . length ; i < cnLength ; i ++ ) {
22
21
const currentNode = cloneChildNodes [ i ] ;
23
22
24
23
if ( helper . isElementNode ( currentNode ) ) {
25
- processElement ( currentNode as DefaultTreeElement , context ) ;
24
+ processElement ( currentNode as DefaultTreeElement , context , jexl ) ;
26
25
}
27
26
}
28
27
}
29
28
}
30
29
31
- function processElement ( node : DefaultTreeElement , context : any ) {
30
+ function processElement ( node : DefaultTreeElement , context : any , jexl : any ) {
32
31
const clonedAttrs = [ ...helper . getAttrList ( node ) ] ;
33
32
34
33
for ( const attr of clonedAttrs ) {
35
34
try {
36
35
const directive = directives . find ( ( d ) => d . match ( attr ) ) ;
37
36
if ( directive ) {
38
- const result = directive . process ( node , attr , context ) ;
37
+ const result = directive . process ( node , attr , context , jexl ) ;
39
38
if ( result . continue === false ) {
40
39
return ;
41
40
}
@@ -44,7 +43,7 @@ function processElement(node: DefaultTreeElement, context: any) {
44
43
handleDirectiveError ( e , node , attr ) ;
45
44
}
46
45
}
47
- processChildNodes ( node , context ) ;
46
+ processChildNodes ( node , context , jexl ) ;
48
47
}
49
48
50
49
function handleDirectiveError ( e : Error , node : DefaultTreeElement , attr ?: Attribute ) {
@@ -61,7 +60,7 @@ function handleDirectiveError(e: Error, node: DefaultTreeElement, attr?: Attribu
61
60
62
61
export interface Directive {
63
62
match : ( attr : Attribute ) => boolean ;
64
- process : ( node : DefaultTreeElement , attr : Attribute , context : any ) => DirectiveResult ;
63
+ process : ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) => DirectiveResult ;
65
64
}
66
65
67
66
export interface DirectiveResult {
@@ -72,7 +71,7 @@ const htmlDirective: Directive = {
72
71
match ( attr : Attribute ) : boolean {
73
72
return attr . name === 'x-html' ;
74
73
} ,
75
- process ( node : DefaultTreeElement , attr : Attribute , context : any ) : DirectiveResult {
74
+ process ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) : DirectiveResult {
76
75
const html = jexl . evalSync ( attr . value , context ) ;
77
76
const fragments = helper . getChildNodes ( parseFragment ( html , PARSE_OPTS ) ) ;
78
77
helper . replaceChildNodes ( node , fragments ) ;
@@ -85,7 +84,7 @@ const textDirective: Directive = {
85
84
match ( attr : Attribute ) : boolean {
86
85
return attr . name === 'x-text' ;
87
86
} ,
88
- process ( node : DefaultTreeElement , attr : Attribute , context : any ) : DirectiveResult {
87
+ process ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) : DirectiveResult {
89
88
const rawText = jexl . evalSync ( attr . value , context ) ;
90
89
const safeText = rawText ? helper . escapeString ( rawText , false ) : '' ;
91
90
const fragments = helper . getChildNodes ( parseFragment ( safeText , PARSE_OPTS ) ) ;
@@ -99,7 +98,7 @@ const attrDirective: Directive = {
99
98
match ( attr : Attribute ) : boolean {
100
99
return attr . name . startsWith ( 'x-attr:' ) ;
101
100
} ,
102
- process ( node : DefaultTreeElement , attr : Attribute , context : any ) : DirectiveResult {
101
+ process ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) : DirectiveResult {
103
102
const attrs = helper . getAttrList ( node ) ;
104
103
const [ , attrName ] = attr . name . split ( ':' ) . map ( ( v ) => v . trim ( ) ) ;
105
104
@@ -114,7 +113,7 @@ const ifDirective: Directive = {
114
113
match ( attr : Attribute ) : boolean {
115
114
return attr . name === 'x-if' ;
116
115
} ,
117
- process ( node : DefaultTreeElement , attr : Attribute , context : any ) : DirectiveResult {
116
+ process ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) : DirectiveResult {
118
117
const condtion = jexl . evalSync ( attr . value , context ) ;
119
118
if ( ! condtion ) {
120
119
helper . detachNode ( node ) ;
@@ -129,7 +128,7 @@ const forDirective: Directive = {
129
128
match ( attr : Attribute ) : boolean {
130
129
return attr . name === 'x-for' ;
131
130
} ,
132
- process ( node : DefaultTreeElement , attr : Attribute , context : any ) : DirectiveResult {
131
+ process ( node : DefaultTreeElement , attr : Attribute , context : any , jexl : any ) : DirectiveResult {
133
132
const parentNode = helper . getParentNode ( node ) as DefaultTreeElement ;
134
133
const currentIndex = parentNode . childNodes . indexOf ( node ) ;
135
134
const prevNode = helper . getPrevNode ( node ) ;
@@ -150,7 +149,7 @@ const forDirective: Directive = {
150
149
const newItemNode = helper . getFirstChild ( fragments ) as DefaultTreeElement ;
151
150
newItemNode . attrs = helper . getAttrList ( newItemNode ) . filter ( ( a ) => a . name !== 'x-for' ) ;
152
151
153
- processElement ( newItemNode as DefaultTreeElement , newContext ) ;
152
+ processElement ( newItemNode as DefaultTreeElement , newContext , jexl ) ;
154
153
155
154
return newItemNode ;
156
155
} ) ;
0 commit comments