From c3bd85e5eeaffd875269a6051987bd5fa2c5de39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Wed, 8 Mar 2017 21:29:34 +0100 Subject: [PATCH] Diff-Bugfix: don't repeat unchanged words --- openslides/motions/static/js/motions/diff.js | 33 +++++++++++++++----- tests/karma/motions/diff.service.test.js | 18 ++++++++++- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/openslides/motions/static/js/motions/diff.js b/openslides/motions/static/js/motions/diff.js index 1b1ecd64d..fce756f35 100644 --- a/openslides/motions/static/js/motions/diff.js +++ b/openslides/motions/static/js/motions/diff.js @@ -760,17 +760,17 @@ angular.module('OpenSlidesApp.motions.diff', ['OpenSlidesApp.motions.lineNumberi }; this._tokenizeHtml = function (str) { - var splitArrayEntries = function (arr, by, prepend) { + var splitArrayEntriesEmbedSeparator = function (arr, by, prepend) { var newArr = []; for (var i = 0; i < arr.length; i++) { - if (arr[i][0] == '<' && (by == " " || by == "\n")) { + if (arr[i][0] === '<' && (by === " " || by === "\n")) { // Don't split HTML tags newArr.push(arr[i]); continue; } var parts = arr[i].split(by); - if (parts.length == 1) { + if (parts.length === 1) { newArr.push(arr[i]); } else { var j; @@ -793,10 +793,29 @@ angular.module('OpenSlidesApp.motions.diff', ['OpenSlidesApp.motions.lineNumberi } return newArr; }; - var arr = splitArrayEntries([str], '<', true); - arr = splitArrayEntries(arr, '>', false); - arr = splitArrayEntries(arr, " ", false); - arr = splitArrayEntries(arr, "\n", false); + var splitArrayEntriesSplitSeparator = function (arr, by) { + var newArr = []; + for (var i = 0; i < arr.length; i++) { + if (arr[i][0] === '<') { + newArr.push(arr[i]); + continue; + } + var parts = arr[i].split(by); + for (var j = 0; j < parts.length; j++) { + if (j > 0) { + newArr.push(by); + } + newArr.push(parts[j]); + } + } + return newArr; + }; + var arr = splitArrayEntriesEmbedSeparator([str], '<', true); + arr = splitArrayEntriesEmbedSeparator(arr, '>', false); + arr = splitArrayEntriesSplitSeparator(arr, " "); + arr = splitArrayEntriesSplitSeparator(arr, "."); + arr = splitArrayEntriesEmbedSeparator(arr, "\n", false); + return arr; }; diff --git a/tests/karma/motions/diff.service.test.js b/tests/karma/motions/diff.service.test.js index 404681cd5..63c2698ea 100644 --- a/tests/karma/motions/diff.service.test.js +++ b/tests/karma/motions/diff.service.test.js @@ -347,7 +347,7 @@ describe('linenumbering', function () { after = "Some additional text to circumvent the threshold Test1 Test6 Test7 Test8 Test9"; var diff = diffService.diff(before, after); - expect(diff).toBe('Some additional text to circumvent the threshold Test1 Test2 Test3 Test4 Test5 Test6 Test7 Test8 Test9'); + expect(diff).toBe('Some additional text to circumvent the threshold Test1 Test2 Test3 Test4 Test5Test6 Test7 Test8 Test9'); }); it('detects insertions and deletions in a word (1)', function () { @@ -416,5 +416,21 @@ describe('linenumbering', function () { var diff = diffService.diff(before, after); expect(diff).toBe('

Dann kam er zurück, klopfte an die Hausthür und rief \'macht auf, ihr lieben Kinder, eure Mutter ist da und hat jedem von Euch etwas mitgebarcht.\' Aber der Wolf hatte seine schwarze Pfote in das Fenster gelegt, das sahen die Kinder und riefen

(hier: Missbrauch von bewusstseinsverändernde Mittel - daher Zensiert)

'); }); + + it('does not repeat the last word (1)', function () { + var before = "

sem. Nulla consequat massa quis enim. 

", + after = "

sem. Nulla consequat massa quis enim. TEST
\nTEST

"; + var diff = diffService.diff(before, after); + + expect(diff).toBe('

sem. Nulla consequat massa quis enim. TEST
' + "\n" + "TEST

"); + }); + + it('does not repeat the last word (2)', function () { + var before = "

...so frißt er Euch alle mit Haut und Haar.

", + after = "

...so frißt er Euch alle mit Haut und Haar und Augen und Därme und alles.

"; + var diff = diffService.diff(before, after); + + expect(diff).toBe("

...so frißt er Euch alle mit Haut und Haar und Augen und Därme und alles.

"); + }); }); });