| Class | Sass::Tree::RuleNode |
| In: |
lib/sass/css.rb
lib/sass/tree/rule_node.rb |
| Parent: | Object |
| PARENT | = | '&' | The character used to include the parent selector |
| parsed_rules | [RW] |
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`). For example,
&.foo, bar, baz,
bip, &.bop, bup
would be
[[[:parent, "foo"], ["bar"], ["baz"]],
[["bip"], [:parent, "bop"], ["bup"]]]
@return [Array<Array<Array<String|Symbol>>>] |
| rules | [RW] |
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"]
@return [Array<String>] |
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
# File lib/sass/tree/rule_node.rb, line 56
56: def ==(other)
57: self.class == other.class && rules == other.rules && super
58: end
@return [Boolean] Whether or not this rule is continued on the next line
# File lib/sass/tree/rule_node.rb, line 68
68: def continued?
69: @rules.last[-1] == ?,
70: end
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 @raise [Sass::SyntaxError] if the rule has no parents but uses `&`
# File lib/sass/tree/rule_node.rb, line 79
79: def to_s(tabs, super_rules = nil)
80: resolved_rules = resolve_parent_refs(super_rules)
81:
82: properties = []
83: sub_rules = []
84:
85: rule_separator = style == :compressed ? ',' : ', '
86: line_separator = [:nested, :expanded].include?(style) ? ",\n" : rule_separator
87: rule_indent = ' ' * (tabs - 1)
88: per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent]
89:
90: total_rule = total_indent + resolved_rules.map do |line|
91: per_rule_indent + line.join(rule_separator)
92: end.join(line_separator)
93:
94: children.each do |child|
95: next if child.invisible?
96: if child.is_a? RuleNode
97: sub_rules << child
98: else
99: properties << child
100: end
101: end
102:
103: to_return = ''
104: if !properties.empty?
105: old_spaces = ' ' * (tabs - 1)
106: spaces = ' ' * tabs
107: if @options[:line_comments] && style != :compressed
108: to_return << "#{old_spaces}/* line #{line}"
109:
110: if filename
111: relative_filename = if @options[:css_filename]
112: begin
113: Pathname.new(filename).relative_path_from(
114: Pathname.new(File.dirname(@options[:css_filename]))).to_s
115: rescue ArgumentError
116: nil
117: end
118: end
119: relative_filename ||= filename
120: to_return << ", #{relative_filename}"
121: end
122:
123: to_return << " */\n"
124: end
125:
126: if style == :compact
127: properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(' ')
128: to_return << "#{total_rule} { #{properties} }\n"
129: elsif style == :compressed
130: properties = properties.map { |a| a.to_s(1) }.select{|a| a && a.length > 0}.join(';')
131: to_return << "#{total_rule}{#{properties}}"
132: else
133: properties = properties.map { |a| a.to_s(tabs + 1) }.select{|a| a && a.length > 0}.join("\n")
134: end_props = (style == :expanded ? "\n" + old_spaces : ' ')
135: to_return << "#{total_rule} {\n#{properties}#{end_props}}\n"
136: end
137: end
138:
139: tabs += 1 unless properties.empty? || style != :nested
140: sub_rules.each do |sub|
141: to_return << sub.to_s(tabs, resolved_rules)
142: end
143:
144: to_return
145: end
@see Node#to_sass
# File lib/sass/css.rb, line 26
26: def to_sass(tabs, opts = {})
27: str = "\n#{' ' * tabs}#{rules.first}#{children.any? { |c| c.is_a? PropNode } ? "\n" : ''}"
28:
29: children.each do |child|
30: str << "#{child.to_sass(tabs + 1, opts)}"
31: end
32:
33: str
34: end
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
# File lib/sass/tree/rule_node.rb, line 154
154: def perform!(environment)
155: @parsed_rules = @rules.map {|r| parse_selector(interpolate(r, environment))}
156: super
157: end