Skip to content

Commit

Permalink
Merge pull request #241 from amay/fix-span-cache-issue
Browse files Browse the repository at this point in the history
stop caching span in memory
  • Loading branch information
xile611 committed Sep 19, 2016
2 parents 2832d77 + 38d328c commit d9c58ad
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/util/DOMUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const STYLE_LIST = [
'paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom',
'marginLeft', 'marginRight', 'marginTop', 'marginBottom',
];
const MEASUREMENT_SPAN_ID = 'recharts_measurement_span';

function autoCompleteStyle(name, value) {
if (STYLE_LIST.indexOf(name) >= 0 && value === +value) {
Expand Down Expand Up @@ -62,18 +63,17 @@ export const getStringSize = (text, style = {}) => {

if (stringCache.widthCache[cacheKey]) { return stringCache.widthCache[cacheKey]; }

if (!stringCache.span) {
const span = document.createElement('span');
span.setAttribute('style', getStyleString(SPAN_STYLE));
document.body.appendChild(span);

stringCache.span = span;
let measurementSpan = document.getElementById(MEASUREMENT_SPAN_ID);
if (!measurementSpan) {
measurementSpan = document.createElement('span');
measurementSpan.setAttribute('id', MEASUREMENT_SPAN_ID);
document.body.appendChild(measurementSpan);
}

stringCache.span.setAttribute('style', getStyleString({ ...SPAN_STYLE, ...style }));
stringCache.span.textContent = str;
measurementSpan.setAttribute('style', getStyleString({ ...SPAN_STYLE, ...style }));
measurementSpan.textContent = str;

const rect = stringCache.span.getBoundingClientRect();
const rect = measurementSpan.getBoundingClientRect();
const result = { width: rect.width, height: rect.height };

stringCache.widthCache[cacheKey] = result;
Expand Down

0 comments on commit d9c58ad

Please sign in to comment.