Permalink
Browse files

Adds support for getBoundingClientRect

This fixes the following warning:

> `SVGElement.offsetWidth` is deprecated and will be removed in
[Chrome] M50, around April 2016. See
https://www.chromestatus.com/features/5724912467574784 for more details.
  • Loading branch information...
1 parent a9a6f74 commit a034fae2e5fd21908806dec4f9918d1e5a597ee2 @kennethormandy kennethormandy committed Feb 6, 2016
Showing with 6 additions and 5 deletions.
  1. +6 −5 index.js
View
@@ -8,15 +8,15 @@ module.exports = function(elements, options) {
var baseline = options.baseline || 16;
var paddingY = options.paddingY || 0;
var doc = options.doc || document;
+ var bounding = typeof doc.createElementNS('http://www.w3.org/2000/svg', 'svg').getBoundingClientRect === 'function';
for (var i = 0; i < elements.length; i++) {
// variable declarations
var content = elements[i].textContent;
var svg = doc.createElementNS('http://www.w3.org/2000/svg', 'svg');
var text = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
- var width;
- var height;
+ var rect;
// assign proper styles and positioning to text elements
text.textContent = content;
@@ -41,10 +41,11 @@ module.exports = function(elements, options) {
svg.appendChild(text);
elements[i].parentNode.replaceChild(svg, elements[i]);
- width = text.offsetWidth || text.getComputedTextLength();
- height = text.offsetHeight || 24;
+ rect = bounding ? text.getBoundingClientRect() : {};
+ rect.width = rect.width || text.offsetWidth || text.getComputedTextLength();
+ rect.height = rect.height || text.offsetHeight || 24;
- svg.setAttribute('viewBox', '0 0 ' + width + ' ' + (height + paddingY));
+ svg.setAttribute('viewBox', '0 0 ' + Math.round(rect.width) + ' ' + (Math.round(rect.height) + paddingY));
}

0 comments on commit a034fae

Please sign in to comment.