Simplify point parsing logic

The SVG spec basically allows delimiting numbers and pairs of numbers
with either whitespace or commas, so convert all commas to whitespace,
split on whitespace and remove the `asPairs` logic
This commit is contained in:
Paul Kaplan 2014-07-14 09:59:03 -05:00
parent a1cde528dc
commit bbd1dd2d99

View file

@ -713,38 +713,19 @@
// points attribute is required and must not be empty
if (!points) return null;
points = points.trim();
var asPairs = points.indexOf(',') > -1;
// replace commas with whitespace and remove bookending whitespace
points = points.replace(/,/g, ' ').trim();
// remove possible whitespace around commas
if (asPairs) {
points = points.replace(/\s*,\s*/g, ',')
}
points = points.split(/\s+/);
var parsedPoints = [ ], i, len;
// points could look like "10,20 30,40" or "10 20 30 40"
if (asPairs) {
i = 0;
len = points.length;
for (; i < len; i++) {
var pair = points[i].split(',');
parsedPoints.push({
x: parseFloat(pair[0]),
y: parseFloat(pair[1])
});
}
}
else {
i = 0;
len = points.length;
for (; i < len; i+=2) {
parsedPoints.push({
x: parseFloat(points[i]),
y: parseFloat(points[i + 1])
});
}
i = 0;
len = points.length;
for (; i < len; i+=2) {
parsedPoints.push({
x: parseFloat(points[i]),
y: parseFloat(points[i + 1])
});
}
// odd number of points is an error