-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_sizer.js
More file actions
34 lines (28 loc) · 780 Bytes
/
html_sizer.js
File metadata and controls
34 lines (28 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// HtmlSizer class.
var _ = require('lodash');
module.exports = HtmlSizer;
function HtmlSizer(parentEl, style) {
this.el_ = document.createElement('div');
_.assign(this.el_.style, style, {
position: 'fixed',
top: '-1000px',
left: '-1000px',
visibilty: 'hidden',
whiteSpace: 'pre'
});
parentEl.appendChild(this.el_);
}
HtmlSizer.prototype.size = function(html) {
this.el_.innerHTML = html;
// Note, getBoundingClientRect returns fractional width and height.
var rect = this.el_.getBoundingClientRect();
var res = [rect.width, rect.height];
this.el_.innerHTML = '';
return res;
};
HtmlSizer.prototype.width = function(html) {
return this.size(html)[0];
};
HtmlSizer.prototype.height = function(html) {
return this.size(html)[1];
};