@@ -8,7 +8,7 @@ class ListDiff extends HtmlDiff
8
8
* This is the minimum percentage a list item can match its counterpart in order to be considered a match.
9
9
* @var integer
10
10
*/
11
- protected static $ listMatchThreshold = 50 ;
11
+ protected static $ listMatchThreshold = 35 ;
12
12
13
13
/** @var array */
14
14
protected $ listWords = array ();
@@ -433,56 +433,118 @@ protected function buildChildLists()
433
433
* Build the content of the class.
434
434
*/
435
435
protected function diff ()
436
- {
436
+ {
437
437
// Add the opening parent node from listType. So if ol, <ol>, etc.
438
438
$ this ->content = $ this ->addListTypeWrapper ();
439
439
440
440
$ oldIndexCount = 0 ;
441
+ $ diffOrderNewKeys = array_keys ($ this ->diffOrderIndex ['new ' ]);
441
442
foreach ($ this ->diffOrderIndex ['new ' ] as $ key => $ index ) {
442
443
443
444
if ($ index ['type ' ] == "list " ) {
445
+
446
+ // Check to see if an old list was deleted.
447
+ $ oldMatch = $ this ->getArrayByColumnValue ($ this ->textMatches , 'old ' , $ index ['position ' ]);
448
+ if ($ oldMatch && $ oldMatch ['new ' ] === null ) {
449
+ $ newList = '' ;
450
+ $ oldList = $ this ->getListByMatch ($ oldMatch , 'old ' );
451
+ $ this ->content .= $ this ->addListElementToContent ($ newList , $ oldList , $ oldMatch );
452
+ }
453
+
444
454
$ match = $ this ->getArrayByColumnValue ($ this ->textMatches , 'new ' , $ index ['position ' ]);
445
455
$ newList = $ this ->childLists ['new ' ][$ match ['new ' ]];
446
- $ oldList = array_key_exists ($ match ['old ' ], $ this ->childLists ['old ' ])
447
- ? $ this ->childLists ['old ' ][$ match ['old ' ]]
448
- : '' ;
449
-
450
- $ content = "<li> " ;
451
- $ content .= $ this ->processPlaceholders (
452
- $ this ->diffElements (
453
- $ this ->convertListContentArrayToString ($ oldList ),
454
- $ this ->convertListContentArrayToString ($ newList ),
455
- false
456
- ),
457
- $ match
458
- );
459
- $ content .= "</li> " ;
460
- $ this ->content .= $ content ;
456
+ $ oldList = $ this ->getListByMatch ($ match , 'old ' );
457
+ $ this ->content .= $ this ->addListElementToContent ($ newList , $ oldList , $ match );
461
458
}
462
459
463
460
if ($ index ['type ' ] == 'content ' ) {
464
- $ newContent = $ this ->contentIndex ['new ' ][$ index ['position ' ]];
465
-
466
- $ oldDiffOrderIndexMatch = array_key_exists ($ oldIndexCount , $ this ->diffOrderIndex ['old ' ])
467
- ? $ this ->diffOrderIndex ['old ' ][$ oldIndexCount ]
468
- : '' ;
469
-
470
- $ oldContent = $ oldDiffOrderIndexMatch && array_key_exists ($ oldDiffOrderIndexMatch ['position ' ], $ this ->contentIndex ['old ' ])
471
- ? $ this ->contentIndex ['old ' ][$ oldDiffOrderIndexMatch ['position ' ]]
472
- : '' ;
473
-
474
- $ diffObject = new HtmlDiff ($ oldContent , $ newContent );
475
- $ content = $ diffObject ->build ();
476
- $ this ->content .= $ content ;
461
+ $ this ->content .= $ this ->addContentElementsToContent ($ oldIndexCount , $ index ['position ' ]);
477
462
}
478
463
479
464
$ oldIndexCount ++;
465
+
466
+ if ($ key == $ diffOrderNewKeys [count ($ diffOrderNewKeys ) - 1 ]) {
467
+ foreach ($ this ->diffOrderIndex ['old ' ] as $ oldKey => $ oldIndex ) {
468
+ if ($ oldKey > $ key ) {
469
+ if ($ oldIndex ['type ' ] == 'list ' ) {
470
+ $ oldMatch = $ this ->getArrayByColumnValue ($ this ->textMatches , 'old ' , $ oldIndex ['position ' ]);
471
+ if ($ oldMatch && $ oldMatch ['new ' ] === null ) {
472
+ $ newList = '' ;
473
+ $ oldList = $ this ->getListByMatch ($ oldMatch , 'old ' );
474
+ $ this ->content .= $ this ->addListElementToContent ($ newList , $ oldList , $ oldMatch );
475
+ }
476
+ } else {
477
+ $ this ->content .= $ this ->addContentElementsToContent ($ oldKey );
478
+ }
479
+ }
480
+ }
481
+ }
480
482
}
481
483
482
484
// Add the closing parent node from listType. So if ol, </ol>, etc.
483
485
$ this ->content .= $ this ->addListTypeWrapper (false );
484
486
}
485
487
488
+ /**
489
+ *
490
+ * @param string $newList
491
+ * @param string $oldList
492
+ * @param array $match
493
+ * @return string
494
+ */
495
+ protected function addListElementToContent ($ newList , $ oldList , array $ match )
496
+ {
497
+ $ content = "<li> " ;
498
+ $ content .= $ this ->processPlaceholders (
499
+ $ this ->diffElements (
500
+ $ this ->convertListContentArrayToString ($ oldList ),
501
+ $ this ->convertListContentArrayToString ($ newList ),
502
+ false
503
+ ),
504
+ $ match
505
+ );
506
+ $ content .= "</li> " ;
507
+ return $ content ;
508
+ }
509
+
510
+ /**
511
+ *
512
+ * @param integer $oldIndexCount
513
+ * @param null|integer $newPosition
514
+ * @return string
515
+ */
516
+ protected function addContentElementsToContent ($ oldIndexCount , $ newPosition = null )
517
+ {
518
+ $ newContent = $ newPosition && array_key_exists ($ newPosition , $ this ->contentIndex ['new ' ])
519
+ ? $ this ->contentIndex ['new ' ][$ newPosition ]
520
+ : '' ;
521
+
522
+ $ oldDiffOrderIndexMatch = array_key_exists ($ oldIndexCount , $ this ->diffOrderIndex ['old ' ])
523
+ ? $ this ->diffOrderIndex ['old ' ][$ oldIndexCount ]
524
+ : '' ;
525
+
526
+ $ oldContent = $ oldDiffOrderIndexMatch && array_key_exists ($ oldDiffOrderIndexMatch ['position ' ], $ this ->contentIndex ['old ' ])
527
+ ? $ this ->contentIndex ['old ' ][$ oldDiffOrderIndexMatch ['position ' ]]
528
+ : '' ;
529
+
530
+ $ diffObject = new HtmlDiff ($ oldContent , $ newContent );
531
+ $ content = $ diffObject ->build ();
532
+ return $ content ;
533
+ }
534
+
535
+ /**
536
+ *
537
+ * @param array $match
538
+ * @param string $type
539
+ * @return array|string
540
+ */
541
+ protected function getListByMatch (array $ match , $ type = 'new ' )
542
+ {
543
+ return array_key_exists ($ match [$ type ], $ this ->childLists [$ type ])
544
+ ? $ this ->childLists [$ type ][$ match [$ type ]]
545
+ : '' ;
546
+ }
547
+
486
548
/**
487
549
* This function replaces array_column function in PHP for older versions of php.
488
550
*
0 commit comments