Make css2sass aware of parent refs.
This commit is contained in:
parent
5aa394dbb7
commit
b45d4a2b64
|
@ -119,7 +119,8 @@ module Sass
|
||||||
directives root
|
directives root
|
||||||
rules root
|
rules root
|
||||||
expand_commas root
|
expand_commas root
|
||||||
nest_rules root
|
parent_ref_rules root
|
||||||
|
remove_parent_refs root
|
||||||
flatten_rules root
|
flatten_rules root
|
||||||
fold_commas root
|
fold_commas root
|
||||||
root
|
root
|
||||||
|
@ -227,7 +228,22 @@ module Sass
|
||||||
root.children.flatten!
|
root.children.flatten!
|
||||||
end
|
end
|
||||||
|
|
||||||
# Nest rules so that
|
# Make rules use parent refs so that
|
||||||
|
#
|
||||||
|
# foo
|
||||||
|
# color: green
|
||||||
|
# foo.bar
|
||||||
|
# color: blue
|
||||||
|
#
|
||||||
|
# becomes
|
||||||
|
#
|
||||||
|
# foo
|
||||||
|
# color: green
|
||||||
|
# &.bar
|
||||||
|
# color: blue
|
||||||
|
#
|
||||||
|
# This has the side effect of nesting rules,
|
||||||
|
# so that
|
||||||
#
|
#
|
||||||
# foo
|
# foo
|
||||||
# color: green
|
# color: green
|
||||||
|
@ -240,29 +256,50 @@ module Sass
|
||||||
#
|
#
|
||||||
# foo
|
# foo
|
||||||
# color: green
|
# color: green
|
||||||
# bar
|
# & bar
|
||||||
# color: red
|
# color: red
|
||||||
# baz
|
# & baz
|
||||||
# color: blue
|
# color: blue
|
||||||
#
|
#
|
||||||
def nest_rules(root)
|
def parent_ref_rules(root)
|
||||||
rules = OrderedHash.new
|
rules = OrderedHash.new
|
||||||
root.children.select { |c| Tree::RuleNode === c }.each do |child|
|
root.children.select { |c| Tree::RuleNode === c }.each do |child|
|
||||||
root.children.delete child
|
root.children.delete child
|
||||||
first, rest = child.rule.split(' ', 2)
|
first, rest = child.rule.scan(/^(&?.[^.#: \[]*)([.#: \[].*)?$/).first
|
||||||
rules[first] ||= Tree::RuleNode.new(first, nil)
|
rules[first] ||= Tree::RuleNode.new(first, nil)
|
||||||
if rest
|
if rest
|
||||||
child.rule = rest
|
child.rule = "&" + rest
|
||||||
rules[first] << child
|
rules[first] << child
|
||||||
else
|
else
|
||||||
rules[first].children += child.children
|
rules[first].children += child.children
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
rules.values.each { |v| nest_rules(v) }
|
rules.values.each { |v| parent_ref_rules(v) }
|
||||||
root.children += rules.values
|
root.children += rules.values
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Remove useless parent refs so that
|
||||||
|
#
|
||||||
|
# foo
|
||||||
|
# & bar
|
||||||
|
# color: blue
|
||||||
|
#
|
||||||
|
# becomes
|
||||||
|
#
|
||||||
|
# foo
|
||||||
|
# bar
|
||||||
|
# color: blue
|
||||||
|
#
|
||||||
|
def remove_parent_refs(root)
|
||||||
|
root.children.each do |child|
|
||||||
|
if child.is_a?(Tree::RuleNode)
|
||||||
|
child.rule.gsub! /^& /, ''
|
||||||
|
remove_parent_refs child
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Flatten rules so that
|
# Flatten rules so that
|
||||||
#
|
#
|
||||||
# foo
|
# foo
|
||||||
|
|
Loading…
Reference in New Issue