Author: atsushi
Date: 2006-11-09 20:39:23 -0500 (Thu, 09 Nov 2006)
New Revision: 67659

Modified:
   trunk/mcs/class/System.XML/System.Xml.XPath/ChangeLog
   trunk/mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs
   trunk/mcs/class/System.XML/Test/System.Xml.XPath/ChangeLog
   trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorReaderTests.cs
   trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorTests.cs
Log:
2006-11-10  Atsushi Enomoto <[EMAIL PROTECTED]>

        * XPathNavigator.cs :
          - ReadSubtree() should reject everything but Element or Root.
          - InnerXml and OuterXml should not use ReadSubtree() when it is
            not positioned on an Element or the Root. Thus, implement them
            in their own ways. Fixed bug #79875.

        * XPathNavigatorTests.cs : added test for bug #79875, and some
          "escaped" text tests for InnerXml.
        * XPathNavigatorReaderTests.cs : make sure that most of XPath nodes
          are rejected to read subtree.



Modified: trunk/mcs/class/System.XML/System.Xml.XPath/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.XPath/ChangeLog       2006-11-10 
01:15:58 UTC (rev 67658)
+++ trunk/mcs/class/System.XML/System.Xml.XPath/ChangeLog       2006-11-10 
01:39:23 UTC (rev 67659)
@@ -1,5 +1,13 @@
 2006-11-10  Atsushi Enomoto <[EMAIL PROTECTED]>
 
+       * XPathNavigator.cs :
+         - ReadSubtree() should reject everything but Element or Root.
+         - InnerXml and OuterXml should not use ReadSubtree() when it is
+           not positioned on an Element or the Root. Thus, implement them
+           in their own ways. Fixed bug #79875.
+
+2006-11-10  Atsushi Enomoto <[EMAIL PROTECTED]>
+
        * XPathNavigator.cs : InnerXml should allow Text-only content. Fixed
          bug #79874, when tied to XPathNavigatorReader fix.
 

