Skip to content

Commit 0598244

Browse files
AnnaLena2704Anna-Lena LumppHackbrettXXX
authored
Accept newlines in Base64 encoded data urls (#268)
accept newlines in Base64 encoded data urls Co-authored-by: Anna-Lena Lumpp <[email protected]> Co-authored-by: Lukas Holländer <[email protected]>
1 parent c24a4e4 commit 0598244

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

src/nodes/image.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { Matrix } from 'jspdf'
1010
import { Viewport } from '../context/viewport'
1111

1212
// groups: 1: mime-type (+ charset), 2: mime-type (w/o charset), 3: charset, 4: base64?, 5: body
13-
export const dataUriRegex = /^\s*data:(([^/,;]+\/[^/,;]+)(?:;([^,;=]+=[^,;=]+))?)?(?:;(base64))?,(.*\s*)$/i
14-
13+
export const dataUriRegex = /^\s*data:(([^/,;]+\/[^/,;]+)(?:;([^,;=]+=[^,;=]+))?)?(?:;(base64))?,((?:.|\s)*)$/i
1514
export class ImageNode extends GraphicsNode {
1615
private readonly imageLoadingPromise: Promise<{ data: string; format: string }> | null = null
1716
private readonly imageUrl: string | null
@@ -87,7 +86,7 @@ export class ImageNode extends GraphicsNode {
8786
} catch (e) {
8887
typeof console === 'object' &&
8988
console.warn &&
90-
console.warn(`Could not load image ${this.imageUrl}.\n${e}`)
89+
console.warn(`Could not load image ${this.imageUrl}. \n${e}`)
9190
}
9291
}
9392
}
@@ -118,7 +117,9 @@ export class ImageNode extends GraphicsNode {
118117
format = mimeTypeParts[1]
119118

120119
data = match[5]
120+
121121
if (match[4] === 'base64') {
122+
data = data.replace(/\s/g, '')
122123
data = atob(data)
123124
} else {
124125
data = decodeURIComponent(data)

test/common/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ window.tests = [
3131
'gradient-units',
3232
'gradients-and-patterns-mixed',
3333
'hidden-clippath',
34+
'image-data-urls-base64-spaces',
3435
'image-svg-urls',
3536
'line-default-coordinates',
3637
'markers',
3.61 KB
Binary file not shown.
Lines changed: 10 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)