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

Prevent adding of duplicate tags during PreProcess() in RazorSiteEngine #297

Merged
merged 2 commits into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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