Fix function body matching regex. Add fabric.util.getFunctionBody

This commit is contained in:
kangax 2013-06-01 18:08:36 +02:00
parent 46183d8a85
commit 1c1b7e4b95
7 changed files with 36 additions and 17 deletions

13
dist/all.js vendored
View file

@ -2448,6 +2448,10 @@ fabric.Collection = {
];
}
function getFunctionBody(fn) {
return String(fn).match(/function[^{]*\{([\s\S]*)\}/)[1];
}
fabric.util.removeFromArray = removeFromArray;
fabric.util.degreesToRadians = degreesToRadians;
fabric.util.radiansToDegrees = radiansToDegrees;
@ -2468,6 +2472,7 @@ fabric.Collection = {
fabric.util.createAccessors = createAccessors;
fabric.util.clipContext = clipContext;
fabric.util.multiplyTransformMatrices = multiplyTransformMatrices;
fabric.util.getFunctionBody = getFunctionBody;
})();
@ -5543,8 +5548,7 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
// callback
if (typeof this.source === 'function') {
source = String(this.source)
.match(/function\s+\w*\s*\(.*\)\s+\{([\s\S]*)\}/)[1];
source = fabric.util.getFunctionBody(this.source);
}
// <img> element
else if (typeof this.source.src === 'string') {
@ -8134,9 +8138,8 @@ fabric.PatternBrush = fabric.util.createClass(fabric.PencilBrush, /** @lends fab
},
getPatternSrcBody: function() {
return String(this.getPatternSrc)
.match(/function\s*\w*\s*(.*)\s*\{([\s\S]*)\}/)[1]
.replace('this.color', '"' + this.color + '"');
return fabric.util.getFunctionBody(this.getPatternSrc)
.replace('this.color', '"' + this.color + '"');
},
/**

12
dist/all.min.js vendored

File diff suppressed because one or more lines are too long

BIN
dist/all.min.js.gz vendored

Binary file not shown.

View file

@ -24,9 +24,8 @@ fabric.PatternBrush = fabric.util.createClass(fabric.PencilBrush, /** @lends fab
},
getPatternSrcBody: function() {
return String(this.getPatternSrc)
.match(/function\s*\w*\s*(.*)\s*\{([\s\S]*)\}/)[1]
.replace('this.color', '"' + this.color + '"');
return fabric.util.getFunctionBody(this.getPatternSrc)
.replace('this.color', '"' + this.color + '"');
},
/**

View file

@ -56,8 +56,7 @@ fabric.Pattern = fabric.util.createClass(/** @lends fabric.Pattern.prototype */
// callback
if (typeof this.source === 'function') {
source = String(this.source)
.match(/function\s+\w*\s*\(.*\)\s+\{([\s\S]*)\}/)[1];
source = fabric.util.getFunctionBody(this.source);
}
// <img> element
else if (typeof this.source.src === 'string') {

View file

@ -446,6 +446,10 @@
];
}
function getFunctionBody(fn) {
return String(fn).match(/function[^{]*\{([\s\S]*)\}/)[1];
}
fabric.util.removeFromArray = removeFromArray;
fabric.util.degreesToRadians = degreesToRadians;
fabric.util.radiansToDegrees = radiansToDegrees;
@ -466,5 +470,6 @@
fabric.util.createAccessors = createAccessors;
fabric.util.clipContext = clipContext;
fabric.util.multiplyTransformMatrices = multiplyTransformMatrices;
fabric.util.getFunctionBody = getFunctionBody;
})();

View file

@ -732,4 +732,17 @@
ok(typeof destination.ffffffffff === 'undefined');
});
})();
test('fabric.util.getFunctionBody', function() {
equal(fabric.util.getFunctionBody('function(){}'), '');
equal(fabric.util.getFunctionBody('function(){return 1 + 2}'),
'return 1 + 2');
equal(fabric.util.getFunctionBody('function () {\n return "blah" }'),
'\n return "blah" ');
equal(fabric.util.getFunctionBody('function foo (a , boo_bar, baz123 )\n{\n if (1) { alert(12345) } }'),
'\n if (1) { alert(12345) } ');
});
})();