2 Autosize v1.18.1 - 2013-11-05
3 Automatically adjust textarea height based on user input.
4 (c) 2013 Jack Moore - http://www.jacklmoore.com/autosize
5 license: http://www.opensource.org/licenses/mit-license.php
10 className: 'autosizejs',
16 // border:0 is unnecessary, but avoids a bug in Firefox on OSX
17 copy = '<textarea tabindex="-1" style="position:absolute; top:-999px; left:0; right:auto; bottom:auto; border:0; padding: 0; -moz-box-sizing:content-box; -webkit-box-sizing:content-box; box-sizing:content-box; word-wrap:break-word; height:0 !important; min-height:0 !important; overflow:hidden; transition:none; -webkit-transition:none; -moz-transition:none;"/>',
19 // line-height is conditionally included because IE7/IE8/old Opera do not return the correct value.
31 // to keep track which textarea is being mirrored when adjust() is called.
34 // the mirror element, which is used to calculate what size the mirrored element should be.
35 mirror = $(copy).data('autosize', true)[0];
37 // test that line-height can be accurately copied.
38 mirror.style.lineHeight = '99px';
39 if ($(mirror).css('lineHeight') === '99px') {
40 typographyStyles.push('lineHeight');
42 mirror.style.lineHeight = '';
44 $.fn.autosize = function (options) {
49 options = $.extend({}, defaults, options || {});
51 if (mirror.parentNode !== document.body) {
52 $(document.body).append(mirror);
55 return this.each(function () {
62 callback = $.isFunction(options.callback),
64 height: ta.style.height,
65 overflow: ta.style.overflow,
66 overflowY: ta.style.overflowY,
67 wordWrap: ta.style.wordWrap,
68 resize: ta.style.resize
73 if ($ta.data('autosize')) {
74 // exit if autosize has already been applied, or if the textarea is the mirror element.
77 $ta.data('autosize', true);
79 if ($ta.css('box-sizing') === 'border-box' || $ta.css('-moz-box-sizing') === 'border-box' || $ta.css('-webkit-box-sizing') === 'border-box'){
80 boxOffset = $ta.outerHeight() - $ta.height();
83 // IE8 and lower return 'auto', which parses to NaN, if no min-height is set.
84 minHeight = Math.max(parseInt($ta.css('minHeight'), 10) - boxOffset || 0, $ta.height());
89 wordWrap: 'break-word', // horizontal overflow is hidden, so break-word is necessary for handling words longer than the textarea width
90 resize: ($ta.css('resize') === 'none' || $ta.css('resize') === 'vertical') ? 'none' : 'horizontal'
93 // The mirror width must exactly match the textarea width, so using getBoundingClientRect because it doesn't round the sub-pixel value.
97 if ('getComputedStyle' in window) {
98 style = window.getComputedStyle(ta, null);
99 width = ta.getBoundingClientRect().width;
101 $.each(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'], function(i,val){
102 width -= parseInt(style[val],10);
105 mirror.style.width = width + 'px';
108 // window.getComputedStyle, getBoundingClientRect returning a width are unsupported and unneeded in IE8 and lower.
109 mirror.style.width = Math.max($ta.width(), 0) + 'px';
113 function initMirror() {
117 mirror.className = options.className;
118 maxHeight = parseInt($ta.css('maxHeight'), 10);
120 // mirror is a duplicate textarea located off-screen that
121 // is automatically updated to contain the same text as the
122 // original textarea. mirror always has a height of 0.
123 // This gives a cross-browser supported way getting the actual
124 // height of the text, through the scrollTop property.
125 $.each(typographyStyles, function(i,val){
126 styles[val] = $ta.css(val);
128 $(mirror).css(styles);
132 // Chrome-specific fix:
133 // When the textarea y-overflow is hidden, Chrome doesn't reflow the text to account for the space
134 // made available by removing the scrollbar. This workaround triggers the reflow for Chrome.
136 var width = ta.style.width;
137 ta.style.width = '0px';
138 var ignore = ta.offsetWidth;
139 ta.style.width = width;
143 // Using mainly bare JS in this function because it is going
144 // to fire very often while typing, and needs to very efficient.
146 var height, original;
148 if (mirrored !== ta) {
154 mirror.value = ta.value + options.append;
155 mirror.style.overflowY = ta.style.overflowY;
156 original = parseInt(ta.style.height,10);
158 // Setting scrollTop to zero is needed in IE8 and lower for the next step to be accurately applied
159 mirror.scrollTop = 0;
161 mirror.scrollTop = 9e4;
163 // Using scrollTop rather than scrollHeight because scrollHeight is non-standard and includes padding.
164 height = mirror.scrollTop;
166 if (maxHeight && height > maxHeight) {
167 ta.style.overflowY = 'scroll';
170 ta.style.overflowY = 'hidden';
171 if (height < minHeight) {
178 if (original !== height) {
179 ta.style.height = height + 'px';
181 options.callback.call(ta,ta);
187 clearTimeout(timeout);
188 timeout = setTimeout(function(){
189 var newWidth = $ta.width();
191 if (newWidth !== width) {
195 }, parseInt(options.resizeDelay,10));
198 if ('onpropertychange' in ta) {
199 if ('oninput' in ta) {
200 // Detects IE9. IE9 does not fire onpropertychange or oninput for deletions,
201 // so binding to onkeyup to catch most of those occasions. There is no way that I
202 // know of to detect something like 'cut' in IE9.
203 $ta.on('input.autosize keyup.autosize', adjust);
206 $ta.on('propertychange.autosize', function(){
207 if(event.propertyName === 'value'){
214 $ta.on('input.autosize', adjust);
217 // Set options.resizeDelay to false if using fixed-width textarea elements.
218 // Uses a timeout and width check to reduce the amount of times adjust needs to be called after window resize.
220 if (options.resizeDelay !== false) {
221 $(window).on('resize.autosize', resize);
224 // Event for manual triggering if needed.
225 // Should only be needed when the value of the textarea is changed through JavaScript rather than user input.
226 $ta.on('autosize.resize', adjust);
228 // Event for manual triggering that also forces the styles to update as well.
229 // Should only be needed if one of typography styles of the textarea change, and the textarea is already the target of the adjust method.
230 $ta.on('autosize.resizeIncludeStyle', function() {
235 $ta.on('autosize.destroy', function(){
237 clearTimeout(timeout);
238 $(window).off('resize', resize);
243 .removeData('autosize');
246 // Call adjust in case the textarea already contains text.
250 }(window.jQuery || window.$)); // jQuery or jQuery-like library, such as Zepto