Browse files

Merge nested media directives during the restructuring phase.

  • Loading branch information...
1 parent 35b8a7b commit 7a45356edd86a8590e1e8d9613d199194844538f @chriseppstein committed Sep 23, 2010
Showing with 84 additions and 11 deletions.
  1. +30 −1 lib/sass/tree/directive_node.rb
  2. +11 −2 lib/sass/tree/node.rb
  3. +43 −8 test/sass/scss/scss_test.rb
View
31 lib/sass/tree/directive_node.rb
@@ -24,9 +24,38 @@ def name
value.split(/\s/, 2).first
end
+ def rest
+ value.split(/\s+/, 2).last
+ end
+
+ def media?
+ name == "@media"
+ end
+
def bubbles?(parent)
- name == "media" && !(parent.is_a?(DirectiveNode) && parent.name == "media")
+ r = media? && !(parent.is_a?(DirectiveNode) && parent.media?)
+ r
+ end
+
+ def merges?(parent)
+ r = media? && parent.is_a?(DirectiveNode) && parent.media?
+ r
end
+
+ def merge_with(node)
+ unless node.is_a?(DirectiveNode) && node.media?
+ raise ArgumentError, "Cannot merge with #{node.inspect}"
+ end
+ query1 = rest.strip
+ query1 = query1[1..-2] if query1[0] == ?( && query1[query1.size - 1] == ?)
+ query2 = node.rest.strip
+ query2 = query2[1..-2] if query2[0] == ?( && query2[query2.size - 1] == ?)
+ n = DirectiveNode.new("@media (#{rest}) and (#{node.rest})")
+ n.children = node.children
+ n.options = options
+ n
+ end
+
protected
# @see Node#to_src
View
13 lib/sass/tree/node.rb
@@ -275,14 +275,14 @@ def _around_dump
# @see Sass::Tree::RootNode#restructure
def restructure
new_children = children.map {|c| c.restructure}.flatten
- unless new_children.any?{|c| c.bubbles?(self)}
+ unless new_children.any?{|c| c.bubbles?(self) || c.merges?(self)}
# optimization path?
self.children = new_children
return [self]
end
child_groups = [[]]
new_children.each do |child|
- if child.bubbles?(self)
+ if child.bubbles?(self) || child.merges?(self)
child_groups << [child]
child_groups << []
else
@@ -297,6 +297,10 @@ def restructure
node, child = children.first, node
child.children = node.children
node.children = [child]
+ elsif children.size == 1 && children.first.merges?(self)
+ # merge the child and parent in the tree
+ node.children = []
+ node = node.merge_with(children.first)
else
node.children = children
end
@@ -310,6 +314,11 @@ def bubbles?(parent)
false
end
+ # Whether this node should bubble up to the next level
+ def merges?(parent)
+ false
+ end
+
protected
# Computes the CSS corresponding to this particular Sass node.
View
51 test/sass/scss/scss_test.rb
@@ -1032,21 +1032,19 @@ def test_media_directives_bubble_up_to_the_top_level
@media print {
.outside {
- color: black;
- }
- .outside .inside {
- border: 1px solid black; } }
+ color: black; }
+ .outside .inside {
+ border: 1px solid black; } }
.outside {
background: blue; }
-
-.outside .middle {
- display: block; }
+ .outside .middle {
+ display: block; }
CSS
.outside {
color: red;
@media print {
- color: back;
+ color: black;
.inside {
border: 1px solid black;
}
@@ -1059,6 +1057,43 @@ def test_media_directives_bubble_up_to_the_top_level
SCSS
end
+ def test_nested_media
+ scss_str = <<SCSS
+.outside {
+ color: red;
+ @media print {
+ color: black;
+ @media nested {
+ .inside {
+ border: 1px solid black;
+ }
+ }
+ }
+ background: blue;
+ .middle {
+ display: block;
+ }
+}
+SCSS
+ css_str = <<CSS
+.outside {
+ color: red; }
+
+@media print {
+ .outside {
+ color: black; } }
+
+@media (print) and (nested) {
+ .outside .inside {
+ border: 1px solid black; } }
+
+.outside {
+ background: blue; }
+ .outside .middle {
+ display: block; }
+CSS
+ assert_equal css_str, render(scss_str)
+ end
# Regression
def test_weird_added_space

0 comments on commit 7a45356

Please sign in to comment.