Skip to content

Commit d089e16

Browse files
author
jdotrjs
committed
[fixes phaserjs#3535] Fix constructed bounding box functions for RTree
The previous patch didn't take into account that the accessors passed in as the format array were dot-prefixed due to the previous eval-based construction. The only two uses of RTree that I found were in World.js: ```javascript this.tree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]); this.staticTree = new RTree(this.maxEntries, [ '.left', '.top', '.right', '.bottom' ]); ``` It's likely that this could be updated to just not pass dotted attribute names but I wasn't super comfortable that they weren't needed in this form elsewhere despite a quick search. I'd honestly say that it might be a better change if we remove the dots before merge but I'll leave that up to the discretion of the reviewers/merger. I'm not super familiar with Phaser's style but I passed lint sooo :D Buyer beware: the only testing I did was on the repo case I left in the bug and I did not verify that this is still within the twitch CSP (though it should be).
1 parent 97cd94b commit d089e16

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

src/structs/RTree.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -465,31 +465,43 @@ rbush.prototype = {
465465

466466
_initFormat: function (format)
467467
{
468-
// data format (minX, minY, maxX, maxY accessors)
468+
// format: [minX, minY, maxX, maxY accessors]
469+
// accessors will be dotted names
470+
471+
// Because we have historically used eval-based function constructor
472+
// the format accerrsors need to have their leading dots stripped to
473+
// obtain the actual accessor
474+
format = format.map(
475+
function (f)
476+
{
477+
return f.substring(1)
478+
}
479+
)
469480

470481
// Do not use string-generated Functions for CSP policies
471-
// Instead a combination of anonymous functions and grabbing
472-
// properties by string is used.
473-
var compareArr = function (accessor)
474-
{
475-
return function (a, b)
476-
{
477-
return this[a + accessor] - this[b + accessor];
478-
};
479-
};
482+
// Instead a combination of anonymous functions and grabbing properties
483+
// by string is used.
484+
// cf. https://github.com/photonstorm/phaser/issues/3441
485+
// and https://github.com/photonstorm/phaser/issues/3535
486+
487+
var mkCompareFn = function(attr) {
488+
return function(a, b) {
489+
return a[attr] - b[attr]
490+
}
491+
}
480492

481-
this.compareMinX = compareArr(format[0]);
482-
this.compareMinY = compareArr(format[1]);
493+
this.compareMinX = mkCompareFn(format[0])
494+
this.compareMinY = mkCompareFn(format[1])
483495

484-
this.toBBox = function (a)
496+
this.toBBox = function(a)
485497
{
486498
return {
487-
minX: a + format[0],
488-
minY: a + format[1],
489-
maxX: a + format[2],
490-
maxy: a + format[3]
491-
};
492-
};
499+
minX: a[format[0]],
500+
minY: a[format[1]],
501+
maxX: a[format[2]],
502+
maxY: a[format[3]],
503+
}
504+
}
493505
}
494506
};
495507

0 commit comments

Comments
 (0)