-
Notifications
You must be signed in to change notification settings - Fork 355
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor scale via offsetHeight use precise API
offsetHeight returns an integer, making it a poor API to base the determination of element scale on. Instead, use an alternative and precise API. Only allow a reasonable amount of precision by decimal place, and for cases where the scale number is reasonably rounded do so. The offsetHeight method includes padding in returned sizes, where getComputedStyle does not. Check them against each other, and if they diverge signifcantly (>1) throw an exception. To make ember-table itself compatible with this appraoch, pass the `scale` value around the system a bit more instead of trying to derive it in many places.
- Loading branch information
Showing
9 changed files
with
184 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
export function getScale(element) { | ||
let rect = element.getBoundingClientRect(); | ||
import { getScale } from 'ember-table/-private/utils/element'; | ||
|
||
if (element.offsetHeight === rect.height || rect.height === 0) { | ||
return 1; | ||
} else { | ||
return element.offsetHeight / rect.height; | ||
} | ||
} | ||
export { getScale }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { module, test } from 'qunit'; | ||
import { getScale } from 'ember-table/-private/utils/element'; | ||
|
||
module('Unit | Private | element', function(hooks) { | ||
hooks.beforeEach(function() { | ||
/* | ||
* Use an element outside the normal test harness as that harness | ||
* uses scale() on the test container. | ||
*/ | ||
this.element = document.createElement('div'); | ||
document.body.append(this.element); | ||
}); | ||
|
||
hooks.afterEach(function() { | ||
this.element.remove(); | ||
}); | ||
|
||
test('can get the scale of a transformed element', function(assert) { | ||
let div = document.createElement('div'); | ||
div.style.height = '4px'; | ||
this.element.append(div); | ||
|
||
assert.equal(getScale(div), 1, 'scale on a simple element is correct'); | ||
|
||
div.style.transform = 'scale(0.5)'; | ||
|
||
assert.equal(getScale(div), 2, 'scale on a scaled element is correct'); | ||
|
||
div.style.height = '1.5px'; | ||
|
||
assert.equal(getScale(div), 2, 'scale on a scaled element is correct'); | ||
}); | ||
|
||
test('gets an integer when a scale is very close to its rounded integer value', function(assert) { | ||
let div = document.createElement('div'); | ||
div.style.height = '4px'; | ||
this.element.append(div); | ||
|
||
assert.equal(getScale(div), 1, 'scale on a simple element is correct'); | ||
|
||
div.style.transform = 'scale(1.000001)'; | ||
|
||
assert.equal(getScale(div), 1, 'scale on a scaled element is correct'); | ||
|
||
div.style.height = '1.5px'; | ||
|
||
assert.equal(getScale(div), 1, 'scale on a scaled element is correct'); | ||
}); | ||
|
||
test('throws if the height from getComputedStyle is diverged from offsetHeight', function(assert) { | ||
let div = document.createElement('div'); | ||
div.textContent = 'aBc'; | ||
div.style.padding = '10px'; | ||
this.element.append(div); | ||
|
||
assert.throws(() => { | ||
getScale(div); | ||
}); | ||
}); | ||
}); |