From fbf13d8cf71c889ed2d0ba429ef67812ce421c0c Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Thu, 27 Jan 2011 12:59:34 -1300 Subject: [PATCH] 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 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) --- umbraco.MacroEngines.Juno/DynamicNode.cs | 54 +++++++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/umbraco.MacroEngines.Juno/DynamicNode.cs b/umbraco.MacroEngines.Juno/DynamicNode.cs index 14c7cd265b..d3db09139d 100644 --- a/umbraco.MacroEngines.Juno/DynamicNode.cs +++ b/umbraco.MacroEngines.Juno/DynamicNode.cs @@ -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 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