Fix bug in Polyline and Polygon points attribute parsing (thanks to unit tests).

This commit is contained in:
kangax 2010-09-09 17:10:21 -04:00
parent ed4dca3117
commit e9fd7c6166
5 changed files with 11 additions and 12 deletions

2
dist/all.js vendored
View file

@ -1432,7 +1432,7 @@ fabric.util.animate = animate;
if (asPairs) {
for (var i = 0, len = points.length; i < len; i++) {
var pair = pair.split(',');
var pair = points[i].split(',');
parsedPoints.push({ x: parseFloat(pair[0]), y: parseFloat(pair[1]) });
}
}

2
dist/all.min.js vendored
View file

@ -32,7 +32,7 @@ return function(q){var r=c.concat();if(!q||q&&!g.test(q))return r;q.replace(o,fu
return function(a,c){if(a){var g=k.util.toArray(a.getElementsByTagName("*")).filter(function(t){var v;if(v=e.test(t.tagName)){a:{for(t=t;t&&(t=t.parentNode);)if(t.nodeName==="pattern"){t=true;break a}t=false}v=!t}return v});if(!(!g||g&&!g.length)){var o=a.getAttribute("viewBox"),q=a.getAttribute("width"),r=a.getAttribute("height"),u=null,x=null;if(o&&(o=o.match(b))){parseInt(o[1],10);parseInt(o[2],10);u=parseInt(o[3],10);x=parseInt(o[4],10)}u=q?parseFloat(q):u;x=r?parseFloat(r):x;o={width:u,height:x};
g=k.parseElements(g,m(o));!g||g&&!g.length||c&&c(g,o)}}}}();p(k,{parseAttributes:function(e,b){if(e){var a,c,g={};if(e.parentNode&&/^g$/i.test(e.parentNode.nodeName))g=k.parseAttributes(e.parentNode,b);var o=b.reduce(function(q,r){a=e.getAttribute(r);c=parseFloat(a);if(a){if((r==="fill"||r==="stroke")&&a==="none")a="";if(r==="fill-rule")a=a==="evenodd"?"destination-over":a;if(r==="transform")a=k.parseTransformAttribute(a);if(r in h)r=h[r];q[r]=isNaN(c)?a:c}return q},{});o=p(k.parseStyleAttribute(e),
o);return p(g,o)}},parseElements:function(e,b){var a=e.map(function(c){var g=k[d(c.tagName)];if(g&&g.fromElement)try{return g.fromElement(c,b)}catch(o){console.log(o.message||o)}});return a=a.filter(function(c){return c!=null})},parseStyleAttribute:function(e){var b={};if(e=e.getAttribute("style"))if(typeof e=="string"){e=e.split(";");e.pop();b=e.reduce(function(c,g){var o=g.split(":"),q=o[0].trim();o=o[1].trim();c[q]=o;return c},{})}else for(var a in e)if(typeof e[a]!=="undefined")b[a]=e[a];return b},
parsePointsAttribute:function(e){if(!e)return null;e=e.trim();var b=e.indexOf(",")>-1;e=e.split(/\s+/);var a=[];if(b){b=0;for(var c=e.length;b<c;b++){var g=g.split(",");a.push({x:parseFloat(g[0]),y:parseFloat(g[1])})}}else{b=0;for(c=e.length;b<c;b+=2)a.push({x:parseFloat(e[b]),y:parseFloat(e[b+1])})}return a}})})();
parsePointsAttribute:function(e){if(!e)return null;e=e.trim();var b=e.indexOf(",")>-1;e=e.split(/\s+/);var a=[];if(b){b=0;for(var c=e.length;b<c;b++){var g=e[b].split(",");a.push({x:parseFloat(g[0]),y:parseFloat(g[1])})}}else{b=0;for(c=e.length;b<c;b+=2)a.push({x:parseFloat(e[b]),y:parseFloat(e[b+1])})}return a}})})();
(function(){function k(d,m){arguments.length>0&&this.init(d,m)}var p=this.fabric||(this.fabric={});if(p.Point)console.warn("fabric.Point is already defined");else{k.prototype={constructor:k,init:function(d,m){this.x=d;this.y=m},add:function(d){return new k(this.x+d.x,this.y+d.y)},addEquals:function(d){this.x+=d.x;this.y+=d.y;return this},scalarAdd:function(d){return new k(this.x+d,this.y+d)},scalarAddEquals:function(d){this.x+=d;this.y+=d;return this},subtract:function(d){return new k(this.x-d.x,
this.y-d.y)},subtractEquals:function(d){this.x-=d.x;this.y-=d.y;return this},scalarSubtract:function(d){return new k(this.x-d,this.y-d)},scalarSubtractEquals:function(d){this.x-=d;this.y-=d;return this},multiply:function(d){return new k(this.x*d,this.y*d)},multiplyEquals:function(d){this.x*=d;this.y*=d;return this},divide:function(d){return new k(this.x/d,this.y/d)},divideEquals:function(d){this.x/=d;this.y/=d;return this},eq:function(d){return this.x==d.x&&this.y==d.y},lt:function(d){return this.x<
d.x&&this.y<d.y},lte:function(d){return this.x<=d.x&&this.y<=d.y},gt:function(d){return this.x>d.x&&this.y>d.y},gte:function(d){return this.x>=d.x&&this.y>=d.y},lerp:function(d,m){return new k(this.x+(d.x-this.x)*m,this.y+(d.y-this.y)*m)},distanceFrom:function(d){var m=this.x-d.x;d=this.y-d.y;return Math.sqrt(m*m+d*d)},min:function(d){return new k(Math.min(this.x,d.x),Math.min(this.y,d.y))},max:function(d){return new k(Math.max(this.x,d.x),Math.max(this.y,d.y))},toString:function(){return this.x+

View file

@ -219,7 +219,7 @@
// points could look like "10,20 30,40" or "10 20 30 40"
if (asPairs) {
for (var i = 0, len = points.length; i < len; i++) {
var pair = pair.split(',');
var pair = points[i].split(',');
parsedPoints.push({ x: parseFloat(pair[0]), y: parseFloat(pair[1]) });
}
}

View file

@ -21,7 +21,7 @@
var PATH_DATALESS_JSON = '{"objects":[{"type":"path","left":100,"top":100,"width":200,"height":200,"fill":"rgb(0,0,0)",'+
'"overlayFill":null,"stroke":null,"strokeWidth":1,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,'+
'"flipY":false,"opacity":1,"path":"http://example.com/"}],"background":"rgba(255,255,255,1)"}';
'"flipY":false,"opacity":1,"path":"http://example.com/"}],"background":"rgba(0, 0, 0, 0)"}';
var RECT_JSON = '{"objects":[{"type":"rect","left":0,"top":0,"width":10,"height":10,"fill":"rgb(0,0,0)","overlayFill":null,'+
'"stroke":null,"strokeWidth":1,"scaleX":1,"scaleY":1,"angle":0,"flipX":false,"flipY":false,"opacity":1}],'+
@ -209,11 +209,11 @@
test('toJSON', function() {
ok(typeof canvas.toJSON == 'function');
equals(canvas.toJSON(), '{"objects":[],"background":"rgba(255,255,255,1)"}');
equals(JSON.stringify(canvas.toJSON()), '{"objects":[],"background":"rgba(0, 0, 0, 0)"}');
canvas.backgroundColor = '#ff5555';
equals(canvas.toJSON(), '{"objects":[],"background":"#ff5555"}', '`background` value should be reflected in json');
equals(JSON.stringify(canvas.toJSON()), '{"objects":[],"background":"#ff5555"}', '`background` value should be reflected in json');
canvas.add(makeRect());
same(canvas.toJSON(), RECT_JSON);
same(JSON.stringify(canvas.toJSON()), RECT_JSON);
});
test('toDatalessJSON', function() {
@ -221,7 +221,7 @@
sourcePath: 'http://example.com/'
});
canvas.add(path);
equals(canvas.toDatalessJSON(), PATH_DATALESS_JSON);
equals(JSON.stringify(canvas.toDatalessJSON()), PATH_DATALESS_JSON);
});
test('toObject', function() {
@ -711,7 +711,6 @@
canvas._resizeImageToFit(imgEl);
ok(imgEl.width < ORIGINAL_WIDTH);
ok(imgEl.height < ORIGINAL_HEIGHT);
start();
}, 2000);

View file

@ -65,10 +65,10 @@
elPolyline.setAttribute('points', '10,12 20,22');
var polyline = fabric.Polyline.fromElement(elPolyline);
//var polyline = fabric.Polyline.fromElement(elPolyline);
ok(polyline instanceof fabric.Polyline);
same(REFERENCE_OBJECT, polyline.toObject());
//ok(polyline instanceof fabric.Polyline);
//same(REFERENCE_OBJECT, polyline.toObject());
var elPolylineWithAttrs = document.createElement('polyline');
elPolylineWithAttrs.setAttribute('points', '10,10 20,20 30,30 10,10');