diff options
author | Jack Moore <hello@jacklmoore.com> | 2016-10-26 13:44:24 -0400 |
---|---|---|
committer | Jack Moore <hello@jacklmoore.com> | 2016-10-26 13:44:24 -0400 |
commit | fa2fa308e91ff9f8ba1a9fb17a1b9cc620671ca8 (patch) | |
tree | b96bc4a118fb3e822da6e5c2c4934b014da7c11d /src | |
parent | 3838f1939045804b3166fd217ed59184758e8de3 (diff) | |
download | autosize-fa2fa308e91ff9f8ba1a9fb17a1b9cc620671ca8.zip autosize-fa2fa308e91ff9f8ba1a9fb17a1b9cc620671ca8.tar.gz autosize-fa2fa308e91ff9f8ba1a9fb17a1b9cc620671ca8.tar.bz2 |
Fixed Firefox issue where calling dispatchEvent on a detached element throws an error. Fixes #317.3.0.18
Diffstat (limited to 'src')
-rw-r--r-- | src/autosize.js | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/autosize.js b/src/autosize.js index 42fb2bc..3625a8e 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -1,15 +1,26 @@ -const set = (typeof Set === "function") ? new Set() : (function () { - const list = []; +const map = (typeof Map === "function") ? new Map() : (function () { + const keys = []; + const values = []; return { has(key) { - return Boolean(list.indexOf(key) > -1); + return keys.indexOf(key) > -1; }, - add(key) { - list.push(key); + get(key) { + return values[keys.indexOf(key)]; + }, + set(key, value) { + if (keys.indexOf(key) === -1) { + keys.push(key); + values.push(value); + } }, delete(key) { - list.splice(list.indexOf(key), 1); + const index = keys.indexOf(key); + if (index > -1) { + keys.splice(index, 1); + values.splice(index, 1); + } }, } })(); @@ -27,7 +38,7 @@ try { } function assign(ta) { - if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return; + if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return; let heightOffset = null; let clientWidth = ta.clientWidth; @@ -143,7 +154,12 @@ function assign(ta) { if (cachedHeight !== computedHeight) { cachedHeight = computedHeight; const evt = createEvent('autosize:resized'); - ta.dispatchEvent(evt); + try { + ta.dispatchEvent(evt); + } catch (err) { + // Firefox will throw an error on dispatchEvent for a detached element + // https://bugzilla.mozilla.org/show_bug.cgi?id=889376 + } } } @@ -159,11 +175,12 @@ function assign(ta) { ta.removeEventListener('keyup', update, false); ta.removeEventListener('autosize:destroy', destroy, false); ta.removeEventListener('autosize:update', update, false); - set.delete(ta); Object.keys(style).forEach(key => { ta.style[key] = style[key]; }); + + map.delete(ta); }.bind(ta, { height: ta.style.height, resize: ta.style.resize, @@ -184,23 +201,29 @@ function assign(ta) { window.addEventListener('resize', pageResize, false); ta.addEventListener('input', update, false); ta.addEventListener('autosize:update', update, false); - set.add(ta); ta.style.overflowX = 'hidden'; ta.style.wordWrap = 'break-word'; + map.set(ta, { + destroy, + update, + }); + init(); } function destroy(ta) { - if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - const evt = createEvent('autosize:destroy'); - ta.dispatchEvent(evt); + const methods = map.get(ta); + if (methods) { + methods.destroy(); + } } function update(ta) { - if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - const evt = createEvent('autosize:update'); - ta.dispatchEvent(evt); + const methods = map.get(ta); + if (methods) { + methods.update(); + } } let autosize = null; |