From 88ee3053c97cb24f1f6391e5c8a7ebb69aec3747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Mon, 19 Nov 2018 20:39:44 +0100 Subject: [PATCH] OS3-Port of #4020 --- .../motions/services/diff.service.spec.ts | 26 ++-- .../app/site/motions/services/diff.service.ts | 117 ++++++++++-------- 2 files changed, 82 insertions(+), 61 deletions(-) diff --git a/client/src/app/site/motions/services/diff.service.spec.ts b/client/src/app/site/motions/services/diff.service.spec.ts index df8cfa809..a19c638a6 100644 --- a/client/src/app/site/motions/services/diff.service.spec.ts +++ b/client/src/app/site/motions/services/diff.service.spec.ts @@ -669,7 +669,7 @@ describe('DiffService', () => { const diff = service.diff(before, after); expect(diff).toBe( '

 Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

\n' + - '

Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.

' + '

Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.

' ); } )); @@ -762,7 +762,7 @@ describe('DiffService', () => { '

Test 123

', expected = "

rief sie alle sieben herbei und sprach 'liebe Kinder, ich will hinaus in den Wald, seid Noch

" + - '

Test 123

'; + '

Test 123

'; const diff = service.diff(before, after); expect(diff).toBe(expected); @@ -778,7 +778,7 @@ describe('DiffService', () => { '

Stet clita kasd gubergren, no sea takimata sanctus est.

', expected = '

 Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.

\n' + - '

Stet clita kasd gubergren, no sea takimata sanctus est.

'; + '

Stet clita kasd gubergren, no sea takimata sanctus est.

'; const diff = service.diff(before, after); expect(diff).toBe(expected); @@ -792,8 +792,8 @@ describe('DiffService', () => { '

Neither should this line

', expected = '

This is a random first line that remains unchanged.

' + - '

Inserting this line should not make any troubles, especially not affect the first line

' + - '

Neither should this line

'; + '

Inserting this line should not make any troubles, especially not affect the first line

' + + '

Neither should this line

'; const diff = service.diff(before, after); expect(diff).toBe(expected); @@ -935,6 +935,16 @@ describe('DiffService', () => { } )); + it('works with multiple inserted paragraphs', inject( + [DiffService], + (service: DiffService) => { + const before = '

This is the text before

', + after = "

This is the text before

\n

This is one added line

\n

Another added line

"; + const diff = service.diff(before, after); + expect(diff).toBe("

This is the text before

\n

This is one added line

\n

Another added line

"); + } + )); + it('does not a change in a very specific case', inject([DiffService], (service: DiffService) => { // See diff._fixWrongChangeDetection const inHtml = @@ -1007,7 +1017,7 @@ describe('DiffService', () => { 'their grammar, their pronunciation and their most common words. Everyone ' + brMarkup(3) + 'realizes why a

\n' + - '

NEW PARAGRAPH 2.

' + '

NEW PARAGRAPH 2.

' ); } )); @@ -1033,8 +1043,8 @@ describe('DiffService', () => { 'their grammar, their pronunciation and their most common words. Everyone ' + brMarkup(3) + 'realizes why a

\n' + - '

NEW PARAGRAPH 1.

\n' + - '

NEW PARAGRAPH 2.

\n' + + '

NEW PARAGRAPH 1.

\n' + + '

NEW PARAGRAPH 2.

