diff options
author | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-08-16 18:00:11 -0700 |
---|---|---|
committer | Eevee (Alex Munroe) <eevee.git@veekun.com> | 2013-08-16 18:02:36 -0700 |
commit | 27523a3752ff418fcc1dcd32960696ca5f364e05 (patch) | |
tree | a2f468747ae9aef6b25ca4ad464097bf46258229 | |
parent | fb7fc88f35ea0a33f7aca17c5202c003691ab5ff (diff) | |
download | pyscss-27523a3752ff418fcc1dcd32960696ca5f364e05.tar.gz |
Fix (and test) nest() to not build lists of Python strings.
-rw-r--r-- | scss/functions/compass/helpers.py | 46 | ||||
-rw-r--r-- | scss/tests/functions/compass/test_helpers.py | 8 |
2 files changed, 35 insertions, 19 deletions
diff --git a/scss/functions/compass/helpers.py b/scss/functions/compass/helpers.py index 27b835e..99e1709 100644 --- a/scss/functions/compass/helpers.py +++ b/scss/functions/compass/helpers.py @@ -323,31 +323,41 @@ def headers(frm=None, to=None): @register('nest') def nest(*arguments): - if isinstance(arguments[0], List): - lst = arguments[0].value - else: - lst = String.unquoted(arguments[0]).value.split(',') - ret = [unicode(s).strip() for s in lst if unicode(s).strip()] - for arg in arguments[1:]: + ret = [''] # Hackery for initial value + + for arg in arguments: if isinstance(arg, List): - lst = arg.value + lst = arg + elif isinstance(arg, String): + lst = arg.value.split(',') else: - lst = String.unquoted(arg).value.split(',') + raise TypeError("Expected list or string, got %r" % (arg,)) + new_ret = [] for s in lst: - s = unicode(s).strip() - if s: - for r in ret: - if '&' in s: - new_ret.append(s.replace('&', r)) + if isinstance(s, String): + s = s.value + elif isinstance(s, six.string_types): + s = s + else: + raise TypeError("Expected string, got %r" % (s,)) + + s = s.strip() + if not s: + continue + + for r in ret: + if '&' in s: + new_ret.append(s.replace('&', r)) + else: + if not r or r[-1] in ('.', ':', '#'): + new_ret.append(r + s) else: - if r[-1] in ('.', ':', '#'): - new_ret.append(r + s) - else: - new_ret.append(r + ' ' + s) + new_ret.append(r + ' ' + s) ret = new_ret - return List(sorted(set(ret)), use_comma=True) + ret = [String.unquoted(s) for s in sorted(set(ret))] + return List(ret, use_comma=True) # This isn't actually from Compass, but it's just a shortcut for enumerate(). diff --git a/scss/tests/functions/compass/test_helpers.py b/scss/tests/functions/compass/test_helpers.py index 48e5222..bb5dfd3 100644 --- a/scss/tests/functions/compass/test_helpers.py +++ b/scss/tests/functions/compass/test_helpers.py @@ -91,7 +91,13 @@ def test_headings(calc): assert calc('headings(2, 5)') == calc('h2, h3, h4, h5') -# nest +def test_nest(calc): + # Using .render() here because the structure is complicated and only the + # output matters + assert calc('nest(selector1, selector2, selector3)').render() == 'selector1 selector2 selector3' + assert calc('nest("a b", "c d")').render() == 'a b c d' + assert calc('nest((a, b), (c, d))').render() == 'a c, a d, b c, b d' + # range |