@@ -8548,32 +8548,48 @@ protected function libListSeparator($args)
85488548 protected static $ libNth = ['list ' , 'n ' ];
85498549 protected function libNth ($ args )
85508550 {
8551- $ list = $ this ->coerceList ($ args [0 ], ', ' , false );
8552- $ n = $ this ->assertNumber ($ args [1 ])->getDimension ();
8551+ $ list = $ this ->coerceList ($ args [0 ]);
8552+ $ n = $ this ->assertInteger ($ args [1 ], 'n ' );
8553+
8554+ if ($ n === 0 ) {
8555+ throw SassScriptException::forArgument ('List index may not be 0. ' , 'n ' );
8556+ }
8557+
8558+ $ listLength = \count ($ list [2 ]);
8559+
8560+ if (abs ($ n ) > $ listLength ) {
8561+ throw SassScriptException::forArgument ("Invalid index $ n for a list with $ listLength elements. " , 'n ' );
8562+ }
85538563
85548564 if ($ n > 0 ) {
85558565 $ n --;
8556- } elseif ( $ n < 0 ) {
8557- $ n += \count ( $ list [ 2 ]) ;
8566+ } else {
8567+ $ n += $ listLength ;
85588568 }
85598569
8560- return isset ( $ list [2 ][$ n ]) ? $ list [ 2 ][ $ n ] : static :: $ defaultValue ;
8570+ return $ list [2 ][$ n ];
85618571 }
85628572
85638573 protected static $ libSetNth = ['list ' , 'n ' , 'value ' ];
85648574 protected function libSetNth ($ args )
85658575 {
85668576 $ list = $ this ->coerceList ($ args [0 ]);
8567- $ n = $ this ->assertNumber ($ args [1 ])-> getDimension ( );
8577+ $ n = $ this ->assertInteger ($ args [1 ], ' n ' );
85688578
8569- if ($ n > 0 ) {
8570- $ n --;
8571- } elseif ($ n < 0 ) {
8572- $ n += \count ($ list [2 ]);
8579+ if ($ n === 0 ) {
8580+ throw SassScriptException::forArgument ('List index may not be 0. ' , 'n ' );
8581+ }
8582+
8583+ $ listLength = \count ($ list [2 ]);
8584+
8585+ if (abs ($ n ) > $ listLength ) {
8586+ throw SassScriptException::forArgument ("Invalid index $ n for a list with $ listLength elements. " , 'n ' );
85738587 }
85748588
8575- if (! isset ($ list [2 ][$ n ])) {
8576- throw $ this ->error ('Invalid argument for "n" ' );
8589+ if ($ n > 0 ) {
8590+ $ n --;
8591+ } else {
8592+ $ n += $ listLength ;
85778593 }
85788594
85798595 $ list [2 ][$ n ] = $ args [2 ];
0 commit comments