From 0c63472b68b1b3ee4a4ca23de9a75f941beb7254 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Thu, 11 May 2017 00:28:43 +0000 Subject: [PATCH] Editor: manually trigger `change` event when text is modified from JS. Props westonruter. Fixes #40726. git-svn-id: https://develop.svn.wordpress.org/trunk@40615 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/js/quicktags.js | 21 +++++++++++++++++++-- src/wp-includes/js/wplink.js | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/js/quicktags.js b/src/wp-includes/js/quicktags.js index f8e30d39b5..e75f0de5d8 100644 --- a/src/wp-includes/js/quicktags.js +++ b/src/wp-includes/js/quicktags.js @@ -406,7 +406,7 @@ function edButton(id, display, tagStart, tagEnd, access) { }; qt.insertContent = function(content) { - var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor); + var sel, startPos, endPos, scrollTop, text, canvas = document.getElementById(wpActiveEditor), event; if ( !canvas ) { return false; @@ -433,6 +433,15 @@ function edButton(id, display, tagStart, tagEnd, access) { canvas.value += content; canvas.focus(); } + + if ( document.createEvent ) { + event = document.createEvent( 'HTMLEvents' ); + event.initEvent( 'change', false, true ); + canvas.dispatchEvent( event ); + } else if ( canvas.fireEvent ) { + canvas.fireEvent( 'onchange' ); + } + return true; }; @@ -515,7 +524,7 @@ function edButton(id, display, tagStart, tagEnd, access) { return ret; }; qt.TagButton.prototype.callback = function(element, canvas, ed) { - var t = this, startPos, endPos, cursorPos, scrollTop, v = canvas.value, l, r, i, sel, endTag = v ? t.tagEnd : ''; + var t = this, startPos, endPos, cursorPos, scrollTop, v = canvas.value, l, r, i, sel, endTag = v ? t.tagEnd : '', event; if ( document.selection ) { // IE canvas.focus(); @@ -590,6 +599,14 @@ function edButton(id, display, tagStart, tagEnd, access) { } canvas.focus(); } + + if ( document.createEvent ) { + event = document.createEvent( 'HTMLEvents' ); + event.initEvent( 'change', false, true ); + canvas.dispatchEvent( event ); + } else if ( canvas.fireEvent ) { + canvas.fireEvent( 'onchange' ); + } }; // removed diff --git a/src/wp-includes/js/wplink.js b/src/wp-includes/js/wplink.js index 4a5b1557af..652ed688a3 100644 --- a/src/wp-includes/js/wplink.js +++ b/src/wp-includes/js/wplink.js @@ -380,6 +380,7 @@ var wpLink; wpLink.close(); textarea.focus(); + $( textarea ).trigger( 'change' ); // Audible confirmation message when a link has been inserted in the Editor. wp.a11y.speak( wpLinkL10n.linkInserted );