Modified: trunk/mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs
===================================================================
--- trunk/mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs       
2006-11-10 01:15:58 UTC (rev 67658)
+++ trunk/mcs/class/System.XML/System.Xml.XPath/XPathNavigator.cs       
2006-11-10 01:39:23 UTC (rev 67659)
@@ -834,7 +834,13 @@
 
                public virtual XmlReader ReadSubtree ()
                {
-                       return new XPathNavigatorReader (this);
+                       switch (NodeType) {
+                       case XPathNodeType.Element:
+                       case XPathNodeType.Root:
+                               return new XPathNavigatorReader (this);
+                       default:
+                               throw new InvalidOperationException 
(String.Format ("NodeType {0} is not supported to read as a subtree of an 
XPathNavigator.", NodeType));
+                       }
                }
 
                public virtual XPathNodeIterator Select (string xpath, 
IXmlNamespaceResolver nsResolver)
@@ -874,9 +880,48 @@
                        writer.WriteNode (this, false);
                }
 
-               [MonoTODO]
+               static readonly char [] escape_text_chars =
+                               new char [] {'&', '<', '>'};
+               static readonly char [] escape_attr_chars =
+                               new char [] {'"', '&', '<', '>', '\r', '\n'};
+
+               static string EscapeString (string value, bool attr)
+               {
+                       StringBuilder sb = null;
+                       char [] escape = attr ? escape_attr_chars : 
escape_text_chars;
+                       if (value.IndexOfAny (escape) < 0)
+                               return value;
+                       sb = new StringBuilder (value, value.Length + 10);
+                       if (attr)
+                               sb.Replace ("\"", "&quot;");
+                       sb.Replace ("<", "&lt;");
+                       sb.Replace (">", "&gt;");
+                       if (attr) {
+                               sb.Replace ("\r\n", "&#10;");
+                               sb.Replace ("\r", "&#10;");
+                               sb.Replace ("\n", "&#10;");
+                       }
+                       return sb.ToString ();
+               }
+
                public virtual string InnerXml {
                        get {
+                               switch (NodeType) {
+                               case XPathNodeType.Element:
+                               case XPathNodeType.Root:
+                                       break;
+                               case XPathNodeType.Attribute:
+                               case XPathNodeType.Namespace:
+                                       return EscapeString (Value, true);
+                               case XPathNodeType.Text:
+                               case XPathNodeType.Whitespace:
+                               case XPathNodeType.SignificantWhitespace:
+                                       return String.Empty;
+                               case XPathNodeType.ProcessingInstruction:
+                               case XPathNodeType.Comment:
+                                       return Value;
+                               }
+
                                XmlReader r = ReadSubtree ();
                                r.Read (); // start
                                // skip the element itself (or will reach to 
@@ -910,6 +955,32 @@
 
                public virtual string OuterXml {
                        get {
+                               switch (NodeType) {
+                               case XPathNodeType.Attribute:
+                                       return String.Concat (
+                                               Prefix,
+                                               Prefix.Length > 0 ? ":" : 
String.Empty,
+                                               LocalName,
+                                               "=\"",
+                                               EscapeString (Value, true),
+                                               "\"");
+                                       break;
+                               case XPathNodeType.Namespace:
+                                       return String.Concat (
+                                               "xmlns",
+                                               LocalName.Length > 0 ? ":" : 
String.Empty,
+                                               LocalName,
+                                               "=\"",
+                                               EscapeString (Value, true),
+                                               "\"");
+                                       break;
+                               case XPathNodeType.Text:
+                                       return EscapeString (Value, false);
+                               case XPathNodeType.Whitespace:
+                               case XPathNodeType.SignificantWhitespace:
+                                       return Value;
+                               }
+
                                XmlWriterSettings s = new XmlWriterSettings ();
                                s.Indent = true;
                                s.OmitXmlDeclaration = true;

Modified: trunk/mcs/class/System.XML/Test/System.Xml.XPath/ChangeLog
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml.XPath/ChangeLog  2006-11-10 
01:15:58 UTC (rev 67658)
+++ trunk/mcs/class/System.XML/Test/System.Xml.XPath/ChangeLog  2006-11-10 
01:39:23 UTC (rev 67659)
@@ -1,5 +1,12 @@
-2006-10-27  Atsushi Enomoto <[EMAIL PROTECTED]>
+2006-11-10  Atsushi Enomoto <[EMAIL PROTECTED]>
 
+       * XPathNavigatorTests.cs : added test for bug #79875, and some
+         "escaped" text tests for InnerXml.
+       * XPathNavigatorReaderTests.cs : make sure that most of XPath nodes
+         are rejected to read subtree.
+
+2006-11-10  Atsushi Enomoto <[EMAIL PROTECTED]>
+
        * XPathNavigatorTests.cs : added test for bug #79874.
 
 2006-10-27  Atsushi Enomoto <[EMAIL PROTECTED]>

Modified: 
trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorReaderTests.cs
===================================================================
--- 
trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorReaderTests.cs   
    2006-11-10 01:15:58 UTC (rev 67658)
+++ 
trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorReaderTests.cs   
    2006-11-10 01:39:23 UTC (rev 67659)
@@ -622,6 +622,48 @@
                        Assert.IsTrue (r.MoveToNextAttribute (), label + "#4");
                        Assert.IsTrue (r.MoveToFirstAttribute (), label + "#5");
                }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void ReadSubtreeAttribute ()
+               {
+                       string xml = "<root a='b' />";
+                       nav = GetXmlDocumentNavigator (xml);
+                       nav.MoveToFirstChild ();
+                       nav.MoveToFirstAttribute ();
+                       nav.ReadSubtree ();
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void ReadSubtreeNamespace ()
+               {
+                       string xml = "<root xmlns='urn:foo' />";
+                       nav = GetXmlDocumentNavigator (xml);
+                       nav.MoveToFirstChild ();
+                       nav.MoveToFirstNamespace ();
+                       nav.ReadSubtree ();
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void ReadSubtreePI ()
+               {
+                       string xml = "<?pi ?><root xmlns='urn:foo' />";
+                       nav = GetXmlDocumentNavigator (xml);
+                       nav.MoveToFirstChild ();
+                       nav.ReadSubtree ();
+               }
+
+               [Test]
+               [ExpectedException (typeof (InvalidOperationException))]
+               public void ReadSubtreeComment ()
+               {
+                       string xml = "<!-- comment --><root xmlns='urn:foo' />";
+                       nav = GetXmlDocumentNavigator (xml);
+                       nav.MoveToFirstChild ();
+                       nav.ReadSubtree ();
+               }
        }
 }
 

Modified: 
trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorTests.cs
===================================================================
--- trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorTests.cs     
2006-11-10 01:15:58 UTC (rev 67658)
+++ trunk/mcs/class/System.XML/Test/System.Xml.XPath/XPathNavigatorTests.cs     
2006-11-10 01:39:23 UTC (rev 67659)
@@ -551,6 +551,47 @@
                        AssertEquals ("#3", String.Empty, 
iter.Current.InnerXml);
                        AssertEquals ("#4", "Hello", iter.Current.OuterXml);
                }
+
+               [Test] // bug #79875
+               public void InnerXmlAttribute ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo 
attr='val1'/></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
+                       iter.MoveNext ();
+                       AssertEquals ("val1", iter.Current.InnerXml);
+               }
+
+               [Test]
+               public void InnerXmlTextEscape ()
+               {
+                       StringReader sr = new StringReader 
("<Abc><Foo>Hello&lt;\r\nInnerXml</Foo></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo");
+                       iter.MoveNext ();
+                       AssertEquals ("#1", "Hello&lt;\r\nInnerXml", 
iter.Current.InnerXml);
+                       AssertEquals ("#2", "<Foo>Hello&lt;\r\nInnerXml</Foo>", 
iter.Current.OuterXml);
+                       iter = nav.Select ("/Abc/Foo/text()");
+                       iter.MoveNext ();
+                       AssertEquals ("#3", String.Empty, 
iter.Current.InnerXml);
+                       AssertEquals ("#4", "Hello&lt;\r\nInnerXml", 
iter.Current.OuterXml);
+               }
+
+               [Test]
+               [Category ("NotDotNet")] // .NET bug; it should escape value
+               public void InnerXmlAttributeEscape ()
+               {
+                       StringReader sr = new StringReader ("<Abc><Foo 
attr='val&quot;1&#13;&#10;&gt;'/></Abc>");
+                       XPathDocument doc = new XPathDocument (sr);
+                       XPathNavigator nav = doc.CreateNavigator ();
+
+                       XPathNodeIterator iter = nav.Select ("/Abc/Foo/@attr");
+                       iter.MoveNext ();
+                       AssertEquals ("val&quot;1&#10;&gt;", 
iter.Current.InnerXml);
+               }
 #endif
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to