2009-04-15 16:46:30 -07:00
|
|
|
require 'pathname'
|
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
module Sass::Tree
|
2009-10-13 14:57:46 -07:00
|
|
|
# A static node reprenting a CSS rule.
|
|
|
|
#
|
|
|
|
# @see Sass::Tree
|
2008-10-13 09:49:35 -07:00
|
|
|
class RuleNode < Node
|
2007-03-17 04:17:01 +00:00
|
|
|
# The character used to include the parent selector
|
|
|
|
PARENT = '&'
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# The CSS selectors for this rule.
|
|
|
|
# Each string is a selector line, and the lines are meant to be separated by commas.
|
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# foo, bar, baz,
|
|
|
|
# bip, bop, bup
|
|
|
|
#
|
|
|
|
# would be
|
|
|
|
#
|
|
|
|
# ["foo, bar, baz",
|
|
|
|
# "bip, bop, bup"]
|
|
|
|
#
|
2009-05-10 14:17:54 -07:00
|
|
|
# @return [Array<String>]
|
|
|
|
attr_accessor :rules
|
|
|
|
|
|
|
|
# The CSS selectors for this rule,
|
|
|
|
# parsed for commas and parent-references.
|
|
|
|
# It's only set once {Tree::Node#perform} has been called.
|
|
|
|
#
|
|
|
|
# It's an array of arrays of arrays.
|
|
|
|
# The first level of arrays represents distinct lines in the Sass file;
|
|
|
|
# the second level represents comma-separated selectors;
|
|
|
|
# the third represents structure within those selectors,
|
|
|
|
# currently only parent-refs (represented by `:parent`).
|
2009-04-25 14:43:39 -07:00
|
|
|
# For example,
|
|
|
|
#
|
2009-05-10 14:17:54 -07:00
|
|
|
# &.foo, bar, baz,
|
|
|
|
# bip, &.bop, bup
|
2009-04-25 14:43:39 -07:00
|
|
|
#
|
|
|
|
# would be
|
|
|
|
#
|
2009-11-25 02:22:30 -08:00
|
|
|
# [[[:parent, ".foo"], ["bar"], ["baz"]],
|
|
|
|
# [["bip"], [:parent, ".bop"], ["bup"]]]
|
2009-04-25 14:43:39 -07:00
|
|
|
#
|
2009-05-10 14:17:54 -07:00
|
|
|
# @return [Array<Array<Array<String|Symbol>>>]
|
|
|
|
attr_accessor :parsed_rules
|
2008-10-13 09:49:35 -07:00
|
|
|
|
2009-11-25 02:22:30 -08:00
|
|
|
# The CSS selectors for this rule,
|
|
|
|
# with all nesting and parent references resolved.
|
|
|
|
# It's only set once {Tree::Node#cssize} has been called.
|
|
|
|
#
|
|
|
|
# The first level of arrays represents distinct lines in the Sass file;
|
|
|
|
# the second level represents comma-separated selectors.
|
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# foo bar, baz,
|
|
|
|
# bang, bip bop, blip
|
|
|
|
#
|
|
|
|
# would be
|
|
|
|
#
|
|
|
|
# [["foo bar", "baz"],
|
|
|
|
# ["bang", "bip bop", "blip"]]
|
|
|
|
#
|
|
|
|
# @return [Array<Array<String>>]
|
|
|
|
attr_accessor :resolved_rules
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# @param rule [String] The first CSS rule. See \{#rules}
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(rule)
|
2009-03-27 19:53:08 -07:00
|
|
|
@rules = [rule]
|
2009-04-21 19:25:18 -07:00
|
|
|
super()
|
2008-10-13 09:49:35 -07:00
|
|
|
end
|
2007-08-10 05:21:56 +00:00
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# Compares the contents of two rules.
|
|
|
|
#
|
|
|
|
# @param other [Object] The object to compare with
|
|
|
|
# @return [Boolean] Whether or not this node and the other object
|
|
|
|
# are the same
|
2009-03-27 19:53:08 -07:00
|
|
|
def ==(other)
|
|
|
|
self.class == other.class && rules == other.rules && super
|
2008-03-30 11:15:37 -07:00
|
|
|
end
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# Adds another {RuleNode}'s rules to this one's.
|
|
|
|
#
|
|
|
|
# @param node [RuleNode] The other node
|
2008-04-28 12:13:31 -07:00
|
|
|
def add_rules(node)
|
2009-03-27 19:53:08 -07:00
|
|
|
@rules += node.rules
|
2008-03-30 11:15:37 -07:00
|
|
|
end
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# @return [Boolean] Whether or not this rule is continued on the next line
|
2007-08-10 05:21:56 +00:00
|
|
|
def continued?
|
2009-03-27 19:53:08 -07:00
|
|
|
@rules.last[-1] == ?,
|
2007-08-10 05:21:56 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-09-14 01:34:16 -07:00
|
|
|
protected
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# Computes the CSS for the rule.
|
|
|
|
#
|
|
|
|
# @param tabs [Fixnum] The level of indentation for the CSS
|
|
|
|
# @param super_rules [Array<Array<String>>] The rules for the parent node
|
|
|
|
# (see \{#rules}), or `nil` if there are no parents
|
|
|
|
# @return [String] The resulting CSS
|
2009-11-25 02:22:30 -08:00
|
|
|
def _to_s(tabs)
|
2009-06-19 03:33:03 -07:00
|
|
|
properties = []
|
2006-12-04 08:41:09 +00:00
|
|
|
sub_rules = []
|
2007-08-10 05:21:56 +00:00
|
|
|
|
2009-04-21 18:39:34 -07:00
|
|
|
rule_separator = style == :compressed ? ',' : ', '
|
|
|
|
line_separator = [:nested, :expanded].include?(style) ? ",\n" : rule_separator
|
2008-03-30 11:15:37 -07:00
|
|
|
rule_indent = ' ' * (tabs - 1)
|
2009-04-21 18:39:34 -07:00
|
|
|
per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent]
|
2009-03-27 23:13:59 -07:00
|
|
|
|
2009-05-10 13:46:36 -07:00
|
|
|
total_rule = total_indent + resolved_rules.map do |line|
|
2009-03-27 23:13:59 -07:00
|
|
|
per_rule_indent + line.join(rule_separator)
|
|
|
|
end.join(line_separator)
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
children.each do |child|
|
2009-05-08 13:42:39 -07:00
|
|
|
next if child.invisible?
|
2007-03-17 23:34:26 +00:00
|
|
|
if child.is_a? RuleNode
|
2006-12-04 08:41:09 +00:00
|
|
|
sub_rules << child
|
2007-03-17 23:34:26 +00:00
|
|
|
else
|
2009-06-19 03:33:03 -07:00
|
|
|
properties << child
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
to_return = ''
|
2009-06-19 03:33:03 -07:00
|
|
|
if !properties.empty?
|
2007-08-12 11:00:41 +00:00
|
|
|
old_spaces = ' ' * (tabs - 1)
|
|
|
|
spaces = ' ' * tabs
|
2009-04-21 18:39:34 -07:00
|
|
|
if @options[:line_comments] && style != :compressed
|
2008-07-20 12:12:37 -04:00
|
|
|
to_return << "#{old_spaces}/* line #{line}"
|
|
|
|
|
2008-08-02 09:15:44 -07:00
|
|
|
if filename
|
|
|
|
relative_filename = if @options[:css_filename]
|
|
|
|
begin
|
|
|
|
Pathname.new(filename).relative_path_from(
|
|
|
|
Pathname.new(File.dirname(@options[:css_filename]))).to_s
|
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
relative_filename ||= filename
|
2008-07-20 12:12:37 -04:00
|
|
|
to_return << ", #{relative_filename}"
|
|
|
|
end
|
|
|
|
|
|
|
|
to_return << " */\n"
|
2008-07-20 00:21:46 -04:00
|
|
|
end
|
|
|
|
|
2009-04-21 18:39:34 -07:00
|
|
|
if style == :compact
|
2009-06-19 03:33:03 -07:00
|
|
|
properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(' ')
|
|
|
|
to_return << "#{total_rule} { #{properties} }\n"
|
2009-04-21 18:39:34 -07:00
|
|
|
elsif style == :compressed
|
2009-06-19 03:33:03 -07:00
|
|
|
properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(';')
|
|
|
|
to_return << "#{total_rule}{#{properties}}"
|
2007-02-06 03:34:49 +00:00
|
|
|
else
|
2009-06-19 03:33:03 -07:00
|
|
|
properties = properties.map { |a| a.to_s(tabs + 1) }.select{|a| a && a.length > 0}.join("\n")
|
|
|
|
end_props = (style == :expanded ? "\n" + old_spaces : ' ')
|
|
|
|
to_return << "#{total_rule} {\n#{properties}#{end_props}}\n"
|
2007-02-06 01:58:23 +00:00
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-06-19 03:33:03 -07:00
|
|
|
tabs += 1 unless properties.empty? || style != :nested
|
2007-08-10 05:21:56 +00:00
|
|
|
sub_rules.each do |sub|
|
2009-11-25 02:22:30 -08:00
|
|
|
to_return << sub.to_s(tabs)
|
2007-08-10 05:21:56 +00:00
|
|
|
end
|
2008-03-30 11:15:37 -07:00
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
to_return
|
|
|
|
end
|
2008-10-13 09:49:35 -07:00
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
# Runs any SassScript that may be embedded in the rule,
|
|
|
|
# and parses the selectors for commas.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The lexical environment containing
|
|
|
|
# variable and mixin values
|
|
|
|
def perform!(environment)
|
2009-05-10 14:10:10 -07:00
|
|
|
@parsed_rules = @rules.map {|r| parse_selector(interpolate(r, environment))}
|
2009-04-25 14:43:39 -07:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-11-25 02:22:30 -08:00
|
|
|
# Resolves parent references and nested selectors.
|
|
|
|
#
|
|
|
|
# @param parent [RuleNode, nil] The parent node of this node,
|
|
|
|
# or nil if the parent isn't a {RuleNode}
|
|
|
|
# @raise [Sass::SyntaxError] if the rule has no parents but uses `&`
|
|
|
|
def cssize!(parent)
|
|
|
|
@resolved_rules = resolve_parent_refs(parent && parent.resolved_rules)
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-04-25 14:43:39 -07:00
|
|
|
private
|
|
|
|
|
2009-05-10 13:46:36 -07:00
|
|
|
def resolve_parent_refs(super_rules)
|
2009-03-27 23:13:59 -07:00
|
|
|
if super_rules.nil?
|
2009-05-10 13:46:36 -07:00
|
|
|
return @parsed_rules.map do |line|
|
|
|
|
line.map do |rule|
|
2009-09-14 01:37:24 -07:00
|
|
|
if rule.include?(:parent)
|
|
|
|
raise Sass::SyntaxError.new("Base-level rules cannot contain the parent-selector-referencing character '#{PARENT}'.")
|
|
|
|
end
|
2009-03-27 23:13:59 -07:00
|
|
|
|
|
|
|
rule.join
|
2009-05-10 13:46:36 -07:00
|
|
|
end.compact
|
2009-03-27 23:13:59 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
new_rules = []
|
|
|
|
super_rules.each do |super_line|
|
2009-05-10 13:46:36 -07:00
|
|
|
@parsed_rules.each do |line|
|
2009-03-27 23:13:59 -07:00
|
|
|
new_rules << []
|
|
|
|
|
|
|
|
super_line.each do |super_rule|
|
|
|
|
line.each do |rule|
|
2009-05-10 13:46:36 -07:00
|
|
|
rule = [:parent, " ", *rule] unless rule.include?(:parent)
|
2009-03-27 23:13:59 -07:00
|
|
|
|
|
|
|
new_rules.last << rule.map do |segment|
|
|
|
|
next segment unless segment == :parent
|
|
|
|
super_rule
|
|
|
|
end.join
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-10 13:46:36 -07:00
|
|
|
new_rules
|
2009-03-27 23:13:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse_selector(text)
|
|
|
|
scanner = StringScanner.new(text)
|
|
|
|
rules = [[]]
|
|
|
|
|
|
|
|
while scanner.rest?
|
|
|
|
rules.last << scanner.scan(/[^",&]*/)
|
|
|
|
case scanner.scan(/./)
|
|
|
|
when '&'; rules.last << :parent
|
|
|
|
when ','
|
|
|
|
scanner.scan(/\s*/)
|
|
|
|
rules << [] if scanner.rest?
|
|
|
|
when '"'
|
|
|
|
rules.last << '"' << scanner.scan(/([^"\\]|\\.)*/)
|
|
|
|
# We don't want to enforce that strings are closed,
|
|
|
|
# but we do want to consume quotes or trailing backslashes.
|
|
|
|
rules.last << scanner.scan(/./) if scanner.rest?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
rules.map! do |l|
|
|
|
|
Haml::Util.merge_adjacent_strings(l).reject {|r| r.is_a?(String) && r.empty?}
|
|
|
|
end
|
|
|
|
|
|
|
|
rules
|
|
|
|
end
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
|
|
|
end
|