diff --git a/changelog.md b/changelog.md index da9bf62..8b9fb04 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,8 @@ ## Changelog +##### v.3.0.3 - 2015-04-23 +* Avoided adjusting the height for hidden textarea elements. Fixes #155. + ##### v.3.0.2 - 2015-04-23 * Reworked to respect max-height of any unit-type. Fixes #191. diff --git a/dest/autosize.js b/dest/autosize.js index 1fd10d2..56420fa 100644 --- a/dest/autosize.js +++ b/dest/autosize.js @@ -63,11 +63,18 @@ var startHeight = ta.style.height; var htmlTop = document.documentElement.scrollTop; var bodyTop = document.body.scrollTop; + var originalHeight = ta.style.height; ta.style.height = 'auto'; var endHeight = ta.scrollHeight + heightOffset; + if (ta.scrollHeight === 0) { + // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM. + ta.style.height = originalHeight; + return; + } + ta.style.height = endHeight + 'px'; // prevents scroll-position jumping diff --git a/dest/autosize.min.js b/dest/autosize.min.js index c2d98c2..3dd49dc 100644 --- a/dest/autosize.min.js +++ b/dest/autosize.min.js @@ -3,4 +3,4 @@ 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 o={exports:{}};t(o.exports,o),e.autosize=o.exports}}(this,function(e,t){"use strict";function o(e){function t(){var t=window.getComputedStyle(e,null);"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),i="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),n()}function o(t){var o=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=o,e.style.overflowY=t,n()}function n(){var t=e.style.height,n=document.documentElement.scrollTop,d=document.body.scrollTop;e.style.height="auto";var s=e.scrollHeight+i;e.style.height=s+"px",document.documentElement.scrollTop=n,document.body.scrollTop=d;var a=window.getComputedStyle(e,null);if(a.height!==e.style.height){if("visible"!==e.style.overflowY)return void o("visible")}else if("hidden"!==e.style.overflowY)return o("hidden"),void r();if(t!==e.style.height){var u=document.createEvent("Event");u.initEvent("autosize:resized",!0,!1),e.dispatchEvent(u)}}if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!e.hasAttribute("data-autosize-on")){var i,d=function(t){window.removeEventListener("resize",n),e.removeEventListener("input",n),e.removeEventListener("keyup",n),e.removeAttribute("data-autosize-on"),e.removeEventListener("autosize:destroy",d),Object.keys(t).forEach(function(o){e.style[o]=t[o]})}.bind(e,{height:e.style.height,resize:e.style.resize,overflowY:e.style.overflowY});e.addEventListener("autosize:destroy",d),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",n),window.addEventListener("resize",n),e.addEventListener("input",n),e.addEventListener("autosize:update",n),e.setAttribute("data-autosize-on",!0),t()}}function n(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 window.getComputedStyle?(r=function(e){return e},r.destroy=function(e){return e},r.update=function(e){return e}):(r=function(e){return e&&e.length?Array.prototype.forEach.call(e,o):e&&e.nodeName&&o(e),e},r.destroy=function(e){return e&&e.length?Array.prototype.forEach.call(e,n):e&&e.nodeName&&n(e),e},r.update=function(e){return e&&e.length?Array.prototype.forEach.call(e,i):e&&e.nodeName&&i(e),e}),t.exports=r}); \ 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 o={exports:{}};t(o.exports,o),e.autosize=o.exports}}(this,function(e,t){"use strict";function o(e){function t(){var t=window.getComputedStyle(e,null);"vertical"===t.resize?e.style.resize="none":"both"===t.resize&&(e.style.resize="horizontal"),i="content-box"===t.boxSizing?-(parseFloat(t.paddingTop)+parseFloat(t.paddingBottom)):parseFloat(t.borderTopWidth)+parseFloat(t.borderBottomWidth),n()}function o(t){var o=e.style.width;e.style.width="0px",e.offsetWidth,e.style.width=o,e.style.overflowY=t,n()}function n(){var t=e.style.height,n=document.documentElement.scrollTop,d=document.body.scrollTop,s=e.style.height;e.style.height="auto";var a=e.scrollHeight+i;if(0===e.scrollHeight)return void(e.style.height=s);e.style.height=a+"px",document.documentElement.scrollTop=n,document.body.scrollTop=d;var u=window.getComputedStyle(e,null);if(u.height!==e.style.height){if("visible"!==e.style.overflowY)return void o("visible")}else if("hidden"!==e.style.overflowY)return o("hidden"),void r();if(t!==e.style.height){var l=document.createEvent("Event");l.initEvent("autosize:resized",!0,!1),e.dispatchEvent(l)}}if(e&&e.nodeName&&"TEXTAREA"===e.nodeName&&!e.hasAttribute("data-autosize-on")){var i,d=function(t){window.removeEventListener("resize",n),e.removeEventListener("input",n),e.removeEventListener("keyup",n),e.removeAttribute("data-autosize-on"),e.removeEventListener("autosize:destroy",d),Object.keys(t).forEach(function(o){e.style[o]=t[o]})}.bind(e,{height:e.style.height,resize:e.style.resize,overflowY:e.style.overflowY});e.addEventListener("autosize:destroy",d),"onpropertychange"in e&&"oninput"in e&&e.addEventListener("keyup",n),window.addEventListener("resize",n),e.addEventListener("input",n),e.addEventListener("autosize:update",n),e.setAttribute("data-autosize-on",!0),t()}}function n(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 window.getComputedStyle?(r=function(e){return e},r.destroy=function(e){return e},r.update=function(e){return e}):(r=function(e){return e&&e.length?Array.prototype.forEach.call(e,o):e&&e.nodeName&&o(e),e},r.destroy=function(e){return e&&e.length?Array.prototype.forEach.call(e,n):e&&e.nodeName&&n(e),e},r.update=function(e){return e&&e.length?Array.prototype.forEach.call(e,i):e&&e.nodeName&&i(e),e}),t.exports=r}); \ No newline at end of file diff --git a/package.json b/package.json index 003db9d..3b6c14f 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.2", + "version": "3.0.3", "keywords": [ "textarea", "form", diff --git a/src/autosize.js b/src/autosize.js index 67b7554..9847ed5 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -43,11 +43,18 @@ function assign(ta) { const startHeight = ta.style.height; const htmlTop = document.documentElement.scrollTop; const bodyTop = document.body.scrollTop; - + const originalHeight = ta.style.height; + ta.style.height = 'auto'; let endHeight = ta.scrollHeight+heightOffset; + if (ta.scrollHeight === 0) { + // If the scrollHeight is 0, then the element probably has display:none or is detached from the DOM. + ta.style.height = originalHeight; + return; + } + ta.style.height = endHeight+'px'; // prevents scroll-position jumping