Skip to content

Commit dc66be5

Browse files
committed
v2.5.23
improve color support for borders
1 parent 37f0cbd commit dc66be5

File tree

7 files changed

+27
-41
lines changed

7 files changed

+27
-41
lines changed

browser.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/browser-2.5.22.js renamed to docs/browser-2.5.23.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ <h1>HTML to PDFMake convertor</h1>
116116
<div id="pdf_ie" style="display:none;padding:3em">The PDF file is sent to you for download. Use a modern browser (like Chrome or Firefox) to display the PDF in this page.</div>
117117
</div>
118118
</div>
119-
<script src="browser-2.5.22.js"></script>
119+
<script src="browser-2.5.23.js"></script>
120120
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js"></script>
121121
<script src="https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.js"></script>
122122
<script>

example.pdf

0 Bytes
Binary file not shown.

index.js

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ function htmlToPdfMake(htmlText, options) {
945945
// otherwise we could have 'border-color' or 'border-width'
946946
else if (b.key === 'border-color') {
947947
properties = _this.topRightBottomLeftToObject(b.value);
948-
borderColor = [ properties.left, properties.top, properties.right, properties.bottom ];
948+
borderColor = [ _this.parseColor(properties.left).color, _this.parseColor(properties.top).color, _this.parseColor(properties.right).color, _this.parseColor(properties.bottom).color ];
949949
}
950950
else if (b.key === 'border-width') {
951951
properties = _this.topRightBottomLeftToObject(b.value);
@@ -975,41 +975,26 @@ function htmlToPdfMake(htmlText, options) {
975975
//
976976
// to an object that gives {top, right, bottom, left}
977977
this.topRightBottomLeftToObject = function(props) {
978-
var splitProps = String(props).split(' ');
979-
switch (splitProps.length) {
980-
case 4: {
981-
return {
982-
top:splitProps[0],
983-
right:splitProps[1],
984-
bottom:splitProps[2],
985-
left:splitProps[3]
986-
}
987-
}
988-
case 3: {
989-
return {
990-
top:splitProps[0],
991-
right:splitProps[1],
992-
left:splitProps[1],
993-
bottom:splitProps[2],
994-
}
995-
}
996-
case 2: {
997-
return {
998-
top:splitProps[0],
999-
bottom:splitProps[0],
1000-
right:splitProps[1],
1001-
left:splitProps[1]
1002-
}
1003-
}
1004-
case 1: {
1005-
return {
1006-
top:splitProps[0],
1007-
right:splitProps[0],
1008-
bottom:splitProps[0],
1009-
left:splitProps[0]
1010-
}
1011-
}
978+
// regexp to capture the colors (hex, rgb, rgba, hsl, hsla, color name)
979+
var colorRegex = /#[0-9a-fA-F]{3,6}|\b(?:rgba?|hsla?)\([^)]*\)|\b[a-zA-Z]+\b/g;
980+
var colors = props.match(colorRegex) || [];
981+
982+
var top=colors[0], right=colors[1], bottom=colors[2], left=colors[3];
983+
984+
switch (colors.length) {
985+
case 1:
986+
right = bottom = left = top;
987+
break;
988+
case 2:
989+
bottom = top;
990+
left = right;
991+
break;
992+
case 3:
993+
left = right;
994+
break;
1012995
}
996+
997+
return { top:top, right:right, bottom:bottom, left:left };
1013998
}
1014999

10151000
// input: h in [0,360] and s,v in [0,1] - output: "rgb(0–255,0–255,0–255)""

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-to-pdfmake",
3-
"version": "2.5.22",
3+
"version": "2.5.23",
44
"description": "Convert HTML code to PDFMake",
55
"main": "index.js",
66
"scripts": {

test/unit.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,19 +1250,20 @@ test("unit tests", function(t) {
12501250
});
12511251

12521252
t.test("borders",function(t) {
1253-
var html = `<table><tr><td style="border:1px solid red">border:1px solid red</td><td style="border-bottom:1px solid blue">border-bottom:1px solid blue</td><td style="border-top-color:green">border-top-color:green</td><td style="border-right-width:0px">border-right-width:0px</td></tr></table>`;
1253+
var html = `<table><tr><td style="border:1px solid red">border:1px solid red</td><td style="border-bottom:1px solid blue">border-bottom:1px solid blue</td><td style="border-top-color:green">border-top-color:green</td><td style="border-right-width:0px">border-right-width:0px</td><td style="border-color:rgb(255, 0, 0) blue green">border-color:rgb(255, 0, 0) blue green</td></tr></table>`;
12541254
var ret = htmlToPdfMake(html, {
12551255
window:window
12561256
});
12571257
if (debug) console.log(JSON.stringify(ret));
12581258
t.check(Array.isArray(ret) && ret.length===1, "return is OK");
12591259
ret = ret[0];
1260-
t.check(Array.isArray(ret.table.body) && ret.table.body[0].length === 4, "table array OK");
1260+
t.check(Array.isArray(ret.table.body) && ret.table.body[0].length === 5, "table array OK");
12611261
ret = ret.table.body[0];
12621262
t.check(ret[0].text === "border:1px solid red" && JSON.stringify(ret[0].border) === '[true,true,true,true]' && JSON.stringify(ret[0].borderColor) === '["red","red","red","red"]', "border:1px solid red");
12631263
t.check(ret[1].text === "border-bottom:1px solid blue" && JSON.stringify(ret[1].border) === '[true,true,true,true]' && JSON.stringify(ret[1].borderColor) === '["#000000","#000000","#000000","blue"]', "border-bottom:1px solid blue");
12641264
t.check(ret[2].text === "border-top-color:green" && JSON.stringify(ret[2].borderColor) === '["#000000","green","#000000","#000000"]', "border-top-color:green");
12651265
t.check(ret[3].text === "border-right-width:0px" && JSON.stringify(ret[3].border) === '[true,true,false,true]', "border-right-width:0px");
1266+
t.check(ret[4].text === "border-color:rgb(255, 0, 0) blue green" && JSON.stringify(ret[4].borderColor) === '["blue","#ff0000","blue","green"]', "border-color:rgb(255, 0, 0) blue green");
12661267

12671268
t.finish();
12681269
});

0 commit comments

Comments
 (0)