Skip to content

Commit

Permalink
Merge pull request #79 from SparkViewEngine/asp-net-core
Browse files Browse the repository at this point in the history
Asp net core support
  • Loading branch information
RobertTheGrey committed Apr 24, 2024
2 parents e04fa77 + 3d8701a commit 452fa92
Show file tree
Hide file tree
Showing 175 changed files with 4,645 additions and 3,359 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Reference Include="System.Web" /> <Reference Include="System.Web" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" /> <PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Console" Version="3.16.3" /> <PackageReference Include="NUnit.Console" Version="3.16.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> <PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
Expand Down
Original file line number Original file line Diff line number Diff line change
@@ -1 +1 @@
<p>${H("This <contains/> html")}</p> <p>${"This <contains/> html"}</p>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ public void RunPrecompiler()
Assert.That(File.Exists(targetFile), "File exists"); Assert.That(File.Exists(targetFile), "File exists");


var result = Assembly.LoadFrom(targetFile); var result = Assembly.LoadFrom(targetFile);
Assert.AreEqual(3, result.GetTypes().Count());
var views = result.GetTypes().Where(x => x.BaseType == typeof(SparkView))
.ToArray();

Assert.AreEqual(3, views.Length);
} }


public class ParentInstaller : Installer public class ParentInstaller : Installer
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public class SparkBatchCompilerTester
[SetUp] [SetUp]
public void Init() public void Init()
{ {
var settings = new SparkSettings(); var settings = new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));


var services = new StubMonoRailServices(); var services = new StubMonoRailServices();
services.AddService(typeof(ISparkSettings), settings);
services.AddService(typeof(IViewSourceLoader), new FileAssemblyViewSourceLoader("MonoRail.Tests.Views")); services.AddService(typeof(IViewSourceLoader), new FileAssemblyViewSourceLoader("MonoRail.Tests.Views"));
services.AddService(typeof(ISparkViewEngine), new SparkViewEngine(settings));
services.AddService(typeof(IControllerDescriptorProvider), services.ControllerDescriptorProvider); services.AddService(typeof(IControllerDescriptorProvider), services.ControllerDescriptorProvider);
_factory = new SparkViewFactory(); _factory = new SparkViewFactory();
_factory.Service(services); _factory.Service(services);
Expand All @@ -59,7 +60,7 @@ public void CompileBatchDescriptor()
var assembly = _factory.Precompile(batch); var assembly = _factory.Precompile(batch);


Assert.IsNotNull(assembly); Assert.IsNotNull(assembly);
Assert.AreEqual(3, assembly.GetTypes().Length); Assert.AreEqual(3, assembly.GetTypes().Count(x => x.BaseType == typeof(SparkView)));
} }


[Test] [Test]
Expand Down Expand Up @@ -98,7 +99,7 @@ public void MultipleLayoutFiles()
var assembly = _factory.Precompile(batch); var assembly = _factory.Precompile(batch);


Assert.IsNotNull(assembly); Assert.IsNotNull(assembly);
Assert.AreEqual(4, assembly.GetTypes().Length); Assert.AreEqual(4, assembly.GetTypes().Count(x => x.BaseType == typeof(SparkView)));
} }


[Test] [Test]
Expand Down Expand Up @@ -131,7 +132,7 @@ public void WildcardIncludeRules()
var assembly = _factory.Precompile(batch); var assembly = _factory.Precompile(batch);


Assert.IsNotNull(assembly); Assert.IsNotNull(assembly);
Assert.AreEqual(3, assembly.GetTypes().Length); Assert.AreEqual(3, assembly.GetTypes().Count(x => x.BaseType == typeof(SparkView)));
} }


[Test] [Test]
Expand Down
4 changes: 2 additions & 2 deletions src/Castle.MonoRail.Views.Spark.Tests/SparkViewDataTests.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void PropertyBagAvailable()
controllerContext.PropertyBag.Add("foo", "bar"); controllerContext.PropertyBag.Add("foo", "bar");


mocks.ReplayAll(); mocks.ReplayAll();
view.Contextualize(engineContext, controllerContext, null, null); view.Contextualize(engineContext, controllerContext, null, null, null);


Assert.AreEqual("bar", view.ViewData["foo"]); Assert.AreEqual("bar", view.ViewData["foo"]);
} }
Expand All @@ -71,7 +71,7 @@ public void MergingCollectionsLikeVelocity()
engineContext.Request.Params.Add("contextParamsKey", "contextParamsValue"); engineContext.Request.Params.Add("contextParamsKey", "contextParamsValue");
controllerContext.Resources.Add("controllerResourcesKey", resource); controllerContext.Resources.Add("controllerResourcesKey", resource);


