Skip to content

Commit 63380ed

Browse files
author
Viktor Kojouharov
committed
Preserve the meaning of BR elements
Since BR elements are line breaks, treat them as such and leave a unicode line separator token in the array of text. When splitting the fragments into lines, treat the line separator as an explicit break.
1 parent 38bc719 commit 63380ed

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

jspdf.plugin.from_html.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@
8787
trailingSpace = true;
8888
i = 0;
8989
while (i !== l) {
90-
fragment = array[i].replace(/\s+/g, " ");
91-
if (trailingSpace) {
92-
fragment = fragment.trimLeft();
93-
}
94-
if (fragment) {
95-
trailingSpace = r.test(fragment);
90+
// Leave the line breaks intact
91+
if (array[i] != "\u2028") {
92+
fragment = array[i].replace(/\s+/g, " ");
93+
if (trailingSpace) {
94+
fragment = fragment.trimLeft();
95+
}
96+
if (fragment) {
97+
trailingSpace = r.test(fragment);
98+
}
99+
array[i] = fragment;
96100
}
97-
array[i] = fragment;
98101
i++;
99102
}
100103
return array;
@@ -463,6 +466,7 @@
463466
renderer.x = temp;
464467
} else if (cn.nodeName === "BR") {
465468
renderer.y += fragmentCSS["font-size"] * renderer.pdf.internal.scaleFactor;
469+
renderer.addText("\u2028", clone(fragmentCSS));
466470
} else {
467471
if (!elementHandledElsewhere(cn, renderer, elementHandlers)) {
468472
DrillForContent(cn, renderer, elementHandlers);
@@ -723,7 +727,10 @@
723727
textIndent : currentLineLength
724728
};
725729
fragmentLength = this.pdf.getStringUnitWidth(fragment, fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k;
726-
if (currentLineLength + fragmentLength > maxLineLength) {
730+
if (fragment == "\u2028") {
731+
line = [];
732+
lines.push(line);
733+
} else if (currentLineLength + fragmentLength > maxLineLength) {
727734
fragmentChopped = this.pdf.splitTextToSize(fragment, maxLineLength, fragmentSpecificMetrics);
728735
line.push([fragmentChopped.shift(), style]);
729736
while (fragmentChopped.length) {

0 commit comments

Comments
 (0)