class Sass::CSS
Sass::CSS.new(“p { color: blue }”).render(:scss) #=> “p {n color: blue; }”
Sass::CSS.new(“p { color: blue }”).render(:sass) #=> “pn color: blue”
Example usage:
to produce more concise and idiomatic Sass/SCSS.
and then applying various transformations to the structure
It works by parsing the CSS document into a {Sass::Tree} structure,
This class converts CSS documents into Sass or SCSS templates.
def build_tree
-
(Tree::Node)
- The root node of the parsed tree
def build_tree root = Sass::SCSS::CssParser.new(@template).parse expand_commas root parent_ref_rules root remove_parent_refs root flatten_rules root fold_commas root root end
def check_encoding!
def check_encoding! return if @checked_encoding @checked_encoding = true @template, @original_encoding = Haml::Util.check_sass_encoding(@template) do |msg, line| raise Sass::SyntaxError.new(msg, :line => line) end end
def expand_commas(root)
-
root
(Tree::Node
) -- The parent node
def expand_commas(root) root.children.map! do |child| unless child.is_a?(Tree::RuleNode) && child.rule.first.include?(',') expand_commas(child) if child.is_a?(Tree::DirectiveNode) next child end child.rule.first.split(',').map do |rule| node = Tree::RuleNode.new([rule.strip]) node.children = child.children node end end root.children.flatten! end
def flatten_rule(rule)
- See: #flatten_rules -
Parameters:
-
rule
(Tree::RuleNode
) -- The candidate for flattening
def flatten_rule(rule) while rule.children.size == 1 && rule.children.first.is_a?(Tree::RuleNode) child = rule.children.first if child.rule.first[0] == ?& rule.rule = [child.rule.first.gsub(/^&/, rule.rule.first)] else rule.rule = ["#{rule.rule.first} #{child.rule.first}"] end rule.children = child.children end flatten_rules(rule) end
def flatten_rules(root)
-
root
(Tree::Node
) -- The parent node
def flatten_rules(root) root.children.each do |child| case child when Tree::RuleNode flatten_rule(child) when Tree::DirectiveNode flatten_rules(child) end end end
def fold_commas(root)
-
rule
(Tree::RuleNode
) -- The candidate for flattening
def fold_commas(root) prev_rule = nil root.children.map! do |child| unless child.is_a?(Tree::RuleNode) fold_commas(child) if child.is_a?(Tree::DirectiveNode) next child end if prev_rule && prev_rule.children == child.children prev_rule.rule.first << ", #{child.rule.first}" next nil end fold_commas(child) prev_rule = child child end root.children.compact! end
def initialize(template, options = {})
(**options)
-
:old
(Boolean
) --
Parameters:
-
template
(String
) -- The CSS stylesheet.
def initialize(template, options = {}) if template.is_a? IO template = template.read end @options = options.dup # Backwards compatibility @options[:old] = true if @options[:alternate] == false @template = template end
def parent_ref_rules(root)
-
root
(Tree::Node
) -- The parent node
def parent_ref_rules(root) current_rule = nil root.children.map! do |child| unless child.is_a?(Tree::RuleNode) parent_ref_rules(child) if child.is_a?(Tree::DirectiveNode) next child end first, rest = child.rule.first.scan(/\A(&?(?: .|[^ ])[^.#: \[]*)([.#: \[].*)?\Z/m).first if current_rule.nil? || current_rule.rule.first != first current_rule = Tree::RuleNode.new([first]) end if rest child.rule = ["&" + rest] current_rule << child else current_rule.children += child.children end current_rule end root.children.compact! root.children.uniq! root.children.each { |v| parent_ref_rules(v) } end
def remove_parent_refs(root)
-
root
(Tree::Node
) -- The parent node
def remove_parent_refs(root) root.children.each do |child| case child when Tree::RuleNode child.rule.first.gsub! /^& +/, '' remove_parent_refs child when Tree::DirectiveNode remove_parent_refs child end end end
def render(fmt = :sass)
-
(Sass::SyntaxError)
- if there's an error parsing the CSS template
Returns:
-
(String)
- The resulting Sass or SCSS code
Parameters:
-
fmt
(Symbol
) -- `:sass` or `:scss`, designating the format to return.
def render(fmt = :sass) check_encoding! build_tree.send("to_#{fmt}", @options).strip + "\n" rescue Sass::SyntaxError => err err.modify_backtrace(:filename => @options[:filename] || '(css)') raise err end
def source_encoding
-
(ArgumentError)
- if the document uses an unknown encoding with `@charset` -
(Encoding::UndefinedConversionError)
- if the source encoding
Returns:
-
(Encoding, nil)
-
def source_encoding check_encoding! @original_encoding end