2006-12-04 08:41:09 +00:00
|
|
|
module Sass::Tree
|
2008-10-13 09:49:35 -07:00
|
|
|
class AttrNode < Node
|
|
|
|
attr_accessor :name, :value
|
|
|
|
|
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-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
|
|
|
|
|
2007-08-12 10:54:35 +00:00
|
|
|
def to_s(tabs, parent_name = nil)
|
2009-04-15 20:25:17 -07:00
|
|
|
if @options[:attribute_syntax] == :normal && @attr_syntax == :new
|
|
|
|
raise Sass::SyntaxError.new("Illegal attribute syntax: can't use alternate syntax when :attribute_syntax => :normal is set.")
|
|
|
|
elsif @options[:attribute_syntax] == :alternate && @attr_syntax == :old
|
|
|
|
raise Sass::SyntaxError.new("Illegal attribute syntax: can't use normal syntax when :attribute_syntax => :alternate is set.")
|
|
|
|
end
|
|
|
|
|
2007-05-03 08:46:02 +00:00
|
|
|
if value[-1] == ?;
|
2008-04-19 10:16:10 -07:00
|
|
|
raise Sass::SyntaxError.new("Invalid attribute: #{declaration.dump} (This isn't CSS!).", @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?
|
2008-04-19 10:16:10 -07:00
|
|
|
raise Sass::SyntaxError.new("Invalid attribute: #{declaration.dump}.", @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
|
|
|
|
|
|
|
|
def perform!(environment)
|
|
|
|
@name = interpolate(@name, environment)
|
|
|
|
@value = @value.is_a?(String) ? interpolate(@value, environment) : @value.perform(environment).to_s
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2007-02-04 05:37:27 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def declaration
|
|
|
|
":#{name} #{value}"
|
|
|
|
end
|
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
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
end
|