Skip to content

Commit

Permalink
Accept newlines in Base64 encoded data urls (#268)
Browse files Browse the repository at this point in the history
accept newlines in Base64 encoded data urls

Co-authored-by: Anna-Lena Lumpp <anna-lena.lumpp@yworks.com>
Co-authored-by: Lukas Holländer <lukas.hollaender@yworks.com>
  • Loading branch information
3 people committed Aug 10, 2023
1 parent c24a4e4 commit 0598244
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/nodes/image.ts
Expand Up @@ -10,8 +10,7 @@ import { Matrix } from 'jspdf'
import { Viewport } from '../context/viewport'

// groups: 1: mime-type (+ charset), 2: mime-type (w/o charset), 3: charset, 4: base64?, 5: body
export const dataUriRegex = /^\s*data:(([^/,;]+\/[^/,;]+)(?:;([^,;=]+=[^,;=]+))?)?(?:;(base64))?,(.*\s*)$/i

export const dataUriRegex = /^\s*data:(([^/,;]+\/[^/,;]+)(?:;([^,;=]+=[^,;=]+))?)?(?:;(base64))?,((?:.|\s)*)$/i
export class ImageNode extends GraphicsNode {
private readonly imageLoadingPromise: Promise<{ data: string; format: string }> | null = null
private readonly imageUrl: string | null
Expand Down Expand Up @@ -87,7 +86,7 @@ export class ImageNode extends GraphicsNode {
} catch (e) {
typeof console === 'object' &&
console.warn &&
console.warn(`Could not load image ${this.imageUrl}.\n${e}`)
console.warn(`Could not load image ${this.imageUrl}. \n${e}`)
}
}
}
Expand Down Expand Up @@ -118,7 +117,9 @@ export class ImageNode extends GraphicsNode {
format = mimeTypeParts[1]

data = match[5]

if (match[4] === 'base64') {
data = data.replace(/\s/g, '')
data = atob(data)
} else {
data = decodeURIComponent(data)
Expand Down
1 change: 1 addition & 0 deletions test/common/tests.js
Expand Up @@ -31,6 +31,7 @@ window.tests = [
'gradient-units',
'gradients-and-patterns-mixed',
'hidden-clippath',
'image-data-urls-base64-spaces',
'image-svg-urls',
'line-default-coordinates',
'markers',
Expand Down
Binary file not shown.
10 changes: 10 additions & 0 deletions test/specs/image-data-urls-base64-spaces/spec.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0598244

Please sign in to comment.