2006-12-04 08:40:23 +00:00
|
|
|
module Sass
|
|
|
|
module Tree
|
|
|
|
class Node
|
2007-01-28 10:14:15 +00:00
|
|
|
attr_accessor :children
|
|
|
|
attr_accessor :line
|
2007-03-25 04:57:03 +00:00
|
|
|
attr_accessor :filename
|
2009-04-21 19:13:49 -07:00
|
|
|
attr_reader :options
|
2006-12-04 08:40:23 +00:00
|
|
|
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize
|
2006-12-04 08:40:23 +00:00
|
|
|
@children = []
|
|
|
|
end
|
|
|
|
|
2009-04-21 19:13:49 -07:00
|
|
|
def options=(options)
|
|
|
|
children.each {|c| c.options = options}
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2006-12-04 08:40:23 +00:00
|
|
|
def <<(child)
|
2007-03-18 04:15:50 +00:00
|
|
|
if msg = invalid_child?(child)
|
|
|
|
raise Sass::SyntaxError.new(msg, child.line)
|
|
|
|
end
|
2006-12-04 08:40:23 +00:00
|
|
|
@children << child
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2008-10-14 23:10:41 -07:00
|
|
|
# We need this because Node duck types as an Array in engine.rb
|
|
|
|
def last
|
|
|
|
children.last
|
|
|
|
end
|
|
|
|
|
2009-02-19 20:05:11 -08:00
|
|
|
def ==(other)
|
|
|
|
self.class == other.class && other.children == children
|
|
|
|
end
|
|
|
|
|
2006-12-04 08:40:23 +00:00
|
|
|
def to_s
|
|
|
|
result = String.new
|
|
|
|
children.each do |child|
|
2007-01-28 10:14:15 +00:00
|
|
|
if child.is_a? AttrNode
|
2008-10-13 09:49:35 -07:00
|
|
|
raise Sass::SyntaxError.new('Attributes aren\'t allowed at the root of a document.', child.line)
|
2007-08-10 05:21:56 +00:00
|
|
|
else
|
2009-02-21 18:09:14 -08:00
|
|
|
child_str = child.to_s(1)
|
|
|
|
next unless child_str && child_str.length > 0
|
2009-04-21 18:39:34 -07:00
|
|
|
result << child_str + (style == :compressed ? '' : "\n")
|
2007-03-25 04:57:03 +00:00
|
|
|
end
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2009-04-21 18:39:34 -07:00
|
|
|
style == :compressed ? result+"\n" : result[0...-1]
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
2007-03-18 04:15:50 +00:00
|
|
|
|
2008-10-13 09:49:35 -07:00
|
|
|
def perform(environment)
|
|
|
|
_perform(environment)
|
|
|
|
rescue Sass::SyntaxError => e
|
|
|
|
e.sass_line ||= line
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
|
2009-04-21 19:13:49 -07:00
|
|
|
def style
|
|
|
|
@options[:style]
|
|
|
|
end
|
|
|
|
|
2008-10-13 09:49:35 -07:00
|
|
|
protected
|
|
|
|
|
|
|
|
def _perform(environment)
|
|
|
|
node = dup
|
|
|
|
node.perform!(environment)
|
|
|
|
node
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform!(environment)
|
2008-10-15 20:00:28 -07:00
|
|
|
self.children = perform_children(Environment.new(environment))
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_children(environment)
|
|
|
|
children.map {|c| c.perform(environment)}.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def interpolate(text, environment)
|
2009-01-09 14:54:17 -08:00
|
|
|
res = ''
|
|
|
|
rest = Haml::Shared.handle_interpolation text do |scan|
|
2008-10-13 09:49:35 -07:00
|
|
|
escapes = scan[2].size
|
2009-01-09 14:54:17 -08:00
|
|
|
res << scan.matched[0...-2 - escapes]
|
2008-10-13 09:49:35 -07:00
|
|
|
if escapes % 2 == 1
|
2009-01-09 14:54:17 -08:00
|
|
|
res << "\\" * (escapes - 1) << '#{'
|
2008-10-13 09:49:35 -07:00
|
|
|
else
|
2009-01-09 14:54:17 -08:00
|
|
|
res << "\\" * [0, escapes - 1].max
|
2009-04-07 14:21:13 +08:00
|
|
|
res << Script::Parser.new(scan, line, scan.pos - scan.matched_size, filename).
|
2009-01-09 14:54:17 -08:00
|
|
|
parse_interpolated.perform(environment).to_s
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
|
|
|
end
|
2009-01-09 14:54:17 -08:00
|
|
|
res + rest
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
2009-01-09 14:54:17 -08:00
|
|
|
|
2008-10-13 09:49:35 -07:00
|
|
|
def balance(*args)
|
|
|
|
res = Haml::Shared.balance(*args)
|
|
|
|
return res if res
|
|
|
|
raise Sass::SyntaxError.new("Unbalanced brackets.", line)
|
|
|
|
end
|
|
|
|
|
2007-03-18 04:15:50 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# This method should be overridden by subclasses to return an error message
|
|
|
|
# if the given child node is invalid,
|
|
|
|
# and false or nil otherwise.
|
|
|
|
def invalid_child?(child)
|
|
|
|
false
|
|
|
|
end
|
2006-12-04 08:40:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|