Skip to content
This repository has been archived by the owner on Jun 25, 2020. It is now read-only.

Commit

Permalink
Merge pull request #297 from hartez/issue296
Browse files Browse the repository at this point in the history
Prevent adding of duplicate tags during PreProcess() in RazorSiteEngine
  • Loading branch information
Jérémie Bertrand committed Feb 7, 2016
2 parents 54024d8 + 1b6ea88 commit edce1ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
35 changes: 28 additions & 7 deletions src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,43 @@ public override void Initialize()
{
}

protected override void PreProcess()
private class TagComparer : IEqualityComparer<ITag>
{
public bool Equals(ITag x, ITag y)
{
if (x == null || y == null)
{
return false;
}

return x.Name == y.Name;
}

public int GetHashCode(ITag obj)
{
return obj.Name.GetHashCode();
}
}

protected override void PreProcess()
{
includesPath = Path.Combine(Context.SourceFolder, "_includes");

if (Tags != null)
{
_allTags.AddRange(Tags);
var toAdd = Tags.Except(_allTags, new TagComparer()).ToList();
_allTags.AddRange(toAdd);
}

if (TagFactories != null)
{
_allTags.AddRange(TagFactories.Select(factory =>
{
factory.Initialize(Context);
return factory.CreateTag();
}));
var toAdd = TagFactories.Select(factory =>
{
factory.Initialize(Context);
return factory.CreateTag();
}).Except(_allTags, new TagComparer()).ToList();

_allTags.AddRange(toAdd);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,25 @@ public void PostUrlTag_should_be_used()
Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
}

[Fact]
public void Engine_can_process_template_multiple_times()
{
// arrange
const string templateContents = "<html><body>@Raw(Model.Content) @Tag.PostUrl(\"index.cshtml\")</body></html>";
const string pageContents = "<h1>Hello</h1>";
const string expected = "<html><body><h1>Hello</h1> /index.html</body></html>";

Subject.TagFactories = new List<TagFactoryBase> { new PostUrlTagFactory() };

// act
// Process contents multiple times (e.g., when a file has changed in taste)
ProcessContents(templateContents, pageContents, new Dictionary<string, object>());
ProcessContents(templateContents, pageContents, new Dictionary<string, object>());

// assert
Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
}

public class CustomTag : DotLiquid.Tag, ITag
{
public new string Name { get { return "Custom"; } }
Expand Down

0 comments on commit edce1ce

Please sign in to comment.