Skip to content
Browse files

Merge branch 'optional-binding' of https://github.com/schotime/spark

…into schotime-optional-binding
  • Loading branch information...
2 parents 9ffce8e + 5fefcc7 commit 68d0f119047b1efa4f33143fe704aa79c4178768 @RobertTheGrey RobertTheGrey committed Sep 9, 2012
View
10 src/Spark.JsTests/Spark.JsTests.csproj
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -14,7 +15,7 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
- <OldToolsVersion>3.5</OldToolsVersion>
+ <OldToolsVersion>4.0</OldToolsVersion>
<UpgradeBackupLocation />
<TargetFrameworkProfile />
</PropertyGroup>
@@ -180,8 +181,13 @@
<Folder Include="App_Data\" />
<Folder Include="jsunit\logs\" />
</ItemGroup>
+ <PropertyGroup>
+ <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
+ <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
+ </PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
- <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
+ <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
+ <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
View
96 src/Spark.Tests/Bindings/BindingExecutionTester.cs
@@ -99,6 +99,35 @@ public void BindingRefersToAttributeWithMixedCodeAndText()
Assert.That(contents, Is.EqualTo(@"<p>success one 7 two /TestApp three!</p>"));
}
+ [Test]
+ public void BindingRefersToAttributeWithMixedCodeAndTextWithOptional()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='hello'>World('@@foo')</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"<p><hello foo='one ${3+4} two ${SiteRoot} three'/></p><macro name='World' beta='string'>success ${beta}!</macro>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"<p>success one 7 two /TestApp three!</p>"));
+ }
+
+ [Test]
+ public void BindingRefersToAttributeWithMixedCodeAndTextWithOptionalNotSupplied()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='hello'>World('@@foo')</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"<p><hello /></p><macro name='World' beta='string'>success ${beta}!</macro>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"<p>success !</p>"));
+ }
+
+ [Test]
+ public void BindingRefersToAttributeWithMixedCodeAndTextWithAttributeNotSupplied()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='hello'>World('@foo')</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"<p><hello /></p><macro name='World' beta='string'>success ${beta}!</macro>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"<p><hello/></p>"));
+ }
[Test]
public void BindingRefersToAttributeWithUnescapedCode() {
@@ -293,5 +322,72 @@ public void ExpressionNextToBindingShouldMaintainWhiteSpaceWithLoopsInternal()
<li>gamma is okay too I suppose. </li>
</ul>"));
}
+
+ [Test]
+ public void BindingShouldMaintainNewLine()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='Text'>child::*</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"
+<p>
+ <Text>John St</Text>
+</p>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"
+<p>
+ John St
+</p>"));
+ }
+
+ [Test]
+ public void BindingNextToBindingShouldMaintainNewLine()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='Text'>child::*</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"
+<p>
+ <Text>John St</Text>
+ <Text>Smith St</Text>
+</p>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"
+<p>
+ John St
+ Smith St
+</p>"));
+ }
+
+ [Test]
+ public void BindingShouldMaintainNewLineWithNoChild()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='Text'>'@tt'</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"
+<p>
+ <Text tt=""John St"" />
+</p>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"
+<p>
+ John St
+</p>"));
+ }
+
+ [Test]
+ public void BindingShouldMaintainNewLineWithNoChildAndCode()
+ {
+ _viewFolder.Add("bindings.xml", @"<bindings><element name='Text'>'@tt'</element></bindings>");
+ _viewFolder.Add(Path.Combine("home", "index.spark"), @"
+<p>
+ <var t=""23"" />
+ <Text tt=""${t}"" />
+</p>");
+
+ var contents = Render("index");
+ Assert.That(contents, Is.EqualTo(@"
+<p>
+ 23
+</p>"));
+ }
}
}
View
9 src/Spark/Bindings/BindingGrammar.cs
@@ -41,13 +41,20 @@ public BindingGrammar()
.Or(Ch("'@").And(Name).And(Ch('\'')))
.Build(hit => (BindingNode)new BindingNameReference(hit.Left.Down) { AssumeStringValue = true });
+ var stringNameReferenceOptional = Ch("\"@@").And(Name).And(Ch('\"'))
+ .Or(Ch("'@@").And(Name).And(Ch('\'')))
+ .Build(hit => (BindingNode)new BindingNameReference(hit.Left.Down) { AssumeStringValue = true, Optional = true });
+
var rawNameReference = Ch('@').And(Name)
.Build(hit => (BindingNode)new BindingNameReference(hit.Down));
+ var rawNameReferenceOptional = Ch("@@").And(Name)
+ .Build(hit => (BindingNode)new BindingNameReference(hit.Down) { Optional = true });
+
var childReference = Ch("child::*").Or(Ch("'child::*'")).Or(Ch("\"child::*\""))
.Build(hit => (BindingNode)new BindingChildReference());
- NameReference = stringNameReference.Or(rawNameReference);
+ NameReference = stringNameReference.Or(rawNameReference).Or(stringNameReferenceOptional).Or(rawNameReferenceOptional);
var anyReference = PrefixReference.Or(NameReference).Or(childReference);
View
1 src/Spark/Bindings/BindingNode.cs
@@ -27,6 +27,7 @@ public BindingNameReference(string name)
public string Name { get; set; }
public bool AssumeStringValue { get; set; }
+ public bool Optional { get; set; }
}
public class BindingPrefixReference : BindingNode
View
4 src/Spark/Compiler/NodeVisitors/BindingExpansionVisitor.cs
@@ -38,6 +38,8 @@ protected override void Visit(ElementNode element)
return;
}
+ if (!string.IsNullOrEmpty(element.PreceedingWhitespace))
+ Nodes.Add(new TextNode(element.PreceedingWhitespace));
BeginBinding(element, binding);
if (element.IsEmptyElement)
EndBinding();
@@ -132,6 +134,8 @@ private static bool RequiredReferencesSatisfied(Binding binding, ElementNode ele
foreach (var reference in AllNodes(binding).OfType<BindingNameReference>())
{
var nameReference = reference;
+ if (nameReference.Optional)
+ continue;
if (!element.Attributes.Any(attr => attr.Name == nameReference.Name))
return false;
}

0 comments on commit 68d0f11

Please sign in to comment.
Something went wrong with that request. Please try again.