Merge pull request #950 from ermshiperete/bug-xamarin-2787
[mono.git] / mcs / class / System.Xml.Linq / System.Xml.Linq / XNodeNavigator.cs
index a64ca7e882172c7fc07cdcc65ca39c7f9afd1528..55702bd1c265d7aeb4a4f298f8a81a5723e31659 100644 (file)
@@ -24,8 +24,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if !MOONLIGHT
-
 using System;
 using System.IO;
 using System.Text;
@@ -68,13 +66,22 @@ namespace System.Xml.Linq
 
                public override bool HasAttributes {
                        get {
+                               if (attr != null)
+                                       return false;
                                XElement el = node as XElement;
-                               return el != null && el.HasAttributes;
+                               if (el == null)
+                                       return false;
+                               foreach (var at in el.Attributes ())
+                                       if (!at.IsNamespaceDeclaration)
+                                               return true;
+                               return false;
                        }
                }
 
                public override bool HasChildren {
                        get {
+                               if (attr != null)
+                                       return false;
                                XContainer c = node as XContainer;
                                return c != null && c.FirstNode != null;
                        }
@@ -82,6 +89,8 @@ namespace System.Xml.Linq
 
                public override bool IsEmptyElement {
                        get {
+                               if (attr != null)
+                                       return false;
                                XElement el = node as XElement;
                                return el != null && el.IsEmpty;
                        }
@@ -130,8 +139,9 @@ namespace System.Xml.Linq
                public override string NamespaceURI {
                        get {
                                switch (NodeType) {
+                               case XPathNodeType.ProcessingInstruction:
                                case XPathNodeType.Namespace:
-                                       return attr.Value;
+                                       return String.Empty;
                                case XPathNodeType.Attribute:
                                        return attr.Name.NamespaceName;
                                case XPathNodeType.Element:
@@ -171,6 +181,9 @@ namespace System.Xml.Linq
                        get {
                                XName name = null;
                                switch (NodeType) {
+                               case XPathNodeType.ProcessingInstruction:
+                               case XPathNodeType.Namespace:
+                                       return String.Empty;
                                case XPathNodeType.Attribute:
                                        name = attr.Name;
                                        break;
@@ -208,7 +221,10 @@ namespace System.Xml.Linq
                                case XPathNodeType.ProcessingInstruction:
                                        return ((XPI) node).Data;
                                case XPathNodeType.Text:
-                                       return ((XText) node).Value;
+                                       string s = String.Empty;
+                                       for (var xn = node as XText; xn != null; xn = xn.NextNode as XText)
+                                               s += xn.Value;
+                                       return s;
                                case XPathNodeType.Element:
                                case XPathNodeType.Root:
                                        return GetInnerText ((XContainer) node);
@@ -257,7 +273,7 @@ namespace System.Xml.Linq
                public override bool MoveTo (XPathNavigator other)
                {
                        XNodeNavigator nav = other as XNodeNavigator;
-                       if (nav == null || nav.node.Owner != node.Owner)
+                       if (nav == null || nav.node.Document != node.Document)
                                return false;
                        node = nav.node;
                        attr = nav.attr;
@@ -266,6 +282,8 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstAttribute ()
                {
+                       if (attr != null)
+                               return false;
                        XElement el = node as XElement;
                        if (el == null || !el.HasAttributes)
                                return false;
@@ -279,8 +297,10 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstChild ()
                {
+                       if (attr != null)
+                               return false;
                        XContainer c = node as XContainer;
-                       if (c == null)
+                       if (c == null || c.FirstNode == null)
                                return false;
                        node = c.FirstNode;
                        attr = null;
@@ -289,6 +309,8 @@ namespace System.Xml.Linq
 
                public override bool MoveToFirstNamespace (XPathNamespaceScope scope)
                {
+                       if (NodeType != XPathNodeType.Element)
+                               return false;
                        for (XElement el = node as XElement; el != null; el = el.Parent) {
                                foreach (XAttribute a in el.Attributes ())
                                        if (a.IsNamespaceDeclaration) {
@@ -311,9 +333,14 @@ namespace System.Xml.Linq
 
                public override bool MoveToNext ()
                {
-                       if (node.NextNode == null)
+                       XNode xn = node.NextNode;
+                       if (xn is XText)
+                               for (; xn != null; xn = xn.NextNode)
+                                       if (!(xn.NextNode is XText))
+                                               break;
+                       if (xn == null)
                                return false;
-                       node = node.NextNode;
+                       node = xn;
                        attr = null;
                        return true;
                }
@@ -345,6 +372,9 @@ namespace System.Xml.Linq
                        if (scope == XPathNamespaceScope.Local)
                                return false;
 
+                       if (attr == attr_ns_xml)
+                               return false; // no next attribute
+
                        for (XElement el = ((XElement) attr.Parent).Parent; el != null; el = el.Parent) {
                                foreach (XAttribute a in el.Attributes ())
                                        if (a.IsNamespaceDeclaration) {
@@ -364,9 +394,9 @@ namespace System.Xml.Linq
                                attr = null;
                                return true;
                        }
-                       if (node.Parent == null)
+                       if (node.Owner == null)
                                return false;
-                       node = node.Parent;
+                       node = node.Owner;
                        return true;
                }
 
@@ -381,10 +411,12 @@ namespace System.Xml.Linq
 
                public override void MoveToRoot ()
                {
-                       node = node.Owner;
                        attr = null;
+                       if (node.Document != null)
+                               node = node.Document;
+                       else
+                               while (node.Owner != null)
+                                       node = node.Owner;
                }
        }
 }
-
-#endif