view.Contextualize(engineContext, controllerContext, null, null); view.Contextualize(engineContext, controllerContext, null, null, null);


Assert.AreEqual("controllerPropertyBagValue", view.ViewData["controllerPropertyBagKey"]); Assert.AreEqual("controllerPropertyBagValue", view.ViewData["controllerPropertyBagKey"]);
Assert.AreEqual("contextFlashValue", view.ViewData["contextFlashKey"]); Assert.AreEqual("contextFlashValue", view.ViewData["contextFlashKey"]);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ public class SparkViewFactoryStrictNullBehaviourTests : SparkViewFactoryTestsBas
{ {
protected override void Configure() protected override void Configure()
{ {
var settings = new SparkSettings(); var settings =
new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));

settings.SetNullBehaviour(NullBehaviour.Strict); settings.SetNullBehaviour(NullBehaviour.Strict);
var sparkViewEngine = new SparkViewEngine(settings);
serviceProvider.AddService(typeof(ISparkViewEngine), sparkViewEngine); serviceProvider.AddService(typeof(ISparkSettings), settings);


factory = new SparkViewFactory(); factory = new SparkViewFactory();
factory.Service(serviceProvider); factory.Service(serviceProvider);
Expand Down
52 changes: 30 additions & 22 deletions src/Castle.MonoRail.Views.Spark.Tests/SparkViewFactoryTests.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ namespace Castle.MonoRail.Views.Spark.Tests


[TestFixture] [TestFixture]
public class SparkViewFactoryTests : SparkViewFactoryTestsBase public class SparkViewFactoryTests : SparkViewFactoryTestsBase
{ {
protected override void Configure() protected override void Configure()
{ {
factory = new SparkViewFactory(); var settings = new SparkSettings()
factory.Service(serviceProvider); .SetBaseClassTypeName(typeof(SparkView));

manager = new DefaultViewEngineManager();
manager.Service(serviceProvider);
serviceProvider.ViewEngineManager = manager;
serviceProvider.AddService(typeof(IViewEngineManager), manager);
serviceProvider.AddService(typeof(ISparkSettings), new SparkSettings());


manager.RegisterEngineForExtesionLookup(factory); settings.AutomaticEncoding = true;
manager.RegisterEngineForView(factory);
} serviceProvider.AddService(typeof(ISparkSettings), settings);

factory = new SparkViewFactory();
factory.Service(serviceProvider);

manager = new DefaultViewEngineManager();
manager.Service(serviceProvider);
serviceProvider.ViewEngineManager = manager;
serviceProvider.AddService(typeof(IViewEngineManager), manager);

manager.RegisterEngineForExtesionLookup(factory);
manager.RegisterEngineForView(factory);
}


[Test] [Test]
public void ExtensionIsSpark() public void ExtensionIsSpark()
Expand Down Expand Up @@ -69,7 +75,7 @@ public void ContextAndControllerContextAvailable()
descriptor.Templates.Add(string.Format("Shared{0}default.spark", Path.DirectorySeparatorChar)); descriptor.Templates.Add(string.Format("Shared{0}default.spark", Path.DirectorySeparatorChar));
var entry = factory.Engine.GetEntry(descriptor); var entry = factory.Engine.GetEntry(descriptor);
var view = (SparkView)entry.CreateInstance(); var view = (SparkView)entry.CreateInstance();
view.Contextualize(engineContext, controllerContext, factory, null); view.Contextualize(engineContext, controllerContext, serviceProvider.GetService<IResourcePathManager>(), factory, null);


var result = new StringWriter(); var result = new StringWriter();
view.RenderView(result); view.RenderView(result);
Expand Down Expand Up @@ -114,20 +120,22 @@ public void NullBehaviourConfiguredToLenient()
{ {
mocks.ReplayAll(); mocks.ReplayAll();
manager.Process(string.Format("Home{0}NullBehaviourConfiguredToLenient", Path.DirectorySeparatorChar), output, engineContext, controller, controllerContext); manager.Process(string.Format("Home{0}NullBehaviourConfiguredToLenient", Path.DirectorySeparatorChar), output, engineContext, controller, controllerContext);
var content = output.ToString(); var content = output.ToString();
Assert.IsFalse(content.Contains("default")); Assert.IsFalse(content.Contains("default"));


ContainsInOrder(content, ContainsInOrder(content,
"<p>name kaboom *${user.Name}*</p>", "<p>name kaboom *${user.Name}*</p>",
"<p>name silently **</p>", "<p>name silently **</p>",
"<p>name fixed *fred*</p>"); "<p>name fixed *fred*</p>");
} }


[Test] [Test]
public void TerseHtmlEncode() public void TerseHtmlEncode()
{ {
mocks.ReplayAll(); mocks.ReplayAll();
manager.Process(string.Format("Home{0}TerseHtmlEncode", Path.DirectorySeparatorChar), output, engineContext, controller, controllerContext); manager.Process(string.Format("Home{0}TerseHtmlEncode", Path.DirectorySeparatorChar), output, engineContext, controller, controllerContext);

// See AutomaticEncoding = true in Configure() method
ContainsInOrder(output.ToString(), ContainsInOrder(output.ToString(),
"<p>This &lt;contains/&gt; html</p>"); "<p>This &lt;contains/&gt; html</p>");
} }
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// //
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.MonoRail.Framework; using Castle.MonoRail.Framework;


namespace Castle.MonoRail.Views.Spark.Tests.Stubs namespace Castle.MonoRail.Views.Spark.Tests.Stubs
Expand All @@ -34,7 +31,6 @@ public void List()
[Layout("ajax")] [Layout("ajax")]
public void _Widget() public void _Widget()
{ {

} }
} }
} }
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public class BaseViewComponentTests
protected StubEngineContext engineContext; protected StubEngineContext engineContext;
protected SparkViewFactory factory; protected SparkViewFactory factory;
protected IController controller; protected IController controller;
protected SparkViewEngine engine;

