Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Romanx committed May 19, 2016
2 parents 47e6daa + 5b9281f commit f46fc5a
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 5 deletions.
32 changes: 32 additions & 0 deletions Nustache.Compilation.Tests/Compiled_Templates_Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ public class SubObject
public bool TestBool { get; set; }

public SubObject Sub { get; set; }

public override string ToString()
{
return "SubObject";
}
}

public class TestObjectWithToString
{
public string TestString { get; set; }
public override string ToString()
{
return "";
}
}

[TestFixture]
Expand Down Expand Up @@ -273,6 +287,24 @@ public void Missing_Properties()
compiled(new TestObject { TestString = "Hello", TestBool = true });
}

[Test]
public void Member_ToString()
{
var template = Template("A template with {{Sub}}");
var compiled = template.Compile<TestObject>(null);
var result = compiled(new TestObject { Sub = new SubObject() });
Assert.AreEqual("A template with SubObject", result);
}

[Test]
public void Model_With_ToString()
{
var template = Template("A template with {{TestString}}");
var compiled = template.Compile<TestObjectWithToString>(null);
var result = compiled(new TestObjectWithToString { TestString = "Hello"});
Assert.AreEqual("A template with Hello", result);
}

private Func<T, string> Compiled<T>(string text) where T : class
{
return Template(text).Compile<T>(null);
Expand Down
4 changes: 2 additions & 2 deletions Nustache.Compilation/CompilePartVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ public void Visit(TemplateInclude include)
public void Visit(VariableReference variable)
{
var getter = context.CompiledGetter(variable.Path);
getter = CompoundExpression.NullCheck(getter, "");
getter = Expression.Call(getter, context.TargetType.GetMethod("ToString"));
var returnIfNotNull = Expression.Call(getter, getter.Type.GetMethod("ToString", new Type[0]));
getter = CompoundExpression.NullCheck(getter, "", returnIfNotNull);

if (variable.Escaped)
{
Expand Down
39 changes: 39 additions & 0 deletions Nustache.Core.Tests/Describe_ValueGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,45 @@ public void It_gets_XmlNode_multiple_child_element_values_as_a_list()
Assert.AreEqual("text2", elements[1].InnerText);
}

[Test]
public void It_gets_single_string_values_as_string_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child>text1</child>
<child>text2</child>
<child>text3</child>
</doc>");
var value = (string)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("text2", value);
}

[Test]
public void It_gets_single_node_values_as_node_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child>text1</child>
<child><grandchild>text2</grandchild></child>
<child>text3</child>
</doc>");
var value = (XmlNode)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("<grandchild>text2</grandchild>", value.InnerXml);
}

[Test]
public void It_gets_single_CDATA_values_as_string_by_Index_from_XmlNodeList()
{
XmlDocument target = new XmlDocument();
target.LoadXml(@"<doc attr='val'>
<child><![CDATA[text1]]></child>
<child><![CDATA[text2]]></child>
<child><![CDATA[text3]]></child>
</doc>");
var value = (string)ValueGetter.GetValue(target.DocumentElement.ChildNodes, "1");
Assert.AreEqual("text2", value);
}

[Test]
public void It_gets_ListValueByIndex_values_from_array()
{
Expand Down
8 changes: 7 additions & 1 deletion Nustache.Core.Tests/RenderContext_Behaviour_Support.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public void It_does_not_throw_an_exception_when_there_is_no_data_and_the_render_
Assert.AreEqual("beforeafter", result);
}


[Test]
public void Use_custom_encoder()
{
var result = Render.StringToString("before{{foo}}after", new {foo = string.Empty},
new RenderContextBehaviour {HtmlEncoder = text => "middle"});
Assert.AreEqual("beforemiddleafter", result);
}
}
}
5 changes: 5 additions & 0 deletions Nustache.Core/RenderContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public class RenderContext
public string ActiveStartDelimiter { get; set; }
public string ActiveEndDelimiter { get; set; }

