* Copyright, Moxiecode Systems AB
* Released under LGPL License.
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
// Forked for WordPress so it can be turned on/off after loading.
/*eslint no-nested-ternary:0 */
* This plugin automatically resizes the content area to fit its content height.
* It will retain a minimum height, which is the height of the content area when
tinymce.PluginManager.add( 'wpautoresize', function( editor ) {
var settings = editor.settings,
if ( editor.settings.inline || tinymce.Env.iOS ) {
function isFullscreen() {
return editor.plugins.fullscreen && editor.plugins.fullscreen.isFullscreen();
return parseInt( n, 10 ) || 0;
* This method gets executed each time the editor needs to resize.
var deltaSize, doc, body, docElm, DOM = tinymce.DOM, resizeHeight, myHeight,
marginTop, marginBottom, paddingTop, paddingBottom, borderTop, borderBottom;
docElm = doc.documentElement;
resizeHeight = settings.autoresize_min_height;
if ( ! body || ( e && e.type === 'setcontent' && e.initial ) || isFullscreen() ) {
body.style.overflowY = 'auto';
docElm.style.overflowY = 'auto'; // Old IE.
// Calculate outer height of the body element using CSS styles.
marginTop = editor.dom.getStyle( body, 'margin-top', true );
marginBottom = editor.dom.getStyle( body, 'margin-bottom', true );
paddingTop = editor.dom.getStyle( body, 'padding-top', true );
paddingBottom = editor.dom.getStyle( body, 'padding-bottom', true );
borderTop = editor.dom.getStyle( body, 'border-top-width', true );
borderBottom = editor.dom.getStyle( body, 'border-bottom-width', true );
myHeight = body.offsetHeight + getInt( marginTop ) + getInt( marginBottom ) +
getInt( paddingTop ) + getInt( paddingBottom ) +
getInt( borderTop ) + getInt( borderBottom );
if ( myHeight && myHeight < docElm.offsetHeight ) {
myHeight = docElm.offsetHeight;
// Make sure we have a valid height.
if ( isNaN( myHeight ) || myHeight <= 0 ) {
// Get height differently depending on the browser used.
myHeight = tinymce.Env.ie ? body.scrollHeight : ( tinymce.Env.webkit && body.clientHeight === 0 ? 0 : body.offsetHeight );
// Don't make it smaller than the minimum height.
if ( myHeight > settings.autoresize_min_height ) {
// If a maximum height has been defined don't exceed this height.
if ( settings.autoresize_max_height && myHeight > settings.autoresize_max_height ) {
resizeHeight = settings.autoresize_max_height;
body.style.overflowY = 'auto';
docElm.style.overflowY = 'auto'; // Old IE.
body.style.overflowY = 'hidden';
docElm.style.overflowY = 'hidden'; // Old IE.
// Resize content element.
if (resizeHeight !== oldSize) {
deltaSize = resizeHeight - oldSize;
DOM.setStyle( editor.iframeElement, 'height', resizeHeight + 'px' );
// WebKit doesn't decrease the size of the body element until the iframe gets resized.
// So we need to continue to resize the iframe down until the size gets fixed.
if ( tinymce.isWebKit && deltaSize < 0 ) {
editor.fire( 'wp-autoresize', { height: resizeHeight, deltaHeight: e.type === 'nodechange' ? deltaSize : null } );
* Calls the resize x times in 100ms intervals. We can't wait for load events since
* the CSS files might load async.
function wait( times, interval, callback ) {
wait( times, interval, callback );
// Define minimum height.
settings.autoresize_min_height = parseInt(editor.getParam( 'autoresize_min_height', editor.getElement().offsetHeight), 10 );
// Define maximum height.
settings.autoresize_max_height = parseInt(editor.getParam( 'autoresize_max_height', 0), 10 );
if ( ! editor.dom.hasClass( editor.getBody(), 'wp-autoresize' ) ) {
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
// Add appropriate listeners for resizing the content area.
editor.on( 'nodechange setcontent keyup FullscreenStateChanged', resize );
// Don't turn off if the setting is 'on'.
if ( ! settings.wp_autoresize_on ) {
editor.dom.removeClass( editor.getBody(), 'wp-autoresize' );
editor.off( 'nodechange setcontent keyup FullscreenStateChanged', resize );
doc.body.style.overflowY = 'auto';
doc.documentElement.style.overflowY = 'auto'; // Old IE.
if ( settings.wp_autoresize_on ) {
// Turn resizing on when the editor loads.
editor.on( 'init', function() {
editor.dom.addClass( editor.getBody(), 'wp-autoresize' );
editor.on( 'nodechange keyup FullscreenStateChanged', resize );
editor.on( 'setcontent', function() {
if ( editor.getParam( 'autoresize_on_init', true ) ) {
editor.on( 'init', function() {
// Hit it 10 times in 200 ms intervals.
wait( 10, 200, function() {
// Hit it 5 times in 1 sec intervals.
// Reset the stored size.
editor.on( 'show', function() {
editor.addCommand( 'wpAutoResize', resize );
editor.addCommand( 'wpAutoResizeOn', on );
editor.addCommand( 'wpAutoResizeOff', off );