Only parenthize an operation if it's needed.
This commit is contained in:
parent
6b4290472c
commit
03c3c2b310
@ -202,6 +202,7 @@ module Sass
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
class Operation < Node
|
class Operation < Node
|
||||||
|
attr_accessor :operator unless method_defined? :operator
|
||||||
unless defined? OPERATORS_TO_SASS
|
unless defined? OPERATORS_TO_SASS
|
||||||
OPERATORS_TO_SASS = {
|
OPERATORS_TO_SASS = {
|
||||||
:plus => ' + ',
|
:plus => ' + ',
|
||||||
@ -209,23 +210,42 @@ module Sass
|
|||||||
:div => ' / ',
|
:div => ' / ',
|
||||||
:times => ' * ',
|
:times => ' * ',
|
||||||
:comma => ', ',
|
:comma => ', ',
|
||||||
:concat => ' ',
|
:concat => ' ',
|
||||||
:neq => ' ! =',
|
:neq => ' != ',
|
||||||
:eq => ' == ',
|
:eq => ' == ',
|
||||||
:or => ' or ',
|
:or => ' or ',
|
||||||
:and => ' and '
|
:and => ' and ',
|
||||||
|
:mod => ' % '
|
||||||
|
}
|
||||||
|
# higher numbers take predence over lower numbers
|
||||||
|
OPERATOR_PRECEDENCE = {
|
||||||
|
:plus => 2,
|
||||||
|
:minus => 2,
|
||||||
|
:div => 3,
|
||||||
|
:times => 3,
|
||||||
|
:comma => 1,
|
||||||
|
:concat => 1,
|
||||||
|
:neq => 0,
|
||||||
|
:eq => 0,
|
||||||
|
:or => 2,
|
||||||
|
:and => 3,
|
||||||
|
:mod => 4
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
def to_sass(format = :text)
|
def to_sass(format = :text)
|
||||||
"#{operand_to_sass(@operand1, format)}#{OPERATORS_TO_SASS[@operator]}#{operand_to_sass(@operand2, format)}"
|
"#{operand_to_sass(@operand1, format)}#{OPERATORS_TO_SASS[@operator]}#{operand_to_sass(@operand2, format)}"
|
||||||
end
|
end
|
||||||
def operand_to_sass(operand, format = :text)
|
def operand_to_sass(operand, format = :text)
|
||||||
if operand.is_a? Operation
|
if parenthize_operand?(operand)
|
||||||
"(#{operand.to_sass(format)})"
|
"(#{operand.to_sass(format)})"
|
||||||
else
|
else
|
||||||
operand.to_sass(format)
|
operand.to_sass(format)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
def parenthize_operand?(operand)
|
||||||
|
return false unless operand.is_a?(Operation)
|
||||||
|
OPERATOR_PRECEDENCE[operand.operator] < OPERATOR_PRECEDENCE[self.operator]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
class String < Literal
|
class String < Literal
|
||||||
def to_sass(format = :text)
|
def to_sass(format = :text)
|
||||||
|
Loading…
Reference in New Issue
Block a user