|
9 | 9 | var properties = [
|
10 | 10 | 'direction', // RTL support
|
11 | 11 | 'boxSizing',
|
12 |
| - 'width', // on Chrome and IE, exclude the scrollbar, so the mirror div wraps exactly as the textarea does |
13 | 12 | 'height',
|
14 | 13 | 'overflowX',
|
15 |
| - 'overflowY', // copy the scrollbar for IE |
16 | 14 |
|
17 | 15 | <<<<<<< HEAD
|
18 | 16 | 'borderTopWidth',
|
@@ -56,6 +54,25 @@ var properties = [
|
56 | 54 |
|
57 | 55 | var isBrowser = (typeof window !== 'undefined');
|
58 | 56 | var isFirefox = (isBrowser && window.mozInnerScreenX != null);
|
| 57 | +var scrollbarWidth = 0; |
| 58 | + |
| 59 | +// modified from http://davidwalsh.name/detect-scrollbar-width |
| 60 | +function getScrollbarWidth() { |
| 61 | + if (!scrollbarWidth) { |
| 62 | + var div = document.createElement('div'), |
| 63 | + style = div.style; |
| 64 | + document.body.appendChild(div); |
| 65 | + style.position = 'absolute'; |
| 66 | + style.top = '-9999px'; |
| 67 | + style.left = 0; |
| 68 | + style.width = style.height = '100px'; |
| 69 | + style.overflow = 'scroll'; |
| 70 | + style.visibility = 'hidden'; |
| 71 | + scrollbarWidth = div.offsetWidth - div.clientWidth; |
| 72 | + document.body.removeChild(div); |
| 73 | + } |
| 74 | + return scrollbarWidth; |
| 75 | +}; |
59 | 76 |
|
60 | 77 | function getCaretCoordinates(element, position, options) {
|
61 | 78 | if (!isBrowser) {
|
@@ -97,14 +114,16 @@ function getCaretCoordinates(element, position, options) {
|
97 | 114 | }
|
98 | 115 | });
|
99 | 116 |
|
| 117 | + style.overflowY = element.scrollHeight > parseInt(computed.height) ? 'scroll' : 'auto'; |
| 118 | + |
100 | 119 | if (isFirefox) {
|
101 | 120 | // Firefox lies about the overflow property for textareas: https://bugzilla.mozilla.org/show_bug.cgi?id=984275
|
102 |
| - if (element.scrollHeight > parseInt(computed.height)) |
103 |
| - style.overflowY = 'scroll'; |
| 121 | + // so we still use computed width |
| 122 | + style.width = computed.width; |
104 | 123 | } else {
|
105 |
| - style.overflow = 'hidden'; // for Chrome to not render a scrollbar; IE keeps overflowY = 'scroll' |
| 124 | + // include scrollbar width - Chrome & IE renders scrollbar and gets width added |
| 125 | + style.width = parseInt( computed.width, 10 ) + getScrollbarWidth() + 'px'; |
106 | 126 | }
|
107 |
| - |
108 | 127 | div.textContent = element.value.substring(0, position);
|
109 | 128 | // The second special handling for input type="text" vs textarea:
|
110 | 129 | // spaces need to be replaced with non-breaking spaces - http://stackoverflow.com/a/13402035/1269037
|
|
0 commit comments