diff --git a/toolkit/content/Geometry.jsm b/toolkit/content/Geometry.jsm index 0bac5e7a2d2..1c15ff7060a 100644 --- a/toolkit/content/Geometry.jsm +++ b/toolkit/content/Geometry.jsm @@ -343,5 +343,14 @@ Rect.prototype = { this.right = f.call(this, this.right); this.bottom = f.call(this, this.bottom); return this; + }, + + /** Ensure this rectangle is inside the other, if possible. Preserves w, h. */ + translateInside: function translateInside(other) { + let offsetX = (this.left < other.left ? other.left - this.left : + (this.right > other.right ? this.right - other.right : 0)); + let offsetY = (this.top < other.top ? other.top - this.top : + (this.bottom > other.bottom ? this.bottom - other.bottom : 0)); + return this.translate(offsetX, offsetY); } };