From 45cac43193a48f7e40ef35edfe014b480f57959a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Wed, 22 Mar 2017 20:39:58 +0100 Subject: [PATCH] Remove erroneous margin around inline diff paragraphs in PDF --- openslides/core/static/js/core/pdf.js | 10 ++++++++-- openslides/motions/static/js/motions/base.js | 17 ++++++++++++----- openslides/motions/static/js/motions/diff.js | 12 ++++++++++++ tests/karma/motions/diff.service.test.js | 14 ++++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/openslides/core/static/js/core/pdf.js b/openslides/core/static/js/core/pdf.js index 6b08fac44..32c9e51bd 100644 --- a/openslides/core/static/js/core/pdf.js +++ b/openslides/core/static/js/core/pdf.js @@ -498,6 +498,7 @@ angular.module('OpenSlidesApp.core.pdf', []) */ ParseElement = function(alreadyConverted, element, currentParagraph, styles, diff_mode) { styles = styles || []; + var classes = []; if (element.getAttribute) { styles = []; var nodeStyle = element.getAttribute("style"); @@ -509,7 +510,8 @@ angular.module('OpenSlidesApp.core.pdf', []) } var nodeClass = element.getAttribute("class"); if (nodeClass) { - nodeClass.split(" ").forEach(function(nodeClass) { + classes = nodeClass.toLowerCase().split(" "); + classes.forEach(function(nodeClass) { if (typeof(classStyles[nodeClass]) != 'undefined') { classStyles[nodeClass].forEach(function(style) { styles.push(style); @@ -678,7 +680,11 @@ angular.module('OpenSlidesApp.core.pdf', []) break; case "p": currentParagraph = create("text"); - currentParagraph.marginTop = 8; + if (classes.indexOf("merge-before") > -1) { + currentParagraph.marginTop = 0; + } else { + currentParagraph.marginTop = 8; + } currentParagraph.lineHeight = 1.25; var stackP = create("stack"); stackP.stack.push(currentParagraph); diff --git a/openslides/motions/static/js/motions/base.js b/openslides/motions/static/js/motions/base.js index bdcd02153..fc6ddb7b5 100644 --- a/openslides/motions/static/js/motions/base.js +++ b/openslides/motions/static/js/motions/base.js @@ -260,8 +260,9 @@ angular.module('OpenSlidesApp.motions', [ var data = diffService.extractRangeByLineNumbers(html, line_from, line_to); - html = data.outerContextStart + data.innerContextStart + data.html + - data.innerContextEnd + data.outerContextEnd; + // Add "merge-before"-css-class if the first line begins in the middle of a paragraph. Used for PDF. + html = diffService.addCSSClassToFirstTag(data.outerContextStart + data.innerContextStart, "merge-before") + + data.html + data.innerContextEnd + data.outerContextEnd; html = lineNumberingService.insertLineNumbers(html, lineLength, highlight, null, line_from); return html; @@ -280,9 +281,9 @@ angular.module('OpenSlidesApp.motions', [ var data = diffService.extractRangeByLineNumbers(html, maxLine, null); if (data.html !== '') { - html = data.outerContextStart + data.innerContextStart + - data.html + - data.innerContextEnd + data.outerContextEnd; + // Add "merge-before"-css-class if the first line begins in the middle of a paragraph. Used for PDF. + html = diffService.addCSSClassToFirstTag(data.outerContextStart + data.innerContextStart, "merge-before") + + data.html + data.innerContextEnd + data.outerContextEnd; html = lineNumberingService.insertLineNumbers(html, lineLength, highlight, null, maxLine); } else { // Prevents empty lines at the end of the motion @@ -763,6 +764,12 @@ angular.module('OpenSlidesApp.motions', [ diff = lineNumberingService.highlightLine(diff, highlight); } + var origBeginning = data.outerContextStart + data.innerContextStart; + if (diff.toLowerCase().indexOf(origBeginning.toLowerCase()) === 0) { + // Add "merge-before"-css-class if the first line begins in the middle of a paragraph. Used for PDF. + diff = diffService.addCSSClassToFirstTag(origBeginning, "merge-before") + diff.substring(origBeginning.length); + } + return diff; }, getType: function(original_full_html) { diff --git a/openslides/motions/static/js/motions/diff.js b/openslides/motions/static/js/motions/diff.js index 37a6025fa..7cd5cd6a9 100644 --- a/openslides/motions/static/js/motions/diff.js +++ b/openslides/motions/static/js/motions/diff.js @@ -983,6 +983,18 @@ angular.module('OpenSlidesApp.motions.diff', ['OpenSlidesApp.motions.lineNumberi return this._serializeDom(mergedFragment); }; + this.addCSSClassToFirstTag = function (html, className) { + return html.replace(/<[a-z][^>]*>/i, function (match) { + if (match.match(/class=["'][a-z0-9 _-]*["']/i)) { + return match.replace(/class=["']([a-z0-9 _-]*)["']/i, function (match2, previousClasses) { + return "class=\"" + previousClasses + " " + className + "\""; + }); + } else { + return match.substring(0, match.length - 1) + " class=\"" + className + "\">"; + } + }); + }; + /** * This function calculates the diff between two strings and tries to fix problems with the resulting HTML. * If lineLength and firstLineNumber is given, line numbers will be returned es well diff --git a/tests/karma/motions/diff.service.test.js b/tests/karma/motions/diff.service.test.js index b73e7270b..ad1052f09 100644 --- a/tests/karma/motions/diff.service.test.js +++ b/tests/karma/motions/diff.service.test.js @@ -482,4 +482,18 @@ describe('linenumbering', function () { ); }); }); + + describe('addCSSClassToFirstTag function', function () { + it('works with plain tags', function () { + var strIn = "
  1. ", + inserted = diffService.addCSSClassToFirstTag(strIn, "newClass"); + expect(inserted).toBe("
    1. ") + }); + + it('works with tags already having classes', function () { + var strIn = "
      1. ", + inserted = diffService.addCSSClassToFirstTag(strIn, "newClass"); + expect(inserted).toBe("
        1. ") + }); + }) });