Browse files

correctly handle css @import rules ( fix ##10 )

  • Loading branch information...
1 parent 78d7735 commit 5ab3ff4d2948fb5c88afcc8c40cff2419717a43b @thomaspierson committed Feb 3, 2013
Showing with 51 additions and 6 deletions.
  1. +15 −6 lib/css2less.rb
  2. +36 −0 spec/css2less_spec.rb
View
21 lib/css2less.rb
@@ -75,6 +75,11 @@ def generate_tree
if rules.include?('>')
rules = rules.gsub(/\s*>\s*/, ' &>')
end
+ if rules.include?("@import")
+ import_rule = rules.match(/@import.*;/)[0]
+ rules = rules.gsub(/@import.*;/, '')
+ add_rule(@tree, [], import_rule)
+ end
# leave multiple rules alone
if rules.include?(',')
add_rule(@tree, [rules], style[1])
@@ -92,13 +97,17 @@ def render_less(tree=nil, indent=0)
tree = @tree
end
tree.each do |element, children|
- @less = @less + ' ' * indent + element + " {\n"
- style = children.delete(:style)
- if style
- @less = @less + style.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| ' ' * (indent+4) + s + ";" }.join("\n") + "\n"
+ if element == :style
+ @less = @less + children.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| s + ";" }.join("\n") + "\n"
+ else
+ @less = @less + ' ' * indent + element + " {\n"
+ style = children.delete(:style)
+ if style
+ @less = @less + style.split(';').map { |s| s.strip }.reject { |s| s.empty? }.map { |s| ' ' * (indent+4) + s + ";" }.join("\n") + "\n"
+ end
+ render_less(children, indent + 4)
+ @less = @less + ' ' * indent + "}\n"
end
- render_less(children, indent + 4)
- @less = @less + ' ' * indent + "}\n"
end
end
View
36 spec/css2less_spec.rb
@@ -57,4 +57,40 @@
converter.get_less.should eq(less)
end
+ it "should correctly handle css @import rules" do
+ css = <<EOF
+@import "style1.css";
+@import "style2.css";
+
+@import "style3.css";
+
+#hello {
+ color: blue;
+}
+
+@import "style4.css";
+
+#hello #buddy {
+ background: red;
+}
+@import "style5.css";
+EOF
+ less = <<EOF
+@import "style1.css";
+@import "style2.css";
+@import "style3.css";
+@import "style4.css";
+@import "style5.css";
+#hello {
+ color: blue;
+ #buddy {
+ background: red;
+ }
+}
+EOF
+ converter = Css2Less::Converter.new(css)
+ converter.process_less
+ converter.get_less.should eq(less)
+ end
+
end

0 comments on commit 5ab3ff4

Please sign in to comment.