[SetUp] [SetUp]
public virtual void Init() public virtual void Init()
{ {
Expand All @@ -45,9 +44,11 @@ public virtual void Init()
services.AddService(typeof(IViewComponentFactory), viewComponentFactory); services.AddService(typeof(IViewComponentFactory), viewComponentFactory);
services.AddService(typeof(IViewComponentRegistry), viewComponentFactory.Registry); services.AddService(typeof(IViewComponentRegistry), viewComponentFactory.Registry);


var settings = new SparkSettings(); var settings = new SparkSettings()
engine = new SparkViewEngine(settings); .SetBaseClassTypeName(typeof(SparkView));
services.AddService(typeof(ISparkViewEngine), engine); services.AddService(typeof(ISparkSettings), settings);

services.AddService(typeof(IResourcePathManager), new DefaultResourcePathManager(settings));


factory = new SparkViewFactory(); factory = new SparkViewFactory();
factory.Service(services); factory.Service(services);
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reflection; using System.Reflection;
using Castle.MonoRail.Framework; using Castle.MonoRail.Framework;
using NUnit.Framework; using NUnit.Framework;
using Spark;
using Spark.FileSystem; using Spark.FileSystem;


namespace Castle.MonoRail.Views.Spark.Tests.ViewComponents namespace Castle.MonoRail.Views.Spark.Tests.ViewComponents
Expand Down Expand Up @@ -81,7 +82,7 @@ public void ComponentRenderViewFromEmbeddedResource()
Assembly.Load("Castle.MonoRail.Views.Spark.Tests"), Assembly.Load("Castle.MonoRail.Views.Spark.Tests"),
"Castle.MonoRail.Views.Spark.Tests.EmbeddedViews"); "Castle.MonoRail.Views.Spark.Tests.EmbeddedViews");


engine.ViewFolder = engine.ViewFolder.Append(embeddedViewFolder); this.factory.Engine.ViewFolder = this.factory.Engine.ViewFolder.Append(embeddedViewFolder);


mocks.ReplayAll(); mocks.ReplayAll();


Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ public override void Install(IDictionary stateSaver)
// Attempt to get the configuration from settings, otherwise use default settings // Attempt to get the configuration from settings, otherwise use default settings
var settings = var settings =
(ISparkSettings)config.GetSection("spark") ?? (ISparkSettings)config.GetSection("spark") ??
new SparkSettings(); new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));


var services = new StubMonoRailServices(); var services = new StubMonoRailServices();
services.AddService(typeof(ISparkSettings), settings);
services.AddService(typeof(IViewSourceLoader), new FileAssemblyViewSourceLoader(viewsLocation)); services.AddService(typeof(IViewSourceLoader), new FileAssemblyViewSourceLoader(viewsLocation));
services.AddService(typeof(ISparkViewEngine), new SparkViewEngine(settings));
services.AddService(typeof(IControllerDescriptorProvider), services.ControllerDescriptorProvider); services.AddService(typeof(IControllerDescriptorProvider), services.ControllerDescriptorProvider);


