From fa2fa308e91ff9f8ba1a9fb17a1b9cc620671ca8 Mon Sep 17 00:00:00 2001 From: Jack Moore Date: Wed, 26 Oct 2016 13:44:24 -0400 Subject: [PATCH] Fixed Firefox issue where calling dispatchEvent on a detached element throws an error. Fixes #317. --- build.js | 2 +- changelog.md | 3 +++ dist/autosize.js | 58 ++++++++++++++++++++++++++++++-------------- dist/autosize.min.js | 4 +-- package.json | 2 +- src/autosize.js | 55 +++++++++++++++++++++++++++++------------ 6 files changed, 86 insertions(+), 38 deletions(-) diff --git a/build.js b/build.js index bab0cd6..83246e9 100644 --- a/build.js +++ b/build.js @@ -32,7 +32,7 @@ function lint(full) { eqeqeq: true, eqnull: true, noarg: true, - predef: ['define', 'module', 'exports', 'Set'] + predef: ['define', 'module', 'exports', 'Map'] }); if (jshint.errors.length) { diff --git a/changelog.md b/changelog.md index 61d73e4..a30aca4 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ## Changelog +##### v.3.0.18 - 2016-10-26 +* Fixed Firefox issue where calling dispatchEvent on a detached element throws an error. Fixes #317. + ##### v.3.0.17 - 2016-7-25 * Fixed Chromium issue where getComputedStyle pixel value did not exactly match the style pixel value. Fixes #306. * Removed undocumented argument, minor refactoring, more comments. diff --git a/dist/autosize.js b/dist/autosize.js index 49b8b0b..6056de4 100644 --- a/dist/autosize.js +++ b/dist/autosize.js @@ -1,5 +1,5 @@ /*! - Autosize 3.0.17 + Autosize 3.0.18 license: MIT http://www.jacklmoore.com/autosize */ @@ -18,18 +18,29 @@ })(this, function (exports, module) { 'use strict'; - var set = typeof Set === 'function' ? new Set() : (function () { - var list = []; + var map = typeof Map === 'function' ? new Map() : (function () { + var keys = []; + var values = []; return { has: function has(key) { - return Boolean(list.indexOf(key) > -1); + return keys.indexOf(key) > -1; }, - add: function add(key) { - list.push(key); + get: function get(key) { + return values[keys.indexOf(key)]; + }, + set: function set(key, value) { + if (keys.indexOf(key) === -1) { + keys.push(key); + values.push(value); + } }, 'delete': function _delete(key) { - list.splice(list.indexOf(key), 1); + var index = keys.indexOf(key); + if (index > -1) { + keys.splice(index, 1); + values.splice(index, 1); + } } }; })(); @@ -48,7 +59,7 @@ } 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; var heightOffset = null; var clientWidth = ta.clientWidth; @@ -163,7 +174,9 @@ if (cachedHeight !== computedHeight) { cachedHeight = computedHeight; var evt = createEvent('autosize:resized'); - ta.dispatchEvent(evt); + try { + ta.dispatchEvent(evt); + } catch (err) {} } } @@ -179,11 +192,12 @@ ta.removeEventListener('keyup', update, false); ta.removeEventListener('autosize:destroy', destroy, false); ta.removeEventListener('autosize:update', update, false); - set['delete'](ta); Object.keys(style).forEach(function (key) { ta.style[key] = style[key]; }); + + map['delete'](ta); }).bind(ta, { height: ta.style.height, resize: ta.style.resize, @@ -203,23 +217,28 @@ 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: destroy, + update: update }); + init(); } function destroy(ta) { - if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - var evt = createEvent('autosize:destroy'); - ta.dispatchEvent(evt); + var methods = map.get(ta); + if (methods) { + methods.destroy(); + } } function update(ta) { - if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - var evt = createEvent('autosize:update'); - ta.dispatchEvent(evt); + var methods = map.get(ta); + if (methods) { + methods.update(); + } } var autosize = null; @@ -259,4 +278,7 @@ } module.exports = autosize; -}); \ No newline at end of file +}); + +// Firefox will throw an error on dispatchEvent for a detached element +// https://bugzilla.mozilla.org/show_bug.cgi?id=889376 \ No newline at end of file diff --git a/dist/autosize.min.js b/dist/autosize.min.js index 2108e90..8528b14 100644 --- a/dist/autosize.min.js +++ b/dist/autosize.min.js @@ -1,6 +1,6 @@ /*! - Autosize 3.0.17 + Autosize 3.0.18 license: MIT http://www.jacklmoore.com/autosize */ -!function(e,t){if("function"==typeof define&&define.amd)define(["exports","module"],t);else if("undefined"!=typeof exports&&"undefined"!=typeof module)t(exports,module);else{var n={exports:{}};t(n.exports,n),e.autosize=n.exports}}(this,function(e,t){"use strict";function n(e){function t(){var t=window.getComputedStyle(e,null);"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),l="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),isNaN(l)&&(l=0),a()}function n(t){var n=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=n,e.style.overflowY=t,r()}function o(e){for(var t=[];e&&e.parentNode&&e.parentNode instanceof Element;)e.parentNode.scrollTop&&t.push({node:e.parentNode,scrollTop:e.parentNode.scrollTop}),e=e.parentNode;return t}function r(){var t=e.style.height,n=o(e),r=document.documentElement&&document.documentElement.scrollTop;e.style.height="auto";var i=e.scrollHeight+l;return 0===e.scrollHeight?void(e.style.height=t):(e.style.height=i+"px",s=e.clientWidth,n.forEach(function(e){e.node.scrollTop=e.scrollTop}),void(r&&(document.documentElement.scrollTop=r)))}function a(){r();var t=window.getComputedStyle(e,null),o=Math.round(parseFloat(t.height)),i=Math.round(parseFloat(e.style.height));if(o!==i?"visible"!==t.overflowY&&n("visible"):"hidden"!==t.overflowY&&n("hidden"),u!==o){u=o;var a=d("autosize:resized");e.dispatchEvent(a)}}if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!i.has(e)){var l=null,s=e.clientWidth,u=null,c=function(){e.clientWidth!==s&&a()},p=function(t){window.removeEventListener("resize",c,!1),e.removeEventListener("input",a,!1),e.removeEventListener("keyup",a,!1),e.removeEventListener("autosize:destroy",p,!1),e.removeEventListener("autosize:update",a,!1),i["delete"](e),Object.keys(t).forEach(function(n){e.style[n]=t[n]})}.bind(e,{height:e.style.height,resize:e.style.resize,overflowY:e.style.overflowY,overflowX:e.style.overflowX,wordWrap:e.style.wordWrap});e.addEventListener("autosize:destroy",p,!1),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",a,!1),window.addEventListener("resize",c,!1),e.addEventListener("input",a,!1),e.addEventListener("autosize:update",a,!1),i.add(e),e.style.overflowX="hidden",e.style.wordWrap="break-word",t()}}function o(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=d("autosize:destroy");e.dispatchEvent(t)}}function r(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=d("autosize:update");e.dispatchEvent(t)}}var i="function"==typeof Set?new Set:function(){var e=[];return{has:function(t){return Boolean(e.indexOf(t)>-1)},add:function(t){e.push(t)},"delete":function(t){e.splice(e.indexOf(t),1)}}}(),d=function(e){return new Event(e)};try{new Event("test")}catch(a){d=function(e){var t=document.createEvent("Event");return t.initEvent(e,!0,!1),t}}var l=null;"undefined"==typeof window||"function"!=typeof window.getComputedStyle?(l=function(e){return e},l.destroy=function(e){return e},l.update=function(e){return e}):(l=function(e,t){return e&&Array.prototype.forEach.call(e.length?e:[e],function(e){return n(e,t)}),e},l.destroy=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],o),e},l.update=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],r),e}),t.exports=l}); \ No newline at end of file +!function(e,t){if("function"==typeof define&&define.amd)define(["exports","module"],t);else if("undefined"!=typeof exports&&"undefined"!=typeof module)t(exports,module);else{var n={exports:{}};t(n.exports,n),e.autosize=n.exports}}(this,function(e,t){"use strict";function n(e){function t(){var t=window.getComputedStyle(e,null);"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),l="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),isNaN(l)&&(l=0),s()}function n(t){var n=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=n,e.style.overflowY=t,r()}function o(e){for(var t=[];e&&e.parentNode&&e.parentNode instanceof Element;)e.parentNode.scrollTop&&t.push({node:e.parentNode,scrollTop:e.parentNode.scrollTop}),e=e.parentNode;return t}function r(){var t=e.style.height,n=o(e),r=document.documentElement&&document.documentElement.scrollTop;e.style.height="auto";var i=e.scrollHeight+l;return 0===e.scrollHeight?void(e.style.height=t):(e.style.height=i+"px",u=e.clientWidth,n.forEach(function(e){e.node.scrollTop=e.scrollTop}),void(r&&(document.documentElement.scrollTop=r)))}function s(){r();var t=window.getComputedStyle(e,null),o=Math.round(parseFloat(t.height)),i=Math.round(parseFloat(e.style.height));if(o!==i?"visible"!==t.overflowY&&n("visible"):"hidden"!==t.overflowY&&n("hidden"),a!==o){a=o;var s=d("autosize:resized");try{e.dispatchEvent(s)}catch(l){}}}if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!i.has(e)){var l=null,u=e.clientWidth,a=null,c=function(){e.clientWidth!==u&&s()},p=function(t){window.removeEventListener("resize",c,!1),e.removeEventListener("input",s,!1),e.removeEventListener("keyup",s,!1),e.removeEventListener("autosize:destroy",p,!1),e.removeEventListener("autosize:update",s,!1),Object.keys(t).forEach(function(n){e.style[n]=t[n]}),i["delete"](e)}.bind(e,{height:e.style.height,resize:e.style.resize,overflowY:e.style.overflowY,overflowX:e.style.overflowX,wordWrap:e.style.wordWrap});e.addEventListener("autosize:destroy",p,!1),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",s,!1),window.addEventListener("resize",c,!1),e.addEventListener("input",s,!1),e.addEventListener("autosize:update",s,!1),e.style.overflowX="hidden",e.style.wordWrap="break-word",i.set(e,{destroy:p,update:s}),t()}}function o(e){var t=i.get(e);t&&t.destroy()}function r(e){var t=i.get(e);t&&t.update()}var i="function"==typeof Map?new Map:function(){var e=[],t=[];return{has:function(t){return e.indexOf(t)>-1},get:function(n){return t[e.indexOf(n)]},set:function(n,o){-1===e.indexOf(n)&&(e.push(n),t.push(o))},"delete":function(n){var o=e.indexOf(n);o>-1&&(e.splice(o,1),t.splice(o,1))}}}(),d=function(e){return new Event(e)};try{new Event("test")}catch(s){d=function(e){var t=document.createEvent("Event");return t.initEvent(e,!0,!1),t}}var l=null;"undefined"==typeof window||"function"!=typeof window.getComputedStyle?(l=function(e){return e},l.destroy=function(e){return e},l.update=function(e){return e}):(l=function(e,t){return e&&Array.prototype.forEach.call(e.length?e:[e],function(e){return n(e,t)}),e},l.destroy=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],o),e},l.update=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],r),e}),t.exports=l}); \ No newline at end of file diff --git a/package.json b/package.json index 8f012b1..b62a2bb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "autosize", "description": "Autosize is a small, stand-alone script to automatically adjust textarea height to fit text.", - "version": "3.0.17", + "version": "3.0.18", "keywords": [ "textarea", "form", 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;