2006-12-04 08:41:09 +00:00
|
|
|
module Sass::Tree
|
2009-04-25 13:12:04 -07:00
|
|
|
# A static node reprenting a CSS property.
|
|
|
|
#
|
|
|
|
# @see Sass::Tree
|
2009-06-19 03:49:40 -07:00
|
|
|
class PropNode < Node
|
2009-04-25 13:12:04 -07:00
|
|
|
# The name of the property.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
# The value of the property,
|
|
|
|
# either a plain string or a SassScript parse tree.
|
|
|
|
#
|
|
|
|
# @return [String, Script::Node]
|
|
|
|
attr_accessor :value
|
|
|
|
|
|
|
|
# @param name [String] See \{#name}
|
|
|
|
# @param value [String] See \{#value}
|
2009-06-19 03:33:03 -07:00
|
|
|
# @param prop_syntax [Symbol] `:new` if this property uses `a: b`-style syntax,
|
2009-04-25 13:19:57 -07:00
|
|
|
# `:old` if it uses `:a b`-style syntax
|
2009-06-19 03:33:03 -07:00
|
|
|
def initialize(name, value, prop_syntax)
|
2006-12-04 08:41:09 +00:00
|
|
|
@name = name
|
2008-10-13 09:49:35 -07:00
|
|
|
@value = value
|
2009-06-19 03:33:03 -07:00
|
|
|
@prop_syntax = prop_syntax
|
2009-04-21 19:25:18 -07:00
|
|
|
super()
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-25 13:12:04 -07:00
|
|
|
# Compares the names and values of two properties.
|
|
|
|
#
|
|
|
|
# @param other [Object] The object to compare with
|
|
|
|
# @return [Boolean] Whether or not this node and the other object
|
|
|
|
# are the same
|
2009-02-19 20:05:11 -08:00
|
|
|
def ==(other)
|
2009-02-19 20:12:23 -08:00
|
|
|
self.class == other.class && name == other.name && value == other.value && super
|
2009-02-19 20:05:11 -08:00
|
|
|
end
|
|
|
|
|
2009-04-25 14:16:59 -07:00
|
|
|
# Computes the CSS for the property.
|
2009-04-25 13:12:04 -07:00
|
|
|
#
|
|
|
|
# @param tabs [Fixnum] The level of indentation for the CSS
|
|
|
|
# @param parent_name [String] The name of the parent property (e.g. `text`) or nil
|
|
|
|
# @return [String] The resulting CSS
|
2009-06-19 03:33:03 -07:00
|
|
|
# @raise [Sass::SyntaxError] if the property uses invalid syntax
|
2007-08-12 10:54:35 +00:00
|
|
|
def to_s(tabs, parent_name = nil)
|
2009-09-13 15:52:59 -07:00
|
|
|
raise Sass::SyntaxError.new("Illegal property syntax: can't use new syntax when :property_syntax => :old is set.",
|
|
|
|
:line => @line) if @options[:property_syntax] == :old && @prop_syntax == :new
|
|
|
|
raise Sass::SyntaxError.new("Illegal property syntax: can't use old syntax when :property_syntax => :new is set.",
|
|
|
|
:line => @line) if @options[:property_syntax] == :new && @prop_syntax == :old
|
|
|
|
raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no \";\" required at end-of-line).",
|
|
|
|
:line => @line) if value[-1] == ?;
|
|
|
|
raise Sass::SyntaxError.new("Invalid property: #{declaration.dump} (no value).",
|
|
|
|
:line => @line) if value.empty? && children.empty?
|
2009-04-15 20:25:17 -07:00
|
|
|
|
2006-12-22 05:11:03 +00:00
|
|
|
real_name = name
|
|
|
|
real_name = "#{parent_name}-#{real_name}" if parent_name
|
2009-09-13 15:52:59 -07:00
|
|
|
|
2009-04-21 18:39:34 -07:00
|
|
|
join_string = case style
|
2007-12-26 02:22:23 +00:00
|
|
|
when :compact; ' '
|
|
|
|
when :compressed; ''
|
2007-09-07 09:00:58 +00:00
|
|
|
else "\n"
|
|
|
|
end
|
2007-08-12 10:54:35 +00:00
|
|
|
spaces = ' ' * (tabs - 1)
|
2007-03-18 04:33:01 +00:00
|
|
|
to_return = ''
|
|
|
|
if !value.empty?
|
2009-04-21 18:39:34 -07:00
|
|
|
to_return << "#{spaces}#{real_name}:#{style == :compressed ? '' : ' '}#{value};#{join_string}"
|
2007-03-18 04:33:01 +00:00
|
|
|
end
|
2008-10-13 09:49:35 -07:00
|
|
|
|
2007-03-18 04:33:01 +00:00
|
|
|
children.each do |kid|
|
2009-05-08 13:42:39 -07:00
|
|
|
next if kid.invisible?
|
2009-02-21 18:09:14 -08:00
|
|
|
to_return << kid.to_s(tabs, real_name) << join_string
|
2006-12-22 05:11:03 +00:00
|
|
|
end
|
2008-10-13 09:49:35 -07:00
|
|
|
|
2009-04-21 18:39:34 -07:00
|
|
|
(style == :compressed && parent_name) ? to_return : to_return[0...-1]
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
2007-02-04 05:37:27 +00:00
|
|
|
|
2008-10-13 09:49:35 -07:00
|
|
|
protected
|
2009-04-25 13:12:04 -07:00
|
|
|
|
|
|
|
# Runs any SassScript that may be embedded in the property.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The lexical environment containing
|
|
|
|
# variable and mixin values
|
2008-10-13 09:49:35 -07:00
|
|
|
def perform!(environment)
|
|
|
|
@name = interpolate(@name, environment)
|
|
|
|
@value = @value.is_a?(String) ? interpolate(@value, environment) : @value.perform(environment).to_s
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-04-25 13:12:04 -07:00
|
|
|
# Returns an error message if the given child node is invalid,
|
|
|
|
# and false otherwise.
|
|
|
|
#
|
2009-06-19 03:49:40 -07:00
|
|
|
# {PropNode} only allows other {PropNode}s and {CommentNode}s as children.
|
2009-04-25 13:12:04 -07:00
|
|
|
# @param child [Tree::Node] A potential child node
|
|
|
|
# @return [String] An error message if the child is invalid, or nil otherwise
|
2007-03-18 04:15:50 +00:00
|
|
|
def invalid_child?(child)
|
2009-06-19 03:49:40 -07:00
|
|
|
if !child.is_a?(PropNode) && !child.is_a?(CommentNode)
|
2009-06-19 03:33:03 -07:00
|
|
|
"Illegal nesting: Only properties may be nested beneath properties."
|
2007-03-18 04:15:50 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-25 13:12:04 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def declaration
|
2009-06-19 03:33:03 -07:00
|
|
|
@prop_syntax == :new ? "#{name}: #{value}" : ":#{name} #{value}"
|
2009-04-25 13:12:04 -07:00
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
end
|