Fix str-insert() with negative indices. (#85)

This commit is contained in:
Natalie Weizenbaum 2016-12-29 17:14:36 -08:00 committed by GitHub
parent a212999554
commit bf464f0539
2 changed files with 19 additions and 2 deletions

View File

@ -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.

View File

@ -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);