Skip to content

Commit 5b57452

Browse files
committed
Fix the handling of list index
1 parent ba00276 commit 5b57452

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/Compiler.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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];

tests/specs/sass-spec-exclude.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,7 @@ core_functions/list/join/multi/map/second/space
225225
core_functions/list/join/single/both/comma/last
226226
core_functions/list/join/single/first/undecided/and_comma
227227
core_functions/list/join/single/non_list/first/comma
228-
core_functions/list/nth/error/index/0
229-
core_functions/list/nth/error/index/too_high
230-
core_functions/list/nth/error/index/too_low
231228
core_functions/list/separator/empty/comma
232-
core_functions/list/set_nth/error/index/0
233229
core_functions/list/utils/empty_map/same_as_empty_list
234230
core_functions/list/utils/real_separator/empty/comma
235231
core_functions/list/utils/real_separator/empty/undecided

0 commit comments

Comments
 (0)