From 1e94f8aa0e6dfa7b52217bd4527e1dfdf38ed364 Mon Sep 17 00:00:00 2001 From: Jack Moore Date: Tue, 26 Jan 2016 03:31:38 -0500 Subject: [PATCH] Used newer Event constructor when available. Fixes #280. --- changelog.md | 3 +++ dist/autosize.js | 25 ++++++++++++++++++------- dist/autosize.min.js | 4 ++-- package.json | 2 +- src/autosize.js | 21 +++++++++++++++------ 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/changelog.md b/changelog.md index f7742b9..52604a3 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ## Changelog +##### v.3.0.15 - 2016-1-26 +* Used newer Event constructor, when available. Fixes #280. + ##### v.3.0.14 - 2015-11-11 * Fixed memory leak on destroy. Merged #271, fixes #270. * Fixed bug in old versions of Firefox (1-5), fixes #246. diff --git a/dist/autosize.js b/dist/autosize.js index cfa49e7..c807509 100644 --- a/dist/autosize.js +++ b/dist/autosize.js @@ -1,5 +1,5 @@ /*! - Autosize 3.0.14 + Autosize 3.0.15 license: MIT http://www.jacklmoore.com/autosize */ @@ -33,6 +33,20 @@ } }; })(); + var createEvent = function createEvent(name) { + return new Event(name); + }; + try { + new Event('test'); + } catch (e) { + // IE does not support `new Event()` + createEvent = function (name) { + var evt = document.createEvent('Event'); + evt.initEvent(name, true, false); + return evt; + }; + } + function assign(ta) { var _ref = arguments[1] === undefined ? {} : arguments[1]; @@ -137,8 +151,7 @@ } if (startHeight !== ta.style.height) { - var evt = document.createEvent('Event'); - evt.initEvent('autosize:resized', true, false); + var evt = createEvent('autosize:resized'); ta.dispatchEvent(evt); } } @@ -191,15 +204,13 @@ function destroy(ta) { if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - var evt = document.createEvent('Event'); - evt.initEvent('autosize:destroy', true, false); + var evt = createEvent('autosize:destroy'); ta.dispatchEvent(evt); } function update(ta) { if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - var evt = document.createEvent('Event'); - evt.initEvent('autosize:update', true, false); + var evt = createEvent('autosize:update'); ta.dispatchEvent(evt); } diff --git a/dist/autosize.min.js b/dist/autosize.min.js index cefedc6..4cd8eed 100644 --- a/dist/autosize.min.js +++ b/dist/autosize.min.js @@ -1,6 +1,6 @@ /*! - Autosize 3.0.14 + Autosize 3.0.15 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);c=t.overflowY,"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),f="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),isNaN(f)&&(f=0),i()}function n(t){var n=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=n,c=t,u&&(e.style.overflowY=t),o()}function o(){var t=window.pageYOffset,n=document.body.scrollTop,o=e.style.height;e.style.height="auto";var i=e.scrollHeight+f;return 0===e.scrollHeight?void(e.style.height=o):(e.style.height=i+"px",v=e.clientWidth,document.documentElement.scrollTop=t,void(document.body.scrollTop=n))}function i(){var t=e.style.height;o();var i=window.getComputedStyle(e,null);if(i.height!==e.style.height?"visible"!==c&&n("visible"):"hidden"!==c&&n("hidden"),t!==e.style.height){var r=document.createEvent("Event");r.initEvent("autosize:resized",!0,!1),e.dispatchEvent(r)}}var d=void 0===arguments[1]?{}:arguments[1],s=d.setOverflowX,l=void 0===s?!0:s,a=d.setOverflowY,u=void 0===a?!0:a;if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!r.has(e)){var f=null,c=null,v=e.clientWidth,p=function(){e.clientWidth!==v&&i()},h=function(t){window.removeEventListener("resize",p,!1),e.removeEventListener("input",i,!1),e.removeEventListener("keyup",i,!1),e.removeEventListener("autosize:destroy",h,!1),e.removeEventListener("autosize:update",i,!1),r["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",h,!1),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",i,!1),window.addEventListener("resize",p,!1),e.addEventListener("input",i,!1),e.addEventListener("autosize:update",i,!1),r.add(e),l&&(e.style.overflowX="hidden",e.style.wordWrap="break-word"),t()}}function o(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=document.createEvent("Event");t.initEvent("autosize:destroy",!0,!1),e.dispatchEvent(t)}}function i(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=document.createEvent("Event");t.initEvent("autosize:update",!0,!1),e.dispatchEvent(t)}}var r="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=null;"undefined"==typeof window||"function"!=typeof window.getComputedStyle?(d=function(e){return e},d.destroy=function(e){return e},d.update=function(e){return e}):(d=function(e,t){return e&&Array.prototype.forEach.call(e.length?e:[e],function(e){return n(e,t)}),e},d.destroy=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],o),e},d.update=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],i),e}),t.exports=d}); \ 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);p=t.overflowY,"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),c="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),isNaN(c)&&(c=0),i()}function n(t){var n=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=n,p=t,f&&(e.style.overflowY=t),o()}function o(){var t=window.pageYOffset,n=document.body.scrollTop,o=e.style.height;e.style.height="auto";var i=e.scrollHeight+c;return 0===e.scrollHeight?void(e.style.height=o):(e.style.height=i+"px",v=e.clientWidth,document.documentElement.scrollTop=t,void(document.body.scrollTop=n))}function i(){var t=e.style.height;o();var i=window.getComputedStyle(e,null);if(i.height!==e.style.height?"visible"!==p&&n("visible"):"hidden"!==p&&n("hidden"),t!==e.style.height){var r=d("autosize:resized");e.dispatchEvent(r)}}var s=void 0===arguments[1]?{}:arguments[1],a=s.setOverflowX,l=void 0===a?!0:a,u=s.setOverflowY,f=void 0===u?!0:u;if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!r.has(e)){var c=null,p=null,v=e.clientWidth,h=function(){e.clientWidth!==v&&i()},y=function(t){window.removeEventListener("resize",h,!1),e.removeEventListener("input",i,!1),e.removeEventListener("keyup",i,!1),e.removeEventListener("autosize:destroy",y,!1),e.removeEventListener("autosize:update",i,!1),r["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",y,!1),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",i,!1),window.addEventListener("resize",h,!1),e.addEventListener("input",i,!1),e.addEventListener("autosize:update",i,!1),r.add(e),l&&(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 i(e){if(e&&e.nodeName&&"TEXTAREA"===e.nodeName){var t=d("autosize:update");e.dispatchEvent(t)}}var r="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(s){d=function(e){var t=document.createEvent("Event");return t.initEvent(e,!0,!1),t}}var a=null;"undefined"==typeof window||"function"!=typeof window.getComputedStyle?(a=function(e){return e},a.destroy=function(e){return e},a.update=function(e){return e}):(a=function(e,t){return e&&Array.prototype.forEach.call(e.length?e:[e],function(e){return n(e,t)}),e},a.destroy=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],o),e},a.update=function(e){return e&&Array.prototype.forEach.call(e.length?e:[e],i),e}),t.exports=a}); \ No newline at end of file diff --git a/package.json b/package.json index 7a30950..18ef012 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.14", + "version": "3.0.15", "keywords": [ "textarea", "form", diff --git a/src/autosize.js b/src/autosize.js index c02f4cf..d0e47ce 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -14,6 +14,18 @@ const set = (typeof Set === "function") ? new Set() : (function () { } })(); +let createEvent = (name)=> new Event(name); +try { + new Event('test'); +} catch(e) { + // IE does not support `new Event()` + createEvent = (name)=> { + const evt = document.createEvent('Event'); + evt.initEvent(name, true, false); + return evt; + }; +} + function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) { if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return; @@ -111,8 +123,7 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) { } if (startHeight !== ta.style.height) { - const evt = document.createEvent('Event'); - evt.initEvent('autosize:resized', true, false); + const evt = createEvent('autosize:resized'); ta.dispatchEvent(evt); } } @@ -166,15 +177,13 @@ function assign(ta, {setOverflowX = true, setOverflowY = true} = {}) { function destroy(ta) { if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - const evt = document.createEvent('Event'); - evt.initEvent('autosize:destroy', true, false); + const evt = createEvent('autosize:destroy'); ta.dispatchEvent(evt); } function update(ta) { if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return; - const evt = document.createEvent('Event'); - evt.initEvent('autosize:update', true, false); + const evt = createEvent('autosize:update'); ta.dispatchEvent(evt); }