@@ -150,6 +150,28 @@ public void TableWithSpecifiedWidths()
150
150
}
151
151
}
152
152
153
+ [ Test ]
154
+ public void Test_InvalidCharacter ( )
155
+ {
156
+ using ( var output = File . Open ( Path . Combine ( _directoryWithFiles , "InvalidCharacters.docx" ) , FileMode . Create ) )
157
+ {
158
+ using ( var doc = DocX . Create ( output ) )
159
+ {
160
+ doc . InsertParagraph ( "\b " ) ;
161
+ Exception ex = null ;
162
+ try
163
+ {
164
+ doc . Save ( ) ;
165
+ }
166
+ catch ( Exception e )
167
+ {
168
+ ex = e ;
169
+ }
170
+ Assert . IsTrue ( ex == null ) ;
171
+ }
172
+ }
173
+ }
174
+
153
175
/// <summary>
154
176
/// TextRemove should not remove empty paragraphs in case the paragraph is alone in the cell.
155
177
/// In the rest cases empty paragraph may be removed.
@@ -274,6 +296,29 @@ public void Test_Table_InsertRow_Keeps_Formatting()
274
296
}
275
297
}
276
298
299
+ [ Test ]
300
+ public void Test_Clone_Table_Twice ( )
301
+ {
302
+ using ( var input = File . Open ( Path . Combine ( _directoryWithFiles , "TableSpecifiedHeights.docx" ) , FileMode . Open ) )
303
+ {
304
+ using ( var doc = DocX . Load ( input ) )
305
+ {
306
+ // Make sure content of the file is ok for test
307
+ Assert . IsTrue ( doc . Tables . Count == 1 ) ;
308
+
309
+ Table tab1 = doc . Tables [ 0 ] ;
310
+ doc . InsertParagraph ( "" ) ;
311
+ Table tab2 = doc . InsertTable ( tab1 ) ;
312
+ Assert . IsTrue ( doc . Tables . Count == 2 ) ;
313
+ doc . InsertParagraph ( "" ) ;
314
+ Table tab3 = doc . InsertTable ( tab2 ) ;
315
+ Assert . IsTrue ( doc . Tables . Count == 3 ) ;
316
+
317
+ doc . SaveAs ( Path . Combine ( _directoryWithFiles , "TwoClonedTables.docx" ) ) ;
318
+ }
319
+ }
320
+ }
321
+
277
322
public string ReplaceFunc ( string findStr )
278
323
{
279
324
var testPatterns = new Dictionary < string , string >
@@ -803,7 +848,105 @@ public void Test_Insert_Picture()
803
848
}
804
849
}
805
850
806
- [ Test ]
851
+ /// <summary>
852
+ /// This test fills two tables with hyperlinks.
853
+ /// </summary>
854
+ [ Test ]
855
+ public void Test_Insert_Hyperlink_In_Tables ( )
856
+ {
857
+ using ( var input = File . Open ( Path . Combine ( _directoryWithFiles , "TableSpecifiedHeights.docx" ) , FileMode . Open ) )
858
+ {
859
+ using ( var doc = DocX . Load ( input ) )
860
+ {
861
+ // Make sure content of the file is ok for test
862
+ Assert . IsTrue ( doc . Tables . Count > 0 ) ;
863
+ Table tab1 = doc . Tables [ 0 ] ;
864
+ Assert . IsTrue ( tab1 . RowCount > 0 ) ;
865
+ Assert . IsTrue ( tab1 . Rows [ 0 ] . ColumnCount > 0 ) ;
866
+ doc . InsertParagraph ( "" ) ;
867
+ Table tab2 = doc . InsertTable ( tab1 ) ;
868
+ Assert . IsTrue ( tab2 . RowCount > 0 ) ;
869
+
870
+ Row row1 = tab1 . Rows [ 0 ] ;
871
+ Row row2 = tab2 . Rows [ 0 ] ;
872
+
873
+ // 10 times insert hyperlinks in both tables in tic-tak order
874
+ for ( int index = 0 ; index < 10 ; index ++ )
875
+ {
876
+ Row newRow1 = tab1 . InsertRow ( row1 ) ;
877
+ Row newRow2 = tab2 . InsertRow ( row2 ) ;
878
+
879
+ Hyperlink h1 = doc . AddHyperlink (
880
+ string . Format ( "Table {0}, Row {1}. Google searches for {0} {1}" , 1 , index + 1 ) ,
881
+ new Uri ( string . Format ( "https://www.google.com/search?q=Table{0}Row{1}" , 1 , index + 1 ) ) ) ;
882
+ newRow1 . Cells [ 0 ] . Paragraphs [ 0 ] . InsertHyperlink ( h1 ) ;
883
+
884
+ Hyperlink h2 = doc . AddHyperlink (
885
+ string . Format ( "Table {0}, Row {1}. Google searches for {0} {1}" , 2 , index + 1 ) ,
886
+ new Uri ( string . Format ( "https://www.google.com/search?q=Table{0}Row{1}" , 2 , index + 1 ) ) ) ;
887
+ newRow2 . Cells [ 0 ] . Paragraphs [ 0 ] . InsertHyperlink ( h2 ) ;
888
+
889
+ }
890
+ //Make sure links are ok and in right order
891
+ for ( int index = 0 ; index < doc . Hyperlinks . Count ; index ++ )
892
+ {
893
+ Hyperlink h = doc . Hyperlinks [ index ] ;
894
+ string text = string . Format ( "Table {0}, Row {1}. Google searches for {0} {1}" , ( index / 10 ) + 1 , ( index ) % 10 + 1 ) ;
895
+ string uri = string . Format ( "https://www.google.com/search?q=Table{0}Row{1}" , ( index / 10 ) + 1 , ( index ) % 10 + 1 ) ;
896
+ Assert . IsTrue ( string . Compare ( h . Text , text ) == 0 ) ;
897
+ Assert . IsTrue ( h . Uri != null ) ;
898
+ Assert . IsTrue ( string . Compare ( h . Uri . ToString ( ) , uri ) == 0 ) ;
899
+ }
900
+ doc . SaveAs ( Path . Combine ( _directoryDocuments , "Test_Insert_Hyperlink_In_Tables.docx" ) ) ;
901
+ }
902
+ }
903
+ }
904
+
905
+ /// <summary>
906
+ /// This test makes 2 file. The first uses InsertHyperlink. The second uses AppendHyperlink.
907
+ /// The both hyperlink collections should be equal to each other.
908
+ /// We need be sure the bug in InsertHyperlink is fixed (id attribute in hyperlink was empty and order of inserteed hyperlinks was broken).
909
+ /// </summary>
910
+ [ Test ]
911
+ public void Test_Compare_InsertHyperlink_And_AppendHyperLinks ( )
912
+ {
913
+ string fileName1 = Path . Combine ( _directoryDocuments , "Test_InsertHyperLinks.docx" ) ;
914
+ string fileName2 = Path . Combine ( _directoryDocuments , "Test_AppendHyperlinks.docx" ) ;
915
+ using ( DocX document1 = DocX . Create ( fileName1 ) )
916
+ {
917
+ using ( DocX document2 = DocX . Create ( fileName2 ) )
918
+ {
919
+ for ( int index = 0 ; index < 10 ; index ++ )
920
+ {
921
+ Hyperlink h = document1 . AddHyperlink (
922
+ string . Format ( "Google searches for {0}" , index + 1 ) ,
923
+ new Uri ( string . Format ( "https://www.google.com/search?q={0}" , index + 1 ) ) ) ;
924
+ document1 . InsertParagraph ( "" ) . InsertHyperlink ( h ) ;
925
+ }
926
+ document1 . Save ( ) ;
927
+
928
+ for ( int index = 0 ; index < 10 ; index ++ )
929
+ {
930
+ Hyperlink h = document2 . AddHyperlink (
931
+ string . Format ( "Google searches for {0}" , index + 1 ) ,
932
+ new Uri ( string . Format ( "https://www.google.com/search?q={0}" , index + 1 ) ) ) ;
933
+ document2 . InsertParagraph ( "" ) . AppendHyperlink ( h ) ;
934
+ }
935
+ document2 . Save ( ) ;
936
+
937
+ Assert . IsTrue ( document1 . Hyperlinks . Count == document2 . Hyperlinks . Count ) ;
938
+ for ( int index = 0 ; index < document1 . Hyperlinks . Count ; index ++ )
939
+ {
940
+ Hyperlink h1 = document1 . Hyperlinks [ index ] ;
941
+ Hyperlink h2 = document2 . Hyperlinks [ index ] ;
942
+ Assert . IsTrue ( string . Compare ( h1 . Text , h2 . Text ) == 0 ) ;
943
+ Assert . IsTrue ( string . Compare ( h1 . Uri . ToString ( ) , h2 . Uri . ToString ( ) ) == 0 ) ;
944
+ }
945
+ }
946
+ }
947
+ }
948
+
949
+ [ Test ]
807
950
public void Test_Insert_Hyperlink ( )
808
951
{
809
952
// Load test document.
0 commit comments