Skip to content

Commit

Permalink
Fixed Firefox issue where calling dispatchEvent on a detached element…
Browse files Browse the repository at this point in the history
… throws an error. Fixes jackmoore#317.
  • Loading branch information
jackmoore committed Oct 26, 2016
1 parent 3838f19 commit fa2fa30
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function lint(full) {
eqeqeq: true,
eqnull: true,
noarg: true,
predef: ['define', 'module', 'exports', 'Set']
predef: ['define', 'module', 'exports', 'Map']
});

if (jshint.errors.length) {
Expand Down
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Changelog

##### v.3.0.18 - 2016-10-26
* Fixed Firefox issue where calling dispatchEvent on a detached element throws an error. Fixes #317.

##### v.3.0.17 - 2016-7-25
* Fixed Chromium issue where getComputedStyle pixel value did not exactly match the style pixel value. Fixes #306.
* Removed undocumented argument, minor refactoring, more comments.
Expand Down
58 changes: 40 additions & 18 deletions dist/autosize.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
Autosize 3.0.17
Autosize 3.0.18
license: MIT
http://www.jacklmoore.com/autosize
*/
Expand All @@ -18,18 +18,29 @@
})(this, function (exports, module) {
'use strict';

var set = typeof Set === 'function' ? new Set() : (function () {
var list = [];
var map = typeof Map === 'function' ? new Map() : (function () {
var keys = [];
var values = [];

return {
has: function has(key) {
return Boolean(list.indexOf(key) > -1);
return keys.indexOf(key) > -1;
},
add: function add(key) {
list.push(key);
get: function get(key) {
return values[keys.indexOf(key)];
},
set: function set(key, value) {
if (keys.indexOf(key) === -1) {
keys.push(key);
values.push(value);
}
},
'delete': function _delete(key) {
list.splice(list.indexOf(key), 1);
var index = keys.indexOf(key);
if (index > -1) {
keys.splice(index, 1);
values.splice(index, 1);
}
} };
})();

Expand All @@ -48,7 +59,7 @@
}

function assign(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return;
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return;

var heightOffset = null;
var clientWidth = ta.clientWidth;
Expand Down Expand Up @@ -163,7 +174,9 @@
if (cachedHeight !== computedHeight) {
cachedHeight = computedHeight;
var evt = createEvent('autosize:resized');
ta.dispatchEvent(evt);
try {
ta.dispatchEvent(evt);
} catch (err) {}
}
}

Expand All @@ -179,11 +192,12 @@
ta.removeEventListener('keyup', update, false);
ta.removeEventListener('autosize:destroy', destroy, false);
ta.removeEventListener('autosize:update', update, false);
set['delete'](ta);

Object.keys(style).forEach(function (key) {
ta.style[key] = style[key];
});

map['delete'](ta);
}).bind(ta, {
height: ta.style.height,
resize: ta.style.resize,
Expand All @@ -203,23 +217,28 @@
window.addEventListener('resize', pageResize, false);
ta.addEventListener('input', update, false);
ta.addEventListener('autosize:update', update, false);
set.add(ta);
ta.style.overflowX = 'hidden';
ta.style.wordWrap = 'break-word';

map.set(ta, {
destroy: destroy,
update: update });

init();
}

function destroy(ta) {
if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
var evt = createEvent('autosize:destroy');
ta.dispatchEvent(evt);
var methods = map.get(ta);
if (methods) {
methods.destroy();
}
}

function update(ta) {
if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
var evt = createEvent('autosize:update');
ta.dispatchEvent(evt);
var methods = map.get(ta);
if (methods) {
methods.update();
}
}

var autosize = null;
Expand Down Expand Up @@ -259,4 +278,7 @@
}

module.exports = autosize;
});
});

// Firefox will throw an error on dispatchEvent for a detached element
// https://bugzilla.mozilla.org/show_bug.cgi?id=889376
4 changes: 2 additions & 2 deletions dist/autosize.min.js

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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.17",
"version": "3.0.18",
"keywords": [
"textarea",
"form",
Expand Down
55 changes: 39 additions & 16 deletions src/autosize.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
const set = (typeof Set === "function") ? new Set() : (function () {
const list = [];
const map = (typeof Map === "function") ? new Map() : (function () {
const keys = [];
const values = [];

return {
has(key) {
return Boolean(list.indexOf(key) > -1);
return keys.indexOf(key) > -1;
},
add(key) {
list.push(key);
get(key) {
return values[keys.indexOf(key)];
},
set(key, value) {
if (keys.indexOf(key) === -1) {
keys.push(key);
values.push(value);
}
},
delete(key) {
list.splice(list.indexOf(key), 1);
const index = keys.indexOf(key);
if (index > -1) {
keys.splice(index, 1);
values.splice(index, 1);
}
},
}
})();
Expand All @@ -27,7 +38,7 @@ try {
}

function assign(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || set.has(ta)) return;
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || map.has(ta)) return;

let heightOffset = null;
let clientWidth = ta.clientWidth;
Expand Down Expand Up @@ -143,7 +154,12 @@ function assign(ta) {
if (cachedHeight !== computedHeight) {
cachedHeight = computedHeight;
const evt = createEvent('autosize:resized');
ta.dispatchEvent(evt);
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
}
}
}

Expand All @@ -159,11 +175,12 @@ function assign(ta) {
ta.removeEventListener('keyup', update, false);
ta.removeEventListener('autosize:destroy', destroy, false);
ta.removeEventListener('autosize:update', update, false);
set.delete(ta);

Object.keys(style).forEach(key => {
ta.style[key] = style[key];
});

map.delete(ta);
}.bind(ta, {
height: ta.style.height,
resize: ta.style.resize,
Expand All @@ -184,23 +201,29 @@ function assign(ta) {
window.addEventListener('resize', pageResize, false);
ta.addEventListener('input', update, false);
ta.addEventListener('autosize:update', update, false);
set.add(ta);
ta.style.overflowX = 'hidden';
ta.style.wordWrap = 'break-word';

map.set(ta, {
destroy,
update,
});

init();
}

function destroy(ta) {
if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
const evt = createEvent('autosize:destroy');
ta.dispatchEvent(evt);
const methods = map.get(ta);
if (methods) {
methods.destroy();
}
}

function update(ta) {
if (!(ta && ta.nodeName && ta.nodeName === 'TEXTAREA')) return;
const evt = createEvent('autosize:update');
ta.dispatchEvent(evt);
const methods = map.get(ta);
if (methods) {
methods.update();
}
}

let autosize = null;
Expand Down

0 comments on commit fa2fa30

Please sign in to comment.