diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b5c8599..7db2ac07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## 1.0.0-alpha.7 +* `str-index()` now correctly inserts at negative indices. + * Properly parse `url()`s that contain comment-like text. * Fix a few more small `@extend` bugs. diff --git a/lib/src/functions.dart b/lib/src/functions.dart index e332270b..52668951 100644 --- a/lib/src/functions.dart +++ b/lib/src/functions.dart @@ -491,9 +491,24 @@ void defineCoreFunctions(Environment environment) { var index = arguments[2].assertNumber("index"); index.assertNoUnits("index"); - var codeUnitIndex = codepointIndexToCodeUnitIndex(string.text, - _codepointForIndex(index.assertInt("index"), string.text.runes.length)); + var indexInt = index.assertInt("index"); + var codepointIndex = _codepointForIndex(indexInt, string.text.runes.length, + allowNegative: true); + // str-insert has unusual behavior for negative inputs. It guarantees that + // the $insert is at $index in the result, which means that we want to + // insert before that point if $index is positive and after if it's + // negative. + if (indexInt < 0) { + if (codepointIndex < 0) { + codepointIndex = 0; + } else { + codepointIndex++; + } + } + + var codeUnitIndex = + codepointIndexToCodeUnitIndex(string.text, codepointIndex); return new SassString( string.text.replaceRange(codeUnitIndex, codeUnitIndex, insert.text), quotes: string.hasQuotes);