Skip to content

Commit

Permalink
Fix arc flags parse (should be more strict), close #41
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaly Puzrin committed Jan 19, 2020
1 parent 5d0dc17 commit 3b938bd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
30 changes: 28 additions & 2 deletions lib/path_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ function isCommand(code) {
return false;
}

function isArc(code) {
return (code | 0x20) === 0x61;
}

function isDigit(code) {
return (code >= 48 && code <= 57); // 0..9
}
Expand Down Expand Up @@ -64,6 +68,25 @@ function skipSpaces(state) {
}


function scanFlag(state) {
var ch = state.path.charCodeAt(state.index);

if (ch === 0x30/* 0 */) {
state.param = 0;
state.index++;
return;
}

if (ch === 0x31/* 1 */) {
state.param = 1;
state.index++;
return;
}

state.err = 'SvgPath: arc flag can be 0 or 1 only (at pos ' + state.index + ')';
}


function scanParam(state) {
var start = state.index,
index = start,
Expand Down Expand Up @@ -187,10 +210,11 @@ function finalizeSegment(state) {

function scanSegment(state) {
var max = state.max,
cmdCode, comma_found, need_params, i;
cmdCode, is_arc, comma_found, need_params, i;

state.segmentStart = state.index;
cmdCode = state.path.charCodeAt(state.index);
is_arc = isArc(cmdCode);

if (!isCommand(cmdCode)) {
state.err = 'SvgPath: bad command ' + state.path[state.index] + ' (at pos ' + state.index + ')';
Expand All @@ -214,7 +238,9 @@ function scanSegment(state) {

for (;;) {
for (i = need_params; i > 0; i--) {
scanParam(state);
if (is_arc && (i === 3 || i === 4)) scanFlag(state);
else scanParam(state);

if (state.err.length) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions test/path_parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ describe('Path parse', function () {
assert.equal(svgpath('M 0 0 r 1 1 2 2').toString(), 'M0 0r1 1 2 2');
});

it('arc flags', function () {
assert.equal(
svgpath('M 0 0 a.625.625 0 01.84-.925').toString(),
'M0 0a0.625 0.625 0 0 1 0.84-0.925'
);
});

it('errors', function () {
assert.equal(svgpath('0').err, 'SvgPath: bad command 0 (at pos 0)');
assert.equal(svgpath('U').err, 'SvgPath: bad command U (at pos 0)');
Expand All @@ -58,5 +65,6 @@ describe('Path parse', function () {
assert.equal(svgpath('M0').err, 'SvgPath: missed param (at pos 2)');
assert.equal(svgpath('M0,0,').err, 'SvgPath: missed param (at pos 5)');
assert.equal(svgpath('M0 .e3').err, 'SvgPath: invalid float exponent (at pos 4)');
assert.equal(svgpath('M0 0a2 2 2 2 2 2 2').err, 'SvgPath: arc flag can be 0 or 1 only (at pos 11)');
});
});

0 comments on commit 3b938bd

Please sign in to comment.