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
|
2008-10-13 09:49:35 -07:00
|
|
|
class AttrNode < 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-04-25 13:19:57 -07:00
|
|
|
# @param attr_syntax [Symbol] `:new` if this property uses `a: b`-style syntax,
|
|
|
|
# `:old` if it uses `:a b`-style syntax
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(name, value, attr_syntax)
|
2006-12-04 08:41:09 +00:00
|
|
|
@name = name
|
2008-10-13 09:49:35 -07:00
|
|
|
@value = value
|
2009-04-15 20:25:17 -07:00
|
|
|
@attr_syntax = attr_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-04-25 13:19:57 -07:00
|
|
|
# @raise [Sass::SyntaxError] if the attribute uses invalid syntax
|
2007-08-12 10:54:35 +00:00
|
|
|
def to_s(tabs, parent_name = nil)
|
2009-06-19 03:01:20 -07:00
|
|
|
if @options[:attribute_syntax] == :old && @attr_syntax == :new
|
|
|
|
raise Sass::SyntaxError.new("Illegal attribute syntax: can't use new syntax when :attribute_syntax => :old is set.")
|
|
|
|
elsif @options[:attribute_syntax] == :new && @attr_syntax == :old
|
|
|
|
raise Sass::SyntaxError.new("Illegal attribute syntax: can't use old syntax when :attribute_syntax => :new is set.")
|
2009-04-15 20:25:17 -07:00
|
|
|
end
|
|
|
|
|
2007-05-03 08:46:02 +00:00
|
|
|
if value[-1] == ?;
|
2009-06-01 01:55:42 -07:00
|
|
|
raise Sass::SyntaxError.new("Invalid attribute: #{declaration.dump} (no \";\" required at end-of-line).", @line)
|
2007-02-04 05:37:27 +00:00
|
|
|
end
|
2006-12-22 05:11:03 +00:00
|
|
|
real_name = name
|
|
|
|
real_name = "#{parent_name}-#{real_name}" if parent_name
|
2008-10-13 09:49:35 -07:00
|
|
|
|
2007-03-18 04:33:01 +00:00
|
|
|
if value.empty? && children.empty?
|
2009-06-01 01:55:42 -07:00
|
|
|
raise Sass::SyntaxError.new("Invalid attribute: #{declaration.dump} (no value).", @line)
|
2007-03-18 04:33:01 +00:00
|
|
|
end
|
2008-10-13 09:49:35 -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.
|
|
|
|
#
|
|
|
|
# {AttrNode} only allows other {AttrNode}s and {CommentNode}s as children.
|
|
|
|
# @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)
|
2007-12-07 20:09:16 +00:00
|
|
|
if !child.is_a?(AttrNode) && !child.is_a?(CommentNode)
|
2007-03-18 04:15:50 +00:00
|
|
|
"Illegal nesting: Only attributes may be nested beneath attributes."
|
|
|
|
end
|
|
|
|
end
|
2009-04-25 13:12:04 -07:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def declaration
|
2009-06-01 01:56:36 -07:00
|
|
|
@attr_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
|