Created
April 13, 2018 15:21
-
-
Save nolanlawson/7375a4b76fd6a3ea656f74d8f9e3ed14 to your computer and use it in GitHub Desktop.
Repro Firefox autosize issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (global, factory) { | |
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | |
typeof define === 'function' && define.amd ? define(['exports'], factory) : | |
(factory((global.autosize = {}))); | |
}(this, (function (exports) { 'use strict'; | |
const process = { | |
browser: true, | |
env: { | |
NODE_ENV: 'development' | |
} | |
} | |
const enabled = process.browser && performance.mark && ( | |
process.env.NODE_ENV !== 'production' || | |
location.search.includes('marks=true') | |
); | |
const perf = process.browser && performance; | |
function mark (name) { | |
if (enabled) { | |
perf.mark(`start ${name}`); | |
} | |
} | |
function stop (name) { | |
if (enabled) { | |
perf.mark(`end ${name}`); | |
perf.measure(name, `start ${name}`, `end ${name}`); | |
} | |
} | |
/** | |
* Checks if `value` is the | |
* [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types) | |
* of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) | |
* | |
* @static | |
* @memberOf _ | |
* @since 0.1.0 | |
* @category Lang | |
* @param {*} value The value to check. | |
* @returns {boolean} Returns `true` if `value` is an object, else `false`. | |
* @example | |
* | |
* _.isObject({}); | |
* // => true | |
* | |
* _.isObject([1, 2, 3]); | |
* // => true | |
* | |
* _.isObject(_.noop); | |
* // => true | |
* | |
* _.isObject(null); | |
* // => false | |
*/ | |
function isObject(value) { | |
var type = typeof value; | |
return value != null && (type == 'object' || type == 'function'); | |
} | |
/** Detect free variable `global` from Node.js. */ | |
var freeGlobal = typeof global == 'object' && global && global.Object === Object && global; | |
/** Detect free variable `self`. */ | |
var freeSelf = typeof self == 'object' && self && self.Object === Object && self; | |
/** Used as a reference to the global object. */ | |
var root = freeGlobal || freeSelf || Function('return this')(); | |
/** | |
* Gets the timestamp of the number of milliseconds that have elapsed since | |
* the Unix epoch (1 January 1970 00:00:00 UTC). | |
* | |
* @static | |
* @memberOf _ | |
* @since 2.4.0 | |
* @category Date | |
* @returns {number} Returns the timestamp. | |
* @example | |
* | |
* _.defer(function(stamp) { | |
* console.log(_.now() - stamp); | |
* }, _.now()); | |
* // => Logs the number of milliseconds it took for the deferred invocation. | |
*/ | |
var now = function() { | |
return root.Date.now(); | |
}; | |
/** Built-in value references. */ | |
var Symbol = root.Symbol; | |
/** Used for built-in method references. */ | |
var objectProto = Object.prototype; | |
/** Used to check objects for own properties. */ | |
var hasOwnProperty = objectProto.hasOwnProperty; | |
/** | |
* Used to resolve the | |
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | |
* of values. | |
*/ | |
var nativeObjectToString = objectProto.toString; | |
/** Built-in value references. */ | |
var symToStringTag = Symbol ? Symbol.toStringTag : undefined; | |
/** | |
* A specialized version of `baseGetTag` which ignores `Symbol.toStringTag` values. | |
* | |
* @private | |
* @param {*} value The value to query. | |
* @returns {string} Returns the raw `toStringTag`. | |
*/ | |
function getRawTag(value) { | |
var isOwn = hasOwnProperty.call(value, symToStringTag), | |
tag = value[symToStringTag]; | |
try { | |
value[symToStringTag] = undefined; | |
var unmasked = true; | |
} catch (e) {} | |
var result = nativeObjectToString.call(value); | |
if (unmasked) { | |
if (isOwn) { | |
value[symToStringTag] = tag; | |
} else { | |
delete value[symToStringTag]; | |
} | |
} | |
return result; | |
} | |
/** Used for built-in method references. */ | |
var objectProto$1 = Object.prototype; | |
/** | |
* Used to resolve the | |
* [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring) | |
* of values. | |
*/ | |
var nativeObjectToString$1 = objectProto$1.toString; | |
/** | |
* Converts `value` to a string using `Object.prototype.toString`. | |
* | |
* @private | |
* @param {*} value The value to convert. | |
* @returns {string} Returns the converted string. | |
*/ | |
function objectToString(value) { | |
return nativeObjectToString$1.call(value); | |
} | |
/** `Object#toString` result references. */ | |
var nullTag = '[object Null]', | |
undefinedTag = '[object Undefined]'; | |
/** Built-in value references. */ | |
var symToStringTag$1 = Symbol ? Symbol.toStringTag : undefined; | |
/** | |
* The base implementation of `getTag` without fallbacks for buggy environments. | |
* | |
* @private | |
* @param {*} value The value to query. | |
* @returns {string} Returns the `toStringTag`. | |
*/ | |
function baseGetTag(value) { | |
if (value == null) { | |
return value === undefined ? undefinedTag : nullTag; | |
} | |
return (symToStringTag$1 && symToStringTag$1 in Object(value)) | |
? getRawTag(value) | |
: objectToString(value); | |
} | |
/** | |
* Checks if `value` is object-like. A value is object-like if it's not `null` | |
* and has a `typeof` result of "object". | |
* | |
* @static | |
* @memberOf _ | |
* @since 4.0.0 | |
* @category Lang | |
* @param {*} value The value to check. | |
* @returns {boolean} Returns `true` if `value` is object-like, else `false`. | |
* @example | |
* | |
* _.isObjectLike({}); | |
* // => true | |
* | |
* _.isObjectLike([1, 2, 3]); | |
* // => true | |
* | |
* _.isObjectLike(_.noop); | |
* // => false | |
* | |
* _.isObjectLike(null); | |
* // => false | |
*/ | |
function isObjectLike(value) { | |
return value != null && typeof value == 'object'; | |
} | |
/** `Object#toString` result references. */ | |
var symbolTag = '[object Symbol]'; | |
/** | |
* Checks if `value` is classified as a `Symbol` primitive or object. | |
* | |
* @static | |
* @memberOf _ | |
* @since 4.0.0 | |
* @category Lang | |
* @param {*} value The value to check. | |
* @returns {boolean} Returns `true` if `value` is a symbol, else `false`. | |
* @example | |
* | |
* _.isSymbol(Symbol.iterator); | |
* // => true | |
* | |
* _.isSymbol('abc'); | |
* // => false | |
*/ | |
function isSymbol(value) { | |
return typeof value == 'symbol' || | |
(isObjectLike(value) && baseGetTag(value) == symbolTag); | |
} | |
/** Used as references for various `Number` constants. */ | |
var NAN = 0 / 0; | |
/** Used to match leading and trailing whitespace. */ | |
var reTrim = /^\s+|\s+$/g; | |
/** Used to detect bad signed hexadecimal string values. */ | |
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; | |
/** Used to detect binary string values. */ | |
var reIsBinary = /^0b[01]+$/i; | |
/** Used to detect octal string values. */ | |
var reIsOctal = /^0o[0-7]+$/i; | |
/** Built-in method references without a dependency on `root`. */ | |
var freeParseInt = parseInt; | |
/** | |
* Converts `value` to a number. | |
* | |
* @static | |
* @memberOf _ | |
* @since 4.0.0 | |
* @category Lang | |
* @param {*} value The value to process. | |
* @returns {number} Returns the number. | |
* @example | |
* | |
* _.toNumber(3.2); | |
* // => 3.2 | |
* | |
* _.toNumber(Number.MIN_VALUE); | |
* // => 5e-324 | |
* | |
* _.toNumber(Infinity); | |
* // => Infinity | |
* | |
* _.toNumber('3.2'); | |
* // => 3.2 | |
*/ | |
function toNumber(value) { | |
if (typeof value == 'number') { | |
return value; | |
} | |
if (isSymbol(value)) { | |
return NAN; | |
} | |
if (isObject(value)) { | |
var other = typeof value.valueOf == 'function' ? value.valueOf() : value; | |
value = isObject(other) ? (other + '') : other; | |
} | |
if (typeof value != 'string') { | |
return value === 0 ? value : +value; | |
} | |
value = value.replace(reTrim, ''); | |
var isBinary = reIsBinary.test(value); | |
return (isBinary || reIsOctal.test(value)) | |
? freeParseInt(value.slice(2), isBinary ? 2 : 8) | |
: (reIsBadHex.test(value) ? NAN : +value); | |
} | |
/** Error message constants. */ | |
var FUNC_ERROR_TEXT = 'Expected a function'; | |
/* Built-in method references for those with the same name as other `lodash` methods. */ | |
var nativeMax = Math.max, | |
nativeMin = Math.min; | |
/** | |
* Creates a debounced function that delays invoking `func` until after `wait` | |
* milliseconds have elapsed since the last time the debounced function was | |
* invoked. The debounced function comes with a `cancel` method to cancel | |
* delayed `func` invocations and a `flush` method to immediately invoke them. | |
* Provide `options` to indicate whether `func` should be invoked on the | |
* leading and/or trailing edge of the `wait` timeout. The `func` is invoked | |
* with the last arguments provided to the debounced function. Subsequent | |
* calls to the debounced function return the result of the last `func` | |
* invocation. | |
* | |
* **Note:** If `leading` and `trailing` options are `true`, `func` is | |
* invoked on the trailing edge of the timeout only if the debounced function | |
* is invoked more than once during the `wait` timeout. | |
* | |
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | |
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | |
* | |
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | |
* for details over the differences between `_.debounce` and `_.throttle`. | |
* | |
* @static | |
* @memberOf _ | |
* @since 0.1.0 | |
* @category Function | |
* @param {Function} func The function to debounce. | |
* @param {number} [wait=0] The number of milliseconds to delay. | |
* @param {Object} [options={}] The options object. | |
* @param {boolean} [options.leading=false] | |
* Specify invoking on the leading edge of the timeout. | |
* @param {number} [options.maxWait] | |
* The maximum time `func` is allowed to be delayed before it's invoked. | |
* @param {boolean} [options.trailing=true] | |
* Specify invoking on the trailing edge of the timeout. | |
* @returns {Function} Returns the new debounced function. | |
* @example | |
* | |
* // Avoid costly calculations while the window size is in flux. | |
* jQuery(window).on('resize', _.debounce(calculateLayout, 150)); | |
* | |
* // Invoke `sendMail` when clicked, debouncing subsequent calls. | |
* jQuery(element).on('click', _.debounce(sendMail, 300, { | |
* 'leading': true, | |
* 'trailing': false | |
* })); | |
* | |
* // Ensure `batchLog` is invoked once after 1 second of debounced calls. | |
* var debounced = _.debounce(batchLog, 250, { 'maxWait': 1000 }); | |
* var source = new EventSource('/stream'); | |
* jQuery(source).on('message', debounced); | |
* | |
* // Cancel the trailing debounced invocation. | |
* jQuery(window).on('popstate', debounced.cancel); | |
*/ | |
function debounce(func, wait, options) { | |
var lastArgs, | |
lastThis, | |
maxWait, | |
result, | |
timerId, | |
lastCallTime, | |
lastInvokeTime = 0, | |
leading = false, | |
maxing = false, | |
trailing = true; | |
if (typeof func != 'function') { | |
throw new TypeError(FUNC_ERROR_TEXT); | |
} | |
wait = toNumber(wait) || 0; | |
if (isObject(options)) { | |
leading = !!options.leading; | |
maxing = 'maxWait' in options; | |
maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait; | |
trailing = 'trailing' in options ? !!options.trailing : trailing; | |
} | |
function invokeFunc(time) { | |
var args = lastArgs, | |
thisArg = lastThis; | |
lastArgs = lastThis = undefined; | |
lastInvokeTime = time; | |
result = func.apply(thisArg, args); | |
return result; | |
} | |
function leadingEdge(time) { | |
// Reset any `maxWait` timer. | |
lastInvokeTime = time; | |
// Start the timer for the trailing edge. | |
timerId = setTimeout(timerExpired, wait); | |
// Invoke the leading edge. | |
return leading ? invokeFunc(time) : result; | |
} | |
function remainingWait(time) { | |
var timeSinceLastCall = time - lastCallTime, | |
timeSinceLastInvoke = time - lastInvokeTime, | |
timeWaiting = wait - timeSinceLastCall; | |
return maxing | |
? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) | |
: timeWaiting; | |
} | |
function shouldInvoke(time) { | |
var timeSinceLastCall = time - lastCallTime, | |
timeSinceLastInvoke = time - lastInvokeTime; | |
// Either this is the first call, activity has stopped and we're at the | |
// trailing edge, the system time has gone backwards and we're treating | |
// it as the trailing edge, or we've hit the `maxWait` limit. | |
return (lastCallTime === undefined || (timeSinceLastCall >= wait) || | |
(timeSinceLastCall < 0) || (maxing && timeSinceLastInvoke >= maxWait)); | |
} | |
function timerExpired() { | |
var time = now(); | |
if (shouldInvoke(time)) { | |
return trailingEdge(time); | |
} | |
// Restart the timer. | |
timerId = setTimeout(timerExpired, remainingWait(time)); | |
} | |
function trailingEdge(time) { | |
timerId = undefined; | |
// Only invoke if we have `lastArgs` which means `func` has been | |
// debounced at least once. | |
if (trailing && lastArgs) { | |
return invokeFunc(time); | |
} | |
lastArgs = lastThis = undefined; | |
return result; | |
} | |
function cancel() { | |
if (timerId !== undefined) { | |
clearTimeout(timerId); | |
} | |
lastInvokeTime = 0; | |
lastArgs = lastCallTime = lastThis = timerId = undefined; | |
} | |
function flush() { | |
return timerId === undefined ? result : trailingEdge(now()); | |
} | |
function debounced() { | |
var time = now(), | |
isInvoking = shouldInvoke(time); | |
lastArgs = arguments; | |
lastThis = this; | |
lastCallTime = time; | |
if (isInvoking) { | |
if (timerId === undefined) { | |
return leadingEdge(lastCallTime); | |
} | |
if (maxing) { | |
// Handle invocations in a tight loop. | |
timerId = setTimeout(timerExpired, wait); | |
return invokeFunc(lastCallTime); | |
} | |
} | |
if (timerId === undefined) { | |
timerId = setTimeout(timerExpired, wait); | |
} | |
return result; | |
} | |
debounced.cancel = cancel; | |
debounced.flush = flush; | |
return debounced; | |
} | |
/** Error message constants. */ | |
var FUNC_ERROR_TEXT$1 = 'Expected a function'; | |
/** | |
* Creates a throttled function that only invokes `func` at most once per | |
* every `wait` milliseconds. The throttled function comes with a `cancel` | |
* method to cancel delayed `func` invocations and a `flush` method to | |
* immediately invoke them. Provide `options` to indicate whether `func` | |
* should be invoked on the leading and/or trailing edge of the `wait` | |
* timeout. The `func` is invoked with the last arguments provided to the | |
* throttled function. Subsequent calls to the throttled function return the | |
* result of the last `func` invocation. | |
* | |
* **Note:** If `leading` and `trailing` options are `true`, `func` is | |
* invoked on the trailing edge of the timeout only if the throttled function | |
* is invoked more than once during the `wait` timeout. | |
* | |
* If `wait` is `0` and `leading` is `false`, `func` invocation is deferred | |
* until to the next tick, similar to `setTimeout` with a timeout of `0`. | |
* | |
* See [David Corbacho's article](https://css-tricks.com/debouncing-throttling-explained-examples/) | |
* for details over the differences between `_.throttle` and `_.debounce`. | |
* | |
* @static | |
* @memberOf _ | |
* @since 0.1.0 | |
* @category Function | |
* @param {Function} func The function to throttle. | |
* @param {number} [wait=0] The number of milliseconds to throttle invocations to. | |
* @param {Object} [options={}] The options object. | |
* @param {boolean} [options.leading=true] | |
* Specify invoking on the leading edge of the timeout. | |
* @param {boolean} [options.trailing=true] | |
* Specify invoking on the trailing edge of the timeout. | |
* @returns {Function} Returns the new throttled function. | |
* @example | |
* | |
* // Avoid excessively updating the position while scrolling. | |
* jQuery(window).on('scroll', _.throttle(updatePosition, 100)); | |
* | |
* // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes. | |
* var throttled = _.throttle(renewToken, 300000, { 'trailing': false }); | |
* jQuery(element).on('click', throttled); | |
* | |
* // Cancel the trailing throttled invocation. | |
* jQuery(window).on('popstate', throttled.cancel); | |
*/ | |
function throttle(func, wait, options) { | |
var leading = true, | |
trailing = true; | |
if (typeof func != 'function') { | |
throw new TypeError(FUNC_ERROR_TEXT$1); | |
} | |
if (isObject(options)) { | |
leading = 'leading' in options ? !!options.leading : leading; | |
trailing = 'trailing' in options ? !!options.trailing : trailing; | |
} | |
return debounce(func, wait, { | |
'leading': leading, | |
'maxWait': wait, | |
'trailing': trailing | |
}); | |
} | |
// Modified from https://github.com/jackmoore/autosize/blob/113f1b3/src/autosize.js | |
const map = new Map(); | |
let createEvent = (name) => new Event(name, {bubbles: true}); | |
function assign (ta) { | |
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) { | |
return | |
} | |
let heightOffset = null; | |
let cachedHeight = null; | |
function init () { | |
const style = window.getComputedStyle(ta, null); | |
if (style.boxSizing === 'content-box') { | |
heightOffset = -(parseFloat(style.paddingTop) + parseFloat(style.paddingBottom)); | |
} else { | |
heightOffset = parseFloat(style.borderTopWidth) + parseFloat(style.borderBottomWidth); | |
} | |
update(); | |
} | |
function resize () { | |
mark('autosize:resize()'); | |
let res = _resize(); | |
stop('autosize:resize()'); | |
return res | |
} | |
function _resize () { | |
const originalHeight = ta.style.height; | |
ta.style.height = ''; // this may change the scrollTop in Firefox | |
const 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'; | |
return endHeight | |
} | |
const deferredUpdate = throttle(() => requestAnimationFrame(update), 100); | |
function update () { | |
mark('autosize:update()'); | |
_update(); | |
stop('autosize:update()'); | |
} | |
function _update () { | |
let newHeight = resize(); | |
if (cachedHeight !== newHeight) { | |
cachedHeight = newHeight; | |
const evt = createEvent('autosize:resized'); | |
try { | |
ta.dispatchEvent(evt); | |
} catch (err) { | |
// Firefox will throw an error on dispatchEvent for a detached element | |
// https://bugzilla.mozilla.org/show_bug.cgi?id=889376 | |
} | |
} | |
} | |
const pageResize = debounce(() => requestAnimationFrame(update), 1000); | |
const destroy = () => { | |
window.removeEventListener('resize', pageResize, false); | |
ta.removeEventListener('input', deferredUpdate, false); | |
ta.removeEventListener('autosize:destroy', destroy, false); | |
ta.removeEventListener('autosize:update', update, false); | |
map.delete(ta); | |
}; | |
ta.addEventListener('autosize:destroy', destroy, false); | |
window.addEventListener('resize', pageResize, false); | |
ta.addEventListener('input', deferredUpdate, false); | |
ta.addEventListener('autosize:update', update, false); | |
map.set(ta, { | |
destroy, | |
update | |
}); | |
init(); | |
} | |
function destroy (ta) { | |
const methods = map.get(ta); | |
if (methods) { | |
methods.destroy(); | |
} | |
} | |
function update (ta) { | |
const methods = map.get(ta); | |
if (methods) { | |
methods.update(); | |
} | |
} | |
let autosize = (el, options) => { | |
if (el) { | |
Array.prototype.forEach.call(el.length ? el : [el], x => assign(x, options)); | |
} | |
return el | |
}; | |
autosize.destroy = el => { | |
if (el) { | |
Array.prototype.forEach.call(el.length ? el : [el], destroy); | |
} | |
return el | |
}; | |
autosize.update = el => { | |
if (el) { | |
Array.prototype.forEach.call(el.length ? el : [el], update); | |
} | |
return el | |
}; | |
exports.autosize = autosize; | |
Object.defineProperty(exports, '__esModule', { value: true }); | |
}))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<title>Repro Firefox textarea autosize bug</title> | |
<style> | |
body { | |
max-width: 600px; | |
margin: 0 auto; | |
padding: 20px; | |
} | |
textarea { | |
font-size: 16px; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Repro Firefox textarea autosize bug</h1> | |
<h2>Scroll to the bottom and type anywhere into this textarea:</h2> | |
<textarea id="theTextarea"></textarea> | |
<script src="autosize.js"></script> | |
<script> | |
(function () { | |
'use strict' | |
let textarea = document.getElementById('theTextarea') | |
textarea.value = 'Show me the way to go home\n\nI\'m tired and I want to go to bed\n\nI had a little drink about an hour ago\n\nAnd it\'s gone right to my head\n\nEverywhere I roam\n\nOver land or sea or foam\n\nYou can always hear me singing this song\n\nShow me the way to go home.\n\nShow me the way to go home\n\nI\'m tired and I want to go to bed\n\nI had a little drink about an hour ago\n\nAnd it\'s gone right to my head\n\nEverywhere that I roam\n\nOver land or sea or foam\n\nYou can always hear me singing this song\n\nShow me the way to go home.\n\nShow me the way to go home\n\nI\'m tired and I want to go to bed\n\nI had a little drink about an hour ago\n\nAnd it\'s gone right to my head\n\nEverywhere I roam\n\nOver land or sea or foam\n\nYou can always hear me singing this song\n\nShow me the way to go home.\n\nShow me the way to go home\n\nI\'m tired and I want to go to bed\n\nI had a little drink about an hour ago\n\nAnd it\'s gone right to my head\n\nEverywhere that I roam\n\nOver land or sea or foam\n\nYou can always hear me singing this song\n\nShow me the way to go home.\n\n' | |
autosize.autosize(textarea) | |
})() | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment