Skip to content

Commit

Permalink
Fixed an issue where the calculated height might be slightly off in m…
Browse files Browse the repository at this point in the history
…odern browsers when the width of the textarea has a subpixel value.
  • Loading branch information
jackmoore committed Jun 11, 2013
1 parent 55bdad6 commit 975b409
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
4 changes: 2 additions & 2 deletions jquery.autosize-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions jquery.autosize.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*!
jQuery Autosize v1.16.15
(c) 2013 Jack Moore - jacklmoore.com
updated: 2013-06-07
updated: 2013-06-11
license: http://www.opensource.org/licenses/mit-license.php
*/
(function ($) {
Expand All @@ -15,6 +15,7 @@
hidden = 'hidden',
borderBox = 'border-box',
lineHeight = 'lineHeight',
useSubpixels,

// border:0 is unnecessary, but avoids a bug in FireFox on OSX
copy = '<textarea tabindex="-1" style="position:absolute; top:-999px; left:0; right:auto; bottom:auto; border: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;"/>',
Expand Down Expand Up @@ -46,6 +47,9 @@
}
mirror.style.lineHeight = '';

// test for subpixel rendering
useSubpixels = mirror.getBoundingClientRect().width !== undefined;

$.fn.autosize = function (options) {
options = $.extend({}, defaults, options || {});

Expand Down Expand Up @@ -111,7 +115,7 @@
// Using mainly bare JS in this function because it is going
// to fire very often while typing, and needs to very efficient.
function adjust() {
var height, overflow, original;
var height, overflow, original, width;

if (mirrored !== ta) {
initMirror();
Expand All @@ -121,9 +125,17 @@
mirror.style.overflowY = ta.style.overflowY;
original = parseInt(ta.style.height,10);

// Update the width in case the original textarea width has changed
// A floor of 0 is needed because IE8 returns a negative value for hidden textareas, raising an error.
mirror.style.width = Math.max($ta.width(), 0) + 'px';
if (useSubpixels) {
// The mirror width much exactly match the textarea width, so using getBoundingClientRect because it doesn't round the subpixel value.
width = ta.getBoundingClientRect().width;
$.each($(ta).css(['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth']), function(){
width -= parseInt(this, 10);
});
mirror.style.width = width + 'px';
}
else {
mirror.style.width = Math.max($ta.width(), 0) + 'px';
}

// Needed for IE8 and lower to reliably return the correct scrollTop
mirror.scrollTop = 0;
Expand Down
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Small jQuery plugin to allow dynamic resizing of textarea height, so that it gro

## Changelog

### v1.16.16 - 2013/6/11
* Fixed an issue where the calculated height might be slightly off in modern browsers when the width of the textarea has a subpixel value.

### v1.16.15 - 2013/6/7
* Reduced how frequently autosize is triggered when resizing the window. Added resizeDelay property so that the frequency can be adjusted or disabled.

Expand Down

0 comments on commit 975b409

Please sign in to comment.