Fixed support for AncestorOrSelf when in Test mode (the INode passed to the razor script in testing has a null parent)

Added AncestorOrSelf overload which doesn't require a Func<DynamicNode,bool> argument

Both changes modify DynamicNode.Parent so that if there is no parent, it returns itself instead of null, (you can't new DynamicNode(null) as it throws exception)
and then in the AncestorOrSelf checks, the loop makes sure that it won't get stuck by continually returning itself (because this.Parent returns this)
This commit is contained in:
agrath@gmail.com
2011-01-27 12:59:34 -13:00
parent dca21f9ce1
commit fbf13d8cf7

View File

@@ -247,14 +247,53 @@ namespace umbraco.MacroEngines
}
return false;
}
public DynamicNode AncestorOrSelf()
{
var node = this;
while (node != null)
{
DynamicNode parent = node.Parent;
if (parent != null)
{
if (this != parent)
{
node = parent;
}
else
{
return node;
}
}
else
{
return node;
}
}
return node;
}
public DynamicNode AncestorOrSelf(Func<DynamicNode, bool> func)
{
var node = this;
while (node != null)
{
if (func(node)) return node;
node = node.Parent;
DynamicNode parent = node.Parent;
if (parent != null)
{
if (this != parent)
{
node = parent;
}
else
{
return node;
}
}
else
{
return node;
}
}
return node;
@@ -262,7 +301,18 @@ namespace umbraco.MacroEngines
public DynamicNode Parent
{
get { if (n == null) return null; return new DynamicNode(n.Parent); }
get
{
if (n == null)
{
return null;
}
if (n.Parent != null)
{
return new DynamicNode(n.Parent);
}
return this;
}
}
public int Id