Skip to content

Commit

Permalink
Fix noSpaceAfterFlags support
Browse files Browse the repository at this point in the history
  • Loading branch information
TrySound committed Mar 4, 2021
1 parent be28d65 commit 0e02fd9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
25 changes: 20 additions & 5 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,29 @@ const stringifyNumber = ({ number, precision }) => {

// elliptical arc large-arc and sweep flags are rendered with spaces
// 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 prev;
for (let i = 0; i < args.length; i += 1) {
const number = args[i];
const numberString = stringifyNumber({ number, precision });
// avoid space before first and negative numbers
if (i === 0 || numberString.startsWith('-')) {
if (
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;
} else if (prev.includes('.') && numberString.startsWith('.')) {
// remove space before decimal with zero whole
// only when previous number is also decimal
result += numberString;
} else {
result += ` ${numberString}`;
Expand All @@ -247,7 +260,7 @@ const stringifyArgs = ({ args, precision }) => {
return result;
};

const stringifyPathData = ({ pathData, precision }) => {
const stringifyPathData = ({ pathData, precision, disableSpaceAfterFlags }) => {
// combine sequence of the same commands
let combined = [];
for (let i = 0; i < pathData.length; i += 1) {
Expand Down Expand Up @@ -281,7 +294,9 @@ const stringifyPathData = ({ pathData, precision }) => {
}
let result = '';
for (const { command, args } of combined) {
result += command + stringifyArgs({ args, precision });
result +=
command +
stringifyArgs({ command, args, precision, disableSpaceAfterFlags });
}
return result;
};
Expand Down
25 changes: 22 additions & 3 deletions lib/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ describe('parse path data', () => {
l 50,-25
a25,25 -30 0,1 50,-25
25,50 -30 0,1 50,-25
25,75 -30 0,1 50,-25
a25,100 -30 0,1 50,-25
25,75 -30 01.2,-25
a25,100 -30 0150,-25
l 50,-25
`
)
Expand All @@ -73,7 +73,7 @@ describe('parse path data', () => {
{ command: 'l', args: [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, 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: 'l', args: [50, -25] },
]);
Expand Down Expand Up @@ -163,4 +163,23 @@ describe('stringify path data', () => {
})
).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');
});
});
1 change: 1 addition & 0 deletions plugins/_path.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ exports.js2path = function (path, data, params) {
path.attr('d').value = stringifyPathData({
pathData,
precision: params.floatPrecision,
disableSpaceAfterFlags: params.noSpaceAfterFlags,
});
};

Expand Down

0 comments on commit 0e02fd9

Please sign in to comment.