Skip to content

Commit

Permalink
Fix literal /es in sass-convert.
Browse files Browse the repository at this point in the history
Closes #1787
  • Loading branch information
nex3 committed Aug 14, 2015
1 parent 1fc8d6d commit fdd9021
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* Make `is-superselector("a > c d", "a > b c d")` return `false`. This also
fixes some related `@extend` behavior.

* In `sass-convert`, ensure that literal `/`es are preserved.

## 3.4.16 (10 July 2015)

* When converting from Sass to SCSS or vice versa, double-space around nested
Expand Down
32 changes: 27 additions & 5 deletions lib/sass/script/tree/list_literal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ def children; elements; end
# @see Value#to_sass
def to_sass(opts = {})
return "()" if elements.empty?
precedence = Sass::Script::Parser.precedence_of(separator)
members = elements.map do |v|
if v.is_a?(ListLiteral) && Sass::Script::Parser.precedence_of(v.separator) <= precedence ||
separator == :space && v.is_a?(UnaryOperation) &&
(v.operator == :minus || v.operator == :plus) ||
separator == :space && v.is_a?(Operation)
if element_needs_parens?(v)
"(#{v.to_sass(opts)})"
else
v.to_sass(opts)
Expand Down Expand Up @@ -69,6 +65,32 @@ def _perform(environment)

private

# Returns whether an element in the list should be wrapped in parentheses
# when serialized to Sass.
def element_needs_parens?(element)
if element.is_a?(ListLiteral)
return Sass::Script::Parser.precedence_of(element.separator) <=
Sass::Script::Parser.precedence_of(separator)
end

return false unless separator == :space

if element.is_a?(UnaryOperation)
return element.operator == :minus || element.operator == :plus
end

return false unless element.is_a?(Operation)
return true unless element.operator == :div
!(is_literal_number?(element.operand1) && is_literal_number?(element.operand2))
end

# Returns whether a value is a number literal that shouldn't be divided.
def is_literal_number?(value)
value.is_a?(Literal) &&
value.value.is_a?((Sass::Script::Value::Number)) &&
!value.value.original.nil?
end

def sep_str(opts = options)
return ' ' if separator == :space
return ',' if opts && opts[:style] == :compressed
Expand Down
10 changes: 10 additions & 0 deletions test/sass/conversion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,16 @@ def test_literal_slash
foo {
a: 1px / 2px;
}
SCSS

# Regression test for issue 1787
assert_renders <<SASS, <<SCSS
foo
a: 1px / 2px 3px
SASS
foo {
a: 1px / 2px 3px;
}
SCSS
end

Expand Down

0 comments on commit fdd9021

Please sign in to comment.