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.
Expand Up @@ -28,6 +28,7 @@
<Reference Include="System.Web" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="3.14.0" />
<PackageReference Include="NUnit.Console" Version="3.16.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
Expand Down
@@ -1 +1 @@
<p>${H("This <contains/> html")}</p>
<p>${"This <contains/> html"}</p>
Expand Up @@ -50,7 +50,11 @@ public void RunPrecompiler()
Assert.That(File.Exists(targetFile), "File exists");

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
Expand Down
Expand Up @@ -35,11 +35,12 @@ public class SparkBatchCompilerTester
[SetUp]
public void Init()
{
var settings = new SparkSettings();
var settings = new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));

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

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

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

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

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

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

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

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

Assert.AreEqual("bar", view.ViewData["foo"]);
}
Expand All @@ -71,7 +71,7 @@ public void MergingCollectionsLikeVelocity()
engineContext.Request.Params.Add("contextParamsKey", "contextParamsValue");
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("contextFlashValue", view.ViewData["contextFlashKey"]);
Expand Down
Expand Up @@ -27,10 +27,13 @@ public class SparkViewFactoryStrictNullBehaviourTests : SparkViewFactoryTestsBas
{
protected override void Configure()
{
var settings = new SparkSettings();
var settings =
new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));

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

serviceProvider.AddService(typeof(ISparkSettings), settings);

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

[TestFixture]
public class SparkViewFactoryTests : SparkViewFactoryTestsBase
{
protected override void Configure()
{
factory = new SparkViewFactory();
factory.Service(serviceProvider);

manager = new DefaultViewEngineManager();
manager.Service(serviceProvider);
serviceProvider.ViewEngineManager = manager;
serviceProvider.AddService(typeof(IViewEngineManager), manager);
serviceProvider.AddService(typeof(ISparkSettings), new SparkSettings());
{
protected override void Configure()
{
var settings = new SparkSettings()
.SetBaseClassTypeName(typeof(SparkView));

manager.RegisterEngineForExtesionLookup(factory);
manager.RegisterEngineForView(factory);
}
settings.AutomaticEncoding = true;

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]
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));
var entry = factory.Engine.GetEntry(descriptor);
var view = (SparkView)entry.CreateInstance();
view.Contextualize(engineContext, controllerContext, factory, null);
view.Contextualize(engineContext, controllerContext, serviceProvider.GetService<IResourcePathManager>(), factory, null);

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

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

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

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

using Castle.MonoRail.Framework;

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

}
}
}
Expand Up @@ -29,8 +29,7 @@ public class BaseViewComponentTests
protected StubEngineContext engineContext;
protected SparkViewFactory factory;
protected IController controller;
protected SparkViewEngine engine;


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

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

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

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

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"),
"Castle.MonoRail.Views.Spark.Tests.EmbeddedViews");

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

mocks.ReplayAll();

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

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

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

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

Expand All @@ -54,7 +54,7 @@ protected SparkView()
public string SiteRoot { get { return _context.ApplicationPath; } }
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; } }
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>(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;
_controllerContext = controllerContext;
_resourcePathManager = resourcePathManager;
_viewEngine = viewEngine;

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

public string H(object value)
public override void OutputValue(object value, bool automaticEncoding)
{
if (value is HtmlString)
return value.ToString();
return Server.HtmlEncode(Convert.ToString(value));
// Always encode when automatic encoding enabled or HtmlString (includes MvcHtmlString)
if (automaticEncoding || value is HtmlString)
{
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)
{
return new HtmlString(Convert.ToString(value));
Expand All @@ -132,7 +153,7 @@ class HtmlString
var service = (IViewComponentFactory)_context.GetService(typeof(IViewComponentFactory));
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;
try
Expand Down

0 comments on commit 452fa92

Please sign in to comment.