Text duplicated within nested contexts

The text writer inside of a nested context was being consolidated for
every generator in the block, rather than once at the end.
This commit is contained in:
jehugaleahsa 2016-09-19 12:25:48 -04:00
parent bb1b6cc936
commit 107b588019
6 changed files with 56 additions and 7 deletions

Binary file not shown.

View File

@ -1,4 +1,4 @@
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" ../mustache-sharp.sln /p:Configuration=Release "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" ../mustache-sharp.sln /p:Configuration=Release
nuget pack ../mustache-sharp/mustache-sharp.csproj -Properties Configuration=Release nuget pack ../mustache-sharp/mustache-sharp.csproj -Properties Configuration=Release
nuget push *.nupkg nuget push *.nupkg -Source https://www.nuget.org/api/v2/package
del *.nupkg del *.nupkg

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Web;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Mustache.Test namespace Mustache.Test
@ -1555,5 +1556,52 @@ Odd
} }
#endregion #endregion
#region Custom Tags
[TestMethod]
public void TestCompile_NestedContext_ConsolidatesWriter()
{
FormatCompiler compiler = new FormatCompiler();
compiler.RegisterTag(new UrlEncodeTagDefinition(), true);
const string format = @"{{#urlencode}}{{url}}{{/urlencode}}";
Generator generator = compiler.Compile(format);
string actual = generator.Render(new { url = "https://google.com" });
string expected = HttpUtility.UrlEncode("https://google.com");
Assert.AreEqual(expected, actual, "Value field didn't work");
}
public class UrlEncodeTagDefinition : ContentTagDefinition
{
public UrlEncodeTagDefinition()
: base("urlencode")
{
}
public override IEnumerable<NestedContext> GetChildContext(TextWriter writer, Scope keyScope, Dictionary<string, object> arguments, Scope contextScope)
{
NestedContext context = new NestedContext()
{
KeyScope = keyScope,
Writer = new StringWriter(),
WriterNeedsConsidated = true,
};
yield return context;
}
public override IEnumerable<TagParameter> GetChildContextParameters()
{
return new TagParameter[] { new TagParameter("collection") };
}
public override string ConsolidateWriter(TextWriter writer, Dictionary<string, object> arguments)
{
return HttpUtility.UrlEncode(writer.ToString());
}
}
#endregion
} }
} }

View File

@ -42,6 +42,7 @@
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Web" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies"> <CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">

View File

@ -81,6 +81,7 @@ namespace Mustache
foreach (IGenerator generator in generators) foreach (IGenerator generator in generators)
{ {
generator.GetText(context.Writer ?? writer, context.KeyScope ?? keyScope, context.ContextScope, postProcessor); generator.GetText(context.Writer ?? writer, context.KeyScope ?? keyScope, context.ContextScope, postProcessor);
}
if (context.WriterNeedsConsidated) if (context.WriterNeedsConsidated)
{ {
writer.Write(_definition.ConsolidateWriter(context.Writer ?? writer, arguments)); writer.Write(_definition.ConsolidateWriter(context.Writer ?? writer, arguments));
@ -88,5 +89,4 @@ namespace Mustache
} }
} }
} }
}
} }

View File

@ -14,6 +14,6 @@ using System.Runtime.InteropServices;
[assembly: CLSCompliant(true)] [assembly: CLSCompliant(true)]
[assembly: ComVisible(false)] [assembly: ComVisible(false)]
[assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")] [assembly: Guid("e5a4263d-d450-4d85-a4d5-44c0a2822668")]
[assembly: AssemblyVersion("0.2.9.0")] [assembly: AssemblyVersion("0.2.10.0")]
[assembly: AssemblyFileVersion("0.2.9.0")] [assembly: AssemblyFileVersion("0.2.10.0")]
[assembly: InternalsVisibleTo("mustache-sharp.test,PublicKey=0024000004800000940000000602000000240000525341310004000001000100755df5a2b24c568812aae0eb194d08a4e3cba960673bcc07a7d446acf52f3f56ae2155b37b8d547bc5d8c562823bd592d1312bef9ad4740a8bb503d0095c31419f9d190882a2fa46090412bf15b13ca0057ba533c85a853333132ec8b70cf19655ef961b06d1c3fc35b3f68680420562be741456cb7a18bd5ab0fa779f8d47b1")] [assembly: InternalsVisibleTo("mustache-sharp.test,PublicKey=0024000004800000940000000602000000240000525341310004000001000100755df5a2b24c568812aae0eb194d08a4e3cba960673bcc07a7d446acf52f3f56ae2155b37b8d547bc5d8c562823bd592d1312bef9ad4740a8bb503d0095c31419f9d190882a2fa46090412bf15b13ca0057ba533c85a853333132ec8b70cf19655ef961b06d1c3fc35b3f68680420562be741456cb7a18bd5ab0fa779f8d47b1")]