diff --git a/src/basic/Point.js b/src/basic/Point.js index 66b87913..ffd6e85b 100644 --- a/src/basic/Point.js +++ b/src/basic/Point.js @@ -142,19 +142,20 @@ var Point = Base.extend(/** @lends Point# */{ if (this.__read) this.__read = arg0 === null ? 1 : 0; } else { - if (Array.isArray(arg0)) { - this.x = arg0[0]; - this.y = arg0.length > 1 ? arg0[1] : arg0[0]; - } else if (arg0.x != null) { - this.x = arg0.x; - this.y = arg0.y; - } else if (arg0.width != null) { - this.x = arg0.width; - this.y = arg0.height; - } else if (arg0.angle != null) { - this.x = arg0.length; + var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0; + if (Array.isArray(obj)) { + this.x = obj[0]; + this.y = obj.length > 1 ? obj[1] : obj[0]; + } else if ('x' in obj) { + this.x = obj.x; + this.y = obj.y; + } else if ('width' in obj) { + this.x = obj.width; + this.y = obj.height; + } else if ('angle' in obj) { + this.x = obj.length; this.y = 0; - this.setAngle(arg0.angle); + this.setAngle(obj.angle); } else { this.x = this.y = 0; if (this.__read) diff --git a/src/basic/Size.js b/src/basic/Size.js index da1a3781..65786c1b 100644 --- a/src/basic/Size.js +++ b/src/basic/Size.js @@ -105,15 +105,16 @@ var Size = Base.extend(/** @lends Size# */{ if (this.__read) this.__read = arg0 === null ? 1 : 0; } else { - if (Array.isArray(arg0)) { - this.width = arg0[0]; - this.height = arg0.length > 1 ? arg0[1] : arg0[0]; - } else if (arg0.width != null) { - this.width = arg0.width; - this.height = arg0.height; - } else if (arg0.x != null) { - this.width = arg0.x; - this.height = arg0.y; + var obj = type === 'string' ? arg0.split(/[\s,]+/) || [] : arg0; + if (Array.isArray(obj)) { + this.width = obj[0]; + this.height = obj.length > 1 ? obj[1] : obj[0]; + } else if ('width' in obj) { + this.width = obj.width; + this.height = obj.height; + } else if ('x' in obj) { + this.width = obj.x; + this.height = obj.y; } else { this.width = this.height = 0; if (this.__read) diff --git a/test/tests/Point.js b/test/tests/Point.js index 71a4e94d..9206648d 100644 --- a/test/tests/Point.js +++ b/test/tests/Point.js @@ -31,18 +31,21 @@ test('new Point({x: 10, y: 20})', function() { }); test('new Point(new Size(10, 20))', function() { - var point = new Point(new Size(10, 20)); - equals(point, new Point(10, 20)); + equals(new Point(new Size(10, 20)), new Point(10, 20)); }); test('new Point({ width: 10, height: 20})', function() { - var point = new Point({width: 10, height: 20}); - equals(point, new Point(10, 20)); + equals(new Point({width: 10, height: 20}), new Point(10, 20)); }); test('new Point({ angle: 45, length: 20})', function() { - var point = new Point({ angle: 40, length: 20 }); - equals(point, new Point(15.32089, 12.85575)); + equals(new Point({ angle: 40, length: 20 }), new Point(15.32089, 12.85575)); +}); + +test('new Point("10, 20")', function() { + equals(new Point('10, 20'), new Point(10, 20)); + equals(new Point('10,20'), new Point(10, 20)); + equals(new Point('10 20'), new Point(10, 20)); }); test('normalize(length)', function() { diff --git a/test/tests/Size.js b/test/tests/Size.js index bb953a7d..ee5871ff 100644 --- a/test/tests/Size.js +++ b/test/tests/Size.js @@ -36,3 +36,9 @@ test('new Size({ x: 10, y: 20})', function() { var size = new Size({x: 10, y: 20}); equals(size.toString(), '{ width: 10, height: 20 }'); }); + +test('new Size("10, 20")', function() { + equals(new Size('10, 20'), new Size(10, 20)); + equals(new Size('10,20'), new Size(10, 20)); + equals(new Size('10 20'), new Size(10, 20)); +});