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

Fix bug stringifying empty path. #35

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ export const stringifyPathData = ({
precision,
disableSpaceAfterFlags,
}) => {
if (pathData.length === 0) {
return '';
}
if (pathData.length === 1) {
const { command, args } = pathData[0];
return (
Expand Down
20 changes: 13 additions & 7 deletions lib/path.test.js → test/lib/path.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { parsePathData, stringifyPathData } from './path.js';

/**
* @typedef {import('../lib/types.js').PathDataItem} PathDataItem
*/
import { parsePathData, stringifyPathData } from '../../lib/path.js';

describe('parse path data', () => {
it('should allow spaces between commands', () => {
Expand Down Expand Up @@ -155,7 +151,7 @@ describe('stringify path data', () => {
});
it('should configure precision', () => {
/**
* @type {PathDataItem[]}
* @type {import('../../lib/types.js').PathDataItem[]}
*/
const pathData = [
{ command: 'M', args: [0, -1.9876] },
Expand All @@ -178,7 +174,7 @@ describe('stringify path data', () => {
});
it('allows to avoid spaces after arc flags', () => {
/**
* @type {PathDataItem[]}
* @type {import('../../lib/types.js').PathDataItem[]}
*/
const pathData = [
{ command: 'M', args: [0, 0] },
Expand All @@ -199,4 +195,14 @@ describe('stringify path data', () => {
}),
).toBe('M0 0A50 50 10 10.2 20a50 50 10 10.2 20 50 50 10 10.2 20');
});

it('handles empty paths', () => {
/** @type {import('../../lib/types.js').PathDataItem[]} */
const pathData = [];
expect(
stringifyPathData({
pathData,
}),
).toBe('');
});
});
21 changes: 21 additions & 0 deletions test/plugins/combinePaths.28.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Make sure empty paths don't cause trouble.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<g style="font-size:16px;">
<path d="M1 3h10" stroke="black"/>
<path d="" stroke="black"/>
<path d=" " stroke="black"/>
<path d="M1 5h10" stroke="black"/>
</g>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<g style="font-size:16px;">
<path d="M1 3h10M1 5h10" stroke="black"/>
</g>
</svg>

14 changes: 14 additions & 0 deletions test/plugins/minifyPathData.48.svg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Make sure empty paths don't cause trouble.

===

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="" stroke="black"/>
</svg>

@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">
<path d="" stroke="black"/>
</svg>