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

Commit edce1ce

Browse files
author
Jérémie Bertrand
committed
Merge pull request #297 from hartez/issue296
Prevent adding of duplicate tags during PreProcess() in RazorSiteEngine
2 parents 54024d8 + 1b6ea88 commit edce1ce

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/Pretzel.Logic/Templating/Razor/RazorSiteEngine.cs

+28-7
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,43 @@ public override void Initialize()
2727
{
2828
}
2929

30-
protected override void PreProcess()
30+
private class TagComparer : IEqualityComparer<ITag>
31+
{
32+
public bool Equals(ITag x, ITag y)
33+
{
34+
if (x == null || y == null)
35+
{
36+
return false;
37+
}
38+
39+
return x.Name == y.Name;
40+
}
41+
42+
public int GetHashCode(ITag obj)
43+
{
44+
return obj.Name.GetHashCode();
45+
}
46+
}
47+
48+
protected override void PreProcess()
3149
{
3250
includesPath = Path.Combine(Context.SourceFolder, "_includes");
3351

3452
if (Tags != null)
3553
{
36-
_allTags.AddRange(Tags);
54+
var toAdd = Tags.Except(_allTags, new TagComparer()).ToList();
55+
_allTags.AddRange(toAdd);
3756
}
3857

3958
if (TagFactories != null)
4059
{
41-
_allTags.AddRange(TagFactories.Select(factory =>
42-
{
43-
factory.Initialize(Context);
44-
return factory.CreateTag();
45-
}));
60+
var toAdd = TagFactories.Select(factory =>
61+
{
62+
factory.Initialize(Context);
63+
return factory.CreateTag();
64+
}).Except(_allTags, new TagComparer()).ToList();
65+
66+
_allTags.AddRange(toAdd);
4667
}
4768
}
4869

src/Pretzel.Tests/Templating/Razor/RazorEngineTests.cs

+19
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,25 @@ public void PostUrlTag_should_be_used()
249249
Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
250250
}
251251

252+
[Fact]
253+
public void Engine_can_process_template_multiple_times()
254+
{
255+
// arrange
256+
const string templateContents = "<html><body>@Raw(Model.Content) @Tag.PostUrl(\"index.cshtml\")</body></html>";
257+
const string pageContents = "<h1>Hello</h1>";
258+
const string expected = "<html><body><h1>Hello</h1> /index.html</body></html>";
259+
260+
Subject.TagFactories = new List<TagFactoryBase> { new PostUrlTagFactory() };
261+
262+
// act
263+
// Process contents multiple times (e.g., when a file has changed in taste)
264+
ProcessContents(templateContents, pageContents, new Dictionary<string, object>());
265+
ProcessContents(templateContents, pageContents, new Dictionary<string, object>());
266+
267+
// assert
268+
Assert.Equal(expected, FileSystem.File.ReadAllText(@"C:\website\_site\index.html"));
269+
}
270+
252271
public class CustomTag : DotLiquid.Tag, ITag
253272
{
254273
public new string Name { get { return "Custom"; } }

0 commit comments

Comments
 (0)