Class: Sass::Tree::RuleNode
Overview
A static node reprenting a CSS rule.
Instance Attribute Summary collapse
-
#group_end ⇒ Boolean
Whether or not this rule is the last rule in a nested group.
-
#parsed_rules ⇒ Array<Array<String, Symbol>>
The CSS selectors for this rule, parsed for commas and parent-references.
-
#resolved_rules ⇒ Array<String>
The CSS selectors for this rule, with all nesting and parent references resolved.
-
#rule ⇒ Array<String, Sass::Script::Node>
The CSS selector for this rule, interspersed with Script::Nodes representing
#{}
-interpolation. -
#tabs ⇒ Fixnum
How deep this rule is indented relative to a base-level rule.
Attributes inherited from Node
#children, #filename, #has_children, #line, #options
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the contents of two rules.
-
#_cssize(parent)
protected
Converts nested rules into a flat list of rules.
-
#_to_s(tabs) ⇒ String
protected
Computes the CSS for the rule.
-
#add_rules(node)
Adds another RuleNode's rules to this one's.
-
#continued? ⇒ Boolean
Whether or not this rule is continued on the next line.
-
#cssize!(parent)
protected
Resolves parent references and nested selectors, and updates the indentation based on the parent's indentation.
-
#debug_info ⇒ {#to_s => #to_s}
protected
A hash that will be associated with this rule in the CSS document if the
:debug_info
option is enabled. -
#initialize(rule) ⇒ RuleNode
constructor
A new instance of RuleNode.
-
#perform!(environment)
protected
Runs any SassScript that may be embedded in the rule, and parses the selectors for commas.
- #to_sass(tabs, opts = {})
- #to_scss(tabs, opts = {})
Methods inherited from Node
#<<, #_perform, #balance, #children_to_src, #cssize, #dasherize, #each, #invalid_child?, #invisible?, #perform, #perform_children, #render, #run_interp, #semi, #style, #to_s, #to_src
Constructor Details
#initialize(rule) ⇒ RuleNode
Returns a new instance of RuleNode.
81 82 83 84 85 86 |
# File 'lib/sass/tree/rule_node.rb', line 81
def initialize(rule)
@rule = Haml::Util.strip_string_array(
Haml::Util.merge_adjacent_strings(rule))
@tabs = 0
super()
end
|
Instance Attribute Details
#group_end ⇒ Boolean
Whether or not this rule is the last rule in a nested group. This is only set in a CSS tree.
77 78 79 |
# File 'lib/sass/tree/rule_node.rb', line 77
def group_end
@group_end
end
|
#parsed_rules ⇒ Array<Array<String, Symbol>>
The CSS selectors for this rule, parsed for commas and parent-references. It's only set once Node#perform has been called.
It's an array of arrays.
The first level of arrays comma-separated selectors;
the second represents structure within those selectors,
currently only parent-refs (represented by :parent
).
Newlines are represented as literal \n
characters in the strings.
For example,
&.foo, bar, baz,
bip, &.bop, bup
would be
[[:parent, ".foo"], ["bar"], ["baz"],
["\nbip"], [:parent, ".bop"], ["bup"]]
41 42 43 |
# File 'lib/sass/tree/rule_node.rb', line 41
def parsed_rules
@parsed_rules
end
|
#resolved_rules ⇒ Array<String>
The CSS selectors for this rule, with all nesting and parent references resolved. It's only set once Node#cssize has been called.
Each element is a distinct selector, separated by commas.
Newlines are represented as literal \n
characters in the strings.
For example,
foo bar, baz,
bang, bip bop, blip
would be
["foo bar", "baz", "\nbang", "bip bop", "blip"]
59 60 61 |
# File 'lib/sass/tree/rule_node.rb', line 59
def resolved_rules
@resolved_rules
end
|
#rule ⇒ Array<String, Sass::Script::Node>
The CSS selector for this rule,
interspersed with Script::Nodes
representing #{}
-interpolation.
Any adjacent strings will be merged together.
19 20 21 |
# File 'lib/sass/tree/rule_node.rb', line 19
def rule
@rule
end
|
#tabs ⇒ Fixnum
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
71 72 73 |
# File 'lib/sass/tree/rule_node.rb', line 71
def tabs
@tabs
end
|
Instance Method Details
#==(other) ⇒ Boolean
Compares the contents of two rules.
93 94 95 |
# File 'lib/sass/tree/rule_node.rb', line 93
def ==(other)
self.class == other.class && rule == other.rule && super
end
|
#_cssize(parent) (protected)
Converts nested rules into a flat list of rules.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/sass/tree/rule_node.rb', line 216
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
rules.each {|r| r.tabs += 1} if style == :nested
rules.unshift(node)
end
rules.last.group_end = true unless parent || rules.empty?
rules
end
|
#_to_s(tabs) ⇒ String (protected)
Computes the CSS for the rule.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/sass/tree/rule_node.rb', line 144
def _to_s(tabs)
tabs = tabs + self.tabs
rule_separator = style == :compressed ? ',' : ', '
line_separator =
case style
when :nested, :expanded; "\n"
when :compressed; ""
else; " "
end
rule_indent = ' ' * (tabs - 1)
per_rule_indent, total_indent = [:nested, :expanded].include?(style) ? [rule_indent, ''] : ['', rule_indent]
total_rule = total_indent + resolved_rules.join(rule_separator).split("\n").map do |line|
per_rule_indent + line.strip
end.join(line_separator)
to_return = ''
old_spaces = ' ' * (tabs - 1)
spaces = ' ' * tabs
if style != :compressed
if @options[:debug_info]
to_return << debug_info_rule.to_s(tabs) << "\n"
elsif @options[:line_comments]
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
end
end
relative_filename ||= filename
to_return << ", #{relative_filename}"
end
to_return << " */\n"
end
end
if style == :compact
properties = children.map { |a| a.to_s(1) }.join(' ')
to_return << "#{total_rule} { #{properties} }#{"\n" if group_end}"
elsif style == :compressed
properties = children.map { |a| a.to_s(1) }.join(';')
to_return << "#{total_rule}{#{properties}}"
else
properties = children.map { |a| a.to_s(tabs + 1) }.join("\n")
end_props = (style == :expanded ? "\n" + old_spaces : ' ')
to_return << "#{total_rule} {\n#{properties}#{end_props}}#{"\n" if group_end}"
end
to_return
end
|
#add_rules(node)
Adds another Sass::Tree::RuleNode's rules to this one's.
100 101 102 103 |
# File 'lib/sass/tree/rule_node.rb', line 100
def add_rules(node)
@rule = Haml::Util.strip_string_array(
Haml::Util.merge_adjacent_strings(@rule + ["\n"] + node.rule))
end
|
#continued? ⇒ Boolean
Returns Whether or not this rule is continued on the next line.
106 107 108 109 |
# File 'lib/sass/tree/rule_node.rb', line 106
def continued?
last = @rule.last
last.is_a?(String) && last[-1] == ?,
end
|
#cssize!(parent) (protected)
Resolves parent references and nested selectors, and updates the indentation based on the parent's indentation.
238 239 240 241 |
# File 'lib/sass/tree/rule_node.rb', line 238
def cssize!(parent)
self.resolved_rules = resolve_parent_refs(parent && parent.resolved_rules)
super
end
|
#debug_info ⇒ {#to_s => #to_s} (protected)
A hash that will be associated with this rule in the CSS document
if the :debug_info
option is enabled.
This data is used by e.g. the FireSass Firebug extension.
248 249 250 251 |
# File 'lib/sass/tree/rule_node.rb', line 248
def debug_info
{:filename => filename && ("file://" + URI.escape(File.expand_path(filename))),
:line => self.line}
end
|
#perform!(environment) (protected)
Runs any SassScript that may be embedded in the rule, and parses the selectors for commas.
207 208 209 210 |
# File 'lib/sass/tree/rule_node.rb', line 207
def perform!(environment)
@parsed_rules = parse_selector(run_interp(@rule, environment))
super
end
|
#to_sass(tabs, opts = {})
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/sass/tree/rule_node.rb', line 112
def to_sass(tabs, opts = {})
name = rule.map do |r|
if r.is_a?(String)
r.gsub(/(,[ \t]*)?\n\s*/) {$1 ? $1 + "\n" : " "}
else
"\#{#{r.to_sass(opts)}}"
end
end.join
name = "\\" + name if name[0] == ?:
name.gsub(/^/, ' ' * tabs) + children_to_src(tabs, opts, :sass)
end
|
#to_scss(tabs, opts = {})
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/sass/tree/rule_node.rb', line 124
def to_scss(tabs, opts = {})
name = rule.map {|r| r.is_a?(String) ? r : "\#{#{r.to_sass(opts)}}"}.
join.gsub(/^[ \t]*/, ' ' * tabs)
res = name + children_to_src(tabs, opts, :scss)
if children.last.is_a?(CommentNode) && children.last.silent
res.slice!(-3..-1)
res << "\n" << (' ' * tabs) << "}\n"
end
res
end
|