Skip to content
Browse files

InlineHtmlTemplateRenderer includes custom HtmlAttributes from bundle.

  • Loading branch information...
1 parent 3233908 commit 2743e6d9c42d5e65edfdb8053b1e9f5a2edf9993 @andrewdavey andrewdavey committed Feb 1, 2012
View
1 src/Cassette.UnitTests/Cassette.UnitTests.csproj
@@ -61,6 +61,7 @@
<ItemGroup>
<Compile Include="Asset.cs" />
<Compile Include="BundleProcessing\AssignHash.cs" />
+ <Compile Include="HtmlTemplates\InlineHtmlTemplateRenderer.cs" />
<Compile Include="Manifests\AssetManifest.cs" />
<Compile Include="Manifests\AssetManifestBuilder.cs" />
<Compile Include="Manifests\AssetManifestReader.cs" />
View
54 src/Cassette.UnitTests/HtmlTemplates/InlineHtmlTemplateRenderer.cs
@@ -0,0 +1,54 @@
+using System;
+using Cassette.Utilities;
+using Moq;
+using Should;
+using Xunit;
+
+namespace Cassette.HtmlTemplates
+{
+ public class InlineHtmlTemplateRenderer_Tests
+ {
+ readonly InlineHtmlTemplateBundleRenderer render;
+
+ public InlineHtmlTemplateRenderer_Tests()
+ {
+ render = new InlineHtmlTemplateBundleRenderer();
+ }
+
+ [Fact]
+ public void GivenHtmlTemplateBundleWithNoAssets_WhenRender_ThenReturnEmptyString()
+ {
+ var bundle = new HtmlTemplateBundle("~");
+ var html = render.Render(bundle);
+ html.ShouldEqual("");
+ }
+
+ [Fact]
+ public void GivenHtmlTemplateBundleWithOneAsset_WhenRender_ThenReturnsAssetsOpenStreamResult()
+ {
+ var bundle = new HtmlTemplateBundle("~");
+ bundle.Assets.Add(StubAsset("content"));
+
+ var html = render.Render(bundle);
+ html.ShouldEqual("content");
+ }
+
+ [Fact]
+ public void GivenHtmlTemplateBundleWithTwoAssets_WhenRender_ThenReturnsEachAssetsOpenStreamResultSeparatedByNewLine()
+ {
+ var bundle = new HtmlTemplateBundle("~");
+ bundle.Assets.Add(StubAsset("content1"));
+ bundle.Assets.Add(StubAsset("content2"));
+
+ var html = render.Render(bundle);
+ html.ShouldEqual("content1" + Environment.NewLine + "content2");
+ }
+
+ IAsset StubAsset(string content)
+ {
+ var asset = new Mock<IAsset>();
+ asset.Setup(a => a.OpenStream()).Returns(content.AsStream());
+ return asset.Object;
+ }
+ }
+}
View
69 src/Cassette.UnitTests/HtmlTemplates/WrapHtmlTemplatesInScriptElements.cs
@@ -1,5 +1,4 @@
-using System.IO;
-using Cassette.Utilities;
+using Cassette.Utilities;
using Moq;
using Should;
using Xunit;
@@ -8,19 +7,73 @@ namespace Cassette.HtmlTemplates
{
public class WrapHtmlTemplateInScriptElement_Tests
{
+ readonly HtmlTemplateBundle bundle;
+ readonly Mock<IAsset> asset;
+ string templateContent = "";
+
+ public WrapHtmlTemplateInScriptElement_Tests()
+ {
+ bundle = new HtmlTemplateBundle("~/test");
+
+ asset = new Mock<IAsset>();
+ asset.Setup(a => a.SourceFile.FullPath).Returns("~/test/asset.htm");
+ bundle.Assets.Add(asset.Object);
+ }
+
+ [Fact]
+ public void WhenTransform_ThenScriptIdAttributeIsDerivedFromFileName()
+ {
+ var html = TransformToHtml();
+
+ html.ShouldStartWith("<script id=\"asset\"");
+ }
+
+ [Fact]
+ public void WhenTransform_ThenScriptTypeAttributeIsBundleContentType()
+ {
+ bundle.ContentType = "TEST-CONTENT-TYPE";
+
+ var html = TransformToHtml();
+
+ html.ShouldStartWith("<script id=\"asset\" type=\"TEST-CONTENT-TYPE\"");
+ }
+
+ [Fact]
+ public void GivenTemplateContent_WhenTransform_ThenScriptContainsTemplateContent()
+ {
+ templateContent = "TEMPLATE";
+
+ var html = TransformToHtml();
+
+ html.ShouldEqual("<script id=\"asset\" type=\"text/html\">TEMPLATE</script>");
+ }
+
[Fact]
public void GivenAssetInSubDirectory_WhenTransform_ThenScriptIdHasSlashesReplacedWithDashes()
{
- var bundle = new HtmlTemplateBundle("~/test");
- var asset = new Mock<IAsset>();
asset.Setup(a => a.SourceFile.FullPath).Returns("~/test/sub/asset.htm");
- bundle.Assets.Add(asset.Object);
- var transformer = new WrapHtmlTemplateInScriptElement(bundle);
- var getResult = transformer.Transform(() => Stream.Null, asset.Object);
- var html = getResult().ReadToEnd();
+ var html = TransformToHtml();
html.ShouldContain("id=\"sub-asset\"");
}
+
+ [Fact]
+ public void GivenBundleWithHtmlAttributes_WhenTransform_ThenTemplateScriptElementHasTheExtraAttributes()
+ {
+ bundle.HtmlAttributes.Add("class", "test");
+
+ var html = TransformToHtml();
+
+ html.ShouldContain(" class=\"test\"");
+ }
+
+ string TransformToHtml()
+ {
+ var transformer = new WrapHtmlTemplateInScriptElement(bundle);
+ var getResult = transformer.Transform(() => templateContent.AsStream(), asset.Object);
+ var html = getResult().ReadToEnd();
+ return html;
+ }
}
}
View
2 src/Cassette/HtmlTemplates/InlineHtmlTemplateBundleRenderer.cs
@@ -14,4 +14,4 @@ public string Render(HtmlTemplateBundle bundle)
);
}
}
-}
+}
View
3 src/Cassette/HtmlTemplates/RemoteHtmlTemplateBundleRenderer.cs
@@ -1,5 +1,4 @@
-
-namespace Cassette.HtmlTemplates
+namespace Cassette.HtmlTemplates
{
class RemoteHtmlTemplateBundleRenderer : IBundleHtmlRenderer<HtmlTemplateBundle>
{
View
7 src/Cassette/HtmlTemplates/WrapHtmlTemplateInScriptElement.cs
@@ -19,13 +19,14 @@ public Func<Stream> Transform(Func<Stream> openSourceStream, IAsset asset)
{
var template = openSourceStream().ReadToEnd();
var scriptElement = String.Format(
- "<script id=\"{0}\" type=\"{1}\">{2}</script>",
+ "<script id=\"{0}\" type=\"{1}\"{2}>{3}</script>",
bundle.GetTemplateId(asset),
bundle.ContentType,
+ bundle.HtmlAttributes.CombinedAttributes,
template
- );
+ );
return scriptElement.AsStream();
};
}
}
-}
+}

0 comments on commit 2743e6d

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