html-to-pdfmake
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -136,2 +136,5 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.htmlToPdfmake = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
if (text) { | ||
// if 'text' is just blank and parentNodeName is a TABLE/THEAD/TBODY/TR, then ignore it | ||
if (/^\s+$/.test(text) && ['table','thead','tbody','tr'].indexOf(parentNodeName) > -1) return ret; | ||
ret = {'text': text}; | ||
@@ -175,9 +178,13 @@ if (parentNodeName) { | ||
// check children | ||
[].forEach.call(element.childNodes, function(child) { | ||
child = parseElement(child, element); | ||
if (child) { | ||
if (Array.isArray(child) && child.length === 1) child=child[0]; | ||
ret.push(child); | ||
} | ||
}); | ||
// if it's a table cell (TH/TD) with an empty content, we need to count it | ||
if (element.childNodes.length === 0 && (nodeName==="th" || nodeName ==="td")) ret.push({text:''}); | ||
else { | ||
[].forEach.call(element.childNodes, function(child) { | ||
child = parseElement(child, element); | ||
if (child) { | ||
if (Array.isArray(child) && child.length === 1) child=child[0]; | ||
ret.push(child); | ||
} | ||
}); | ||
} | ||
@@ -229,3 +236,3 @@ if (ret.length===0) ret=""; | ||
for (var i=0; i<cell.colSpan-1; i++) { | ||
r.stack.splice(index+1, 0, {text:'', style: ['html-td', 'html-tr']}) | ||
r.stack.splice(index+1, 0, "") | ||
} | ||
@@ -250,2 +257,8 @@ } | ||
td.push(r); | ||
// insert empty cells due to colspan | ||
if (r.colSpan > 1) { | ||
for (var i=0; i<r.colSpan-1; i++) { | ||
td.push(""); | ||
} | ||
} | ||
} | ||
@@ -391,3 +404,3 @@ }); | ||
case "margin": { | ||
value = value.replace(/(\d+)([^\d]+)/g,"$1 ").trim().split(' '); | ||
value = value.replace(/(\d+)(\.\d+)?([^\d]+)/g,"$1$2 ").trim().split(' '); | ||
// pdfMake uses a different order than CSS | ||
@@ -428,3 +441,3 @@ if (value.length===1) value=+value[0]; // single value | ||
if (value) { | ||
value = value.replace(/(\d+)([^\d]+)/g,"$1 ").trim(); | ||
value = value.replace(/(\d+)(\.\d+)?([^\d]+)/g,"$1$2 ").trim(); | ||
if (!isNaN(value)) value=+value; // turn it into a number | ||
@@ -431,0 +444,0 @@ ret.push({key:key, value:value}); |
@@ -155,3 +155,2 @@ var pdfMake = require("pdfmake/build/pdfmake"); | ||
var docDefinition = { | ||
@@ -158,0 +157,0 @@ content: [ |
29
index.js
@@ -135,2 +135,5 @@ // source: https://github.com/OpenSlides/OpenSlides/blob/f4f8b8422f9b3fbab58e35ac3f8f870d35813b7d/client/src/app/core/ui-services/html-to-pdf.service.ts | ||
if (text) { | ||
// if 'text' is just blank and parentNodeName is a TABLE/THEAD/TBODY/TR, then ignore it | ||
if (/^\s+$/.test(text) && ['table','thead','tbody','tr'].indexOf(parentNodeName) > -1) return ret; | ||
ret = {'text': text}; | ||
@@ -174,9 +177,13 @@ if (parentNodeName) { | ||
// check children | ||
[].forEach.call(element.childNodes, function(child) { | ||
child = parseElement(child, element); | ||
if (child) { | ||
if (Array.isArray(child) && child.length === 1) child=child[0]; | ||
ret.push(child); | ||
} | ||
}); | ||
// if it's a table cell (TH/TD) with an empty content, we need to count it | ||
if (element.childNodes.length === 0 && (nodeName==="th" || nodeName ==="td")) ret.push({text:''}); | ||
else { | ||
[].forEach.call(element.childNodes, function(child) { | ||
child = parseElement(child, element); | ||
if (child) { | ||
if (Array.isArray(child) && child.length === 1) child=child[0]; | ||
ret.push(child); | ||
} | ||
}); | ||
} | ||
@@ -228,3 +235,3 @@ if (ret.length===0) ret=""; | ||
for (var i=0; i<cell.colSpan-1; i++) { | ||
r.stack.splice(index+1, 0, {text:'', style: ['html-td', 'html-tr']}) | ||
r.stack.splice(index+1, 0, "") | ||
} | ||
@@ -249,2 +256,8 @@ } | ||
td.push(r); | ||
// insert empty cells due to colspan | ||
if (r.colSpan > 1) { | ||
for (var i=0; i<r.colSpan-1; i++) { | ||
td.push(""); | ||
} | ||
} | ||
} | ||
@@ -251,0 +264,0 @@ }); |
{ | ||
"name": "html-to-pdfmake", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Convert HTML code to PDFMake", | ||
@@ -9,3 +9,4 @@ "main": "index.js", | ||
"test": "node test/unit.js", | ||
"browser": "browserify index.js --standalone htmlToPdfmake > docs/browser.js" | ||
"browser": "browserify index.js --standalone htmlToPdfmake > docs/browser.js", | ||
"build": "npm run test && node example.js && npm run browser" | ||
}, | ||
@@ -12,0 +13,0 @@ "repository": { |
@@ -371,28 +371,34 @@ var htmlToPdfMake = require('../index.js'); | ||
Array.isArray(ret.table.body) && | ||
ret.table.body.length === 6 && | ||
ret.table.body.length === 6, "base"); | ||
t.check( | ||
ret.table.body[1][0].text === "Cell A1" && | ||
ret.table.body[1][0].style[0] === 'html-td' && | ||
ret.table.body[1][0].style[1] === 'html-tr' && | ||
ret.table.body[1][0].style[1] === 'html-tr', "row 1"); | ||
t.check( | ||
ret.table.body[1][1].text === "Cell B1 & B2" && | ||
ret.table.body[1][2].text === "Cell C1" && | ||
ret.table.body[1][3].text === "Cell D1 & D2" && | ||
ret.table.body[1][3].text === "Cell D1 & D2", "row 2"); | ||
t.check( | ||
ret.table.body[2][0].text === "Cell A2" && | ||
ret.table.body[2][1].text === "" && | ||
ret.table.body[2][2].text === "Cell C2" && | ||
ret.table.body[2][3].text === "" && | ||
ret.table.body[2][3].text === "", "row 3"); | ||
t.check( | ||
ret.table.body[3][0].text === "Cell A3" && | ||
ret.table.body[3][1].text === "Cell B3 & C3" && | ||
ret.table.body[3][2].text === "" && | ||
ret.table.body[3][3].text === "Cell D3" && | ||
ret.table.body[3][2] === "" && | ||
ret.table.body[3][3].text === "Cell D3", "row 4"); | ||
t.check( | ||
ret.table.body[4][0].text === "Cell A4 & A5 & B4 & B5 & C4 & C5" && | ||
ret.table.body[4][1].text === "" && | ||
ret.table.body[4][2].text === "" && | ||
ret.table.body[4][3].text === "Cell D4" && | ||
ret.table.body[4][1] === "" && | ||
ret.table.body[4][2] === "" && | ||
ret.table.body[4][3].text === "Cell D4", "row 5"); | ||
t.check( | ||
ret.table.body[5][0].text === "" && | ||
ret.table.body[5][1].text === "" && | ||
ret.table.body[5][2].text === "" && | ||
ret.table.body[5][3].text === "Cell D5" && | ||
ret.table.body[5][1] === "" && | ||
ret.table.body[5][2] === "" && | ||
ret.table.body[5][3].text === "Cell D5", "row 6"); | ||
t.check( | ||
Array.isArray(ret.style) && | ||
ret.style[0] === 'html-table', | ||
"table (rowspan/colspan)"); | ||
ret.style[0] === 'html-table', "table style"); | ||
@@ -402,2 +408,48 @@ t.finish(); | ||
test("table (colspan + empty cell)", function(t) { | ||
var html = `<table> | ||
<thead> | ||
<tr> | ||
<th colspan="2">header</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>Cell A1</td> | ||
<td>Cell A2</td> | ||
</tr> | ||
<tr> | ||
<td>Cell B1</td> | ||
<td></td> | ||
</tr> | ||
</tbody> | ||
</table>`; | ||
var ret = htmlToPdfMake(html, window); | ||
t.check(Array.isArray(ret) && ret.length===1, "return is OK"); | ||
ret = ret[0]; | ||
t.check( | ||
ret.table && | ||
Array.isArray(ret.table.body) && | ||
ret.table.body.length === 3, "base"); | ||
t.check( | ||
ret.table.body[0].length === 2 && | ||
ret.table.body[0][0].text === "header" && | ||
ret.table.body[0][0].style[0] === 'html-th' && | ||
ret.table.body[0][0].style[1] === 'html-tr', "row 1"); | ||
t.check( | ||
ret.table.body[1].length === 2 && | ||
ret.table.body[1][0].text === "Cell A1" && | ||
ret.table.body[1][1].text === "Cell A2", "row 2"); | ||
t.check( | ||
ret.table.body[2].length === 2 && | ||
ret.table.body[2][0].text === "Cell B1" && | ||
ret.table.body[2][1].text === "", "row 3"); | ||
t.check( | ||
Array.isArray(ret.style) && | ||
ret.style[0] === 'html-table', "table style"); | ||
t.finish(); | ||
}) | ||
test("img",function(t) { | ||
@@ -404,0 +456,0 @@ var ret = htmlToPdfMake('<img width="10" style="height:10px" src="data:image/jpeg;base64,...encodedContent...">', window); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113310
1603