diff options
author | German M. Bravo <german.mb@deipi.com> | 2013-10-08 06:34:10 -0700 |
---|---|---|
committer | German M. Bravo <german.mb@deipi.com> | 2013-10-08 06:34:10 -0700 |
commit | 3694d8087ea1a6d4764ee5829e37d98303b955af (patch) | |
tree | f0a473eab7bfacfd80098e069aa10fa26d20cf17 | |
parent | e77cec393badc83cfa9ba9db3c494520b56ee739 (diff) | |
download | pyscss-3694d8087ea1a6d4764ee5829e37d98303b955af.tar.gz |
Allow for circular nth
-rw-r--r-- | scss/functions/core.py | 3 | ||||
-rw-r--r-- | scss/tests/files/kronuz/lists-circular-nth.css | 6 | ||||
-rw-r--r-- | scss/tests/files/kronuz/lists-circular-nth.scss | 9 | ||||
-rw-r--r-- | scss/types.py | 13 |
4 files changed, 25 insertions, 6 deletions
diff --git a/scss/functions/core.py b/scss/functions/core.py index b1fb6a0..e916aca 100644 --- a/scss/functions/core.py +++ b/scss/functions/core.py @@ -593,7 +593,8 @@ def nth(lst, n): else: raise ValueError("Invalid index %r" % (n,)) else: - i = n.to_python_index(len(lst)) + # DEVIATION: nth treats lists as circular lists + i = n.to_python_index(len(lst), circular=True) return lst[i] diff --git a/scss/tests/files/kronuz/lists-circular-nth.css b/scss/tests/files/kronuz/lists-circular-nth.css new file mode 100644 index 0000000..9cefdef --- /dev/null +++ b/scss/tests/files/kronuz/lists-circular-nth.css @@ -0,0 +1,6 @@ +div { + margin-top: 4px; + margin-right: 2px; + margin-bottom: 4px; + margin-left: 2px; +} diff --git a/scss/tests/files/kronuz/lists-circular-nth.scss b/scss/tests/files/kronuz/lists-circular-nth.scss new file mode 100644 index 0000000..851a305 --- /dev/null +++ b/scss/tests/files/kronuz/lists-circular-nth.scss @@ -0,0 +1,9 @@ +@option style:legacy; + +div { + $list: 4px 2px; + margin-top: nth($list, 1); + margin-right: nth($list, 2); + margin-bottom: nth($list, 3); + margin-left: nth($list, 4); +} diff --git a/scss/types.py b/scss/types.py index 3351f7f..d0079d3 100644 --- a/scss/types.py +++ b/scss/types.py @@ -462,7 +462,7 @@ class Number(Value): return wrapped - def to_python_index(self, length, check_bounds=True): + def to_python_index(self, length, check_bounds=True, circular=False): """Return a plain Python integer appropriate for indexing a sequence of the given length. Raise if this is impossible for any reason whatsoever. @@ -477,13 +477,16 @@ class Number(Value): if ret == 0: raise ValueError("Index cannot be zero") - if check_bounds and abs(ret) > length: + if check_bounds and not circular and abs(ret) > length: raise ValueError("Index {0!r} out of bounds for length {1}".format(ret, length)) if ret > 0: - return ret - 1 - else: - return ret + ret -= 1 + + if circular: + ret = ret % length + + return ret @property def has_simple_unit(self): |