mirror of
https://github.com/svg/svgo.git
synced 2025-08-01 18:46:52 +03:00
Fix noSpaceAfterFlags support
This commit is contained in:
25
lib/path.js
25
lib/path.js
@ -228,16 +228,29 @@ const stringifyNumber = ({ number, precision }) => {
|
|||||||
|
|
||||||
// elliptical arc large-arc and sweep flags are rendered with spaces
|
// elliptical arc large-arc and sweep flags are rendered with spaces
|
||||||
// because many non-browser environments are not able to parse such paths
|
// because many non-browser environments are not able to parse such paths
|
||||||
const stringifyArgs = ({ args, precision }) => {
|
const stringifyArgs = ({
|
||||||
|
command,
|
||||||
|
args,
|
||||||
|
precision,
|
||||||
|
disableSpaceAfterFlags,
|
||||||
|
}) => {
|
||||||
let result = '';
|
let result = '';
|
||||||
let prev;
|
let prev;
|
||||||
for (let i = 0; i < args.length; i += 1) {
|
for (let i = 0; i < args.length; i += 1) {
|
||||||
const number = args[i];
|
const number = args[i];
|
||||||
const numberString = stringifyNumber({ number, precision });
|
const numberString = stringifyNumber({ number, precision });
|
||||||
// avoid space before first and negative numbers
|
if (
|
||||||
if (i === 0 || numberString.startsWith('-')) {
|
disableSpaceAfterFlags &&
|
||||||
|
(command === 'A' || command === 'a') &&
|
||||||
|
(i === 4 || i === 5)
|
||||||
|
) {
|
||||||
|
result += numberString;
|
||||||
|
} else if (i === 0 || numberString.startsWith('-')) {
|
||||||
|
// avoid space before first and negative numbers
|
||||||
result += numberString;
|
result += numberString;
|
||||||
} else if (prev.includes('.') && numberString.startsWith('.')) {
|
} else if (prev.includes('.') && numberString.startsWith('.')) {
|
||||||
|
// remove space before decimal with zero whole
|
||||||
|
// only when previous number is also decimal
|
||||||
result += numberString;
|
result += numberString;
|
||||||
} else {
|
} else {
|
||||||
result += ` ${numberString}`;
|
result += ` ${numberString}`;
|
||||||
@ -247,7 +260,7 @@ const stringifyArgs = ({ args, precision }) => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const stringifyPathData = ({ pathData, precision }) => {
|
const stringifyPathData = ({ pathData, precision, disableSpaceAfterFlags }) => {
|
||||||
// combine sequence of the same commands
|
// combine sequence of the same commands
|
||||||
let combined = [];
|
let combined = [];
|
||||||
for (let i = 0; i < pathData.length; i += 1) {
|
for (let i = 0; i < pathData.length; i += 1) {
|
||||||
@ -281,7 +294,9 @@ const stringifyPathData = ({ pathData, precision }) => {
|
|||||||
}
|
}
|
||||||
let result = '';
|
let result = '';
|
||||||
for (const { command, args } of combined) {
|
for (const { command, args } of combined) {
|
||||||
result += command + stringifyArgs({ args, precision });
|
result +=
|
||||||
|
command +
|
||||||
|
stringifyArgs({ command, args, precision, disableSpaceAfterFlags });
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
@ -63,8 +63,8 @@ describe('parse path data', () => {
|
|||||||
l 50,-25
|
l 50,-25
|
||||||
a25,25 -30 0,1 50,-25
|
a25,25 -30 0,1 50,-25
|
||||||
25,50 -30 0,1 50,-25
|
25,50 -30 0,1 50,-25
|
||||||
25,75 -30 0,1 50,-25
|
25,75 -30 01.2,-25
|
||||||
a25,100 -30 0,1 50,-25
|
a25,100 -30 0150,-25
|
||||||
l 50,-25
|
l 50,-25
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
@ -73,7 +73,7 @@ describe('parse path data', () => {
|
|||||||
{ command: 'l', args: [50, -25] },
|
{ command: 'l', args: [50, -25] },
|
||||||
{ command: 'a', args: [25, 25, -30, 0, 1, 50, -25] },
|
{ command: 'a', args: [25, 25, -30, 0, 1, 50, -25] },
|
||||||
{ command: 'a', args: [25, 50, -30, 0, 1, 50, -25] },
|
{ command: 'a', args: [25, 50, -30, 0, 1, 50, -25] },
|
||||||
{ command: 'a', args: [25, 75, -30, 0, 1, 50, -25] },
|
{ command: 'a', args: [25, 75, -30, 0, 1, 0.2, -25] },
|
||||||
{ command: 'a', args: [25, 100, -30, 0, 1, 50, -25] },
|
{ command: 'a', args: [25, 100, -30, 0, 1, 50, -25] },
|
||||||
{ command: 'l', args: [50, -25] },
|
{ command: 'l', args: [50, -25] },
|
||||||
]);
|
]);
|
||||||
@ -163,4 +163,23 @@ describe('stringify path data', () => {
|
|||||||
})
|
})
|
||||||
).to.equal('M0-2 0 3 100 200');
|
).to.equal('M0-2 0 3 100 200');
|
||||||
});
|
});
|
||||||
|
it('allows to avoid spaces after arc flags', () => {
|
||||||
|
const pathData = [
|
||||||
|
{ command: 'M', args: [0, 0] },
|
||||||
|
{ command: 'A', args: [50, 50, 10, 1, 0, 0.2, 20] },
|
||||||
|
{ command: 'a', args: [50, 50, 10, 1, 0, 0.2, 20] },
|
||||||
|
];
|
||||||
|
expect(
|
||||||
|
stringifyPathData({
|
||||||
|
pathData,
|
||||||
|
disableSpaceAfterFlags: false,
|
||||||
|
})
|
||||||
|
).to.equal('M0 0A50 50 10 1 0 .2 20a50 50 10 1 0 .2 20');
|
||||||
|
expect(
|
||||||
|
stringifyPathData({
|
||||||
|
pathData,
|
||||||
|
disableSpaceAfterFlags: true,
|
||||||
|
})
|
||||||
|
).to.equal('M0 0A50 50 10 10.2 20a50 50 10 10.2 20');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -553,6 +553,7 @@ exports.js2path = function (path, data, params) {
|
|||||||
path.attr('d').value = stringifyPathData({
|
path.attr('d').value = stringifyPathData({
|
||||||
pathData,
|
pathData,
|
||||||
precision: params.floatPrecision,
|
precision: params.floatPrecision,
|
||||||
|
disableSpaceAfterFlags: params.noSpaceAfterFlags,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user