Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/60816


Change subject: Alpha: Rewrite makePrettyDiff function
......................................................................

Alpha: Rewrite makePrettyDiff function

Add test case current function doesn't satisfy
Rewrite and simplify function

Change-Id: Iedf7643307247b6672ee63351dbd58a7431e98cc
---
M javascripts/specials/mobilediff.js
M tests/javascripts/specials/test_mobilediff.js
2 files changed, 31 insertions(+), 39 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/16/60816/1

diff --git a/javascripts/specials/mobilediff.js 
b/javascripts/specials/mobilediff.js
index 1c70966..2e21156 100644
--- a/javascripts/specials/mobilediff.js
+++ b/javascripts/specials/mobilediff.js
@@ -1,51 +1,39 @@
 ( function( $, M ) {
 
        function makePrettyDiff( $diff ) {
-               var $diffclone = $diff.clone();
+               var $diffclone = $diff.clone(), before = '', after = '',
+                       diff;
 
-               // simple case where just an insert or just a delete
-               if ( $diff.children( 'ins' ).length === 0 || $diff.children( 
'del' ).length === 0 ) {
-                       $diff.empty().addClass( 'prettyDiff' );
-                       $diffclone.find( 'del,ins' ).each( function() {
-                               var $el = $( this ).clone();
-                               if ( $el.text() ) { // don't add empty elements
-                                       $el.appendTo( $diff );
-                                       $( '<br>' ).appendTo( $diff );
-                               }
-                       } );
-                       return $diff;
-               } else if ( $diff.children().length > 1 ) { // if there is only 
one line it is not a complicated diff
-                       $diff.empty().addClass( 'prettyDiff' );
-                       $diffclone.find( 'ins' ).each( function() {
-                               var $del = $( this ).prev( 'del' ), $add = $( 
this ),
-                                       diffChars;
+               $diffclone.find( 'del,ins' ).each( function() {
+                       if ( this.tagName === 'DEL' ) {
+                               before += $( this ).text() + '<br>';
+                       } else {
+                               after += $( this ).text() + '<br>';
+                       }
+               } );
+               $diff.empty().addClass( 'prettyDiff' );
 
-                               if ( $del.length > 0 ) {
-                                       while ( $add.next()[ 0 ] && 
$add.next()[ 0 ].tagName === 'INS' ) {
-                                               $add.clone().appendTo( $diff );
+               diff = JsDiff.diffWords( before, after );
+               diff.forEach( function( change ) {
+                       var tag, vals;
+                       if ( change.added ) {
+                               tag = '<ins>';
+                       } else if ( change.removed ) {
+                               tag = '<del>';
+                       } else {
+                               tag = '<span>';
+                       }
+                       vals = change.value.split( '<br>' );
+                       vals.forEach( function( val ) {
+                               if ( val ) {
+                                       $( tag ).text( val ).appendTo( $diff );
+                                       if ( vals.length > 1 ) {
                                                $( '<br>' ).appendTo( $diff );
-                                               $add = $add.next();
                                        }
-
-                                       diffChars = JsDiff.diffWords( 
$del.text(), $add.text() );
-                                       diffChars.forEach( function( change ) {
-                                               var tag;
-                                               if ( change.added ) {
-                                                       tag = '<ins>';
-                                               } else if ( change.removed ) {
-                                                       tag = '<del>';
-                                               } else {
-                                                       tag = '<span>';
-                                               }
-                                               $( tag ).text( change.value 
).appendTo( $diff );
-                                       } );
-                               } else if ( $add.prev()[ 0 ].tagName !== 'INS' 
){
-                                       $add.clone().css( 'display', 'inline' 
).appendTo( $diff );
                                }
-
-                               $( '<br>' ).appendTo( $diff );
                        } );
-               }
+               } );
+
                return $diff;
        }
 
diff --git a/tests/javascripts/specials/test_mobilediff.js 
b/tests/javascripts/specials/test_mobilediff.js
index c2e4c37..ab22b97 100644
--- a/tests/javascripts/specials/test_mobilediff.js
+++ b/tests/javascripts/specials/test_mobilediff.js
@@ -12,6 +12,10 @@
                        '<span>Remove the </span><del>one 
</del><span>please</span><br>'
                ],
                [
+                       $( '<div><ins>Added text</ins><ins>Blah, bloh, foo and 
bleh [[linkchange]]</ins><del>Blah, bloh and bleh [[link]]</del><ins>more 
text</ins></div>' ),
+                       '<ins>Added text</ins><br><span>Blah, 
bloh</span><ins>,</ins><span> </span><ins>foo </ins><span>and bleh 
[[</span><ins>linkchange</ins><del>link</del><span>]]</span><br><ins>more 
text</ins><br>'
+               ],
+               [
                        $( '<div id="mw-mf-minidiff"><del>According to popular 
legend, the \'\'\'Shanghai Tunnels,\'\'\' less commonly known as the 
\'\'\'Portland Underground,\'\'\' are supposedly a group of passages running 
underneath [[Old Town Chinatown, Portland, Oregon|Old Town/Chinatown]] down to 
the central downtown section of [[Portland, Oregon|Portland]], [[Oregon]], 
[[United States]]. The tunnels connected the [[basement]]s of many downtown 
[[hotel]]s and [[bar (establishment)|bar]]s to the waterfront of the 
[[Willamette River]]. They were built to move goods from the ships docked on 
the Willamette to the basement storage areas, which allowed businesses to avoid 
streetcar and train traffic on the streets when delivering their 
goods.{{fact|date=March 2012}}</del><ins>According to popular legend, the 
\'\'\'Shanghai Tunnels,\'\'\' less commonly known as the \'\'\'Portland 
Underground,\'\'\' are a group of passages running underneath [[Old Town 
Chinatown, Portland, Oregon|Old Town/Chinatown]] down to the central downtown 
section of [[Portland, Oregon|Portland]], [[Oregon]], [[United States]]. The 
tunnels connected the [[basement]]s of many downtown [[hotel]]s and [[bar 
(establishment)|bar]]s to the waterfront of the [[Willamette River]]. They were 
built to move goods from the ships docked on the Willamette to the basement 
storage areas, which allowed businesses to avoid streetcar and train traffic on 
the streets when delivering their goods.{{fact|date=March 2012}}</ins></div>' ),
                        '<span>According to popular legend, the \'\'\'Shanghai 
Tunnels,\'\'\' less commonly known as the \'\'\'Portland Underground,\'\'\' are 
</span><del>supposedly </del><span>a group of passages running underneath [[Old 
Town Chinatown, Portland, Oregon|Old Town/Chinatown]] down to the central 
downtown section of [[Portland, Oregon|Portland]], [[Oregon]], [[United 
States]]. The tunnels connected the [[basement]]s of many downtown [[hotel]]s 
and [[bar (establishment)|bar]]s to the waterfront of the [[Willamette River]]. 
They were built to move goods from the ships docked on the Willamette to the 
basement storage areas, which allowed businesses to avoid streetcar and train 
traffic on the streets when delivering their goods.{{fact|date=March 
2012}}</span><br>'
                ],

-- 
To view, visit https://gerrit.wikimedia.org/r/60816
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iedf7643307247b6672ee63351dbd58a7431e98cc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to