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 = '&'
|
|
|
|
|
2010-01-05 20:49:05 -08:00
|
|
|
# The CSS selector for this rule,
|
|
|
|
# interspersed with {Sass::Script::Node}s
|
|
|
|
# representing `#{}`-interpolation.
|
|
|
|
# Any adjacent strings will be merged together.
|
2009-04-25 14:43:39 -07:00
|
|
|
#
|
2010-01-05 20:49:05 -08:00
|
|
|
# @return [Array<String, Sass::Script::Node>]
|
2009-12-21 12:51:09 -08:00
|
|
|
attr_accessor :rule
|
2009-05-10 14:17:54 -07:00
|
|
|
|
|
|
|
# The CSS selectors for this rule,
|
|
|
|
# parsed for commas and parent-references.
|
|
|
|
# It's only set once {Tree::Node#perform} has been called.
|
|
|
|
#
|
2009-12-31 19:50:32 -08:00
|
|
|
# It's an array of arrays.
|
|
|
|
# The first level of arrays comma-separated selectors;
|
|
|
|
# the second represents structure within those selectors,
|
2009-05-10 14:17:54 -07:00
|
|
|
# currently only parent-refs (represented by `:parent`).
|
2009-12-31 19:50:32 -08:00
|
|
|
# Newlines are represented as literal `\n` characters in the strings.
|
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-12-31 19:50:32 -08:00
|
|
|
# [[:parent, ".foo"], ["bar"], ["baz"],
|
|
|
|
# ["\nbip"], [:parent, ".bop"], ["bup"]]
|
2009-04-25 14:43:39 -07:00
|
|
|
#
|
2010-01-05 20:49:05 -08:00
|
|
|
# @return [Array<Array<String, Symbol>>]
|
2009-05-10 14:17:54 -07:00
|
|
|
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.
|
|
|
|
#
|
2009-12-31 19:50:32 -08:00
|
|
|
# Each element is a distinct selector, separated by commas.
|
|
|
|
# Newlines are represented as literal `\n` characters in the strings.
|
2009-11-25 02:22:30 -08:00
|
|
|
# For example,
|
|
|
|
#
|
|
|
|
# foo bar, baz,
|
|
|
|
# bang, bip bop, blip
|
|
|
|
#
|
|
|
|
# would be
|
|
|
|
#
|
2009-12-31 19:50:32 -08:00
|
|
|
# ["foo bar", "baz", "\nbang", "bip bop", "blip"]
|
2009-11-25 02:22:30 -08:00
|
|
|
#
|
2009-12-31 19:50:32 -08:00
|
|
|
# @return [Array<String>]
|
2009-11-25 02:22:30 -08:00
|
|
|
attr_accessor :resolved_rules
|
|
|
|
|
2009-11-25 03:39:43 -08:00
|
|
|
# How deep this rule is indented
|
|
|
|
# relative to a base-level rule.
|
|
|
|
# This is only greater than 0 in the case that:
|
|
|
|
#
|
|
|
|
# * This node is in a CSS tree
|
|
|
|
# * The style is :nested
|
|
|
|
# * This is a child rule of another rule
|
|
|
|
# * The parent rule has properties, and thus will be rendered
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
attr_accessor :tabs
|
|
|
|
|
|
|
|
# Whether or not this rule is the last rule in a nested group.
|
|
|
|
# This is only set in a CSS tree.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
attr_accessor :group_end
|
|
|
|
|
2010-01-05 20:49:05 -08:00
|
|
|
# @param rule [Array<String, Sass::Script::Node>]
|
|
|
|
# The CSS rule. See \{#rule}
|
2009-04-21 19:25:18 -07:00
|
|
|
def initialize(rule)
|
2009-12-21 12:51:09 -08:00
|
|
|
@rule = rule
|
2009-11-25 03:39:43 -08:00
|
|
|
@tabs = 0
|
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)
|
2009-12-21 12:51:09 -08:00
|
|
|
self.class == other.class && rule == other.rule && 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)
|
2010-01-05 20:49:05 -08:00
|
|
|
@rule += ["\n"] + node.rule
|
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?
|
2010-01-05 20:49:05 -08:00
|
|
|
last = @rule.last
|
|
|
|
last.is_a?(String) && 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
|
|
|
|
# @return [String] The resulting CSS
|
2009-11-25 02:22:30 -08:00
|
|
|
def _to_s(tabs)
|
2009-11-25 03:39:43 -08:00
|
|
|
tabs = tabs + self.tabs
|
2007-08-10 05:21:56 +00:00
|
|
|
|
2009-04-21 18:39:34 -07:00
|
|
|
rule_separator = style == :compressed ? ',' : ', '
|
2009-12-21 12:51:09 -08:00
|
|
|
line_separator =
|
|
|
|
case style
|
|
|
|
when :nested, :expanded; "\n"
|
|
|
|
when :compressed; ""
|
|
|
|
else; " "
|
|
|
|
end
|
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-12-21 12:51:09 -08:00
|
|
|
total_rule = total_indent + resolved_rules.join(rule_separator).split("\n").map do |line|
|
|
|
|
per_rule_indent + line.strip
|
2009-03-27 23:13:59 -07:00
|
|
|
end.join(line_separator)
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2006-12-04 08:41:09 +00:00
|
|
|
to_return = ''
|
2009-11-25 03:39:43 -08:00
|
|
|
old_spaces = ' ' * (tabs - 1)
|
|
|
|
spaces = ' ' * tabs
|
|
|
|
if @options[:line_comments] && style != :compressed
|
|
|
|
to_return << "#{old_spaces}/* line #{line}"
|
|
|
|
|
|
|
|
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
|
2008-08-02 09:15:44 -07:00
|
|
|
end
|
2008-07-20 12:12:37 -04:00
|
|
|
end
|
2009-11-25 03:39:43 -08:00
|
|
|
relative_filename ||= filename
|
|
|
|
to_return << ", #{relative_filename}"
|
2008-07-20 00:21:46 -04:00
|
|
|
end
|
|
|
|
|
2009-11-25 03:39:43 -08:00
|
|
|
to_return << " */\n"
|
2006-12-04 08:41:09 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-11-25 03:39:43 -08:00
|
|
|
if style == :compact
|
2010-01-05 17:43:47 -08:00
|
|
|
properties = children.map { |a| a.to_s(1) }.join(' ')
|
2009-11-25 03:39:43 -08:00
|
|
|
to_return << "#{total_rule} { #{properties} }#{"\n" if group_end}"
|
|
|
|
elsif style == :compressed
|
2010-01-05 17:43:47 -08:00
|
|
|
properties = children.map { |a| a.to_s(1) }.join(';')
|
2009-11-25 03:39:43 -08:00
|
|
|
to_return << "#{total_rule}{#{properties}}"
|
|
|
|
else
|
2010-01-05 17:43:47 -08:00
|
|
|
properties = children.map { |a| a.to_s(tabs + 1) }.join("\n")
|
2009-11-25 03:39:43 -08:00
|
|
|
end_props = (style == :expanded ? "\n" + old_spaces : ' ')
|
|
|
|
to_return << "#{total_rule} {\n#{properties}#{end_props}}#{"\n" if group_end}"
|
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)
|
2010-01-05 20:49:05 -08:00
|
|
|
@parsed_rules = parse_selector(run_interp(@rule, environment))
|
2009-04-25 14:43:39 -07:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2009-11-25 03:39:43 -08:00
|
|
|
# Converts nested rules into a flat list of rules.
|
|
|
|
#
|
|
|
|
# @param parent [RuleNode, nil] The parent node of this node,
|
|
|
|
# or nil if the parent isn't a {RuleNode}
|
|
|
|
def _cssize(parent)
|
|
|
|
node = super
|
|
|
|
rules = node.children.select {|c| c.is_a?(RuleNode)}
|
|
|
|
props = node.children.reject {|c| c.is_a?(RuleNode) || c.invisible?}
|
|
|
|
|
|
|
|
unless props.empty?
|
|
|
|
node.children = props
|
2009-11-26 00:51:06 -08:00
|
|
|
rules.each {|r| r.tabs += 1} if style == :nested
|
2009-11-25 03:39:43 -08:00
|
|
|
rules.unshift(node)
|
|
|
|
end
|
|
|
|
|
|
|
|
rules.last.group_end = true unless parent || rules.empty?
|
|
|
|
|
|
|
|
rules
|
|
|
|
end
|
|
|
|
|
|
|
|
# Resolves parent references and nested selectors,
|
|
|
|
# and updates the indentation based on the parent's indentation.
|
2009-11-25 02:22:30 -08:00
|
|
|
#
|
|
|
|
# @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)
|
2009-11-25 03:39:43 -08:00
|
|
|
self.resolved_rules = resolve_parent_refs(parent && parent.resolved_rules)
|
2009-11-25 02:22:30 -08:00
|
|
|
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-12-21 12:51:09 -08:00
|
|
|
return @parsed_rules.map do |rule|
|
|
|
|
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
|
|
|
|
2009-12-21 12:51:09 -08:00
|
|
|
rule.join
|
2009-03-27 23:13:59 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
new_rules = []
|
2009-12-21 12:51:09 -08:00
|
|
|
super_rules.each do |super_rule|
|
|
|
|
@parsed_rules.each do |rule|
|
2009-03-27 23:13:59 -07:00
|
|
|
new_rules << []
|
|
|
|
|
2009-12-21 12:51:09 -08:00
|
|
|
# An initial newline of the child rule
|
|
|
|
# should be moved to the beginning of the entire rule
|
|
|
|
rule.first.slice!(0) if nl = (rule.first.is_a?(String) && rule.first[0] == ?\n)
|
|
|
|
rule = [nl ? "\n" : "", :parent, " ", *rule] unless rule.include?(:parent)
|
2009-03-27 23:13:59 -07:00
|
|
|
|
2009-12-21 12:51:09 -08:00
|
|
|
new_rules.last << rule.map do |segment|
|
|
|
|
next segment unless segment == :parent
|
|
|
|
super_rule
|
|
|
|
end.join
|
2009-03-27 23:13:59 -07:00
|
|
|
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*/)
|
2009-12-21 12:51:09 -08:00
|
|
|
if scanner.rest?
|
|
|
|
rules << []
|
|
|
|
rules.last << "\n" if scanner.matched.include?("\n")
|
|
|
|
end
|
2009-03-27 23:13:59 -07:00
|
|
|
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
|