public Encoders.HtmlEncoder HtmlEncoder
{
get { return _renderContextBehaviour.HtmlEncoder ?? Encoders.HtmlEncode; }
}

public RenderContext(Section section, object data, TextWriter writer, TemplateLocator templateLocator, RenderContextBehaviour renderContextBehaviour = null)
{
_sectionStack.Push(section);
Expand Down
2 changes: 2 additions & 0 deletions Nustache.Core/RenderContextBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class RenderContextBehaviour
public bool RaiseExceptionOnDataContextMiss { get; set; }
public bool RaiseExceptionOnEmptyStringValue { get; set; }

public Encoders.HtmlEncoder HtmlEncoder { get; set; }

public static RenderContextBehaviour GetDefaultRenderContextBehaviour()
{
return new RenderContextBehaviour
Expand Down
32 changes: 32 additions & 0 deletions Nustache.Core/ValueGetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,38 @@ private bool TryGetStringByAttributeName(string attributeName)
}
}

internal class XmlNodeListIndexGetter : ValueGetter
{
private readonly XmlNodeList _target;
private readonly int _index;
private object _foundSingleValue;

public XmlNodeListIndexGetter(XmlNodeList target, int index)
{
_target = target;
_index = index;
}

private object GetNodeValue(XmlNode node)
{
if (node.ChildNodes.Count == 1
&& (node.ChildNodes[0].NodeType == XmlNodeType.Text || node.ChildNodes[0].NodeType == XmlNodeType.CDATA)
)
{
return node.ChildNodes[0].Value;
}
else
{
return node;
}
}

public override object GetValue()
{
return GetNodeValue(_target[_index]);
}
}

internal class PropertyDescriptorValueGetter : ValueGetter
{
private readonly object _target;
Expand Down
27 changes: 27 additions & 0 deletions Nustache.Core/ValueGetterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public static class ValueGetterFactories
private static readonly ValueGetterFactoryCollection _factories = new ValueGetterFactoryCollection
{
new XmlNodeValueGetterFactory(),
new XmlNodeListIndexGetterFactory(),
new PropertyDescriptorValueGetterFactory(),
new GenericDictionaryValueGetterFactory(),
new DataRowGetterFactory(),
Expand Down Expand Up @@ -112,6 +113,32 @@ public override ValueGetter GetValueGetter(object target, Type targetType, strin
}
}

internal class XmlNodeListIndexGetterFactory : ValueGetterFactory
{
public override ValueGetter GetValueGetter(object target, Type targetType, string name)
{
if (target is XmlNodeList)
{
var listTarget = target as XmlNodeList;
int arrayIndex;
bool parseSuccess = Int32.TryParse(name, out arrayIndex);

/*
* There is an index as per the success of the parse, it is not greater than the count
* (minus one since index is zero referenced) or less than zero.
*/
if (parseSuccess &&
!(arrayIndex > (listTarget.Count - 1)) &&
!(arrayIndex < 0))
{
return new XmlNodeListIndexGetter(listTarget, arrayIndex);
}
}

return null;
}
}

internal class PropertyDescriptorValueGetterFactory : ValueGetterFactory
{
public override ValueGetter GetValueGetter(object target, Type targetType, string name)
Expand Down
4 changes: 2 additions & 2 deletions Nustache.Core/VariableReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public override void Render(RenderContext context)
var lambdaResult = lambda().ToString();

lambdaResult = _escaped
? Encoders.HtmlEncode(lambdaResult.ToString())
? context.HtmlEncoder(lambdaResult.ToString())
: lambdaResult.ToString();

using (System.IO.TextReader sr = new System.IO.StringReader(lambdaResult))
Expand All @@ -73,7 +73,7 @@ public override void Render(RenderContext context)
else if (value != null)
{
context.Write(_escaped
? Encoders.HtmlEncode(value.ToString())
? context.HtmlEncoder(value.ToString())
: value.ToString());
}
}
Expand Down

0 comments on commit f46fc5a

Please sign in to comment.