@@ -325,9 +325,8 @@ module.exports = function (Twig) {
325
325
*/
326
326
Twig . tokenize = function ( template ) {
327
327
const tokens = [ ] ;
328
- // An offset for reporting errors locations in the template.
329
- let errorOffset = 0 ;
330
-
328
+ // An offset for reporting errors locations and the position of the nodes in the template.
329
+ let currentPosition = 0 ;
331
330
// The start and type of the first token found in the template.
332
331
let foundToken = null ;
333
332
// The end position of the matched token.
@@ -343,29 +342,41 @@ module.exports = function (Twig) {
343
342
// No more tokens -> add the rest of the template as a raw-type token
344
343
tokens . push ( {
345
344
type : Twig . token . type . raw ,
346
- value : template
345
+ value : template ,
346
+ position : {
347
+ start : currentPosition ,
348
+ end : currentPosition + foundToken . position
349
+ }
347
350
} ) ;
348
351
template = '' ;
349
352
} else {
350
353
// Add a raw type token for anything before the start of the token
351
354
if ( foundToken . position > 0 ) {
352
355
tokens . push ( {
353
356
type : Twig . token . type . raw ,
354
- value : template . slice ( 0 , Math . max ( 0 , foundToken . position ) )
357
+ value : template . slice ( 0 , Math . max ( 0 , foundToken . position ) ) ,
358
+ position : {
359
+ start : currentPosition ,
360
+ end : currentPosition + Math . max ( 0 , foundToken . position )
361
+ }
355
362
} ) ;
356
363
}
357
364
358
365
template = template . slice ( foundToken . position + foundToken . def . open . length ) ;
359
- errorOffset += foundToken . position + foundToken . def . open . length ;
366
+ currentPosition += foundToken . position + foundToken . def . open . length ;
360
367
361
368
// Find the end of the token
362
- end = Twig . token . findEnd ( template , foundToken . def , errorOffset ) ;
369
+ end = Twig . token . findEnd ( template , foundToken . def , currentPosition ) ;
363
370
364
371
Twig . log . trace ( 'Twig.tokenize: ' , 'Token ends at ' , end ) ;
365
372
366
373
tokens . push ( {
367
374
type : foundToken . def . type ,
368
- value : template . slice ( 0 , Math . max ( 0 , end ) ) . trim ( )
375
+ value : template . slice ( 0 , Math . max ( 0 , end ) ) . trim ( ) ,
376
+ position : {
377
+ start : currentPosition - foundToken . def . open . length ,
378
+ end : currentPosition + end + foundToken . def . close . length
379
+ }
369
380
} ) ;
370
381
371
382
if ( template . slice ( end + foundToken . def . close . length , end + foundToken . def . close . length + 1 ) === '\n' ) {
@@ -385,7 +396,7 @@ module.exports = function (Twig) {
385
396
template = template . slice ( end + foundToken . def . close . length ) ;
386
397
387
398
// Increment the position in the template
388
- errorOffset += end + foundToken . def . close . length ;
399
+ currentPosition += end + foundToken . def . close . length ;
389
400
}
390
401
}
391
402
@@ -434,6 +445,7 @@ module.exports = function (Twig) {
434
445
const compileLogic = function ( token ) {
435
446
// Compile the logic token
436
447
logicToken = Twig . logic . compile . call ( self , token ) ;
448
+ logicToken . position = token . position ;
437
449
438
450
type = logicToken . type ;
439
451
open = Twig . logic . handler [ type ] . open ;
@@ -458,8 +470,13 @@ module.exports = function (Twig) {
458
470
459
471
tokOutput = {
460
472
type : Twig . token . type . logic ,
461
- token : prevToken
473
+ token : prevToken ,
474
+ position : {
475
+ open : prevToken . position ,
476
+ close : token . position
477
+ }
462
478
} ;
479
+
463
480
if ( stack . length > 0 ) {
464
481
intermediateOutput . push ( tokOutput ) ;
465
482
} else {
@@ -486,7 +503,8 @@ module.exports = function (Twig) {
486
503
} else if ( open !== undefined && open ) {
487
504
tokOutput = {
488
505
type : Twig . token . type . logic ,
489
- token : logicToken
506
+ token : logicToken ,
507
+ position : logicToken . position
490
508
} ;
491
509
// Standalone token (like {% set ... %}
492
510
if ( stack . length > 0 ) {
0 commit comments