Skip to content

Commit

Permalink
Bubble up CSS @imports.
Browse files Browse the repository at this point in the history
Closes #261
  • Loading branch information
nex3 committed Feb 2, 2012
1 parent b66be21 commit f0ca6ba
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions doc-src/SASS_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Make sure `:after` and `:before` selectors end up on the end of
selectors resulting from `@extend`.
* Fix a bug when using imports containing invalid path characters on Windows.
* Bubble CSS `@import` statements to the top of stylesheets.

### Deprecations -- Must Read!

Expand Down
26 changes: 20 additions & 6 deletions lib/sass/tree/visitors/cssize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,26 @@ def with_parent(parent)
def visit_root(node)
yield

# In Ruby 1.9 we can make all @charset nodes invisible
# and infer the final @charset from the encoding of the final string.
if Sass::Util.ruby1_8? && parent.nil?
charset = node.children.find {|c| c.is_a?(Sass::Tree::CharsetNode)}
node.children.reject! {|c| c.is_a?(Sass::Tree::CharsetNode)}
node.children.unshift charset if charset
if parent.nil?
# In Ruby 1.9 we can make all @charset nodes invisible
# and infer the final @charset from the encoding of the final string.
if Sass::Util.ruby1_8?
charset = node.children.find {|c| c.is_a?(Sass::Tree::CharsetNode)}
node.children.reject! {|c| c.is_a?(Sass::Tree::CharsetNode)}
node.children.unshift charset if charset
end

imports = Sass::Util.extract!(node.children) do |c|
c.is_a?(Sass::Tree::DirectiveNode) && c.value =~ /^@import /i
end
charset_and_index = Sass::Util.ruby1_8? &&
node.children.each_with_index.find {|c, _| c.is_a?(Sass::Tree::CharsetNode)}
if charset_and_index
index = charset_and_index.last
node.children = node.children[0..index] + imports + node.children[index+1..-1]
else
node.children = imports + node.children
end
end

return node, @extends
Expand Down
4 changes: 2 additions & 2 deletions test/sass/more_results/more_import.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url(basic.css);
@import url(../results/complex.css);
imported { otherconst: hello; myconst: goodbye; pre-mixin: here; }

body { font: Arial; background: blue; }
Expand All @@ -22,8 +24,6 @@ body { font: Arial; background: blue; }
#content.user.show #container.top #column.right { width: 600px; }
#content.user.show #container.bottom { background: brown; }

@import url(basic.css);
@import url(../results/complex.css);
#foo { background-color: #bbaaff; }

nonimported { myconst: hello; otherconst: goodbye; post-mixin: here; }
4 changes: 2 additions & 2 deletions test/sass/results/import.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url(basic.css);
@import url(../results/complex.css);
imported { otherconst: hello; myconst: goodbye; pre-mixin: here; }

body { font: Arial; background: blue; }
Expand All @@ -24,8 +26,6 @@ body { font: Arial; background: blue; }
#content.user.show #container.top #column.right { width: 600px; }
#content.user.show #container.bottom { background: brown; }

@import url(basic.css);
@import url(../results/complex.css);
#foo { background-color: #bbaaff; }

nonimported { myconst: hello; otherconst: goodbye; post-mixin: here; }
1 change: 1 addition & 0 deletions test/sass/results/import_charset.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@charset "UTF-8";
@import url(foo.css);
.foo { a: b; }

.bar { a: щ; }
1 change: 1 addition & 0 deletions test/sass/results/import_charset_1_8.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@charset "IBM866";
@import url(foo.css);
.foo { a: b; }

.bar { a: é; }
1 change: 1 addition & 0 deletions test/sass/results/import_charset_ibm866.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@charset "IBM866";
@import url(foo.css);
.foo { a: b; }

.bar { a: é; }
4 changes: 2 additions & 2 deletions test/sass/results/scss_import.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url(basic.css);
@import url(../results/complex.css);
imported { otherconst: hello; myconst: goodbye; pre-mixin: here; }

body { font: Arial; background: blue; }
Expand All @@ -24,8 +26,6 @@ body { font: Arial; background: blue; }
#content.user.show #container.top #column.right { width: 600px; }
#content.user.show #container.bottom { background: brown; }

@import url(basic.css);
@import url(../results/complex.css);
#foo { background-color: #bbaaff; }

nonimported { myconst: hello; otherconst: goodbye; post-mixin: here; }
2 changes: 2 additions & 0 deletions test/sass/templates/import_charset.sass
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.foo
a: b

@import "foo.css"

// Even though the imported file is in IBM866,
// since the root file is in UTF-8/ASCII
// the output will end up being UTF-8.
Expand Down
2 changes: 2 additions & 0 deletions test/sass/templates/import_charset_1_8.sass
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.foo
a: b

@import "foo.css"

@import "imported_charset_ibm866"
2 changes: 2 additions & 0 deletions test/sass/templates/import_charset_ibm866.sass
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.foo
a: b

@import "foo.css"

// Even though the imported file is in UTF-8,
// since the root file is in IBM866
// the output will end up being IBM866.
Expand Down

0 comments on commit f0ca6ba

Please sign in to comment.