Преглед изворни кода

Bug fix in Paragraph.InsertHyperlink.

Paragraph.AppendHyperlink works fine. Paragraph.InsertHyperlink generates hyperlinks with empty ids. MS Word doesn't open files after Paragraph.InsertHyperlink.
master
Viktor Loktev пре 9 година
родитељ
комит
86d7b3206d
2 измењених фајлова са 105 додато и 5 уклоњено
  1. 6
    4
      DocX/Paragraph.cs
  2. 99
    1
      UnitTests/DocXUnitTests.cs

+ 6
- 4
DocX/Paragraph.cs Прегледај датотеку

@@ -1092,7 +1092,7 @@ namespace Novacode
XElement h_xml;
if (index == 0)
{
// Add this hyperlink as the last element.
// Add this hyperlink as the first element.
Xml.AddFirst(h.Xml);
// Extract the picture back out of the DOM.
@@ -1133,10 +1133,12 @@ namespace Novacode
h_xml = (XElement)run.Xml.NextNode;
}
h_xml.SetAttributeValue(DocX.r + "id", Id);
}
}
h_xml.SetAttributeValue( DocX.r + "id", Id );
return this;
this.runs = Xml.Elements().Last().Elements( XName.Get( "r", DocX.w.NamespaceName ) ).ToList();
return this;
}
/// <summary>

+ 99
- 1
UnitTests/DocXUnitTests.cs Прегледај датотеку

@@ -848,7 +848,105 @@ namespace UnitTests
}
}
[Test]
/// <summary>
/// This test fills two tables with hyperlinks.
/// </summary>
[Test]
public void Test_Insert_Hyperlink_In_Tables()
{
using( var input = File.Open( Path.Combine( _directoryWithFiles, "TableSpecifiedHeights.docx" ), FileMode.Open ) )
{
using( var doc = DocX.Load( input ) )
{
// Make sure content of the file is ok for test
Assert.IsTrue( doc.Tables.Count > 0 );
Table tab1 = doc.Tables[ 0 ];
Assert.IsTrue( tab1.RowCount > 0 );
Assert.IsTrue( tab1.Rows[0].ColumnCount > 0 );
doc.InsertParagraph( "" );
Table tab2 = doc.InsertTable( tab1 );
Assert.IsTrue( tab2.RowCount > 0 );
Row row1 = tab1.Rows[ 0 ];
Row row2 = tab2.Rows[ 0 ];
// 10 times insert hyperlinks in both tables in tic-tak order
for( int index = 0; index < 10; index++ )
{
Row newRow1 = tab1.InsertRow( row1 );
Row newRow2 = tab2.InsertRow( row2 );
Hyperlink h1 = doc.AddHyperlink(
string.Format( "Table {0}, Row {1}. Google searches for {0} {1}", 1, index + 1 ),
new Uri( string.Format( "https://www.google.com/search?q=Table{0}Row{1}", 1, index + 1 ) ) );
newRow1.Cells[ 0 ].Paragraphs[ 0 ].InsertHyperlink( h1 );
Hyperlink h2 = doc.AddHyperlink(
string.Format( "Table {0}, Row {1}. Google searches for {0} {1}", 2, index + 1 ),
new Uri( string.Format( "https://www.google.com/search?q=Table{0}Row{1}", 2, index + 1 ) ) );
newRow2.Cells[ 0 ].Paragraphs[ 0 ].InsertHyperlink( h2 );
}
//Make sure links are ok and in right order
for( int index = 0; index < doc.Hyperlinks.Count; index++ )
{
Hyperlink h = doc.Hyperlinks[ index ];
string text = string.Format( "Table {0}, Row {1}. Google searches for {0} {1}", ( index / 10 ) + 1, ( index ) % 10 + 1 );
string uri = string.Format( "https://www.google.com/search?q=Table{0}Row{1}", ( index / 10 ) + 1, ( index ) % 10 + 1 );
Assert.IsTrue( string.Compare( h.Text, text ) == 0 );
Assert.IsTrue( h.Uri != null );
Assert.IsTrue( string.Compare( h.Uri.ToString(), uri ) == 0 );
}
doc.SaveAs( Path.Combine( _directoryDocuments, "Test_Insert_Hyperlink_In_Tables.docx" ) );
}
}
}
/// <summary>
/// This test makes 2 file. The first uses InsertHyperlink. The second uses AppendHyperlink.
/// The both hyperlink collections should be equal to each other.
/// We need be sure the bug in InsertHyperlink is fixed (id attribute in hyperlink was empty and order of inserteed hyperlinks was broken).
/// </summary>
[Test]
public void Test_Compare_InsertHyperlink_And_AppendHyperLinks()
{
string fileName1 = Path.Combine( _directoryDocuments, "Test_InsertHyperLinks.docx" );
string fileName2 = Path.Combine( _directoryDocuments, "Test_AppendHyperlinks.docx" );
using( DocX document1 = DocX.Create( fileName1 ) )
{
using( DocX document2 = DocX.Create( fileName2 ) )
{
for( int index = 0; index < 10; index++ )
{
Hyperlink h = document1.AddHyperlink(
string.Format( "Google searches for {0}", index + 1 ),
new Uri( string.Format( "https://www.google.com/search?q={0}", index + 1 ) ) );
document1.InsertParagraph( "" ).InsertHyperlink( h );
}
document1.Save();
for( int index = 0; index < 10; index++ )
{
Hyperlink h = document2.AddHyperlink(
string.Format( "Google searches for {0}", index + 1 ),
new Uri( string.Format( "https://www.google.com/search?q={0}", index + 1 ) ) );
document2.InsertParagraph( "" ).AppendHyperlink( h );
}
document2.Save();
Assert.IsTrue( document1.Hyperlinks.Count == document2.Hyperlinks.Count );
for( int index = 0; index < document1.Hyperlinks.Count; index++ )
{
Hyperlink h1 = document1.Hyperlinks[ index ];
Hyperlink h2 = document2.Hyperlinks[ index ];
Assert.IsTrue( string.Compare( h1.Text, h2.Text ) == 0 );
Assert.IsTrue( string.Compare( h1.Uri.ToString(), h2.Uri.ToString() ) == 0 );
}
}
}
}
[Test]
public void Test_Insert_Hyperlink()
{
// Load test document.

Loading…
Откажи
Сачувај