Permalink
Browse files

Make media a first class node.

  • Loading branch information...
1 parent d63a616 commit e8de2027755908215cb9decbcade5ad2cf9f86ec @chriseppstein committed Sep 23, 2010
Showing with 141 additions and 29 deletions.
  1. +3 −0 lib/sass/engine.rb
  2. +1 −1 lib/sass/scss/parser.rb
  3. +0 −28 lib/sass/tree/directive_node.rb
  4. +27 −0 lib/sass/tree/media_node.rb
  5. +110 −0 test/sass/engine_test.rb
View
@@ -7,6 +7,7 @@
require 'sass/tree/comment_node'
require 'sass/tree/prop_node'
require 'sass/tree/directive_node'
+require 'sass/tree/media_node'
require 'sass/tree/variable_node'
require 'sass/tree/mixin_def_node'
require 'sass/tree/mixin_node'
@@ -625,6 +626,8 @@ def parse_directive(parent, line, root)
:line => @line + 1) unless line.children.empty?
offset = line.offset + line.text.index(value).to_i
Tree::WarnNode.new(parse_script(value, :offset => offset))
+ elsif directive == "media"
+ Tree::MediaNode.new(line.text)
else
Tree::DirectiveNode.new(line.text)
end
View
@@ -240,7 +240,7 @@ def use_css_import?; false; end
def media_directive
val = str {media_query_list}.strip
- block(node(Sass::Tree::DirectiveNode.new("@media #{val}")), :directive)
+ block(node(Sass::Tree::MediaNode.new("@media #{val}")), :directive)
end
# http://www.w3.org/TR/css3-mediaqueries/#syntax
@@ -28,34 +28,6 @@ def rest
value.split(/\s+/, 2).last
end
- def media?
- name == "@media"
- end
-
- def bubbles?(parent)
- 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
@@ -0,0 +1,27 @@
+module Sass::Tree
+ class MediaNode < DirectiveNode
+
+ def bubbles?(parent)
+ !parent.is_a?(MediaNode)
+ end
+
+ def merges?(parent)
+ parent.is_a?(MediaNode)
+ end
+
+ def query
+ rest.strip
+ end
+
+ def merge_with(node)
+ unless node.is_a?(MediaNode)
+ raise ArgumentError, "Cannot merge with #{node.inspect}"
+ end
+ n = MediaNode.new("@media (#{self.query}) and (#{node.query})")
+ n.children = node.children
+ n.options = options
+ n
+ end
+
+ end
+end
View
@@ -2210,6 +2210,116 @@ def test_utf32be_bom
end
end
+ def test_media_directives_bubble_up_to_the_top_level
+ assert_equal <<CSS, render(<<SASS)
+.outside {
+ color: red; }
+
+@media print {
+ .outside {
+ color: black; }
+ .outside .inside {
+ border: 1px solid black; } }
+
+.outside {
+ background: blue; }
+ .outside .middle {
+ display: block; }
+CSS
+.outside
+ color: red
+ @media print
+ color: black
+ .inside
+ border: 1px solid black
+ background: blue
+ .middle
+ display: block
+SASS
+ end
+
+ def test_nested_media
+ sass_str = <<SASS
+.outside
+ color: red
+ @media print
+ color: black
+ @media nested
+ .inside
+ border: 1px solid black
+ background: blue
+ .middle
+ display: block
+SASS
+ 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(sass_str)
+ end
+
+ def test_nested_media_around_properties
+ sass_str = <<SASS
+.outside
+ color: red
+ @media print
+ color: black
+ .inside
+ @media nested
+ border: 1px solid black
+ background: blue
+ .middle
+ display: block
+SASS
+ 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(sass_str)
+ end
+
+ def test_media_with_parent_references
+ sass_str = <<SASS
+.outside
+ @media print
+ &.inside
+ border: 1px solid black
+SASS
+ css_str = <<CSS
+@media print {
+ .outside.inside {
+ border: 1px solid black; } }
+CSS
+ assert_equal css_str, render(sass_str)
+ end
+
+
private
def assert_hash_has(hash, expected)

0 comments on commit e8de202

Please sign in to comment.