var factory = new SparkViewFactory(); var factory = new SparkViewFactory();
Expand Down
File renamed without changes.
39 changes: 30 additions & 9 deletions src/Castle.MonoRail.Views.Spark/SparkView.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Castle.MonoRail.Views.Spark namespace Castle.MonoRail.Views.Spark
{ {
using System; using System;
using System.Linq;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
Expand All @@ -35,6 +34,7 @@ protected SparkView()


private IEngineContext _context; private IEngineContext _context;
private IControllerContext _controllerContext; private IControllerContext _controllerContext;
private IResourcePathManager _resourcePathManager;
private SparkViewFactory _viewEngine; private SparkViewFactory _viewEngine;
private IDictionary _contextVars; private IDictionary _contextVars;


Expand All @@ -54,7 +54,7 @@ protected SparkView()
public string SiteRoot { get { return _context.ApplicationPath; } } public string SiteRoot { get { return _context.ApplicationPath; } }
public string SiteResource(string path) public string SiteResource(string path)
{ {
return _viewEngine.Engine.ResourcePathManager.GetResourcePath(SiteRoot, path); return this._resourcePathManager.GetResourcePath(SiteRoot, path);
} }


public IDictionary PropertyBag { get { return _contextVars ?? _controllerContext.PropertyBag; } } public IDictionary PropertyBag { get { return _contextVars ?? _controllerContext.PropertyBag; } }
Expand Down Expand Up @@ -91,10 +91,11 @@ public string Eval(string expression, string format)
public T Helper<T>() where T : class { return ControllerContext.Helpers[typeof(T).Name] as T; } public T Helper<T>() where T : class { return ControllerContext.Helpers[typeof(T).Name] as T; }
public T Helper<T>(string name) where T : class { return ControllerContext.Helpers[name] as T; } public T Helper<T>(string name) where T : class { return ControllerContext.Helpers[name] as T; }


public virtual void Contextualize(IEngineContext context, IControllerContext controllerContext, SparkViewFactory viewEngine, SparkView outerView) public virtual void Contextualize(IEngineContext context, IControllerContext controllerContext, IResourcePathManager resourcePathManager, SparkViewFactory viewEngine, SparkView outerView)
{ {
_context = context; _context = context;
_controllerContext = controllerContext; _controllerContext = controllerContext;
_resourcePathManager = resourcePathManager;
_viewEngine = viewEngine; _viewEngine = viewEngine;


if (_viewEngine != null && _viewEngine.CacheServiceProvider != null) if (_viewEngine != null && _viewEngine.CacheServiceProvider != null)
Expand All @@ -104,13 +105,33 @@ public virtual void Contextualize(IEngineContext context, IControllerContext con
OnceTable = outerView.OnceTable; OnceTable = outerView.OnceTable;
} }


public string H(object value) public override void OutputValue(object value, bool automaticEncoding)
{ {
if (value is HtmlString) // Always encode when automatic encoding enabled or HtmlString (includes MvcHtmlString)
return value.ToString(); if (automaticEncoding || value is HtmlString)
return Server.HtmlEncode(Convert.ToString(value)); {
OutputEncodedValue(value);
}
else
{
Output.Write(value);
}
}

public void OutputEncodedValue(object value)
{
if (value is string stringValue)
{
var encoded = System.Web.HttpUtility.HtmlEncode(stringValue);

Output.Write(encoded);
}
else
{
Output.Write(value.ToString());
}
} }

public object HTML(object value) public object HTML(object value)
{ {
return new HtmlString(Convert.ToString(value)); return new HtmlString(Convert.ToString(value));
Expand All @@ -132,7 +153,7 @@ public void RenderComponent(
var service = (IViewComponentFactory)_context.GetService(typeof(IViewComponentFactory)); var service = (IViewComponentFactory)_context.GetService(typeof(IViewComponentFactory));
var component = service.Create(name); var component = service.Create(name);


IViewComponentContext viewComponentContext = new ViewComponentContext(this, _viewEngine, name, parameters, body, sections); IViewComponentContext viewComponentContext = new ViewComponentContext(this, _resourcePathManager, _viewEngine, name, parameters, body, sections);


var oldContextVars = _contextVars; var oldContextVars = _contextVars;
try try
Expand Down
Loading

0 comments on commit 452fa92

Please sign in to comment.