lib/sass/tree/prop_node.rb in haml-edge-2.3.179 vs lib/sass/tree/prop_node.rb in haml-edge-2.3.180

- old
+ new

@@ -1,21 +1,37 @@ module Sass::Tree # A static node reprenting a CSS property. # # @see Sass::Tree class PropNode < Node - # The name of the property. + # The name of the property, + # interspersed with {Sass::Script::Node}s + # representing `#{}`-interpolation. + # Any adjacent strings will be merged together. # - # @return [String] + # @return [Array<String, Sass::Script::Node>] attr_accessor :name - # The value of the property, - # either a plain string or a SassScript parse tree. + # The name of the property + # after any interpolated SassScript has been resolved. + # Only set once \{Tree::Node#perform} has been called. # - # @return [String, Script::Node] + # @return [String] + attr_accessor :resolved_name + + # The value of the property. + # + # @return [Sass::Script::Node] attr_accessor :value + # The value of the property + # after any interpolated SassScript has been resolved. + # Only set once \{Tree::Node#perform} has been called. + # + # @return [String] + attr_accessor :resolved_value + # How deep this property is indented # relative to a normal property. # This is only greater than 0 in the case that: # # * This node is in a CSS tree @@ -24,16 +40,17 @@ # * The parent property has a value, and thus will be rendered # # @return [Fixnum] attr_accessor :tabs - # @param name [String] See \{#name} - # @param value [String] See \{#value} + # @param name [Array<String, Sass::Script::Node>] See \{#name} + # @param value [Sass::Script::Node] See \{#value} # @param prop_syntax [Symbol] `:new` if this property uses `a: b`-style syntax, # `:old` if it uses `:a b`-style syntax def initialize(name, value, prop_syntax) - @name = name + @name = Haml::Util.strip_string_array( + Haml::Util.merge_adjacent_strings(name)) @value = value @tabs = 0 @prop_syntax = prop_syntax super() end @@ -49,36 +66,48 @@ # Returns a appropriate message indicating how to escape pseudo-class selectors. # This only applies for old-style properties with no value, # so returns the empty string if this is new-style. # + # This should only be called once \{#perform} has been called. + # # @return [String] The message def pseudo_class_selector_message - return "" if @prop_syntax == :new || !value.empty? + return "" if @prop_syntax == :new || !resolved_value.empty? "\nIf #{declaration.dump} should be a selector, use \"\\#{declaration}\" instead." end protected + def to_src(tabs, opts, fmt) + name = self.name.map {|n| n.is_a?(String) ? n : "\#{#{n.to_sass}}"}.join + old = opts[:old] && fmt == :sass + initial = old ? ':' : '' + mid = old ? '' : ':' + res = "#{' ' * tabs}#{initial}#{name}#{mid} #{self.class.val_to_sass(value)}" + return res + "#{semi fmt}\n" if children.empty? + res.rstrip + children_to_src(tabs, opts, fmt) + end + # Computes the CSS for the property. # # @param tabs [Fixnum] The level of indentation for the CSS # @return [String] The resulting CSS def _to_s(tabs) - to_return = ' ' * (tabs - 1 + self.tabs) + name + ":" + - (style == :compressed ? '' : ' ') + value + (style == :compressed ? "" : ";") + to_return = ' ' * (tabs - 1 + self.tabs) + resolved_name + ":" + + (style == :compressed ? '' : ' ') + resolved_value + (style == :compressed ? "" : ";") end # Converts nested properties into flat properties. # # @param parent [PropNode, nil] The parent node of this node, # or nil if the parent isn't a {PropNode} # @raise [Sass::SyntaxError] if the property uses invalid syntax def _cssize(parent) node = super result = node.children.dup - if !node.value.empty? || node.children.empty? + if !node.resolved_value.empty? || node.children.empty? node.send(:check!) result.unshift(node) end result end @@ -87,23 +116,29 @@ # and nesting level. # # @param parent [PropNode, nil] The parent node of this node, # or nil if the parent isn't a {PropNode} def cssize!(parent) - self.name = "#{parent.name}-#{name}" if parent - self.tabs = parent.tabs + (parent.value.empty? ? 0 : 1) if parent && style == :nested + self.resolved_name = "#{parent.resolved_name}-#{resolved_name}" if parent + self.tabs = parent.tabs + (parent.resolved_value.empty? ? 0 : 1) if parent && style == :nested super end # Runs any SassScript that may be embedded in the property, # and invludes the parent property, if any. # # @param environment [Sass::Environment] The lexical environment containing # variable and mixin values def perform!(environment) - @name = interpolate(@name, environment) - @value = @value.is_a?(String) ? interpolate(@value, environment) : @value.perform(environment).to_s + @resolved_name = run_interp(@name, environment) + val = @value.perform(environment) + @resolved_value = + if @value.context == :equals && val.is_a?(Sass::Script::String) + val.value + else + val.to_s + end super end # Returns an error message if the given child node is invalid, # and false otherwise. @@ -122,18 +157,60 @@ def check! if @options[:property_syntax] == :old && @prop_syntax == :new raise Sass::SyntaxError.new("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.") elsif @options[:property_syntax] == :new && @prop_syntax == :old raise Sass::SyntaxError.new("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.") - elsif value[-1] == ?; - raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no \";\" required at end-of-line).") - elsif value.empty? + elsif resolved_value.empty? raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no value)." + pseudo_class_selector_message) end end def declaration - (@prop_syntax == :new ? "#{name}: #{value}" : ":#{name} #{value}").strip + if @prop_syntax == :new + "#{resolved_name}: #{resolved_value}" + else + ":#{resolved_name} #{resolved_value}" + end.strip + end + + class << self + # @private + def val_to_sass(value) + return value.to_sass unless value.context == :equals + val_to_sass_comma(value).to_sass + end + + private + + def val_to_sass_comma(node) + return node unless node.is_a?(Sass::Script::Operation) + return val_to_sass_concat(node) unless node.operator == :comma + + Sass::Script::Operation.new( + val_to_sass_concat(node.operand1), + val_to_sass_comma(node.operand2), + node.operator) + end + + def val_to_sass_concat(node) + return node unless node.is_a?(Sass::Script::Operation) + return val_to_sass_div(node) unless node.operator == :concat + + Sass::Script::Operation.new( + val_to_sass_div(node.operand1), + val_to_sass_concat(node.operand2), + node.operator) + end + + def val_to_sass_div(node) + unless node.is_a?(Sass::Script::Operation) && node.operator == :div && + node.operand1.is_a?(Sass::Script::Number) && + node.operand2.is_a?(Sass::Script::Number) + return node + end + + Sass::Script::String.new("(#{node.to_sass})") + end end end end