Browse Source

Refactored previous change to avoid other null references that could occur when typing in spaces around element names.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3003 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 18 years ago
parent
commit
3204788971
  1. 9
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs
  2. 30
      src/AddIns/DisplayBindings/XmlEditor/Test/Parser/ActiveElementStartPathTestFixture.cs

9
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs

@ -570,6 +570,10 @@ namespace ICSharpCode.XmlEditor @@ -570,6 +570,10 @@ namespace ICSharpCode.XmlEditor
static XmlElementPath GetActiveElementStartPath(string xml, int index, string elementText, QualifiedNameCollection namespaces)
{
QualifiedName elementName = GetElementName(elementText);
if (elementName == null) {
return new XmlElementPath();
}
NamespaceURI elementNamespace = GetElementNamespace(elementText);
XmlElementPath path = GetFullParentElementPath(xml.Substring(0, index), namespaces);
@ -661,10 +665,7 @@ namespace ICSharpCode.XmlEditor @@ -661,10 +665,7 @@ namespace ICSharpCode.XmlEditor
elementEndIndex = xml.IndexOf(' ', elementStartIndex);
}
if (elementEndIndex >= elementStartIndex) {
string elementName = xml.Substring(elementStartIndex, elementEndIndex - elementStartIndex).Trim();
if (elementName.Length > 1) {
return elementName;
}
return xml.Substring(elementStartIndex, elementEndIndex - elementStartIndex);
}
}
return null;

30
src/AddIns/DisplayBindings/XmlEditor/Test/Parser/ActiveElementStartPathTestFixture.cs

@ -119,7 +119,7 @@ namespace XmlEditor.Tests.Parser @@ -119,7 +119,7 @@ namespace XmlEditor.Tests.Parser
/// tabs.
/// </summary>
[Test]
public void EmptyElement()
public void ActiveElementIsEmptyElementFollowedBySpaces()
{
string xml = "<Page>\r\n" +
" <Grid><\r\n" + // Cursor position is after the opening tag < at the end of this line.
@ -136,5 +136,33 @@ namespace XmlEditor.Tests.Parser @@ -136,5 +136,33 @@ namespace XmlEditor.Tests.Parser
XmlElementPath path = XmlParser.GetActiveElementStartPathAtIndex(xml, index);
Assert.AreEqual(0, path.Elements.Count, "Should be no elements since there is no element at the index.");
}
/// <summary>
/// If the user presses the space bar only having typed in the start tag then there is null
/// reference exception.
/// </summary>
[Test]
public void EmptyElementStartTagFollowedBySpace()
{
string xml = "<Xml1>\r\n" +
"\t< ";
XmlElementPath path = XmlParser.GetActiveElementStartPath(xml, xml.Length - 1);
Assert.AreEqual(0, path.Elements.Count, "Should be no elements since there is no element at the index.");
}
/// <summary>
/// If the user presses the space bar after typing in an element name starting with a space
/// character then a null exception occurs.
/// </summary>
[Test]
public void SpacesAroundElementName()
{
string xml = "<Xml1>\r\n" +
"\t< a ";
XmlElementPath path = XmlParser.GetActiveElementStartPath(xml, xml.Length - 1);
Assert.AreEqual(0, path.Elements.Count, "Should be no elements since there is no element at the index.");
}
}
}

Loading…
Cancel
Save