From 431a1d1471de6d86dd8cdf66924a606fea0df60e Mon Sep 17 00:00:00 2001 From: "German M. Bravo" Date: Sun, 6 Oct 2013 12:55:29 -0500 Subject: Unquote strings when used from variables in selectors --- scss/__init__.py | 2 +- scss/expression.py | 8 ++++---- scss/tests/files/kronuz/selectors-unquoted.css | 3 +++ scss/tests/files/kronuz/selectors-unquoted.scss | 5 +++++ scss/types.py | 25 ++++++++++++------------- 5 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 scss/tests/files/kronuz/selectors-unquoted.css create mode 100644 scss/tests/files/kronuz/selectors-unquoted.scss diff --git a/scss/__init__.py b/scss/__init__.py index 9cda5f5..1f7793a 100644 --- a/scss/__init__.py +++ b/scss/__init__.py @@ -1287,7 +1287,7 @@ class Scss(object): Implements Nested CSS rules """ calculator = Calculator(rule.namespace) - raw_selectors = calculator.do_glob_math(block.prop) + raw_selectors = calculator.do_glob_math(block.prop, unquote=True) # DEVIATION: ruby sass doesn't support bare variables in selectors raw_selectors = calculator.apply_vars(raw_selectors) c_selectors, c_parents = self.parse_selectors(raw_selectors) diff --git a/scss/expression.py b/scss/expression.py index cacd8ec..12c867a 100644 --- a/scss/expression.py +++ b/scss/expression.py @@ -36,7 +36,7 @@ class Calculator(object): else: self.namespace = namespace - def _pound_substitute(self, result): + def _pound_substitute(self, result, unquote): expr = result.group(1) value = self.evaluate_expression(expr) @@ -45,9 +45,9 @@ class Calculator(object): elif value.is_null: return "" else: - return dequote(value.render()) + return dequote(value.render(unquote=unquote)) - def do_glob_math(self, cont): + def do_glob_math(self, cont, unquote=False): """Performs #{}-interpolation. The result is always treated as a fixed syntactic unit and will not be re-evaluated. """ @@ -55,7 +55,7 @@ class Calculator(object): cont = str(cont) if '#{' not in cont: return cont - cont = _expr_glob_re.sub(self._pound_substitute, cont) + cont = _expr_glob_re.sub(lambda r: self._pound_substitute(r, unquote), cont) return cont def apply_vars(self, cont): diff --git a/scss/tests/files/kronuz/selectors-unquoted.css b/scss/tests/files/kronuz/selectors-unquoted.css new file mode 100644 index 0000000..bbdbf8e --- /dev/null +++ b/scss/tests/files/kronuz/selectors-unquoted.css @@ -0,0 +1,3 @@ +p li.l1.current, p:hover li.l1.current, li.l1.current { + color: red; +} diff --git a/scss/tests/files/kronuz/selectors-unquoted.scss b/scss/tests/files/kronuz/selectors-unquoted.scss new file mode 100644 index 0000000..567f6a4 --- /dev/null +++ b/scss/tests/files/kronuz/selectors-unquoted.scss @@ -0,0 +1,5 @@ +@option style:legacy; + +#{append(nest('p, p:hover', 'li.l1.current'), 'li.l1.current')} { + color: red; +} diff --git a/scss/types.py b/scss/types.py index cc834a4..2f283ad 100644 --- a/scss/types.py +++ b/scss/types.py @@ -101,7 +101,7 @@ class Value(object): def __neg__(self): return String("-" + self.render()) - def render(self, compress=False): + def render(self, compress=False, unquote=False): return self.__str__() @@ -130,7 +130,7 @@ class Null(Value): def __ne__(self, other): return Boolean(not self.__eq__(other)) - def render(self, compress=False): + def render(self, compress=False, unquote=False): return self.sass_type_name @@ -198,7 +198,7 @@ class Boolean(Value): def __bool__(self): return self.value - def render(self, compress=False): + def render(self, compress=False, unquote=False): if self.value: return 'true' else: @@ -332,7 +332,6 @@ class Number(Value): unit_denom=self.unit_denom * int(exp.value), ) - def __mul__(self, other): if not isinstance(other, Number): return NotImplemented @@ -471,7 +470,7 @@ class Number(Value): def is_unitless(self): return not self.unit_numer and not self.unit_denom - def render(self, compress=False): + def render(self, compress=False, unquote=False): if not self.has_simple_unit: raise ValueError("Can't express compound units in CSS: %r" % (self,)) @@ -601,14 +600,14 @@ class List(Value): def __getitem__(self, key): return self.value[key] - def render(self, compress=False): + def render(self, compress=False, unquote=False): if not self.value: raise ValueError("Can't render empty list as CSS") delim = self.delimiter(compress) return delim.join( - item.render(compress=compress) + item.render(compress=compress, unquote=unquote) for item in self.value ) @@ -833,7 +832,7 @@ class Color(Value): return Color.from_rgb(*new_rgb, alpha=self.alpha) - def render(self, compress=False): + def render(self, compress=False, unquote=False): """Return a rendered representation of the color. If `compress` is true, the shortest possible representation is used; otherwise, named colors are rendered as names and all others are rendered as hex (or @@ -928,8 +927,8 @@ class String(Value): def __hash__(self): return hash(self.value) - def __str__(self): - if self.quotes: + def __str__(self, unquote=False): + if self.quotes and not unquote: return self.quotes + escape(self.value) + self.quotes else: return self.value @@ -969,8 +968,8 @@ class String(Value): return String(self.value * int(other.value), quotes=self.quotes) - def render(self, compress=False): - return self.__str__() + def render(self, compress=False, unquote=False): + return self.__str__(unquote) ### XXX EXPERIMENTAL XXX @@ -1005,7 +1004,7 @@ class Map(Value): def get_by_pos(self, key): return self.pairs[key][1] - def render(self, compress=False): + def render(self, compress=False, unquote=False): raise TypeError("Cannot render map %r as CSS" % (self,)) -- cgit v1.2.1