Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept newlines in Base64 encoded data urls #268

Merged
merged 7 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/nodes/image.ts
Original file line number Diff line number Diff line change
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace ((?:(.|\s)*)) by ((?:.|\s)*), should do the same and has less groups.

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 || /\n/g, '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work. If you want to replace \s or \n, you need to put them in the same regex. But \n is part of \s, so this change is not necessary.

data = atob(data)
} else {
data = decodeURIComponent(data)
Expand Down
1 change: 1 addition & 0 deletions test/common/tests.js
Original file line number Diff line number Diff line change
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
10 changes: 10 additions & 0 deletions test/specs/image-data-urls-base64-spaces/spec.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading