@@ -660,6 +660,90 @@ describe('Value Node Types', () => {
660660 } )
661661 } )
662662
663+ describe ( 'value_as_number getter' , ( ) => {
664+ it ( 'should return number for NUMBER nodes' , ( ) => {
665+ const root = parse ( 'div { opacity: 0.5; }' )
666+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
667+ const numberNode = decl ?. values [ 0 ]
668+
669+ expect ( numberNode ?. type ) . toBe ( NUMBER )
670+ expect ( numberNode ?. value_as_number ) . toBe ( 0.5 )
671+ } )
672+
673+ it ( 'should return number for DIMENSION nodes' , ( ) => {
674+ const root = parse ( 'div { width: 100px; }' )
675+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
676+ const dimNode = decl ?. values [ 0 ]
677+
678+ expect ( dimNode ?. type ) . toBe ( DIMENSION )
679+ expect ( dimNode ?. value_as_number ) . toBe ( 100 )
680+ } )
681+
682+ it ( 'should handle negative numbers' , ( ) => {
683+ const root = parse ( 'div { margin: -10px; }' )
684+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
685+ const dimNode = decl ?. values [ 0 ]
686+
687+ expect ( dimNode ?. type ) . toBe ( DIMENSION )
688+ expect ( dimNode ?. value_as_number ) . toBe ( - 10 )
689+ } )
690+
691+ it ( 'should handle zero' , ( ) => {
692+ const root = parse ( 'div { margin: 0; }' )
693+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
694+ const numberNode = decl ?. values [ 0 ]
695+
696+ expect ( numberNode ?. type ) . toBe ( NUMBER )
697+ expect ( numberNode ?. value_as_number ) . toBe ( 0 )
698+ } )
699+
700+ it ( 'should handle decimal numbers' , ( ) => {
701+ const root = parse ( 'div { line-height: 1.5; }' )
702+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
703+ const numberNode = decl ?. values [ 0 ]
704+
705+ expect ( numberNode ?. type ) . toBe ( NUMBER )
706+ expect ( numberNode ?. value_as_number ) . toBe ( 1.5 )
707+ } )
708+
709+ it ( 'should handle percentage dimensions' , ( ) => {
710+ const root = parse ( 'div { width: 50%; }' )
711+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
712+ const dimNode = decl ?. values [ 0 ]
713+
714+ expect ( dimNode ?. type ) . toBe ( DIMENSION )
715+ expect ( dimNode ?. value_as_number ) . toBe ( 50 )
716+ expect ( dimNode ?. unit ) . toBe ( '%' )
717+ } )
718+
719+ it ( 'should return null for IDENTIFIER nodes' , ( ) => {
720+ const root = parse ( 'div { color: red; }' )
721+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
722+ const identNode = decl ?. values [ 0 ]
723+
724+ expect ( identNode ?. type ) . toBe ( IDENTIFIER )
725+ expect ( identNode ?. value_as_number ) . toBeNull ( )
726+ } )
727+
728+ it ( 'should return null for STRING nodes' , ( ) => {
729+ const root = parse ( 'div { content: "hello"; }' )
730+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
731+ const stringNode = decl ?. values [ 0 ]
732+
733+ expect ( stringNode ?. type ) . toBe ( STRING )
734+ expect ( stringNode ?. value_as_number ) . toBeNull ( )
735+ } )
736+
737+ it ( 'should return null for FUNCTION nodes' , ( ) => {
738+ const root = parse ( 'div { width: calc(100% - 20px); }' )
739+ const decl = root . first_child ?. first_child ?. next_sibling ?. first_child
740+ const funcNode = decl ?. values [ 0 ]
741+
742+ expect ( funcNode ?. type ) . toBe ( FUNCTION )
743+ expect ( funcNode ?. value_as_number ) . toBeNull ( )
744+ } )
745+ } )
746+
663747 describe ( 'Case-insensitive function names' , ( ) => {
664748 const getValue = ( css : string ) => {
665749 const root = parse ( css )
0 commit comments