From 02c7faa1aa484ea903f0b87435eaa11be258b834 Mon Sep 17 00:00:00 2001 From: Satish Date: Tue, 25 Aug 2020 15:30:08 +0530 Subject: [PATCH] Fix-splitTextToSize method crashes if cell value is not a string #2849 --- src/modules/cell.js | 10 +++++++--- src/modules/split_text_to_size.js | 2 +- test/specs/cell.spec.js | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/modules/cell.js b/src/modules/cell.js index c044670ad..40a783233 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -188,9 +188,13 @@ import { jsPDF } from "../jspdf.js"; var tempWidth = 0; if (!Array.isArray(text) && typeof text !== "string") { - throw new Error( - "getTextDimensions expects text-parameter to be of type String or an Array of Strings." - ); + if (typeof text === "number") { + text = String(text); + } else { + throw new Error( + "getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings." + ); + } } const maxWidth = options.maxWidth; diff --git a/src/modules/split_text_to_size.js b/src/modules/split_text_to_size.js index e87005d5d..fed4fdbec 100755 --- a/src/modules/split_text_to_size.js +++ b/src/modules/split_text_to_size.js @@ -357,7 +357,7 @@ import { jsPDF } from "../jspdf.js"; if (Array.isArray(text)) { paragraphs = text; } else { - paragraphs = text.split(/\r?\n/); + paragraphs = String(text).split(/\r?\n/); } // now we convert size (max length of line) into "font size units" diff --git a/test/specs/cell.spec.js b/test/specs/cell.spec.js index 8f7091d53..0a011a982 100644 --- a/test/specs/cell.spec.js +++ b/test/specs/cell.spec.js @@ -34,7 +34,7 @@ describe("Module: Cell", () => { doc.getTextDimensions(); }).toThrow( new Error( - "getTextDimensions expects text-parameter to be of type String or an Array of Strings." + "getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings." ) ); });