Skip to content

Commit

Permalink
Implemented parent selector
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesfoster-excelpoint committed May 18, 2011
1 parent e7fefab commit 468e650
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 18 deletions.
37 changes: 31 additions & 6 deletions 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;
Expand All @@ -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
Expand Down
9 changes: 6 additions & 3 deletions src/dotless.Core/Parser/Parsers.cs
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
11 changes: 7 additions & 4 deletions src/dotless.Core/Parser/Tree/Combinator.cs
Expand Up @@ -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();
}
Expand All @@ -24,6 +26,7 @@ public override string ToCSS(Env env)
{ "", "" },
{ " ", " " },
{ "&", "" },
{ "& ", " " },
{ ":", " :" },
{ "::", "::" },
{ "+", env.Compress ? "+" : " + " },
Expand Down
2 changes: 1 addition & 1 deletion src/dotless.Core/Parser/Tree/Element.cs
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/dotless.Core/Parser/Tree/Selector.cs
Expand Up @@ -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 = " ";
Expand Down
34 changes: 33 additions & 1 deletion src/dotless.Test/Specs/SelectorsFixture.cs
Expand Up @@ -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
Expand Down Expand Up @@ -70,6 +70,38 @@ public void ParentSelector2()
AssertLess(input, expected);
}

[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()
{
Expand Down

0 comments on commit 468e650

Please sign in to comment.