From c773c2e3939d0e14d4202f3acf50216e3775b35f Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Wed, 8 Apr 2009 05:25:14 +0000 Subject: [PATCH] Added empty string checks to XmlParser. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3960 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../XmlEditor/Project/Src/XmlParser.cs | 12 ++++++--- .../Test/Parser/AttributeNameTestFixture.cs | 25 +++++++++++++++++++ .../Parser/InsideAttributeValueTestFixture.cs | 6 +++++ .../Parser/NamespaceDeclarationTestFixture.cs | 8 +++++- 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs index a4e40dcc7e..92b1c304d9 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs @@ -118,7 +118,7 @@ namespace ICSharpCode.XmlEditor /// public static bool IsNamespaceDeclaration(string xml, int index) { - if (xml.Length == 0) { + if (String.IsNullOrEmpty(xml)) { return false; } @@ -185,7 +185,7 @@ namespace ICSharpCode.XmlEditor /// public static string GetAttributeName(string xml, int index) { - if (xml.Length == 0) { + if (String.IsNullOrEmpty(xml)) { return String.Empty; } @@ -203,7 +203,7 @@ namespace ICSharpCode.XmlEditor { string name = GetAttributeNameAtIndex(xml, index); QualifiedName qualifiedName = GetQualifiedName(name); - if (String.IsNullOrEmpty(qualifiedName.Namespace) && includeNamespace) { + if (qualifiedName != null && String.IsNullOrEmpty(qualifiedName.Namespace) && includeNamespace) { QualifiedNameCollection namespaces = new QualifiedNameCollection(); XmlElementPath path = GetActiveElementStartPathAtIndex(xml, index, namespaces); qualifiedName.Namespace = GetNamespaceForPrefix(namespaces, path.Elements.LastPrefix); @@ -226,6 +226,10 @@ namespace ICSharpCode.XmlEditor /// public static string GetAttributeNameAtIndex(string xml, int index) { + if (String.IsNullOrEmpty(xml)) { + return String.Empty; + } + index = GetCorrectedIndex(xml.Length, index); bool ignoreWhitespace = true; @@ -308,7 +312,7 @@ namespace ICSharpCode.XmlEditor /// public static bool IsInsideAttributeValue(string xml, int index) { - if (xml.Length == 0) { + if (String.IsNullOrEmpty(xml)) { return false; } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs index f543414054..bde890ed25 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/AttributeNameTestFixture.cs @@ -114,6 +114,31 @@ namespace XmlEditor.Tests.Parser QualifiedName name = XmlParser.GetQualifiedAttributeName(text, text.Length); Assert.AreEqual(expectedName, name); } + + [Test] + public void GetQualifiedAttributeNameWithEmptyString() + { + Assert.IsNull(XmlParser.GetQualifiedAttributeNameAtIndex(String.Empty, 0, true)); + } + + [Test] + public void GetAttributeNameAtIndexWithNullString() + { + Assert.AreEqual(String.Empty, XmlParser.GetAttributeNameAtIndex(null, 0)); + } + + [Test] + public void GetAttributeNameWithNullString() + { + Assert.AreEqual(String.Empty, XmlParser.GetAttributeName(null, 0)); + } + + + [Test] + public void GetQualifiedAttributeNameWithSingleXmlCharacter() + { + Assert.IsNull(XmlParser.GetQualifiedAttributeNameAtIndex("<", 0, true)); + } } } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs b/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs index afb997e7e9..f093ae92a3 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs @@ -104,5 +104,11 @@ namespace XmlEditor.Tests.Parser string xml = "