Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implemented parent selector

  • Loading branch information...
commit 468e650cc7f6b184256061480e32cb2e833e4c13 1 parent e7fefab
@jamesfoster jamesfoster authored
View
37 src/dotless.Core/Parser/Infrastructure/Context.cs
@@ -1,4 +1,6 @@
-namespace dotless.Core.Parser.Infrastructure
+using System;
+
+namespace dotless.Core.Parser.Infrastructure
{
using System.Collections;
using System.Collections.Generic;
@@ -23,12 +25,35 @@ public void AppendSelectors(Context context, IEnumerable<Selector> selectors)
return;
}
- foreach (var selector in selectors)
- {
- Paths.AddRange(context.Paths.Select(path => path.Concat(new[] {selector}).ToList()));
+ foreach (var selector in selectors)
+ {
+ AppendSelector(context, selector);
}
- }
-
+ }
+
+ private void AppendSelector(Context context, Selector selector)
+ {
+ if (!selector.Elements.Any(e => e.Combinator.Value[0] == '&'))
+ {
+ Paths.AddRange(context.Paths.Select(path => path.Concat(new[] {selector}).ToList()));
+ return;
+ }
+
+ var beforeEl = selector.Elements.TakeWhile(s => s.Combinator.Value[0] != '&');
+ var afterEl = selector.Elements.SkipWhile(s => s.Combinator.Value[0] != '&');
+
+ var before = new List<Selector>();
+ var after = new List<Selector>();
+
+ if (beforeEl.Any())
+ before.Add(new Selector(beforeEl));
+
+ if (afterEl.Any())
+ after.Add(new Selector(afterEl));
+
+ Paths.AddRange(context.Paths.Select(path => before.Concat(path).Concat(after).ToList()));
+ }
+
public string ToCSS(Env env)
{
return Paths
View
9 src/dotless.Core/Parser/Parsers.cs
@@ -543,7 +543,10 @@ public Element Element(Parser parser)
parser.Tokenizer.Match(@"\([^)@]+\)");
if (e)
- return NodeProvider.Element(c, e.Value, index);
+ return NodeProvider.Element(c, e.Value, index);
+
+ if (!string.IsNullOrEmpty(c.Value) && c.Value[0] == '&')
+ return NodeProvider.Element(c, null, index);
return null;
}
@@ -561,8 +564,8 @@ public Combinator Combinator(Parser parser)
{
var index = parser.Tokenizer.Location.Index;
- Node match;
- if (match = parser.Tokenizer.Match(@"[+>~]") || parser.Tokenizer.Match('&') || parser.Tokenizer.Match(@"::"))
+ Node match;
+ if (match = parser.Tokenizer.Match(@"[+>~]") || parser.Tokenizer.Match(@"&[ ]?") || parser.Tokenizer.Match(@"::"))
return NodeProvider.Combinator(match.ToString(), index);
return NodeProvider.Combinator(parser.Tokenizer.PreviousChar == ' ' ? " " : null, index);
View
11 src/dotless.Core/Parser/Tree/Combinator.cs
@@ -10,10 +10,12 @@ public class Combinator : Node
public Combinator(string value)
{
- if (string.IsNullOrEmpty(value))
- Value = "";
- else if (value == " ")
- Value = " ";
+ if (string.IsNullOrEmpty(value))
+ Value = "";
+ else if (value == " ")
+ Value = " ";
+ else if (value == "& ")
+ Value = "& ";
else
Value = value.Trim();
}
@@ -24,6 +26,7 @@ public override string ToCSS(Env env)
{ "", "" },
{ " ", " " },
{ "&", "" },
+ { "& ", " " },
{ ":", " :" },
{ "::", "::" },
{ "+", env.Compress ? "+" : " + " },
View
2  src/dotless.Core/Parser/Tree/Element.cs
@@ -11,7 +11,7 @@ public class Element : Node
public Element(Combinator combinator, string value)
{
Combinator = combinator ?? new Combinator("");
- Value = value.Trim();
+ Value = value == null ? "" : value.Trim();
}
public override string ToCSS(Env env)
View
9 src/dotless.Core/Parser/Tree/Selector.cs
@@ -13,9 +13,12 @@ public class Selector : Node
public NodeList<Comment> PreComments { get; set; }
public NodeList<Comment> PostComments { get; set; }
- public Selector(NodeList<Element> elements)
- {
- Elements = elements;
+ public Selector(IEnumerable<Element> elements)
+ {
+ if (elements is NodeList<Element>)
+ Elements = elements as NodeList<Element>;
+ else
+ Elements = new NodeList<Element>(elements);
if (Elements[0].Combinator.Value == "")
Elements[0].Combinator.Value = " ";
View
34 src/dotless.Test/Specs/SelectorsFixture.cs
@@ -33,7 +33,7 @@ public void ParentSelector1()
AssertLess(input, expected);
}
- [Test, Ignore("Unsupported")]
+ [Test]
public void ParentSelector2()
{
// Note: http://github.com/cloudhead/less.js/issues/issue/9
@@ -71,6 +71,38 @@ public void ParentSelector2()
}
[Test]
+ public void ParentSelector3()
+ {
+ // Note: http://github.com/cloudhead/less.js/issues/issue/9
+
+ var input =
+ @"
+.foo {
+ .bar, .baz {
+ & .qux {
+ display: block;
+ }
+ .qux & {
+ display:inline;
+ }
+ }
+}
+";
+
+ var expected =
+ @"
+.foo .bar .qux, .foo .baz .qux {
+ display: block;
+}
+.qux .foo .bar, .qux .foo .baz {
+ display: inline;
+}
+";
+
+ AssertLess(input, expected);
+ }
+
+ [Test]
public void IdSelectors()
{
var input =
Please sign in to comment.
Something went wrong with that request. Please try again.