@@ -411,7 +411,7 @@ public virtual void ReplaceText( string searchValue,
411411
412412 // Update paragraph Indexes once to have valid values.
413413 this . RefreshParagraphIndexes ( ) ;
414- // Paragraph indexes won't be update during replacement, only after.
414+ // Paragraph indexes won't be updated during replacement, only after.
415415 this . PreventUpdateParagraphIndexes = true ;
416416
417417 foreach ( var p in this . Paragraphs )
@@ -422,7 +422,7 @@ public virtual void ReplaceText( string searchValue,
422422
423423 if ( this . Paragraphs . Count > 0 )
424424 {
425- this . Paragraphs [ 0 ] . GetMainParentContainer ( ) . NeedRefreshParagraphIndexes = true ;
425+ this . Paragraphs [ 0 ] . NeedRefreshIndexes ( ) ;
426426 }
427427 }
428428
@@ -482,7 +482,7 @@ public virtual void ReplaceText( string searchValue,
482482
483483 if ( this . Paragraphs . Count > 0 )
484484 {
485- this . Paragraphs [ 0 ] . GetMainParentContainer ( ) . NeedRefreshParagraphIndexes = true ;
485+ this . Paragraphs [ 0 ] . NeedRefreshIndexes ( ) ;
486486 }
487487 }
488488
@@ -530,7 +530,7 @@ public virtual void ReplaceTextWithObject( string searchValue,
530530
531531 if ( this . Paragraphs . Count > 0 )
532532 {
533- this . Paragraphs [ 0 ] . GetMainParentContainer ( ) . NeedRefreshParagraphIndexes = true ;
533+ this . Paragraphs [ 0 ] . NeedRefreshIndexes ( ) ;
534534 }
535535 }
536536
@@ -564,6 +564,8 @@ public virtual bool ReplaceTextWithObject( ObjectReplaceTextOptions replaceTextO
564564 return this . ReplaceTextCore ( replaceTextOptions ) ;
565565 }
566566
567+
568+
567569 /// Inserts the provided text at a bookmark location in this Container, using the specified formatting.
568570 public virtual void InsertAtBookmark ( string toInsert , string bookmarkName , Formatting formatting = null )
569571 {
@@ -680,7 +682,7 @@ split[ 1 ]
680682 }
681683 this . SetParentContainer ( p ) ;
682684 // Clear Paragraph cache because the split modified the paragraphs.
683- this . ClearParagraphsCache ( ) ;
685+ this . ClearParagraphsCache ( ) ;
684686
685687 return p ;
686688 }
@@ -855,7 +857,7 @@ public virtual Paragraph InsertParagraph( string text, bool trackChanges, Format
855857 }
856858
857859 this . SetParentContainer ( newParagraphAdded ) ;
858- this . AddParagraphInCache ( newParagraphAdded ) ;
860+ this . AddParagraphInCache ( newParagraphAdded ) ;
859861
860862 return newParagraphAdded ;
861863 }
@@ -869,6 +871,8 @@ public virtual Paragraph InsertParagraph( string text, bool trackChanges, Format
869871
870872
871873
874+
875+
872876 /// <summary>
873877 /// Removes paragraph at specified position
874878 /// </summary>
@@ -1047,6 +1051,8 @@ public virtual List InsertList( int index, List list )
10471051 elements . Add ( split [ 1 ] ) ;
10481052 p . Xml . ReplaceWith ( elements . ToArray ( ) ) ;
10491053
1054+ this . ClearParagraphsCache ( ) ;
1055+
10501056 return list ;
10511057 }
10521058
@@ -1111,10 +1117,13 @@ internal List<Paragraph> GetParagraphs()
11111117 }
11121118
11131119 var paragraph = new Paragraph ( this . Document , xElement , index ) ;
1120+ paragraph . _startIndex = index ;
1121+ var length = HelperFunctions . GetText ( xElement ) . Length ;
1122+ index += Math . Max ( 1 , length ) ;
1123+ paragraph . _endIndex = Math . Max ( 1 , index - 1 ) ;
11141124 paragraph . ParentContainer = this . GetParentFromXmlName ( paragraph . Xml . Ancestors ( ) . First ( ) . Name . LocalName ) ;
11151125 paragraph . PackagePart = this . PackagePart ;
11161126 paragraphs . Add ( paragraph ) ;
1117- index += Math . Max ( 1 , HelperFunctions . GetText ( xElement ) . Length ) ;
11181127 }
11191128 }
11201129
@@ -1265,7 +1274,7 @@ internal void AddParagraphInCache( Paragraph p )
12651274 {
12661275 this . Document . ClearParagraphsCache ( ) ;
12671276 }
1268- p . GetMainParentContainer ( ) . NeedRefreshParagraphIndexes = true ;
1277+ p . NeedRefreshIndexes ( ) ;
12691278 }
12701279
12711280 #endregion
@@ -1274,31 +1283,29 @@ internal void AddParagraphInCache( Paragraph p )
12741283
12751284 private void RemoveParagraphFromCache ( int index )
12761285 {
1277- if ( ( index != - 1 )
1286+ if ( ( index >= 0 )
12781287 && ( _editableParagraphsCollection . Count > 0 )
12791288 && ( index < _editableParagraphsCollection . Count ) )
12801289 {
1281- var mainContainer = _editableParagraphsCollection [ index ] . GetMainParentContainer ( ) ;
12821290 _editableParagraphsCollection . RemoveAt ( index ) ;
12831291 if ( this is Section )
12841292 {
12851293 this . Document . ClearParagraphsCache ( ) ;
12861294 }
1287- mainContainer . NeedRefreshParagraphIndexes = true ;
12881295 }
12891296 }
12901297
12911298 private void RemoveParagraphFromCache ( Paragraph paragraph )
12921299 {
12931300 var index = _editableParagraphsCollection . FindIndex ( p => p . Xml == paragraph . Xml ) ;
1294- if ( index != - 1 )
1301+ if ( index >= 0 )
12951302 {
12961303 _editableParagraphsCollection . RemoveAt ( index ) ;
12971304 if ( this is Section )
12981305 {
12991306 this . Document . ClearParagraphsCache ( ) ;
13001307 }
1301- paragraph . GetMainParentContainer ( ) . NeedRefreshParagraphIndexes = true ;
1308+ paragraph . NeedRefreshIndexes ( ) ;
13021309 }
13031310 }
13041311
@@ -1427,35 +1434,49 @@ private bool ReplaceTextCore( ReplaceTextOptionsBase replaceTextOptions )
14271434
14281435 // Update paragraph Indexes once to have valid values.
14291436 this . RefreshParagraphIndexes ( ) ;
1430- // Paragraph indexes won't be update during replacement, only after.
1437+ // Paragraph indexes won't be updated during replacement, only after.
14311438 this . PreventUpdateParagraphIndexes = true ;
14321439
1433- foreach ( var p in this . Paragraphs . ToList ( ) )
1434- {
1435- if ( ( replaceTextOptions . StartIndex >= 0 ) && ( p . EndIndex < replaceTextOptions . StartIndex ) )
1436- continue ;
1437- if ( ( replaceTextOptions . EndIndex >= 0 ) && ( p . StartIndex > replaceTextOptions . EndIndex ) )
1438- break ;
14391440
1440- var result = replaceTextOptions is StringReplaceTextOptions
1441- ? p . ReplaceText ( replaceTextOptions as StringReplaceTextOptions )
1442- : replaceTextOptions is FunctionReplaceTextOptions
1443- ? p . ReplaceText ( replaceTextOptions as FunctionReplaceTextOptions )
1444- : p . ReplaceTextWithObject ( replaceTextOptions as ObjectReplaceTextOptions ) ;
14451441
1446- if ( ! replaceSuccess )
1442+
1443+
1444+
1445+ foreach ( var p in this . Paragraphs . ToList ( ) )
14471446 {
1448- replaceSuccess = result ;
1449- }
1447+ if ( ( replaceTextOptions . StartIndex >= 0 ) && ( p . EndIndex < replaceTextOptions . StartIndex ) )
1448+ continue ;
1449+ if ( ( replaceTextOptions . EndIndex >= 0 ) && ( p . StartIndex > replaceTextOptions . EndIndex ) )
1450+ break ;
14501451
1451- if ( replaceTextOptions . StopAfterOneReplacement && result )
1452- break ;
1452+ bool result = false ;
1453+ if ( replaceTextOptions is StringReplaceTextOptions )
1454+ {
1455+ result = p . ReplaceText ( replaceTextOptions as StringReplaceTextOptions ) ;
1456+ }
1457+ else if ( replaceTextOptions is FunctionReplaceTextOptions )
1458+ {
1459+ result = p . ReplaceText ( replaceTextOptions as FunctionReplaceTextOptions ) ;
1460+ }
1461+ else
1462+ {
1463+ result = p . ReplaceTextWithObject ( replaceTextOptions as ObjectReplaceTextOptions ) ;
1464+ }
1465+
1466+ if ( ! replaceSuccess )
1467+ {
1468+ replaceSuccess = result ;
1469+ }
1470+
1471+ if ( replaceTextOptions . StopAfterOneReplacement && result )
1472+ break ;
14531473
14541474 }
14551475
14561476 this . PreventUpdateParagraphIndexes = false ;
14571477 this . NeedRefreshParagraphIndexes = true ;
14581478
1479+
14591480 return replaceSuccess ;
14601481 }
14611482
0 commit comments