@@ -76,6 +76,26 @@ function tailLinebreak(raw: string) {
76
76
return match ? match [ 1 ] : '' ;
77
77
}
78
78
79
+ function legacyTrimResult ( content : string , ifTag : IfTag , ifCon : IfCondition | null ) {
80
+ if ( ! ifCon ) {
81
+ return ifTag . isBlock ? '\n' : '' ;
82
+ }
83
+
84
+ content = content . substring ( ifCon . start , ifCon . end ) ;
85
+
86
+ const head = headLinebreak ( ifCon . rawStart ) ;
87
+ if ( head ) {
88
+ content = ( ifTag . isBlock ? '\n' : head ) + content ;
89
+ }
90
+
91
+ const tail = tailLinebreak ( ifCon . rawEnd ) ;
92
+ if ( tail ) {
93
+ content = content + ( ifTag . isBlock ? '\n' : tail ) ;
94
+ }
95
+
96
+ return content ;
97
+ }
98
+
79
99
function trimResult ( content : string , ifTag : IfTag , ifCon : IfCondition | null ) {
80
100
if ( ! ifCon ) {
81
101
const head = headLinebreak ( ifTag . rawStart ) ;
@@ -208,6 +228,7 @@ function inlineConditions(
208
228
ifTag : IfTag ,
209
229
vars : Record < string , unknown > ,
210
230
strict : boolean ,
231
+ useLegacyConditions : boolean ,
211
232
) {
212
233
let ifCon = null ;
213
234
@@ -232,13 +253,22 @@ function inlineConditions(
232
253
233
254
const start = content . slice ( 0 , ifTag . start ) ;
234
255
const end = content . slice ( ifTag . end ) ;
235
- const result = trimResult ( content , ifTag , ifCon ) ;
256
+ let result ;
257
+ if ( useLegacyConditions ) {
258
+ result = legacyTrimResult ( content , ifTag , ifCon ) ;
259
+ } else {
260
+ result = trimResult ( content , ifTag , ifCon ) ;
261
+ }
236
262
237
- return {
263
+ const r = {
238
264
result : start + result + end ,
239
- lastIndex : start . length + result . length - tailLinebreak ( ifTag . rawEnd ) . length ,
265
+ lastIndex : start . length + result . length ,
240
266
ifCon,
241
267
} ;
268
+ if ( useLegacyConditions ) {
269
+ r . lastIndex -= tailLinebreak ( ifTag . rawEnd ) . length ;
270
+ }
271
+ return r ;
242
272
}
243
273
244
274
export = function conditions (
@@ -248,15 +278,20 @@ export = function conditions(
248
278
settings ?: {
249
279
sourceMap : Record < number , number > ;
250
280
strict ?: boolean ;
281
+ useLegacyConditions ?: boolean ;
251
282
} ,
252
283
) {
253
284
const sourceMap = settings ?. sourceMap || { } ;
254
285
const strict = settings ?. strict || false ;
286
+ const useLegacyConditions = settings ?. useLegacyConditions || false ;
255
287
const tagStack : IfTag [ ] = [ ] ;
256
288
257
289
// Consumes all between curly braces
258
290
// and all closest upon to first linebreak before and after braces.
259
- const R_LIQUID = / ( (?: \n [ \t ] * ) ? { % - ? ( [ \s \S ] * ?) - ? % } (?: [ \t ] * \n ) ? ) / g;
291
+ let R_LIQUID = / ( (?: \n [ ^ \n { ] * ) ? { % - ? ( [ \s \S ] * ?) - ? % } (?: \s * \n ) ? ) / g;
292
+ if ( useLegacyConditions ) {
293
+ R_LIQUID = / ( (?: \n [ \t ] * ) ? { % - ? ( [ \s \S ] * ?) - ? % } (?: [ \t ] * \n ) ? ) / g;
294
+ }
260
295
261
296
let match ;
262
297
while ( ( match = R_LIQUID . exec ( input ) ) !== null ) {
@@ -301,7 +336,13 @@ export = function conditions(
301
336
302
337
ifTag . closeCondition ( match [ 1 ] , match . index ) ;
303
338
304
- const { result, lastIndex, ifCon} = inlineConditions ( input , ifTag , vars , strict ) ;
339
+ const { result, lastIndex, ifCon} = inlineConditions (
340
+ input ,
341
+ ifTag ,
342
+ vars ,
343
+ strict ,
344
+ useLegacyConditions ,
345
+ ) ;
305
346
306
347
resourcemap ( input , ifTag , ifCon , createSourceMapApi ( sourceMap ) ) ;
307
348
@@ -313,7 +354,9 @@ export = function conditions(
313
354
default :
314
355
// This is not condition.
315
356
// Step back last linebreaks to match them on next condition
316
- R_LIQUID . lastIndex -= tailLinebreak ( match [ 1 ] ) . length ;
357
+ if ( useLegacyConditions ) {
358
+ R_LIQUID . lastIndex -= tailLinebreak ( match [ 1 ] ) . length ;
359
+ }
317
360
}
318
361
}
319
362
0 commit comments