\n' + '
' + noMarkup(4) + 'Go on
' diff --git a/client/src/app/site/motions/services/diff.service.ts b/client/src/app/site/motions/services/diff.service.ts index 844fede67..2b5d8711c 100644 --- a/client/src/app/site/motions/services/diff.service.ts +++ b/client/src/app/site/motions/services/diff.service.ts @@ -1012,6 +1012,34 @@ export class DiffService { } } + /** + * Add the CSS-class to the existing "class"-attribute, or add one. + * Works on strings, not nodes + * + * @param {string} tagStr + * @param {string} className + * @returns {string} + */ + private addClassToHtmlTag(tagStr: string, className: string): string { + return tagStr.replace( + /<(\w+)( [^>]*)?>/gi, + (whole: string, tag: string, tagArguments: string): string => { + tagArguments = (tagArguments ? tagArguments : ''); + if (tagArguments.match(/class="/gi)) { + // class="someclass" => class="someclass insert" + tagArguments = tagArguments.replace(/(class\s*=\s*)(["'])([^\2]*)\2/gi, + (classWhole: string, attr: string, para: string, content: string): string => { + return attr + para + content + ' ' + className + para; + } + ); + } else { + tagArguments += ' class="' + className + '"'; + } + return '<' + tag + tagArguments + '>'; + } + ); + }; + /** * This fixes a very specific, really weird bug that is tested in the test case "does not a change in a very specific case". * @@ -1831,41 +1859,9 @@ export class DiffService { } ); + // Merging individual insert/delete statements into bigger blocks diffUnnormalized = diffUnnormalized.replace(/<\/ins>/gi, '').replace(/<\/del>/gi, ''); - // Move whitespaces around inserted P's out of the INS-tag - diffUnnormalized = diffUnnormalized.replace( - /(\s*)(]*)?>[\s\S]*?<\/p>)(\s*)<\/ins>/gim, - (match: string, whiteBefore: string, inner: string, tagInner: string, whiteAfter: string): string => { - return ( - whiteBefore + - inner - .replace( - /]*)?>/gi, - (match2: string): string => { - return match2 + ''; - } - ) - .replace(/<\/p>/gi, '

') + - whiteAfter - ); - } - ); - - // Fixes HTML produced by the diff like this: - // from:

Inserted Text

\n

More inserted text

- // into: Inserted Text

\n

More inserted text

- diffUnnormalized = diffUnnormalized.replace( - /<\/p><\/del>([\s\S]*?)<\/p><\/ins>/gim, - '$1

' - ); - diffUnnormalized = diffUnnormalized.replace( - /[\s\S]*?<\/ins>/gim, - (match: string): string => { - return match.replace(/(<\/p>\s*

)/gi, '$1'); - } - ); - // If only a few characters of a word have changed, don't display this as a replacement of the whole word, // but only of these specific characters diffUnnormalized = diffUnnormalized.replace( @@ -1919,28 +1915,43 @@ export class DiffService { } ); - //

  • ...
  • =>
  • ...
  • + // If larger inserted HTML text contains block elements, we separate the inserted text into + // inline elements and "insert"-class-based block elements. + // ...
    ...
    ...
    => ...
    ...
    ... diffUnnormalized = diffUnnormalized.replace( - /<(ins|del)><(p|div|blockquote|li)([^>]*)>([\s\S]*)<\/\2><\/\1>/gi, - (whole: string, insDel: string, block: string, blockArguments: string, content: string): string => { - // Prevent accidental matches like

    ...

    ...
    ...

    - if (content.match(/<(ins|del)>/gi)) { - return whole; - } - // Add the CSS-class to the existing "class"-attribute, or add one - let newArguments = blockArguments; + /<(ins|del)>([\s\S]*?)<\/\1>/gi, + (whole: string, insDel: string): string => { const modificationClass = (insDel.toLowerCase() === 'ins' ? 'insert' : 'delete'); - if (newArguments.match(/class="/gi)) { - // class="someclass" => class="someclass insert" - newArguments = newArguments.replace(/(class\s*=\s*)(["'])([^\2]*)\2/gi, - (classWhole: string, attr: string, para: string, classContent: string): string => { - return attr + para + classContent + ' ' + modificationClass + para; - } - ); - } else { - newArguments += ' class="' + modificationClass + '"'; - } - return '<' + block + newArguments + '>' + content + ''; + return whole.replace( + /(<(p|div|blockquote|li)[^>]*>)([\s\S]*?)(<\/\2>)/gi, + (whole2: string, opening: string, blockTag: string, content: string, closing: string): string => { + const modifiedTag = this.addClassToHtmlTag(opening, modificationClass); + return '' + modifiedTag + content + closing + '<' + insDel + '>'; + } + ); + } + ); + + // Cleanup leftovers from the operation above, when -tags ore -tags are left + // around block tags. It should be safe to remove them and just leave the whitespaces. + diffUnnormalized = diffUnnormalized.replace( + /<(ins|del)>(\s*)<\/\1>/gi, + (whole: string, insDel: string, space: string): string => space + ); + + //

    Added text

    -> Added text

    + diffUnnormalized = diffUnnormalized.replace( + /<\/(p|div|blockquote|li)><\/del>([\s\S]*?)<\/\1>(\s*)<\/ins>/gi, + (whole: string, blockTag: string, content: string, space: string): string => { + return '' + content + '' + space; + } + ); + + //

    ->

    + diffUnnormalized = diffUnnormalized.replace( + /(<\/(p|div|blockquote|li)>)(\s*)<\/(ins|del)>/gi, + (whole: string, ending: string, blockTag: string, space: string, insdel: string): string => { + return '' + ending + space; } );