Переглянути джерело

Added the function Paragraph.RemoveHyperlink(int index)

Added unit tests for Paragraph.RemoveHyperlink(int index)
master
coffeycathal_cp 15 роки тому
джерело
коміт
782cb374cd
2 змінених файлів з 102 додано та 0 видалено
  1. 63
    0
      DocX/Paragraph.cs
  2. 39
    0
      UnitTests/UnitTest1.cs

+ 63
- 0
DocX/Paragraph.cs Переглянути файл

@@ -869,6 +869,69 @@ namespace Novacode
return this;
}
/// <summary>
/// Remove the Hyperlink at the provided index. The first hyperlink is at index 0.
/// Using a negative index or an index greater than the index of the last hyperlink will cause an ArgumentOutOfRangeException() to be thrown.
/// </summary>
/// <param name="index">The index of the hyperlink to be removed.</param>
/// <example>
/// <code>
/// // Crete a new document.
/// using (DocX document = DocX.Create("Test.docx"))
/// {
/// // Add a Hyperlink into this document.
/// Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
///
/// // Insert a new Paragraph into the document.
/// Paragraph p1 = document.InsertParagraph("AC");
///
/// // Insert the hyperlink into this Paragraph.
/// p1.InsertHyperlink(1, h);
/// Assert.IsTrue(p1.Text == "AlinkC"); // Make sure the hyperlink was inserted correctly;
///
/// // Remove the hyperlink
/// p1.RemoveHyperlink(0);
/// Assert.IsTrue(p1.Text == "AC"); // Make sure the hyperlink was removed correctly;
/// }
/// </code>
/// </example>
public void RemoveHyperlink(int index)
{
// Dosen't make sense to remove a Hyperlink at a negative index.
if (index < 0)
throw new ArgumentOutOfRangeException();
// Need somewhere to store the count.
int count = 0;
bool found = false;
RemoveHyperlinkRecursive(Xml, index, ref count, ref found);
// If !found then the user tried to remove a hyperlink at an index greater than the last.
if (!found)
throw new ArgumentOutOfRangeException();
}
internal void RemoveHyperlinkRecursive(XElement xml, int index, ref int count, ref bool found)
{
if (xml.Name.LocalName.Equals("hyperlink", StringComparison.CurrentCultureIgnoreCase))
{
// This is the hyperlink to be removed.
if (count == index)
{
found = true;
xml.Remove();
}
else
count++;
}
if (xml.HasElements)
foreach (XElement e in xml.Elements())
if (!found)
RemoveHyperlinkRecursive(e, index, ref count, ref found);
}
/// <summary>
/// Insert a Paragraph after this Paragraph, this Paragraph may have come from the same or another document.
/// </summary>

+ 39
- 0
UnitTests/UnitTest1.cs Переглянути файл

@@ -154,6 +154,45 @@ namespace UnitTests
}
}
[TestMethod]
public void Test_Paragraph_RemoveHyperlink()
{
// Create a new document
using (DocX document = DocX.Create("Test.docx"))
{
// Add a Hyperlink to this document.
Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
// Simple
Paragraph p1 = document.InsertParagraph("AC");
p1.InsertHyperlink(0, h); Assert.IsTrue(p1.Text == "linkAC");
p1.InsertHyperlink(p1.Text.Length, h); Assert.IsTrue(p1.Text == "linkAClink");
p1.InsertHyperlink(p1.Text.IndexOf("C"), h); Assert.IsTrue(p1.Text == "linkAlinkClink");
// Try and remove a Hyperlink using a negative index.
// This should throw an exception.
try
{
p1.RemoveHyperlink(-1);
Assert.Fail();
}
catch (ArgumentException e) { }
// Try and remove a Hyperlink at an index greater than the last.
// This should throw an exception.
try
{
p1.RemoveHyperlink(3);
Assert.Fail();
}
catch (ArgumentException e) {}
p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AlinkClink");
p1.RemoveHyperlink(1); Assert.IsTrue(p1.Text == "AlinkC");
p1.RemoveHyperlink(0); Assert.IsTrue(p1.Text == "AC");
}
}
[TestMethod]
public void Test_Paragraph_ReplaceText()
{

Завантаження…
Відмінити
Зберегти