Permalink
Browse files

correctly handle css child selectors ( fix #12 )

  • Loading branch information...
thomaspierson committed Feb 3, 2013
1 parent fd29a36 commit 78d7735df1574bf7c579f0bb8c341368664f59ce
Showing with 34 additions and 1 deletion.
  1. +8 −1 lib/css2less.rb
  2. +26 −0 spec/css2less_spec.rb
View
@@ -71,11 +71,18 @@ def add_rule(tree, selectors, style)
def generate_tree
@css.split("\n").map { |l| l.strip }.join.gsub(/\/\*+[^\*]*\*+\//, '').split(/[\{\}]/).each_slice(2) do |style|
rules = style[0].strip
+ # handle child selector case - step1
+ if rules.include?('>')
+ rules = rules.gsub(/\s*>\s*/, ' &>')
+ end
# leave multiple rules alone
if rules.include?(',')
add_rule(@tree, [rules], style[1])
else
- add_rule(@tree, rules.split(/\s+/), style[1])
+ rules_split = rules.split(/\s+/)
+ # handle child selector case - step2
+ rules_split.map! {|rule| rule.gsub('&>', '& > ')}
+ add_rule(@tree, rules_split, style[1])
end
end
end
View
@@ -25,6 +25,32 @@
background: red;
}
}
+EOF
+ converter = Css2Less::Converter.new(css)
+ converter.process_less
+ converter.get_less.should eq(less)
+ end
+
+ it "should correctly handle css child selectors" do
+ css = <<EOF
+body .navbar .nav > li > a {
+ color: #333;
+ font-weight: bold;
+}
+EOF
+ less = <<EOF
+body {
+ .navbar {
+ .nav {
+ & > li {
+ & > a {
+ color: #333;
+ font-weight: bold;
+ }
+ }
+ }
+ }
+}
EOF
converter = Css2Less::Converter.new(css)
converter.process_less

0 comments on commit 78d7735

Please sign in to comment.