@@ -453,6 +453,29 @@ test('positionInside() supports multi-root documents', () => {
453
453
equal ( a . positionInside ( 1 ) , { column : 9 , line : 1 } )
454
454
} )
455
455
456
+ test ( 'positionBy() returns position' , ( ) => {
457
+ let css = parse ( 'a { one: X }' )
458
+ let a = css . first as Rule
459
+ let one = a . first as Declaration
460
+ equal ( one . positionBy ( ) , { column : 6 , line : 1 , offset : 5 } )
461
+ equal ( a . positionBy ( ) , { column : 1 , line : 1 , offset : 0 } )
462
+ } )
463
+
464
+ test ( 'positionBy() returns position after AST mutations' , ( ) => {
465
+ let css = parse ( 'a {\n\tone: 1;\n\ttwo: 2;}' )
466
+ let a = css . first as Rule
467
+ let one = a . first as Declaration
468
+ let two = one . next ( ) as Declaration
469
+
470
+ equal ( a . positionBy ( ) , { column : 1 , line : 1 , offset : 0 } )
471
+ equal ( two . positionBy ( ) , { column : 2 , line : 3 , offset : 14 } )
472
+
473
+ one . remove ( )
474
+
475
+ equal ( a . positionBy ( ) , { column : 1 , line : 1 , offset : 0 } )
476
+ equal ( two . positionBy ( ) , { column : 2 , line : 3 , offset : 14 } )
477
+ } )
478
+
456
479
test ( 'positionBy() returns position for word' , ( ) => {
457
480
let css = parse ( 'a { one: X }' )
458
481
let a = css . first as Rule
@@ -527,6 +550,59 @@ test('positionBy() supports multi-root documents', () => {
527
550
equal ( a . positionBy ( { word : 'a' } ) , { column : 8 , line : 1 } )
528
551
} )
529
552
553
+ test ( 'rangeBy() returns range' , ( ) => {
554
+ let css = parse ( 'a { one: X }' )
555
+ let a = css . first as Rule
556
+ let one = a . first as Declaration
557
+ equal ( one . rangeBy ( ) , {
558
+ end : { column : 12 , line : 1 } ,
559
+ start : { column : 6 , line : 1 }
560
+ } )
561
+ } )
562
+
563
+ test ( 'rangeBy() returns range when offsets are missing' , ( ) => {
564
+ let css = parse ( 'a { one: X }' )
565
+ let a = css . first as Rule
566
+ let one = a . first as Declaration
567
+
568
+ // @ts -expect-error Testing non-standard AST
569
+ if ( one . source ?. start ) delete one . source . start . offset
570
+ // @ts -expect-error Testing non-standard AST
571
+ if ( one . source ?. end ) delete one . source . end . offset
572
+
573
+ equal ( one . rangeBy ( ) , {
574
+ end : { column : 12 , line : 1 } ,
575
+ start : { column : 6 , line : 1 }
576
+ } )
577
+ } )
578
+
579
+ test ( 'rangeBy() returns range for empty object even after AST mutations' , ( ) => {
580
+ let css = parse ( 'a {\n\tone: 1;\n\ttwo: 2;}' )
581
+ let a = css . first as Rule
582
+ let one = a . first as Declaration
583
+ let two = one . next ( ) as Declaration
584
+
585
+ equal ( a . rangeBy ( ) , {
586
+ end : { column : 10 , line : 3 } ,
587
+ start : { column : 1 , line : 1 }
588
+ } )
589
+ equal ( two . rangeBy ( ) , {
590
+ end : { column : 9 , line : 3 } ,
591
+ start : { column : 2 , line : 3 }
592
+ } )
593
+
594
+ one . remove ( )
595
+
596
+ equal ( a . rangeBy ( ) , {
597
+ end : { column : 10 , line : 3 } ,
598
+ start : { column : 1 , line : 1 }
599
+ } )
600
+ equal ( two . rangeBy ( ) , {
601
+ end : { column : 9 , line : 3 } ,
602
+ start : { column : 2 , line : 3 }
603
+ } )
604
+ } )
605
+
530
606
test ( 'rangeBy() returns range for word' , ( ) => {
531
607
let css = parse ( 'a { one: X }' )
532
608
let a = css . first as Rule
0 commit comments