Merge pull request #3132 from CatoTH/pdf-inline-diff-remove-extra-spaces

Remove erroneous margin around inline diff paragraphs in PDF
This commit is contained in:
Norman Jäckel 2017-03-23 00:27:27 +01:00 committed by GitHub
commit cd105f2271
4 changed files with 46 additions and 7 deletions

View File

@ -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);
@ -679,7 +681,11 @@ angular.module('OpenSlidesApp.core.pdf', [])
case "p":
var pObjectToPush; //determine what to push later
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);

View File

@ -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) {

View File

@ -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

View File

@ -482,4 +482,18 @@ describe('linenumbering', function () {
);
});
});
describe('addCSSClassToFirstTag function', function () {
it('works with plain tags', function () {
var strIn = "<ol start='2'><li>",
inserted = diffService.addCSSClassToFirstTag(strIn, "newClass");
expect(inserted).toBe("<ol start='2' class=\"newClass\"><li>")
});
it('works with tags already having classes', function () {
var strIn = "<ol start='2' class='my-old-class'><li>",
inserted = diffService.addCSSClassToFirstTag(strIn, "newClass");
expect(inserted).toBe("<ol start='2' class=\"my-old-class newClass\"><li>")
});
})
});