From e366658072aa19374a79c0cd2e39be38b22d4cab Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Sun, 21 Jun 2015 22:00:42 +0000 Subject: [PATCH] Update the TinyMCE initialization: - Replace `wp_htmledit_pre()` and `wp_richedit_pre()` with `format_for_editor()`. - Replace the `'htmledit_pre'` and `'richedit_pre'` filters with `'format_for_editor'`. - Do not run the post content through `wpautop()` in PHP when the visual editor is default. Run the textarea content through the JS wpautop on initializing TinyMCE. - Simplify both editors initialization. - Improve setting of `wpActiveEditor` in Quicktags. - Improve editor.js, use `tinymce.$` when possible. See #32425. git-svn-id: https://develop.svn.wordpress.org/trunk@32899 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/css/edit.css | 4 + src/wp-admin/js/editor.js | 576 ++++++++++-------- src/wp-includes/class-wp-editor.php | 197 +++--- src/wp-includes/css/editor.css | 2 +- src/wp-includes/default-filters.php | 3 + src/wp-includes/deprecated.php | 72 +++ src/wp-includes/formatting.php | 67 +- src/wp-includes/js/quicktags.js | 33 +- .../js/tinymce/plugins/wordpress/plugin.js | 88 +-- 9 files changed, 577 insertions(+), 465 deletions(-) diff --git a/src/wp-admin/css/edit.css b/src/wp-admin/css/edit.css index afa6909120..bd87fe9e40 100644 --- a/src/wp-admin/css/edit.css +++ b/src/wp-admin/css/edit.css @@ -425,6 +425,10 @@ td.plugin-title p { visibility: visible !important; } +.mce-fullscreen #wp-content-wrap .mce-tinymce .mce-wp-dfw { + display: none; +} + .post-php.mce-fullscreen #wpadminbar, .mce-fullscreen #wp-content-wrap .mce-wp-dfw { display: none; diff --git a/src/wp-admin/js/editor.js b/src/wp-admin/js/editor.js index 4ff70df848..3d56e36bd4 100644 --- a/src/wp-admin/js/editor.js +++ b/src/wp-admin/js/editor.js @@ -1,38 +1,29 @@ -/* global tinymce, tinyMCEPreInit, QTags, setUserSetting */ -window.switchEditors = { +( function( $ ) { + function SwitchEditors() { + var tinymce, $$, + exports = {}; - switchto: function( el ) { - var aid = el.id, - l = aid.length, - id = aid.substr( 0, l - 5 ), - mode = aid.substr( l - 4 ); + function init() { + if ( ! tinymce && window.tinymce ) { + tinymce = window.tinymce; + $$ = tinymce.$; - this.go( id, mode ); - }, + $$( document ).on( 'click', function( event ) { + var id, mode, + target = $$( event.target ); - // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab. - go: function( id, mode ) { - var t = this, ed, wrap_id, txtarea_el, iframe, editorHeight, toolbarHeight, - DOM = tinymce.DOM; //DOMUtils outside the editor iframe - - id = id || 'content'; - mode = mode || 'toggle'; - - ed = tinymce.get( id ); - wrap_id = 'wp-' + id + '-wrap'; - txtarea_el = DOM.get( id ); - - if ( 'toggle' === mode ) { - if ( ed && ! ed.isHidden() ) { - mode = 'html'; - } else { - mode = 'tmce'; + if ( target.hasClass( 'wp-switch-editor' ) ) { + id = target.attr( 'data-wp-editor-id' ); + mode = target.hasClass( 'switch-tmce' ) ? 'tmce' : 'html'; + switchEditor( id, mode ); + } + }); } } - function getToolbarHeight() { - var node = DOM.select( '.mce-toolbar-grp', ed.getContainer() )[0], + function getToolbarHeight( editor ) { + var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0], height = node && node.clientHeight; if ( height && height > 10 && height < 200 ) { @@ -42,283 +33,332 @@ window.switchEditors = { return 30; } - if ( 'tmce' === mode || 'tinymce' === mode ) { - if ( ed && ! ed.isHidden() ) { - return false; - } + function switchEditor( id, mode ) { + id = id || 'content'; + mode = mode || 'toggle'; - if ( typeof( QTags ) !== 'undefined' ) { - QTags.closeAllTags( id ); - } + var editorHeight, toolbarHeight, iframe, + editor = tinymce.get( id ), + wrap = $$( '#wp-' + id + '-wrap' ), + $textarea = $$( '#' + id ), + textarea = $textarea[0]; - editorHeight = txtarea_el ? parseInt( txtarea_el.style.height, 10 ) : 0; - - if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) { - txtarea_el.value = t.wpautop( txtarea_el.value ); - } - - if ( ed ) { - ed.show(); - - // No point resizing the iframe in iOS - if ( ! tinymce.Env.iOS && editorHeight ) { - toolbarHeight = getToolbarHeight(); - editorHeight = editorHeight - toolbarHeight + 14; - - // height cannot be under 50 or over 5000 - if ( editorHeight > 50 && editorHeight < 5000 ) { - ed.theme.resizeTo( null, editorHeight ); - } + if ( 'toggle' === mode ) { + if ( editor && ! editor.isHidden() ) { + mode = 'html'; + } else { + mode = 'tmce'; } - } else { - tinymce.init( tinyMCEPreInit.mceInit[id] ); } - DOM.removeClass( wrap_id, 'html-active' ); - DOM.addClass( wrap_id, 'tmce-active' ); - DOM.setAttrib( txtarea_el, 'aria-hidden', true ); - setUserSetting( 'editor', 'tinymce' ); + if ( 'tmce' === mode || 'tinymce' === mode ) { + if ( editor && ! editor.isHidden() ) { + return false; + } - } else if ( 'html' === mode ) { + if ( typeof( window.QTags ) !== 'undefined' ) { + window.QTags.closeAllTags( id ); + } - if ( ed && ed.isHidden() ) { - return false; - } + editorHeight = parseInt( textarea.style.height, 10 ) || 0; - if ( ed ) { - if ( ! tinymce.Env.iOS ) { - iframe = DOM.get( id + '_ifr' ); - editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0; + if ( editor ) { + editor.show(); - if ( editorHeight ) { - toolbarHeight = getToolbarHeight(); - editorHeight = editorHeight + toolbarHeight - 14; + // No point resizing the iframe in iOS + if ( ! tinymce.Env.iOS && editorHeight ) { + toolbarHeight = getToolbarHeight( editor ); + editorHeight = editorHeight - toolbarHeight + 14; // height cannot be under 50 or over 5000 if ( editorHeight > 50 && editorHeight < 5000 ) { - txtarea_el.style.height = editorHeight + 'px'; + editor.theme.resizeTo( null, editorHeight ); } } + } else { + tinymce.init( window.tinyMCEPreInit.mceInit[id] ); } - ed.hide(); - } else { - // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea - if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) { - txtarea_el.value = t.pre_wpautop( txtarea_el.value ); + wrap.removeClass( 'html-active' ).addClass( 'tmce-active' ); + $textarea.attr( 'aria-hidden', true ); + window.setUserSetting( 'editor', 'tinymce' ); + + } else if ( 'html' === mode ) { + if ( editor && editor.isHidden() ) { + return false; } - DOM.setStyles( txtarea_el, {'display': '', 'visibility': ''} ); + if ( editor ) { + if ( ! tinymce.Env.iOS ) { + iframe = editor.iframeElement; + editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0; + + if ( editorHeight ) { + toolbarHeight = getToolbarHeight( editor ); + editorHeight = editorHeight + toolbarHeight - 14; + + // height cannot be under 50 or over 5000 + if ( editorHeight > 50 && editorHeight < 5000 ) { + textarea.style.height = editorHeight + 'px'; + } + } + } + + editor.hide(); + } else { + // The TinyMCE instance doesn't exist, show the textarea + $textarea.css({ 'display': '', 'visibility': '' }); + } + + wrap.removeClass( 'tmce-active' ).addClass( 'html-active' ); + $textarea.attr( 'aria-hidden', false ); + window.setUserSetting( 'editor', 'html' ); + } + } + + // Replace paragraphs with double line breaks + function removep( html ) { + var blocklist = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset', + blocklist1 = blocklist + '|div|p', + blocklist2 = blocklist + '|pre', + preserve_linebreaks = false, + preserve_br = false; + + if ( ! html ) { + return ''; } - DOM.removeClass( wrap_id, 'tmce-active' ); - DOM.addClass( wrap_id, 'html-active' ); - DOM.setAttrib( txtarea_el, 'aria-hidden', false ); - setUserSetting( 'editor', 'html' ); - } - return false; - }, - - _wp_Nop: function( content ) { - var blocklist1, blocklist2, - preserve_linebreaks = false, - preserve_br = false; - - // Protect pre|script tags - if ( content.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { - a = a.replace( /(\r\n|\n)?/g, '' ); - a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); - return a.replace( /\r?\n/g, '' ); - }); - } - - // keep tags inside captions and remove line breaks - if ( content.indexOf( '[caption' ) !== -1 ) { - preserve_br = true; - content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { - return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); - }); - } - - // Pretty it up for the source editor - blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset'; - content = content.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); - content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); - - // Mark
]*>[\s\S]+?<\/\1>/g, function( a ) { - a = a.replace( /(\r\n|\n)?/g, '' ); - a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); - return a.replace( /\r?\n/g, '' ); - }); - } - - // keep tags inside captions and remove line breaks - if ( content.indexOf( '[caption' ) !== -1 ) { - preserve_br = true; - content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { - return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); - }); - } - - // Pretty it up for the source editor - blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset'; - content = content.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); - content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); - - // Mark
]+>.*?)<\/p>/g, '$1
- content = content.replace( /
/gi, '
and - content = content.replace( /\s*
/gi, '' ); - content = content.replace( /\s*<\/p>\s*/gi, '\n\n' ); - content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); - content = content.replace( /\s*\s*/gi, '\n' ); - - // Fix some block element newline issues - content = content.replace( /\s*
]+>[\s\S]*?<\/p>)/g, '\n$1' ); - - // Trim whitespace - content = content.replace( /^\s+/, '' ); - content = content.replace( /[\s\u00a0]+$/, '' ); - - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - content = content.replace( //g, '\n' ); - } - - // and the tags in captions - if ( preserve_br ) { - content = content.replace( /]*)>/g, '' ); - } - - return content; - }, - - _wp_Autop: function(pee) { - var preserve_linebreaks = false, - preserve_br = false, - blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' + - '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' + - '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; - - if ( pee.indexOf( '/g, function( a ) { - return a.replace( /[\r\n]+/g, '' ); - }); - } - - pee = pee.replace( /<[^<>]+>/g, function( a ){ - return a.replace( /[\r\n]+/g, ' ' ); - }); - - // Protect pre|script tags - if ( pee.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { - return a.replace( /(\r\n|\n)/g, '' ); - }); - } - - // keep tags inside captions and convert line breaks - if ( pee.indexOf( '[caption' ) !== -1 ) { - preserve_br = true; - pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { - // keep existing - a = a.replace( /]*)>/g, '' ); - // no line breaks inside HTML tags - a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) { - return b.replace( /[\r\n\t]+/, ' ' ); + // Protect pre|script tags + if ( html.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { + a = a.replace( /(\r\n|\n)?/g, '' ); + a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); + return a.replace( /\r?\n/g, '' ); }); - // convert remaining line breaks to - return a.replace( /\s*\n\s*/g, '' ); - }); - } - - pee = pee + '\n\n'; - pee = pee.replace( /\s*/gi, '\n\n' ); - pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); - pee = pee.replace( new RegExp( '((?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); - pee = pee.replace( /]*)?>/gi, '\n\n' ); // hr is self closing block element - pee = pee.replace( /\s* or around - pee = pee.replace( /<\/option>\s*/gi, '' ); - pee = pee.replace( /\r\n|\r/g, '\n' ); - pee = pee.replace( /\n\s*\n+/g, '\n\n' ); - pee = pee.replace( /([\s\S]+?)\n\n/g, '$1\n' ); - pee = pee.replace( /\s*?<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(/gi, '$1'); - pee = pee.replace( /\s*]*)>/gi, ''); - pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /\s*\n/gi, '\n'); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); - pee = pee.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); - - pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { - if ( c.match( /]*)?>/ ) ) { - return a; } - return b + '' + c + ''; - }); + // keep tags inside captions and remove line breaks + if ( html.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); + }); + } - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - pee = pee.replace( //g, '\n' ); + // Pretty it up for the source editor + html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); + html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); + + // Mark
]*>[\s\S]+?<\/\1>/g, function( a ) { - return a.replace( /(\r\n|\n)/g, '' ); - }); - } - - // keep tags inside captions and convert line breaks - if ( pee.indexOf( '[caption' ) !== -1 ) { - preserve_br = true; - pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { - // keep existing - a = a.replace( /]*)>/g, '' ); - // no line breaks inside HTML tags - a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ) { - return b.replace( /[\r\n\t]+/, ' ' ); + // Protect pre|script tags + if ( html.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { + a = a.replace( /(\r\n|\n)?/g, '' ); + a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); + return a.replace( /\r?\n/g, '' ); }); - // convert remaining line breaks to - return a.replace( /\s*\n\s*/g, '' ); - }); - } - - pee = pee + '\n\n'; - pee = pee.replace( /\s*/gi, '\n\n' ); - pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); - pee = pee.replace( new RegExp( '((?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); - pee = pee.replace( /]*)?>/gi, '\n\n' ); // hr is self closing block element - pee = pee.replace( /\s* or around - pee = pee.replace( /<\/option>\s*/gi, '' ); - pee = pee.replace( /\r\n|\r/g, '\n' ); - pee = pee.replace( /\n\s*\n+/g, '\n\n' ); - pee = pee.replace( /([\s\S]+?)\n\n/g, '$1\n' ); - pee = pee.replace( /\s*?<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(/gi, '$1'); - pee = pee.replace( /\s*]*)>/gi, ''); - pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /\s*\n/gi, '\n'); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); - pee = pee.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); - - pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { - if ( c.match( /]*)?>/ ) ) { - return a; } - return b + '' + c + ''; - }); + // keep tags inside captions and remove line breaks + if ( html.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); + }); + } - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - pee = pee.replace( //g, '\n' ); + // Pretty it up for the source editor + html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); + html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); + + // Mark
]*>[\s\S]+?<\/\1>/g, function( a ) { + a = a.replace( /(\r\n|\n)?/g, '' ); + a = a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '' ); + return a.replace( /\r?\n/g, '' ); }); - // convert remaining line breaks to - return a.replace( /\s*\n\s*/g, '' ); - }); - } - - pee = pee + '\n\n'; - pee = pee.replace( /\s*/gi, '\n\n' ); - pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); - pee = pee.replace( new RegExp( '((?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); - pee = pee.replace( /]*)?>/gi, '\n\n' ); // hr is self closing block element - pee = pee.replace( /\s* or around - pee = pee.replace( /<\/option>\s*/gi, '' ); - pee = pee.replace( /\r\n|\r/g, '\n' ); - pee = pee.replace( /\n\s*\n+/g, '\n\n' ); - pee = pee.replace( /([\s\S]+?)\n\n/g, '$1\n' ); - pee = pee.replace( /\s*?<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(/gi, '$1'); - pee = pee.replace( /\s*]*)>/gi, ''); - pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /\s*\n/gi, '\n'); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); - pee = pee.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); - - pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { - if ( c.match( /]*)?>/ ) ) { - return a; } - return b + '' + c + ''; - }); + // keep tags inside captions and remove line breaks + if ( html.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); + }); + } - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - pee = pee.replace( //g, '\n' ); + // Pretty it up for the source editor + html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); + html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); + + // Mark
$1
\s*?<\/p>/gi, ''); - pee = pee.replace( new RegExp( '
\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*
(
\s*
]*)>/gi, ''); - pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, ''); - pee = pee.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /\s*\n/gi, '\n'); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); - pee = pee.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); - pee = pee.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); - - pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { - if ( c.match( /]*)?>/ ) ) { - return a; } - return b + '' + c + ''; - }); + // keep tags inside captions and remove line breaks + if ( html.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + return a.replace( /]*)>/g, '' ).replace( /[\r\n\t]+/, '' ); + }); + } - // put back the line breaks in pre|script - if ( preserve_linebreaks ) { - pee = pee.replace( //g, '\n' ); + // Pretty it up for the source editor + html = html.replace( new RegExp( '\\s*(' + blocklist1 + ')>\\s*', 'g' ), '$1>\n' ); + html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); + + // Mark
'); - pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '
\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); - pee = pee.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*
|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); - - pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { - if ( c.match( /
]*)?>/ ) ) { - return a; } - return b + '
' + c + '
+ html = html.replace( /
and + html = html.replace( /\s*
/gi, '' ); + html = html.replace( /\s*<\/p>\s*/gi, '\n\n' ); + html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); + html = html.replace( /\s*\s*/gi, '\n' ); + + // Fix some block element newline issues + html = html.replace( /\s*
]+>[\s\S]*?<\/p>)/g, '\n$1' ); + + // Trim whitespace + html = html.replace( /^\s+/, '' ); + html = html.replace( /[\s\u00a0]+$/, '' ); + + // put back the line breaks in pre|script + if ( preserve_linebreaks ) { + html = html.replace( //g, '\n' ); + } + + // and the tags in captions + if ( preserve_br ) { + html = html.replace( /]*)>/g, '' ); + } + + return html; } - if ( preserve_br ) { - pee = pee.replace( /]*)>/g, '' ); + // Similar to `wpautop()` in formatting.php + function autop( text ) { + var preserve_linebreaks = false, + preserve_br = false, + blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' + + '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' + + '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; + + // Normalize line breaks + text = text.replace( /\r\n|\r/g, '\n' ); + + if ( text.indexOf( '\n' ) === -1 ) { + return text; + } + + if ( text.indexOf( '/g, function( a ) { + return a.replace( /\n+/g, '' ); + }); + } + + text = text.replace( /<[^<>]+>/g, function( a ) { + return a.replace( /[\n\t ]+/g, ' ' ); + }); + + // Protect pre|script tags + if ( text.indexOf( ']*>[\s\S]+?<\/\1>/g, function( a ) { + return a.replace( /\n/g, '' ); + }); + } + + // keep tags inside captions and convert line breaks + if ( text.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + // keep existing + a = a.replace( /]*)>/g, '' ); + // no line breaks inside HTML tags + a = a.replace( /<[^<>]+>/g, function( b ) { + return b.replace( /[\n\t ]+/, ' ' ); + }); + // convert remaining line breaks to + return a.replace( /\s*\n\s*/g, '' ); + }); + } + + text = text + '\n\n'; + text = text.replace( /\s*/gi, '\n\n' ); + text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); + text = text.replace( new RegExp( '((?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); + text = text.replace( /]*)?>/gi, '\n\n' ); // hr is self closing block element + text = text.replace( /\s* or around + text = text.replace( /<\/option>\s*/gi, '' ); + text = text.replace( /\n\s*\n+/g, '\n\n' ); + text = text.replace( /([\s\S]+?)\n\n/g, '$1\n' ); + text = text.replace( /\s*?<\/p>/gi, ''); + text = text.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); + text = text.replace( /(/gi, '$1'); + text = text.replace( /\s*]*)>/gi, ''); + text = text.replace( /<\/blockquote>\s*<\/p>/gi, ''); + text = text.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); + text = text.replace( /\s*\n/gi, '\n'); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); + text = text.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); + text = text.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); + + text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { + if ( c.match( /]*)?>/ ) ) { + return a; + } + + return b + '' + c + ''; + }); + + // put back the line breaks in pre|script + if ( preserve_linebreaks ) { + text = text.replace( //g, '\n' ); + } + + if ( preserve_br ) { + text = text.replace( /]*)>/g, '' ); + } + + return text; } - return pee; - }, + // Add old events + function pre_wpautop( html ) { + var obj = { o: exports, data: html, unfiltered: html }; - pre_wpautop: function( content ) { - var t = this, o = { o: t, data: content, unfiltered: content }, - q = typeof( jQuery ) !== 'undefined'; + if ( $ ) { + $( 'body' ).trigger( 'beforePreWpautop', [ obj ] ); + } - if ( q ) { - jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] ); + obj.data = removep( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterPreWpautop', [ obj ] ); + } + + return obj.data; } - o.data = t._wp_Nop( o.data ); + function wpautop( text ) { + var obj = { o: exports, data: text, unfiltered: text }; - if ( q ) { - jQuery('body').trigger('afterPreWpautop', [ o ] ); + if ( $ ) { + $( 'body' ).trigger( 'beforeWpautop', [ obj ] ); + } + + obj.data = autop( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterWpautop', [ obj ] ); + } + + return obj.data; } - return o.data; - }, - - wpautop: function( pee ) { - var t = this, o = { o: t, data: pee, unfiltered: pee }, - q = typeof( jQuery ) !== 'undefined'; - - if ( q ) { - jQuery( 'body' ).trigger('beforeWpautop', [ o ] ); + if ( $ ) { + $( document ).ready( init ); + } else if ( document.addEventListener ) { + document.addEventListener( 'DOMContentLoaded', init, false ); + window.addEventListener( 'load', init, false ); + } else if ( window.attachEvent ) { + window.attachEvent( 'onload', init ); + document.attachEvent( 'onreadystatechange', function() { + if ( 'complete' === document.readyState ) { + init(); + } + } ); } - o.data = t._wp_Autop( o.data ); + window.wp = window.wp || {}; + window.wp.editor = window.wp.editor || {}; + window.wp.editor.autop = wpautop; + window.wp.editor.removep = pre_wpautop; - if ( q ) { - jQuery( 'body' ).trigger('afterWpautop', [ o ] ); - } + exports = { + go: switchEditor, + wpautop: wpautop, + pre_wpautop: pre_wpautop, + _wp_Autop: autop, + _wp_Nop: removep + }; - return o.data; + return exports; } -}; + + window.switchEditors = new SwitchEditors(); +}( window.jQuery )); diff --git a/src/wp-includes/class-wp-editor.php b/src/wp-includes/class-wp-editor.php index acd789532c..707cc9258e 100644 --- a/src/wp-includes/class-wp-editor.php +++ b/src/wp-includes/class-wp-editor.php @@ -143,71 +143,74 @@ final class _WP_Editors { * @param array $settings See the _parse_settings() method for description. */ public static function editor( $content, $editor_id, $settings = array() ) { - $set = self::parse_settings( $editor_id, $settings ); - $editor_class = ' class="' . trim( $set['editor_class'] . ' wp-editor-area' ) . '"'; + $editor_class = ' class="' . trim( esc_attr( $set['editor_class'] ) . ' wp-editor-area' ) . '"'; $tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : ''; - $switch_class = 'html-active'; + $default_editor = 'html'; $toolbar = $buttons = $autocomplete = ''; + $editor_id_attr = esc_attr( $editor_id ); if ( $set['drag_drop_upload'] ) { self::$drag_drop_upload = true; } - if ( ! empty( $set['editor_height'] ) ) - $height = ' style="height: ' . $set['editor_height'] . 'px"'; - else - $height = ' rows="' . $set['textarea_rows'] . '"'; - - if ( !current_user_can( 'upload_files' ) ) - $set['media_buttons'] = false; - - if ( ! self::$this_quicktags && self::$this_tinymce ) { - $switch_class = 'tmce-active'; - $autocomplete = ' autocomplete="off"'; - } elseif ( self::$this_quicktags && self::$this_tinymce ) { - $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); - $autocomplete = ' autocomplete="off"'; - - // 'html' is used for the "Text" editor tab. - if ( 'html' === $default_editor ) { - add_filter('the_editor_content', 'wp_htmledit_pre'); - $switch_class = 'html-active'; - } else { - add_filter('the_editor_content', 'wp_richedit_pre'); - $switch_class = 'tmce-active'; - } - - $buttons .= '' . __('Visual') . "\n"; - $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + if ( ! empty( $set['editor_height'] ) ) { + $height = ' style="height: ' . (int) $set['editor_height'] . 'px"'; + } else { + $height = ' rows="' . (int) $set['textarea_rows'] . '"'; } + if ( ! current_user_can( 'upload_files' ) ) { + $set['media_buttons'] = false; + } + + if ( self::$this_tinymce ) { + $autocomplete = ' autocomplete="off"'; + + if ( self::$this_quicktags ) { + $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); + // 'html' is used for the "Text" editor tab. + if ( 'html' !== $default_editor ) { + $default_editor = 'tinymce'; + } + + $buttons .= '' . __('Visual') . "\n"; + $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + } else { + $default_editor = 'tinymce'; + } + } + + $switch_class = 'html' === $default_editor ? 'html-active' : 'tmce-active'; $wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class; if ( $set['_content_editor_dfw'] ) { $wrap_class .= ' has-dfw'; } - echo ''; + echo ''; if ( self::$editor_buttons_css ) { - wp_print_styles('editor-buttons'); + wp_print_styles( 'editor-buttons' ); self::$editor_buttons_css = false; } - if ( !empty($set['editor_css']) ) + if ( ! empty( $set['editor_css'] ) ) { echo $set['editor_css'] . "\n"; + } - if ( !empty($buttons) || $set['media_buttons'] ) { - echo ''; + if ( ! empty( $buttons ) || $set['media_buttons'] ) { + echo ''; if ( $set['media_buttons'] ) { self::$has_medialib = true; - if ( !function_exists('media_buttons') ) - include(ABSPATH . 'wp-admin/includes/media.php'); + if ( ! function_exists( 'media_buttons' ) ) + include( ABSPATH . 'wp-admin/includes/media.php' ); - echo ''; + echo ''; /** * Fires after the default media button(s) are displayed. @@ -224,6 +227,18 @@ final class _WP_Editors { echo "\n"; } + $quicktags_toolbar = ''; + + if ( self::$this_quicktags ) { + if ( 'content' === $editor_id && ! empty( $GLOBALS['current_screen'] ) && $GLOBALS['current_screen']->base === 'post' ) { + $toolbar_id = 'ed_toolbar'; + } else { + $toolbar_id = 'qt_' . $editor_id_attr . '_toolbar'; + } + + $quicktags_toolbar = ''; + } + /** * Filter the HTML markup output that displays the editor. * @@ -231,9 +246,10 @@ final class _WP_Editors { * * @param string $output Editor's HTML markup. */ - $the_editor = apply_filters( 'the_editor', '' . - '%s' ); + $the_editor = apply_filters( 'the_editor', '' . + $quicktags_toolbar . + '%s' ); /** * Filter the default editor content. @@ -242,12 +258,22 @@ final class _WP_Editors { * * @param string $content Default editor content. */ - $content = apply_filters( 'the_editor_content', $content ); + $content = apply_filters( 'the_editor_content', $content, $default_editor ); + + // Back-compat for the `htmledit_pre` and `richedit_pre` filters + if ( 'html' === $default_editor && has_filter( 'htmledit_pre' ) ) { + // TODO: needs _deprecated_filter(), use _deprecated_function() as substitute for now + _deprecated_function( 'add_filter( htmledit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'htmledit_pre', $content ); + } elseif ( 'tinymce' === $default_editor && has_filter( 'richedit_pre' ) ) { + _deprecated_function( 'add_filter( richedit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'richedit_pre', $content ); + } printf( $the_editor, $content ); echo "\n\n\n"; - self::editor_settings($editor_id, $set); + self::editor_settings( $editor_id, $set ); } /** @@ -478,21 +504,21 @@ final class _WP_Editors { 'theme' => 'modern', 'skin' => 'lightgray', 'language' => self::$mce_locale, - 'formats' => "{ - alignleft: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'left'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignleft'} - ], - aligncenter: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'center'}}, - {selector: 'img,table,dl.wp-caption', classes: 'aligncenter'} - ], - alignright: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'right'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignright'} - ], - strikethrough: {inline: 'del'} - }", + 'formats' => '{' . + 'alignleft: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"left"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignleft"}' . + '],' . + 'aligncenter: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"center"}},' . + '{selector: "img,table,dl.wp-caption", classes: "aligncenter"}' . + '],' . + 'alignright: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"right"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignright"}' . + '],' . + 'strikethrough: {inline: "del"}' . + '}', 'relative_urls' => false, 'remove_script_host' => false, 'convert_urls' => false, @@ -650,8 +676,8 @@ final class _WP_Editors { 'body_class' => $body_class ); - if ( $first_run ) - $mceInit = array_merge( self::$first_init, $mceInit ); + // Merge with the first part of the init array + $mceInit = array_merge( self::$first_init, $mceInit ); if ( is_array( $set['tinymce'] ) ) $mceInit = array_merge( $mceInit, $set['tinymce'] ); @@ -662,7 +688,7 @@ final class _WP_Editors { * before tinyMCE.init. Setting "valid_elements", "invalid_elements" * and "extended_valid_elements" can be done through this filter. Best * is to use the default cleanup by not specifying valid_elements, - * as TinyMCE contains full set of XHTML 1.0. + * as TinyMCE checks against the full set of HTML 5.0 elements and attributes. */ if ( $set['teeny'] ) { @@ -1182,54 +1208,29 @@ final class _WP_Editors { ?> ( function() { - var init, edId, qtId, firstInit, wrapper; + var init, id, $wrap; if ( typeof tinymce !== 'undefined' ) { - for ( edId in tinyMCEPreInit.mceInit ) { - if ( firstInit ) { - init = tinyMCEPreInit.mceInit[edId] = tinymce.extend( {}, firstInit, tinyMCEPreInit.mceInit[edId] ); - } else { - init = firstInit = tinyMCEPreInit.mceInit[edId]; - } + for ( id in tinyMCEPreInit.mceInit ) { + init = tinyMCEPreInit.mceInit[id]; + $wrap = tinymce.$( '#wp-' + id + '-wrap' ); - wrapper = tinymce.DOM.select( '#wp-' + edId + '-wrap' )[0]; + if ( ( $wrap.hasClass( 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) && ! init.wp_skip_init ) { + tinymce.init( init ); - if ( ( tinymce.DOM.hasClass( wrapper, 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( edId ) ) && - ! init.wp_skip_init ) { - - try { - tinymce.init( init ); - - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = edId; - } - } catch(e){} + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; + } } } } if ( typeof quicktags !== 'undefined' ) { - for ( qtId in tinyMCEPreInit.qtInit ) { - try { - quicktags( tinyMCEPreInit.qtInit[qtId] ); + for ( id in tinyMCEPreInit.qtInit ) { + quicktags( tinyMCEPreInit.qtInit[id] ); - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = qtId; - } - } catch(e){}; - } - } - - if ( typeof jQuery !== 'undefined' ) { - jQuery('.wp-editor-wrap').on( 'click.wp-editor', function() { - if ( this.id ) { - window.wpActiveEditor = this.id.slice( 3, -5 ); - } - }); - } else { - for ( qtId in tinyMCEPreInit.qtInit ) { - document.getElementById( 'wp-' + qtId + '-wrap' ).onclick = function() { - window.wpActiveEditor = this.id.slice( 3, -5 ); + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; } } } diff --git a/src/wp-includes/css/editor.css b/src/wp-includes/css/editor.css index ff398a51c6..f5ac98bd6b 100644 --- a/src/wp-includes/css/editor.css +++ b/src/wp-includes/css/editor.css @@ -286,7 +286,6 @@ div.mce-path { .mce-path-item, .mce-path .mce-divider { font-size: 12px; - line-height: 18px; } .mce-toolbar .mce-btn, @@ -1059,6 +1058,7 @@ i.mce-i-wp_code:before { position: relative; border-bottom: 1px solid #dedede; background: #f5f5f5; + min-height: 30px; } .has-dfw .quicktags-toolbar { diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 8d926e698e..257cbfa3ad 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -203,6 +203,9 @@ add_filter( 'title_save_pre', 'trim' ); add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 ); +// Prepare the content for the Visual or Text editor +add_filter( 'the_editor_content', 'format_for_editor', 10, 2 ); + // Actions add_action( 'wp_head', '_wp_render_title_tag', 1 ); add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 6345bd7ca4..19b059ad93 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -3491,3 +3491,75 @@ function url_is_accessable_via_ssl( $url ) { return false; } + +/** + * Formats text for the rich text editor. + * + * The filter 'richedit_pre' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $text The text to be formatted. + * @return string The formatted text after filter is applied. + */ +function wp_richedit_pre($text) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( empty( $text ) ) { + /** + * Filter text returned for the rich text editor. + * + * This filter is first evaluated, and the value returned, if an empty string + * is passed to wp_richedit_pre(). If an empty string is passed, it results + * in a break tag and line feed. + * + * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() + * return after being formatted. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $output Text for the rich text editor. + */ + return apply_filters( 'richedit_pre', '' ); + } + + $output = convert_chars($text); + $output = wpautop($output); + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); + + /** This filter is documented in wp-includes/deprecated.php */ + return apply_filters( 'richedit_pre', $output ); +} + +/** + * Formats text for the HTML editor. + * + * Unless $output is empty it will pass through htmlspecialchars before the + * 'htmledit_pre' filter is applied. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The text to be formatted. + * @return string Formatted text after filter applied. + */ +function wp_htmledit_pre($output) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( !empty($output) ) + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & + + /** + * Filter the text before it is formatted for the HTML editor. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The HTML-formatted text. + */ + return apply_filters( 'htmledit_pre', $output ); +} + diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 76a06888e3..72085ddba5 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3062,66 +3062,35 @@ function ent2ncr( $text ) { } /** - * Formats text for the rich text editor. + * Formats text for the editor. * - * The filter 'richedit_pre' is applied here. If $text is empty the filter will - * be applied to an empty string. + * Generally the browsers treat everything inside a textarea as text, but + * it is still a good idea to HTML entity encode `<`, `>` and `&` in the content. * - * @since 2.0.0 + * The filter 'format_for_editor' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 4.3.0 * * @param string $text The text to be formatted. - * @return string The formatted text after filter is applied. + * @return string The formatted text after filter is applied. */ -function wp_richedit_pre( $text ) { - if ( empty( $text ) ) { - /** - * Filter text returned for the rich text editor. - * - * This filter is first evaluated, and the value returned, if an empty string - * is passed to wp_richedit_pre(). If an empty string is passed, it results - * in a break tag and line feed. - * - * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() - * return after being formatted. - * - * @since 2.0.0 - * - * @param string $output Text for the rich text editor. - */ - return apply_filters( 'richedit_pre', '' ); +function format_for_editor( $text, $default_editor = null ) { + // Back-compat: check if any characters need encoding. + if ( ! empty( $text ) && ( false !== strpos( $text, '<' ) || false !== strpos( $text, '>' ) || + preg_match( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', $text ) ) ) { + + $text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) ); } - $output = convert_chars($text); - $output = wpautop($output); - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); - - /** This filter is documented in wp-includes/formatting.php */ - return apply_filters( 'richedit_pre', $output ); -} - -/** - * Formats text for the HTML editor. - * - * Unless $output is empty it will pass through htmlspecialchars before the - * 'htmledit_pre' filter is applied. - * - * @since 2.5.0 - * - * @param string $output The text to be formatted. - * @return string Formatted text after filter applied. - */ -function wp_htmledit_pre( $output ) { - if ( !empty($output) ) - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & - /** - * Filter the text before it is formatted for the HTML editor. + * Filter the text after it is formatted for the editor. * - * @since 2.5.0 + * @since 4.3.0 * - * @param string $output The HTML-formatted text. + * @param string $text The formatted text. */ - return apply_filters( 'htmledit_pre', $output ); + return apply_filters( 'format_for_editor', $text, $default_editor ); } /** diff --git a/src/wp-includes/js/quicktags.js b/src/wp-includes/js/quicktags.js index b7b1ee73dc..11f8785059 100644 --- a/src/wp-includes/js/quicktags.js +++ b/src/wp-includes/js/quicktags.js @@ -163,7 +163,7 @@ function edButton(id, display, tagStart, tagEnd, access) { id = settings.id, canvas = document.getElementById(id), name = 'qt_' + id, - tb, onclick, toolbar_id; + tb, onclick, toolbar_id, wrap, setActiveEditor; if ( !id || !canvas ) { return false; @@ -182,12 +182,13 @@ function edButton(id, display, tagStart, tagEnd, access) { toolbar_id = name + '_toolbar'; } - tb = document.createElement('div'); - tb.id = toolbar_id; - tb.className = 'quicktags-toolbar'; - tb.onclick = function() { - window.wpActiveEditor = id; - }; + tb = document.getElementById( toolbar_id ); + + if ( ! tb ) { + tb = document.createElement('div'); + tb.id = toolbar_id; + tb.className = 'quicktags-toolbar'; + } canvas.parentNode.insertBefore(tb, canvas); t.toolbar = tb; @@ -214,10 +215,24 @@ function edButton(id, display, tagStart, tagEnd, access) { } }; + setActiveEditor = function() { + window.wpActiveEditor = id; + }; + + wrap = document.getElementById( 'wp-' + id + '-wrap' ); + if ( tb.addEventListener ) { - tb.addEventListener('click', onclick, false); + tb.addEventListener( 'click', onclick, false ); + + if ( wrap ) { + wrap.addEventListener( 'click', setActiveEditor, false ); + } } else if ( tb.attachEvent ) { - tb.attachEvent('onclick', onclick); + tb.attachEvent( 'onclick', onclick ); + + if ( wrap ) { + wrap.attachEvent( 'onclick', setActiveEditor ); + } } t.getButton = function(id) { diff --git a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js index 73075e6f89..9c27c53022 100644 --- a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js @@ -1,16 +1,19 @@ -/* global tinymce, getUserSetting, setUserSetting */ - +/* global getUserSetting, setUserSetting */ +( function( tinymce ) { // Set the minimum value for the modals z-index higher than #wpadminbar (100000) tinymce.ui.FloatPanel.zIndex = 100100; tinymce.PluginManager.add( 'wordpress', function( editor ) { - var DOM = tinymce.DOM, + var wpAdvButton, style, + DOM = tinymce.DOM, each = tinymce.each, __ = editor.editorManager.i18n.translate, - wpAdvButton, style; + $ = window.jQuery, + wp = window.wp, + hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) ); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); } function toggleToolbars( state ) { @@ -88,26 +91,34 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Replace Read More/Next Page tags with images - editor.on( 'BeforeSetContent', function( e ) { - var title; + editor.on( 'BeforeSetContent', function( event ) { + var title, + paragraph = tinymce.Env.webkit ? '' : ''; - if ( e.content ) { - if ( e.content.indexOf( '/g, function( match, moretext ) { + event.content = event.content.replace( //g, function( match, moretext ) { return ''; }); } - if ( e.content.indexOf( '' ) !== -1 ) { + if ( event.content.indexOf( '' ) !== -1 ) { title = __( 'Page break' ); - e.content = e.content.replace( //g, + event.content = event.content.replace( //g, '' ); } + + // Remove spaces from empty paragraphs. + event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + + if ( event.load && event.format !== 'raw' && hasWpautop ) { + event.content = wp.editor.autop( event.content ); + } } }); @@ -201,7 +212,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); editor.addCommand( 'WP_Medialib', function() { - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { wp.media.editor.open( editor.id ); } }); @@ -234,7 +245,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { // Menubar // Insert->Add Media - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { editor.addMenuItem( 'add_media', { text: 'Add Media', icon: 'wp-media-library', @@ -288,7 +299,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { doc = editor.getDoc(), dom = editor.dom; - if ( tinymce.Env.iOS ) { + if ( env.iOS ) { dom.addClass( doc.documentElement, 'ios' ); } @@ -318,23 +329,22 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Remove invalid parent paragraphs when inserting HTML - // TODO: still needed? - editor.on( 'BeforeSetContent', function( e ) { - if ( e.content ) { - e.content = e.content.replace(/\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>'); - e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '$1>'); + editor.on( 'BeforeSetContent', function( event ) { + if ( event.content ) { + event.content = event.content.replace( /\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' ) + .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '$1>' ); } }); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-init', [editor] ); } if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) { dom.bind( doc, 'dragstart dragend dragover drop', function( event ) { - if ( typeof window.jQuery !== 'undefined' ) { + if ( $ ) { // Trigger the jQuery handlers. - window.jQuery( document ).trigger( new window.jQuery.Event( event ) ); + $( document ).trigger( new $.Event( event ) ); } }); } @@ -362,27 +372,18 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } }); - editor.on( 'SaveContent', function( e ) { + editor.on( 'SaveContent', function( event ) { // If editor is hidden, we just want the textarea's value to be saved if ( ! editor.inline && editor.isHidden() ) { - e.content = e.element.value; + event.content = event.element.value; return; } // Keep empty paragraphs :( - e.content = e.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); + event.content = event.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); - if ( editor.getParam( 'wpautop', true ) && typeof window.switchEditors !== 'undefined' ) { - e.content = window.switchEditors.pre_wpautop( e.content ); - } - }); - - // Remove spaces from empty paragraphs. - editor.on( 'BeforeSetContent', function( event ) { - var paragraph = tinymce.Env.webkit ? '' : ''; - - if ( event.content ) { - event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + if ( hasWpautop ) { + event.content = wp.editor.removep( event.content ); } }); @@ -394,6 +395,11 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { editor.settings.height = 300; } + // Start hidden when the Text editor is set to load first. + if ( tinymce.$( '#wp-' + editor.id + '-wrap' ).hasClass( 'html-active' ) ) { + editor.hide(); + } + each( { c: 'JustifyCenter', r: 'JustifyRight', @@ -415,7 +421,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } ); editor.addShortcut( 'meta+s', '', function() { - if ( typeof wp !== 'undefined' && wp.autosave ) { + if ( wp && wp.autosave ) { wp.autosave.server.triggerSave(); } } ); @@ -737,3 +743,5 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { _getEmbed: noop }; }); + +}( window.tinymce ));
]*>[\s\S]+?<\/\1>/g, function( a ) { + return a.replace( /\n/g, '' ); + }); + } + + // keep tags inside captions and convert line breaks + if ( text.indexOf( '[caption' ) !== -1 ) { + preserve_br = true; + text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { + // keep existing + a = a.replace( /]*)>/g, '' ); + // no line breaks inside HTML tags + a = a.replace( /<[^<>]+>/g, function( b ) { + return b.replace( /[\n\t ]+/, ' ' ); + }); + // convert remaining line breaks to + return a.replace( /\s*\n\s*/g, '' ); + }); + } + + text = text + '\n\n'; + text = text.replace( /\s*/gi, '\n\n' ); + text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); + text = text.replace( new RegExp( '((?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); + text = text.replace( /]*)?>/gi, '\n\n' ); // hr is self closing block element + text = text.replace( /\s* or around + text = text.replace( /<\/option>\s*/gi, '' ); + text = text.replace( /\n\s*\n+/g, '\n\n' ); + text = text.replace( /([\s\S]+?)\n\n/g, '$1\n' ); + text = text.replace( /\s*?<\/p>/gi, ''); + text = text.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); + text = text.replace( /(/gi, '$1'); + text = text.replace( /\s*]*)>/gi, ''); + text = text.replace( /<\/blockquote>\s*<\/p>/gi, ''); + text = text.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); + text = text.replace( /\s*\n/gi, '\n'); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); + text = text.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); + text = text.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); + + text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { + if ( c.match( /]*)?>/ ) ) { + return a; + } + + return b + '' + c + ''; + }); + + // put back the line breaks in pre|script + if ( preserve_linebreaks ) { + text = text.replace( //g, '\n' ); + } + + if ( preserve_br ) { + text = text.replace( /]*)>/g, '' ); + } + + return text; } - return pee; - }, + // Add old events + function pre_wpautop( html ) { + var obj = { o: exports, data: html, unfiltered: html }; - pre_wpautop: function( content ) { - var t = this, o = { o: t, data: content, unfiltered: content }, - q = typeof( jQuery ) !== 'undefined'; + if ( $ ) { + $( 'body' ).trigger( 'beforePreWpautop', [ obj ] ); + } - if ( q ) { - jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] ); + obj.data = removep( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterPreWpautop', [ obj ] ); + } + + return obj.data; } - o.data = t._wp_Nop( o.data ); + function wpautop( text ) { + var obj = { o: exports, data: text, unfiltered: text }; - if ( q ) { - jQuery('body').trigger('afterPreWpautop', [ o ] ); + if ( $ ) { + $( 'body' ).trigger( 'beforeWpautop', [ obj ] ); + } + + obj.data = autop( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterWpautop', [ obj ] ); + } + + return obj.data; } - return o.data; - }, - - wpautop: function( pee ) { - var t = this, o = { o: t, data: pee, unfiltered: pee }, - q = typeof( jQuery ) !== 'undefined'; - - if ( q ) { - jQuery( 'body' ).trigger('beforeWpautop', [ o ] ); + if ( $ ) { + $( document ).ready( init ); + } else if ( document.addEventListener ) { + document.addEventListener( 'DOMContentLoaded', init, false ); + window.addEventListener( 'load', init, false ); + } else if ( window.attachEvent ) { + window.attachEvent( 'onload', init ); + document.attachEvent( 'onreadystatechange', function() { + if ( 'complete' === document.readyState ) { + init(); + } + } ); } - o.data = t._wp_Autop( o.data ); + window.wp = window.wp || {}; + window.wp.editor = window.wp.editor || {}; + window.wp.editor.autop = wpautop; + window.wp.editor.removep = pre_wpautop; - if ( q ) { - jQuery( 'body' ).trigger('afterWpautop', [ o ] ); - } + exports = { + go: switchEditor, + wpautop: wpautop, + pre_wpautop: pre_wpautop, + _wp_Autop: autop, + _wp_Nop: removep + }; - return o.data; + return exports; } -}; + + window.switchEditors = new SwitchEditors(); +}( window.jQuery )); diff --git a/src/wp-includes/class-wp-editor.php b/src/wp-includes/class-wp-editor.php index acd789532c..707cc9258e 100644 --- a/src/wp-includes/class-wp-editor.php +++ b/src/wp-includes/class-wp-editor.php @@ -143,71 +143,74 @@ final class _WP_Editors { * @param array $settings See the _parse_settings() method for description. */ public static function editor( $content, $editor_id, $settings = array() ) { - $set = self::parse_settings( $editor_id, $settings ); - $editor_class = ' class="' . trim( $set['editor_class'] . ' wp-editor-area' ) . '"'; + $editor_class = ' class="' . trim( esc_attr( $set['editor_class'] ) . ' wp-editor-area' ) . '"'; $tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : ''; - $switch_class = 'html-active'; + $default_editor = 'html'; $toolbar = $buttons = $autocomplete = ''; + $editor_id_attr = esc_attr( $editor_id ); if ( $set['drag_drop_upload'] ) { self::$drag_drop_upload = true; } - if ( ! empty( $set['editor_height'] ) ) - $height = ' style="height: ' . $set['editor_height'] . 'px"'; - else - $height = ' rows="' . $set['textarea_rows'] . '"'; - - if ( !current_user_can( 'upload_files' ) ) - $set['media_buttons'] = false; - - if ( ! self::$this_quicktags && self::$this_tinymce ) { - $switch_class = 'tmce-active'; - $autocomplete = ' autocomplete="off"'; - } elseif ( self::$this_quicktags && self::$this_tinymce ) { - $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); - $autocomplete = ' autocomplete="off"'; - - // 'html' is used for the "Text" editor tab. - if ( 'html' === $default_editor ) { - add_filter('the_editor_content', 'wp_htmledit_pre'); - $switch_class = 'html-active'; - } else { - add_filter('the_editor_content', 'wp_richedit_pre'); - $switch_class = 'tmce-active'; - } - - $buttons .= '' . __('Visual') . "\n"; - $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + if ( ! empty( $set['editor_height'] ) ) { + $height = ' style="height: ' . (int) $set['editor_height'] . 'px"'; + } else { + $height = ' rows="' . (int) $set['textarea_rows'] . '"'; } + if ( ! current_user_can( 'upload_files' ) ) { + $set['media_buttons'] = false; + } + + if ( self::$this_tinymce ) { + $autocomplete = ' autocomplete="off"'; + + if ( self::$this_quicktags ) { + $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); + // 'html' is used for the "Text" editor tab. + if ( 'html' !== $default_editor ) { + $default_editor = 'tinymce'; + } + + $buttons .= '' . __('Visual') . "\n"; + $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + } else { + $default_editor = 'tinymce'; + } + } + + $switch_class = 'html' === $default_editor ? 'html-active' : 'tmce-active'; $wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class; if ( $set['_content_editor_dfw'] ) { $wrap_class .= ' has-dfw'; } - echo ''; + echo ''; if ( self::$editor_buttons_css ) { - wp_print_styles('editor-buttons'); + wp_print_styles( 'editor-buttons' ); self::$editor_buttons_css = false; } - if ( !empty($set['editor_css']) ) + if ( ! empty( $set['editor_css'] ) ) { echo $set['editor_css'] . "\n"; + } - if ( !empty($buttons) || $set['media_buttons'] ) { - echo ''; + if ( ! empty( $buttons ) || $set['media_buttons'] ) { + echo ''; if ( $set['media_buttons'] ) { self::$has_medialib = true; - if ( !function_exists('media_buttons') ) - include(ABSPATH . 'wp-admin/includes/media.php'); + if ( ! function_exists( 'media_buttons' ) ) + include( ABSPATH . 'wp-admin/includes/media.php' ); - echo ''; + echo ''; /** * Fires after the default media button(s) are displayed. @@ -224,6 +227,18 @@ final class _WP_Editors { echo "\n"; } + $quicktags_toolbar = ''; + + if ( self::$this_quicktags ) { + if ( 'content' === $editor_id && ! empty( $GLOBALS['current_screen'] ) && $GLOBALS['current_screen']->base === 'post' ) { + $toolbar_id = 'ed_toolbar'; + } else { + $toolbar_id = 'qt_' . $editor_id_attr . '_toolbar'; + } + + $quicktags_toolbar = ''; + } + /** * Filter the HTML markup output that displays the editor. * @@ -231,9 +246,10 @@ final class _WP_Editors { * * @param string $output Editor's HTML markup. */ - $the_editor = apply_filters( 'the_editor', '' . - '%s' ); + $the_editor = apply_filters( 'the_editor', '' . + $quicktags_toolbar . + '%s' ); /** * Filter the default editor content. @@ -242,12 +258,22 @@ final class _WP_Editors { * * @param string $content Default editor content. */ - $content = apply_filters( 'the_editor_content', $content ); + $content = apply_filters( 'the_editor_content', $content, $default_editor ); + + // Back-compat for the `htmledit_pre` and `richedit_pre` filters + if ( 'html' === $default_editor && has_filter( 'htmledit_pre' ) ) { + // TODO: needs _deprecated_filter(), use _deprecated_function() as substitute for now + _deprecated_function( 'add_filter( htmledit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'htmledit_pre', $content ); + } elseif ( 'tinymce' === $default_editor && has_filter( 'richedit_pre' ) ) { + _deprecated_function( 'add_filter( richedit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'richedit_pre', $content ); + } printf( $the_editor, $content ); echo "\n\n\n"; - self::editor_settings($editor_id, $set); + self::editor_settings( $editor_id, $set ); } /** @@ -478,21 +504,21 @@ final class _WP_Editors { 'theme' => 'modern', 'skin' => 'lightgray', 'language' => self::$mce_locale, - 'formats' => "{ - alignleft: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'left'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignleft'} - ], - aligncenter: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'center'}}, - {selector: 'img,table,dl.wp-caption', classes: 'aligncenter'} - ], - alignright: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'right'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignright'} - ], - strikethrough: {inline: 'del'} - }", + 'formats' => '{' . + 'alignleft: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"left"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignleft"}' . + '],' . + 'aligncenter: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"center"}},' . + '{selector: "img,table,dl.wp-caption", classes: "aligncenter"}' . + '],' . + 'alignright: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"right"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignright"}' . + '],' . + 'strikethrough: {inline: "del"}' . + '}', 'relative_urls' => false, 'remove_script_host' => false, 'convert_urls' => false, @@ -650,8 +676,8 @@ final class _WP_Editors { 'body_class' => $body_class ); - if ( $first_run ) - $mceInit = array_merge( self::$first_init, $mceInit ); + // Merge with the first part of the init array + $mceInit = array_merge( self::$first_init, $mceInit ); if ( is_array( $set['tinymce'] ) ) $mceInit = array_merge( $mceInit, $set['tinymce'] ); @@ -662,7 +688,7 @@ final class _WP_Editors { * before tinyMCE.init. Setting "valid_elements", "invalid_elements" * and "extended_valid_elements" can be done through this filter. Best * is to use the default cleanup by not specifying valid_elements, - * as TinyMCE contains full set of XHTML 1.0. + * as TinyMCE checks against the full set of HTML 5.0 elements and attributes. */ if ( $set['teeny'] ) { @@ -1182,54 +1208,29 @@ final class _WP_Editors { ?> ( function() { - var init, edId, qtId, firstInit, wrapper; + var init, id, $wrap; if ( typeof tinymce !== 'undefined' ) { - for ( edId in tinyMCEPreInit.mceInit ) { - if ( firstInit ) { - init = tinyMCEPreInit.mceInit[edId] = tinymce.extend( {}, firstInit, tinyMCEPreInit.mceInit[edId] ); - } else { - init = firstInit = tinyMCEPreInit.mceInit[edId]; - } + for ( id in tinyMCEPreInit.mceInit ) { + init = tinyMCEPreInit.mceInit[id]; + $wrap = tinymce.$( '#wp-' + id + '-wrap' ); - wrapper = tinymce.DOM.select( '#wp-' + edId + '-wrap' )[0]; + if ( ( $wrap.hasClass( 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) && ! init.wp_skip_init ) { + tinymce.init( init ); - if ( ( tinymce.DOM.hasClass( wrapper, 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( edId ) ) && - ! init.wp_skip_init ) { - - try { - tinymce.init( init ); - - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = edId; - } - } catch(e){} + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; + } } } } if ( typeof quicktags !== 'undefined' ) { - for ( qtId in tinyMCEPreInit.qtInit ) { - try { - quicktags( tinyMCEPreInit.qtInit[qtId] ); + for ( id in tinyMCEPreInit.qtInit ) { + quicktags( tinyMCEPreInit.qtInit[id] ); - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = qtId; - } - } catch(e){}; - } - } - - if ( typeof jQuery !== 'undefined' ) { - jQuery('.wp-editor-wrap').on( 'click.wp-editor', function() { - if ( this.id ) { - window.wpActiveEditor = this.id.slice( 3, -5 ); - } - }); - } else { - for ( qtId in tinyMCEPreInit.qtInit ) { - document.getElementById( 'wp-' + qtId + '-wrap' ).onclick = function() { - window.wpActiveEditor = this.id.slice( 3, -5 ); + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; } } } diff --git a/src/wp-includes/css/editor.css b/src/wp-includes/css/editor.css index ff398a51c6..f5ac98bd6b 100644 --- a/src/wp-includes/css/editor.css +++ b/src/wp-includes/css/editor.css @@ -286,7 +286,6 @@ div.mce-path { .mce-path-item, .mce-path .mce-divider { font-size: 12px; - line-height: 18px; } .mce-toolbar .mce-btn, @@ -1059,6 +1058,7 @@ i.mce-i-wp_code:before { position: relative; border-bottom: 1px solid #dedede; background: #f5f5f5; + min-height: 30px; } .has-dfw .quicktags-toolbar { diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 8d926e698e..257cbfa3ad 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -203,6 +203,9 @@ add_filter( 'title_save_pre', 'trim' ); add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 ); +// Prepare the content for the Visual or Text editor +add_filter( 'the_editor_content', 'format_for_editor', 10, 2 ); + // Actions add_action( 'wp_head', '_wp_render_title_tag', 1 ); add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 6345bd7ca4..19b059ad93 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -3491,3 +3491,75 @@ function url_is_accessable_via_ssl( $url ) { return false; } + +/** + * Formats text for the rich text editor. + * + * The filter 'richedit_pre' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $text The text to be formatted. + * @return string The formatted text after filter is applied. + */ +function wp_richedit_pre($text) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( empty( $text ) ) { + /** + * Filter text returned for the rich text editor. + * + * This filter is first evaluated, and the value returned, if an empty string + * is passed to wp_richedit_pre(). If an empty string is passed, it results + * in a break tag and line feed. + * + * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() + * return after being formatted. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $output Text for the rich text editor. + */ + return apply_filters( 'richedit_pre', '' ); + } + + $output = convert_chars($text); + $output = wpautop($output); + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); + + /** This filter is documented in wp-includes/deprecated.php */ + return apply_filters( 'richedit_pre', $output ); +} + +/** + * Formats text for the HTML editor. + * + * Unless $output is empty it will pass through htmlspecialchars before the + * 'htmledit_pre' filter is applied. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The text to be formatted. + * @return string Formatted text after filter applied. + */ +function wp_htmledit_pre($output) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( !empty($output) ) + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & + + /** + * Filter the text before it is formatted for the HTML editor. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The HTML-formatted text. + */ + return apply_filters( 'htmledit_pre', $output ); +} + diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 76a06888e3..72085ddba5 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3062,66 +3062,35 @@ function ent2ncr( $text ) { } /** - * Formats text for the rich text editor. + * Formats text for the editor. * - * The filter 'richedit_pre' is applied here. If $text is empty the filter will - * be applied to an empty string. + * Generally the browsers treat everything inside a textarea as text, but + * it is still a good idea to HTML entity encode `<`, `>` and `&` in the content. * - * @since 2.0.0 + * The filter 'format_for_editor' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 4.3.0 * * @param string $text The text to be formatted. - * @return string The formatted text after filter is applied. + * @return string The formatted text after filter is applied. */ -function wp_richedit_pre( $text ) { - if ( empty( $text ) ) { - /** - * Filter text returned for the rich text editor. - * - * This filter is first evaluated, and the value returned, if an empty string - * is passed to wp_richedit_pre(). If an empty string is passed, it results - * in a break tag and line feed. - * - * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() - * return after being formatted. - * - * @since 2.0.0 - * - * @param string $output Text for the rich text editor. - */ - return apply_filters( 'richedit_pre', '' ); +function format_for_editor( $text, $default_editor = null ) { + // Back-compat: check if any characters need encoding. + if ( ! empty( $text ) && ( false !== strpos( $text, '<' ) || false !== strpos( $text, '>' ) || + preg_match( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', $text ) ) ) { + + $text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) ); } - $output = convert_chars($text); - $output = wpautop($output); - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); - - /** This filter is documented in wp-includes/formatting.php */ - return apply_filters( 'richedit_pre', $output ); -} - -/** - * Formats text for the HTML editor. - * - * Unless $output is empty it will pass through htmlspecialchars before the - * 'htmledit_pre' filter is applied. - * - * @since 2.5.0 - * - * @param string $output The text to be formatted. - * @return string Formatted text after filter applied. - */ -function wp_htmledit_pre( $output ) { - if ( !empty($output) ) - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & - /** - * Filter the text before it is formatted for the HTML editor. + * Filter the text after it is formatted for the editor. * - * @since 2.5.0 + * @since 4.3.0 * - * @param string $output The HTML-formatted text. + * @param string $text The formatted text. */ - return apply_filters( 'htmledit_pre', $output ); + return apply_filters( 'format_for_editor', $text, $default_editor ); } /** diff --git a/src/wp-includes/js/quicktags.js b/src/wp-includes/js/quicktags.js index b7b1ee73dc..11f8785059 100644 --- a/src/wp-includes/js/quicktags.js +++ b/src/wp-includes/js/quicktags.js @@ -163,7 +163,7 @@ function edButton(id, display, tagStart, tagEnd, access) { id = settings.id, canvas = document.getElementById(id), name = 'qt_' + id, - tb, onclick, toolbar_id; + tb, onclick, toolbar_id, wrap, setActiveEditor; if ( !id || !canvas ) { return false; @@ -182,12 +182,13 @@ function edButton(id, display, tagStart, tagEnd, access) { toolbar_id = name + '_toolbar'; } - tb = document.createElement('div'); - tb.id = toolbar_id; - tb.className = 'quicktags-toolbar'; - tb.onclick = function() { - window.wpActiveEditor = id; - }; + tb = document.getElementById( toolbar_id ); + + if ( ! tb ) { + tb = document.createElement('div'); + tb.id = toolbar_id; + tb.className = 'quicktags-toolbar'; + } canvas.parentNode.insertBefore(tb, canvas); t.toolbar = tb; @@ -214,10 +215,24 @@ function edButton(id, display, tagStart, tagEnd, access) { } }; + setActiveEditor = function() { + window.wpActiveEditor = id; + }; + + wrap = document.getElementById( 'wp-' + id + '-wrap' ); + if ( tb.addEventListener ) { - tb.addEventListener('click', onclick, false); + tb.addEventListener( 'click', onclick, false ); + + if ( wrap ) { + wrap.addEventListener( 'click', setActiveEditor, false ); + } } else if ( tb.attachEvent ) { - tb.attachEvent('onclick', onclick); + tb.attachEvent( 'onclick', onclick ); + + if ( wrap ) { + wrap.attachEvent( 'onclick', setActiveEditor ); + } } t.getButton = function(id) { diff --git a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js index 73075e6f89..9c27c53022 100644 --- a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js @@ -1,16 +1,19 @@ -/* global tinymce, getUserSetting, setUserSetting */ - +/* global getUserSetting, setUserSetting */ +( function( tinymce ) { // Set the minimum value for the modals z-index higher than #wpadminbar (100000) tinymce.ui.FloatPanel.zIndex = 100100; tinymce.PluginManager.add( 'wordpress', function( editor ) { - var DOM = tinymce.DOM, + var wpAdvButton, style, + DOM = tinymce.DOM, each = tinymce.each, __ = editor.editorManager.i18n.translate, - wpAdvButton, style; + $ = window.jQuery, + wp = window.wp, + hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) ); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); } function toggleToolbars( state ) { @@ -88,26 +91,34 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Replace Read More/Next Page tags with images - editor.on( 'BeforeSetContent', function( e ) { - var title; + editor.on( 'BeforeSetContent', function( event ) { + var title, + paragraph = tinymce.Env.webkit ? '' : ''; - if ( e.content ) { - if ( e.content.indexOf( '/g, function( match, moretext ) { + event.content = event.content.replace( //g, function( match, moretext ) { return ''; }); } - if ( e.content.indexOf( '' ) !== -1 ) { + if ( event.content.indexOf( '' ) !== -1 ) { title = __( 'Page break' ); - e.content = e.content.replace( //g, + event.content = event.content.replace( //g, '' ); } + + // Remove spaces from empty paragraphs. + event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + + if ( event.load && event.format !== 'raw' && hasWpautop ) { + event.content = wp.editor.autop( event.content ); + } } }); @@ -201,7 +212,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); editor.addCommand( 'WP_Medialib', function() { - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { wp.media.editor.open( editor.id ); } }); @@ -234,7 +245,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { // Menubar // Insert->Add Media - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { editor.addMenuItem( 'add_media', { text: 'Add Media', icon: 'wp-media-library', @@ -288,7 +299,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { doc = editor.getDoc(), dom = editor.dom; - if ( tinymce.Env.iOS ) { + if ( env.iOS ) { dom.addClass( doc.documentElement, 'ios' ); } @@ -318,23 +329,22 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Remove invalid parent paragraphs when inserting HTML - // TODO: still needed? - editor.on( 'BeforeSetContent', function( e ) { - if ( e.content ) { - e.content = e.content.replace(/\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>'); - e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '$1>'); + editor.on( 'BeforeSetContent', function( event ) { + if ( event.content ) { + event.content = event.content.replace( /\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' ) + .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '$1>' ); } }); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-init', [editor] ); } if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) { dom.bind( doc, 'dragstart dragend dragover drop', function( event ) { - if ( typeof window.jQuery !== 'undefined' ) { + if ( $ ) { // Trigger the jQuery handlers. - window.jQuery( document ).trigger( new window.jQuery.Event( event ) ); + $( document ).trigger( new $.Event( event ) ); } }); } @@ -362,27 +372,18 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } }); - editor.on( 'SaveContent', function( e ) { + editor.on( 'SaveContent', function( event ) { // If editor is hidden, we just want the textarea's value to be saved if ( ! editor.inline && editor.isHidden() ) { - e.content = e.element.value; + event.content = event.element.value; return; } // Keep empty paragraphs :( - e.content = e.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); + event.content = event.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); - if ( editor.getParam( 'wpautop', true ) && typeof window.switchEditors !== 'undefined' ) { - e.content = window.switchEditors.pre_wpautop( e.content ); - } - }); - - // Remove spaces from empty paragraphs. - editor.on( 'BeforeSetContent', function( event ) { - var paragraph = tinymce.Env.webkit ? '' : ''; - - if ( event.content ) { - event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + if ( hasWpautop ) { + event.content = wp.editor.removep( event.content ); } }); @@ -394,6 +395,11 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { editor.settings.height = 300; } + // Start hidden when the Text editor is set to load first. + if ( tinymce.$( '#wp-' + editor.id + '-wrap' ).hasClass( 'html-active' ) ) { + editor.hide(); + } + each( { c: 'JustifyCenter', r: 'JustifyRight', @@ -415,7 +421,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } ); editor.addShortcut( 'meta+s', '', function() { - if ( typeof wp !== 'undefined' && wp.autosave ) { + if ( wp && wp.autosave ) { wp.autosave.server.triggerSave(); } } ); @@ -737,3 +743,5 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { _getEmbed: noop }; }); + +}( window.tinymce ));
\s*?<\/p>/gi, ''); + text = text.replace( new RegExp( '
]*)>/gi, ''); + text = text.replace( /<\/blockquote>\s*<\/p>/gi, ''); + text = text.replace( new RegExp( '\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*', 'gi' ), '$1' ); + text = text.replace( /\s*\n/gi, '\n'); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')[^>]*>)\\s*', 'gi' ), '$1' ); + text = text.replace( /(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); + text = text.replace( /(?:|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); + + text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { + if ( c.match( /]*)?>/ ) ) { + return a; + } + + return b + '' + c + ''; + }); + + // put back the line breaks in pre|script + if ( preserve_linebreaks ) { + text = text.replace( //g, '\n' ); + } + + if ( preserve_br ) { + text = text.replace( /]*)>/g, '' ); + } + + return text; } - return pee; - }, + // Add old events + function pre_wpautop( html ) { + var obj = { o: exports, data: html, unfiltered: html }; - pre_wpautop: function( content ) { - var t = this, o = { o: t, data: content, unfiltered: content }, - q = typeof( jQuery ) !== 'undefined'; + if ( $ ) { + $( 'body' ).trigger( 'beforePreWpautop', [ obj ] ); + } - if ( q ) { - jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] ); + obj.data = removep( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterPreWpautop', [ obj ] ); + } + + return obj.data; } - o.data = t._wp_Nop( o.data ); + function wpautop( text ) { + var obj = { o: exports, data: text, unfiltered: text }; - if ( q ) { - jQuery('body').trigger('afterPreWpautop', [ o ] ); + if ( $ ) { + $( 'body' ).trigger( 'beforeWpautop', [ obj ] ); + } + + obj.data = autop( obj.data ); + + if ( $ ) { + $( 'body' ).trigger( 'afterWpautop', [ obj ] ); + } + + return obj.data; } - return o.data; - }, - - wpautop: function( pee ) { - var t = this, o = { o: t, data: pee, unfiltered: pee }, - q = typeof( jQuery ) !== 'undefined'; - - if ( q ) { - jQuery( 'body' ).trigger('beforeWpautop', [ o ] ); + if ( $ ) { + $( document ).ready( init ); + } else if ( document.addEventListener ) { + document.addEventListener( 'DOMContentLoaded', init, false ); + window.addEventListener( 'load', init, false ); + } else if ( window.attachEvent ) { + window.attachEvent( 'onload', init ); + document.attachEvent( 'onreadystatechange', function() { + if ( 'complete' === document.readyState ) { + init(); + } + } ); } - o.data = t._wp_Autop( o.data ); + window.wp = window.wp || {}; + window.wp.editor = window.wp.editor || {}; + window.wp.editor.autop = wpautop; + window.wp.editor.removep = pre_wpautop; - if ( q ) { - jQuery( 'body' ).trigger('afterWpautop', [ o ] ); - } + exports = { + go: switchEditor, + wpautop: wpautop, + pre_wpautop: pre_wpautop, + _wp_Autop: autop, + _wp_Nop: removep + }; - return o.data; + return exports; } -}; + + window.switchEditors = new SwitchEditors(); +}( window.jQuery )); diff --git a/src/wp-includes/class-wp-editor.php b/src/wp-includes/class-wp-editor.php index acd789532c..707cc9258e 100644 --- a/src/wp-includes/class-wp-editor.php +++ b/src/wp-includes/class-wp-editor.php @@ -143,71 +143,74 @@ final class _WP_Editors { * @param array $settings See the _parse_settings() method for description. */ public static function editor( $content, $editor_id, $settings = array() ) { - $set = self::parse_settings( $editor_id, $settings ); - $editor_class = ' class="' . trim( $set['editor_class'] . ' wp-editor-area' ) . '"'; + $editor_class = ' class="' . trim( esc_attr( $set['editor_class'] ) . ' wp-editor-area' ) . '"'; $tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : ''; - $switch_class = 'html-active'; + $default_editor = 'html'; $toolbar = $buttons = $autocomplete = ''; + $editor_id_attr = esc_attr( $editor_id ); if ( $set['drag_drop_upload'] ) { self::$drag_drop_upload = true; } - if ( ! empty( $set['editor_height'] ) ) - $height = ' style="height: ' . $set['editor_height'] . 'px"'; - else - $height = ' rows="' . $set['textarea_rows'] . '"'; - - if ( !current_user_can( 'upload_files' ) ) - $set['media_buttons'] = false; - - if ( ! self::$this_quicktags && self::$this_tinymce ) { - $switch_class = 'tmce-active'; - $autocomplete = ' autocomplete="off"'; - } elseif ( self::$this_quicktags && self::$this_tinymce ) { - $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); - $autocomplete = ' autocomplete="off"'; - - // 'html' is used for the "Text" editor tab. - if ( 'html' === $default_editor ) { - add_filter('the_editor_content', 'wp_htmledit_pre'); - $switch_class = 'html-active'; - } else { - add_filter('the_editor_content', 'wp_richedit_pre'); - $switch_class = 'tmce-active'; - } - - $buttons .= '' . __('Visual') . "\n"; - $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + if ( ! empty( $set['editor_height'] ) ) { + $height = ' style="height: ' . (int) $set['editor_height'] . 'px"'; + } else { + $height = ' rows="' . (int) $set['textarea_rows'] . '"'; } + if ( ! current_user_can( 'upload_files' ) ) { + $set['media_buttons'] = false; + } + + if ( self::$this_tinymce ) { + $autocomplete = ' autocomplete="off"'; + + if ( self::$this_quicktags ) { + $default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor(); + // 'html' is used for the "Text" editor tab. + if ( 'html' !== $default_editor ) { + $default_editor = 'tinymce'; + } + + $buttons .= '' . __('Visual') . "\n"; + $buttons .= '' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "\n"; + } else { + $default_editor = 'tinymce'; + } + } + + $switch_class = 'html' === $default_editor ? 'html-active' : 'tmce-active'; $wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class; if ( $set['_content_editor_dfw'] ) { $wrap_class .= ' has-dfw'; } - echo ''; + echo ''; if ( self::$editor_buttons_css ) { - wp_print_styles('editor-buttons'); + wp_print_styles( 'editor-buttons' ); self::$editor_buttons_css = false; } - if ( !empty($set['editor_css']) ) + if ( ! empty( $set['editor_css'] ) ) { echo $set['editor_css'] . "\n"; + } - if ( !empty($buttons) || $set['media_buttons'] ) { - echo ''; + if ( ! empty( $buttons ) || $set['media_buttons'] ) { + echo ''; if ( $set['media_buttons'] ) { self::$has_medialib = true; - if ( !function_exists('media_buttons') ) - include(ABSPATH . 'wp-admin/includes/media.php'); + if ( ! function_exists( 'media_buttons' ) ) + include( ABSPATH . 'wp-admin/includes/media.php' ); - echo ''; + echo ''; /** * Fires after the default media button(s) are displayed. @@ -224,6 +227,18 @@ final class _WP_Editors { echo "\n"; } + $quicktags_toolbar = ''; + + if ( self::$this_quicktags ) { + if ( 'content' === $editor_id && ! empty( $GLOBALS['current_screen'] ) && $GLOBALS['current_screen']->base === 'post' ) { + $toolbar_id = 'ed_toolbar'; + } else { + $toolbar_id = 'qt_' . $editor_id_attr . '_toolbar'; + } + + $quicktags_toolbar = ''; + } + /** * Filter the HTML markup output that displays the editor. * @@ -231,9 +246,10 @@ final class _WP_Editors { * * @param string $output Editor's HTML markup. */ - $the_editor = apply_filters( 'the_editor', '' . - '%s' ); + $the_editor = apply_filters( 'the_editor', '' . + $quicktags_toolbar . + '%s' ); /** * Filter the default editor content. @@ -242,12 +258,22 @@ final class _WP_Editors { * * @param string $content Default editor content. */ - $content = apply_filters( 'the_editor_content', $content ); + $content = apply_filters( 'the_editor_content', $content, $default_editor ); + + // Back-compat for the `htmledit_pre` and `richedit_pre` filters + if ( 'html' === $default_editor && has_filter( 'htmledit_pre' ) ) { + // TODO: needs _deprecated_filter(), use _deprecated_function() as substitute for now + _deprecated_function( 'add_filter( htmledit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'htmledit_pre', $content ); + } elseif ( 'tinymce' === $default_editor && has_filter( 'richedit_pre' ) ) { + _deprecated_function( 'add_filter( richedit_pre )', '4.3.0', 'add_filter( format_for_editor )' ); + $content = apply_filters( 'richedit_pre', $content ); + } printf( $the_editor, $content ); echo "\n\n\n"; - self::editor_settings($editor_id, $set); + self::editor_settings( $editor_id, $set ); } /** @@ -478,21 +504,21 @@ final class _WP_Editors { 'theme' => 'modern', 'skin' => 'lightgray', 'language' => self::$mce_locale, - 'formats' => "{ - alignleft: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'left'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignleft'} - ], - aligncenter: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'center'}}, - {selector: 'img,table,dl.wp-caption', classes: 'aligncenter'} - ], - alignright: [ - {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li', styles: {textAlign:'right'}}, - {selector: 'img,table,dl.wp-caption', classes: 'alignright'} - ], - strikethrough: {inline: 'del'} - }", + 'formats' => '{' . + 'alignleft: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"left"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignleft"}' . + '],' . + 'aligncenter: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"center"}},' . + '{selector: "img,table,dl.wp-caption", classes: "aligncenter"}' . + '],' . + 'alignright: [' . + '{selector: "p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li", styles: {textAlign:"right"}},' . + '{selector: "img,table,dl.wp-caption", classes: "alignright"}' . + '],' . + 'strikethrough: {inline: "del"}' . + '}', 'relative_urls' => false, 'remove_script_host' => false, 'convert_urls' => false, @@ -650,8 +676,8 @@ final class _WP_Editors { 'body_class' => $body_class ); - if ( $first_run ) - $mceInit = array_merge( self::$first_init, $mceInit ); + // Merge with the first part of the init array + $mceInit = array_merge( self::$first_init, $mceInit ); if ( is_array( $set['tinymce'] ) ) $mceInit = array_merge( $mceInit, $set['tinymce'] ); @@ -662,7 +688,7 @@ final class _WP_Editors { * before tinyMCE.init. Setting "valid_elements", "invalid_elements" * and "extended_valid_elements" can be done through this filter. Best * is to use the default cleanup by not specifying valid_elements, - * as TinyMCE contains full set of XHTML 1.0. + * as TinyMCE checks against the full set of HTML 5.0 elements and attributes. */ if ( $set['teeny'] ) { @@ -1182,54 +1208,29 @@ final class _WP_Editors { ?> ( function() { - var init, edId, qtId, firstInit, wrapper; + var init, id, $wrap; if ( typeof tinymce !== 'undefined' ) { - for ( edId in tinyMCEPreInit.mceInit ) { - if ( firstInit ) { - init = tinyMCEPreInit.mceInit[edId] = tinymce.extend( {}, firstInit, tinyMCEPreInit.mceInit[edId] ); - } else { - init = firstInit = tinyMCEPreInit.mceInit[edId]; - } + for ( id in tinyMCEPreInit.mceInit ) { + init = tinyMCEPreInit.mceInit[id]; + $wrap = tinymce.$( '#wp-' + id + '-wrap' ); - wrapper = tinymce.DOM.select( '#wp-' + edId + '-wrap' )[0]; + if ( ( $wrap.hasClass( 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( id ) ) && ! init.wp_skip_init ) { + tinymce.init( init ); - if ( ( tinymce.DOM.hasClass( wrapper, 'tmce-active' ) || ! tinyMCEPreInit.qtInit.hasOwnProperty( edId ) ) && - ! init.wp_skip_init ) { - - try { - tinymce.init( init ); - - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = edId; - } - } catch(e){} + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; + } } } } if ( typeof quicktags !== 'undefined' ) { - for ( qtId in tinyMCEPreInit.qtInit ) { - try { - quicktags( tinyMCEPreInit.qtInit[qtId] ); + for ( id in tinyMCEPreInit.qtInit ) { + quicktags( tinyMCEPreInit.qtInit[id] ); - if ( ! window.wpActiveEditor ) { - window.wpActiveEditor = qtId; - } - } catch(e){}; - } - } - - if ( typeof jQuery !== 'undefined' ) { - jQuery('.wp-editor-wrap').on( 'click.wp-editor', function() { - if ( this.id ) { - window.wpActiveEditor = this.id.slice( 3, -5 ); - } - }); - } else { - for ( qtId in tinyMCEPreInit.qtInit ) { - document.getElementById( 'wp-' + qtId + '-wrap' ).onclick = function() { - window.wpActiveEditor = this.id.slice( 3, -5 ); + if ( ! window.wpActiveEditor ) { + window.wpActiveEditor = id; } } } diff --git a/src/wp-includes/css/editor.css b/src/wp-includes/css/editor.css index ff398a51c6..f5ac98bd6b 100644 --- a/src/wp-includes/css/editor.css +++ b/src/wp-includes/css/editor.css @@ -286,7 +286,6 @@ div.mce-path { .mce-path-item, .mce-path .mce-divider { font-size: 12px; - line-height: 18px; } .mce-toolbar .mce-btn, @@ -1059,6 +1058,7 @@ i.mce-i-wp_code:before { position: relative; border-bottom: 1px solid #dedede; background: #f5f5f5; + min-height: 30px; } .has-dfw .quicktags-toolbar { diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 8d926e698e..257cbfa3ad 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -203,6 +203,9 @@ add_filter( 'title_save_pre', 'trim' ); add_filter( 'http_request_host_is_external', 'allowed_http_request_hosts', 10, 2 ); +// Prepare the content for the Visual or Text editor +add_filter( 'the_editor_content', 'format_for_editor', 10, 2 ); + // Actions add_action( 'wp_head', '_wp_render_title_tag', 1 ); add_action( 'wp_head', 'wp_enqueue_scripts', 1 ); diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php index 6345bd7ca4..19b059ad93 100644 --- a/src/wp-includes/deprecated.php +++ b/src/wp-includes/deprecated.php @@ -3491,3 +3491,75 @@ function url_is_accessable_via_ssl( $url ) { return false; } + +/** + * Formats text for the rich text editor. + * + * The filter 'richedit_pre' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $text The text to be formatted. + * @return string The formatted text after filter is applied. + */ +function wp_richedit_pre($text) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( empty( $text ) ) { + /** + * Filter text returned for the rich text editor. + * + * This filter is first evaluated, and the value returned, if an empty string + * is passed to wp_richedit_pre(). If an empty string is passed, it results + * in a break tag and line feed. + * + * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() + * return after being formatted. + * + * @since 2.0.0 + * @deprecated 4.3.0 + * + * @param string $output Text for the rich text editor. + */ + return apply_filters( 'richedit_pre', '' ); + } + + $output = convert_chars($text); + $output = wpautop($output); + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); + + /** This filter is documented in wp-includes/deprecated.php */ + return apply_filters( 'richedit_pre', $output ); +} + +/** + * Formats text for the HTML editor. + * + * Unless $output is empty it will pass through htmlspecialchars before the + * 'htmledit_pre' filter is applied. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The text to be formatted. + * @return string Formatted text after filter applied. + */ +function wp_htmledit_pre($output) { + _deprecated_function( __FUNCTION__, '4.3', 'format_for_editor()' ); + + if ( !empty($output) ) + $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & + + /** + * Filter the text before it is formatted for the HTML editor. + * + * @since 2.5.0 + * @deprecated 4.3.0 + * + * @param string $output The HTML-formatted text. + */ + return apply_filters( 'htmledit_pre', $output ); +} + diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 76a06888e3..72085ddba5 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3062,66 +3062,35 @@ function ent2ncr( $text ) { } /** - * Formats text for the rich text editor. + * Formats text for the editor. * - * The filter 'richedit_pre' is applied here. If $text is empty the filter will - * be applied to an empty string. + * Generally the browsers treat everything inside a textarea as text, but + * it is still a good idea to HTML entity encode `<`, `>` and `&` in the content. * - * @since 2.0.0 + * The filter 'format_for_editor' is applied here. If $text is empty the filter will + * be applied to an empty string. + * + * @since 4.3.0 * * @param string $text The text to be formatted. - * @return string The formatted text after filter is applied. + * @return string The formatted text after filter is applied. */ -function wp_richedit_pre( $text ) { - if ( empty( $text ) ) { - /** - * Filter text returned for the rich text editor. - * - * This filter is first evaluated, and the value returned, if an empty string - * is passed to wp_richedit_pre(). If an empty string is passed, it results - * in a break tag and line feed. - * - * If a non-empty string is passed, the filter is evaluated on the wp_richedit_pre() - * return after being formatted. - * - * @since 2.0.0 - * - * @param string $output Text for the rich text editor. - */ - return apply_filters( 'richedit_pre', '' ); +function format_for_editor( $text, $default_editor = null ) { + // Back-compat: check if any characters need encoding. + if ( ! empty( $text ) && ( false !== strpos( $text, '<' ) || false !== strpos( $text, '>' ) || + preg_match( '/&(?!#(?:\d+|x[a-f0-9]+);|[a-z1-4]{1,8};)/i', $text ) ) ) { + + $text = htmlspecialchars( $text, ENT_NOQUOTES, get_option( 'blog_charset' ) ); } - $output = convert_chars($text); - $output = wpautop($output); - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); - - /** This filter is documented in wp-includes/formatting.php */ - return apply_filters( 'richedit_pre', $output ); -} - -/** - * Formats text for the HTML editor. - * - * Unless $output is empty it will pass through htmlspecialchars before the - * 'htmledit_pre' filter is applied. - * - * @since 2.5.0 - * - * @param string $output The text to be formatted. - * @return string Formatted text after filter applied. - */ -function wp_htmledit_pre( $output ) { - if ( !empty($output) ) - $output = htmlspecialchars($output, ENT_NOQUOTES, get_option( 'blog_charset' ) ); // convert only < > & - /** - * Filter the text before it is formatted for the HTML editor. + * Filter the text after it is formatted for the editor. * - * @since 2.5.0 + * @since 4.3.0 * - * @param string $output The HTML-formatted text. + * @param string $text The formatted text. */ - return apply_filters( 'htmledit_pre', $output ); + return apply_filters( 'format_for_editor', $text, $default_editor ); } /** diff --git a/src/wp-includes/js/quicktags.js b/src/wp-includes/js/quicktags.js index b7b1ee73dc..11f8785059 100644 --- a/src/wp-includes/js/quicktags.js +++ b/src/wp-includes/js/quicktags.js @@ -163,7 +163,7 @@ function edButton(id, display, tagStart, tagEnd, access) { id = settings.id, canvas = document.getElementById(id), name = 'qt_' + id, - tb, onclick, toolbar_id; + tb, onclick, toolbar_id, wrap, setActiveEditor; if ( !id || !canvas ) { return false; @@ -182,12 +182,13 @@ function edButton(id, display, tagStart, tagEnd, access) { toolbar_id = name + '_toolbar'; } - tb = document.createElement('div'); - tb.id = toolbar_id; - tb.className = 'quicktags-toolbar'; - tb.onclick = function() { - window.wpActiveEditor = id; - }; + tb = document.getElementById( toolbar_id ); + + if ( ! tb ) { + tb = document.createElement('div'); + tb.id = toolbar_id; + tb.className = 'quicktags-toolbar'; + } canvas.parentNode.insertBefore(tb, canvas); t.toolbar = tb; @@ -214,10 +215,24 @@ function edButton(id, display, tagStart, tagEnd, access) { } }; + setActiveEditor = function() { + window.wpActiveEditor = id; + }; + + wrap = document.getElementById( 'wp-' + id + '-wrap' ); + if ( tb.addEventListener ) { - tb.addEventListener('click', onclick, false); + tb.addEventListener( 'click', onclick, false ); + + if ( wrap ) { + wrap.addEventListener( 'click', setActiveEditor, false ); + } } else if ( tb.attachEvent ) { - tb.attachEvent('onclick', onclick); + tb.attachEvent( 'onclick', onclick ); + + if ( wrap ) { + wrap.attachEvent( 'onclick', setActiveEditor ); + } } t.getButton = function(id) { diff --git a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js index 73075e6f89..9c27c53022 100644 --- a/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wordpress/plugin.js @@ -1,16 +1,19 @@ -/* global tinymce, getUserSetting, setUserSetting */ - +/* global getUserSetting, setUserSetting */ +( function( tinymce ) { // Set the minimum value for the modals z-index higher than #wpadminbar (100000) tinymce.ui.FloatPanel.zIndex = 100100; tinymce.PluginManager.add( 'wordpress', function( editor ) { - var DOM = tinymce.DOM, + var wpAdvButton, style, + DOM = tinymce.DOM, each = tinymce.each, __ = editor.editorManager.i18n.translate, - wpAdvButton, style; + $ = window.jQuery, + wp = window.wp, + hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) ); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] ); } function toggleToolbars( state ) { @@ -88,26 +91,34 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Replace Read More/Next Page tags with images - editor.on( 'BeforeSetContent', function( e ) { - var title; + editor.on( 'BeforeSetContent', function( event ) { + var title, + paragraph = tinymce.Env.webkit ? '' : ''; - if ( e.content ) { - if ( e.content.indexOf( '/g, function( match, moretext ) { + event.content = event.content.replace( //g, function( match, moretext ) { return ''; }); } - if ( e.content.indexOf( '' ) !== -1 ) { + if ( event.content.indexOf( '' ) !== -1 ) { title = __( 'Page break' ); - e.content = e.content.replace( //g, + event.content = event.content.replace( //g, '' ); } + + // Remove spaces from empty paragraphs. + event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + + if ( event.load && event.format !== 'raw' && hasWpautop ) { + event.content = wp.editor.autop( event.content ); + } } }); @@ -201,7 +212,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); editor.addCommand( 'WP_Medialib', function() { - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { wp.media.editor.open( editor.id ); } }); @@ -234,7 +245,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { // Menubar // Insert->Add Media - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { editor.addMenuItem( 'add_media', { text: 'Add Media', icon: 'wp-media-library', @@ -288,7 +299,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { doc = editor.getDoc(), dom = editor.dom; - if ( tinymce.Env.iOS ) { + if ( env.iOS ) { dom.addClass( doc.documentElement, 'ios' ); } @@ -318,23 +329,22 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Remove invalid parent paragraphs when inserting HTML - // TODO: still needed? - editor.on( 'BeforeSetContent', function( e ) { - if ( e.content ) { - e.content = e.content.replace(/\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>'); - e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '$1>'); + editor.on( 'BeforeSetContent', function( event ) { + if ( event.content ) { + event.content = event.content.replace( /\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' ) + .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '$1>' ); } }); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-init', [editor] ); } if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) { dom.bind( doc, 'dragstart dragend dragover drop', function( event ) { - if ( typeof window.jQuery !== 'undefined' ) { + if ( $ ) { // Trigger the jQuery handlers. - window.jQuery( document ).trigger( new window.jQuery.Event( event ) ); + $( document ).trigger( new $.Event( event ) ); } }); } @@ -362,27 +372,18 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } }); - editor.on( 'SaveContent', function( e ) { + editor.on( 'SaveContent', function( event ) { // If editor is hidden, we just want the textarea's value to be saved if ( ! editor.inline && editor.isHidden() ) { - e.content = e.element.value; + event.content = event.element.value; return; } // Keep empty paragraphs :( - e.content = e.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); + event.content = event.content.replace( /(?:|\u00a0|\uFEFF| )*<\/p>/g, ' ' ); - if ( editor.getParam( 'wpautop', true ) && typeof window.switchEditors !== 'undefined' ) { - e.content = window.switchEditors.pre_wpautop( e.content ); - } - }); - - // Remove spaces from empty paragraphs. - editor.on( 'BeforeSetContent', function( event ) { - var paragraph = tinymce.Env.webkit ? '' : ''; - - if ( event.content ) { - event.content = event.content.replace( /(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + if ( hasWpautop ) { + event.content = wp.editor.removep( event.content ); } }); @@ -394,6 +395,11 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { editor.settings.height = 300; } + // Start hidden when the Text editor is set to load first. + if ( tinymce.$( '#wp-' + editor.id + '-wrap' ).hasClass( 'html-active' ) ) { + editor.hide(); + } + each( { c: 'JustifyCenter', r: 'JustifyRight', @@ -415,7 +421,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } ); editor.addShortcut( 'meta+s', '', function() { - if ( typeof wp !== 'undefined' && wp.autosave ) { + if ( wp && wp.autosave ) { wp.autosave.server.triggerSave(); } } ); @@ -737,3 +743,5 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { _getEmbed: noop }; }); + +}( window.tinymce ));
'); + text = text.replace( /<\/blockquote>\s*<\/p>/gi, '
\\s*(?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); + text = text.replace( new RegExp( '(?(?:' + blocklist + ')(?: [^>]*)?>)\\s*
|)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|)*/gi, '[caption$1[/caption]' ); + + text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { + if ( c.match( /
]*)?>/ ) ) { + return a; + } + + return b + '
(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + + if ( event.load && event.format !== 'raw' && hasWpautop ) { + event.content = wp.editor.autop( event.content ); + } } }); @@ -201,7 +212,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); editor.addCommand( 'WP_Medialib', function() { - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { wp.media.editor.open( editor.id ); } }); @@ -234,7 +245,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { // Menubar // Insert->Add Media - if ( typeof wp !== 'undefined' && wp.media && wp.media.editor ) { + if ( wp && wp.media && wp.media.editor ) { editor.addMenuItem( 'add_media', { text: 'Add Media', icon: 'wp-media-library', @@ -288,7 +299,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { doc = editor.getDoc(), dom = editor.dom; - if ( tinymce.Env.iOS ) { + if ( env.iOS ) { dom.addClass( doc.documentElement, 'ios' ); } @@ -318,23 +329,22 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { }); // Remove invalid parent paragraphs when inserting HTML - // TODO: still needed? - editor.on( 'BeforeSetContent', function( e ) { - if ( e.content ) { - e.content = e.content.replace(/
\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)( [^>]*)?>/gi, '<$1$2>'); - e.content = e.content.replace(/<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre|address)>\s*<\/p>/gi, '$1>'); + editor.on( 'BeforeSetContent', function( event ) { + if ( event.content ) { + event.content = event.content.replace( /
\s*<(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)( [^>]*)?>/gi, '<$1$2>' ) + .replace( /<\/(p|div|ul|ol|dl|table|blockquote|h[1-6]|fieldset|pre)>\s*<\/p>/gi, '$1>' ); } }); - if ( typeof window.jQuery !== 'undefined' ) { - window.jQuery( document ).triggerHandler( 'tinymce-editor-init', [editor] ); + if ( $ ) { + $( document ).triggerHandler( 'tinymce-editor-init', [editor] ); } if ( window.tinyMCEPreInit && window.tinyMCEPreInit.dragDropUpload ) { dom.bind( doc, 'dragstart dragend dragover drop', function( event ) { - if ( typeof window.jQuery !== 'undefined' ) { + if ( $ ) { // Trigger the jQuery handlers. - window.jQuery( document ).trigger( new window.jQuery.Event( event ) ); + $( document ).trigger( new $.Event( event ) ); } }); } @@ -362,27 +372,18 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } }); - editor.on( 'SaveContent', function( e ) { + editor.on( 'SaveContent', function( event ) { // If editor is hidden, we just want the textarea's value to be saved if ( ! editor.inline && editor.isHidden() ) { - e.content = e.element.value; + event.content = event.element.value; return; } // Keep empty paragraphs :( - e.content = e.content.replace( /
(?:|\u00a0|\uFEFF| )*<\/p>/g, '
(?: |\u00a0|\uFEFF|\s)+<\/p>/gi, paragraph ); + if ( hasWpautop ) { + event.content = wp.editor.removep( event.content ); } }); @@ -394,6 +395,11 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { editor.settings.height = 300; } + // Start hidden when the Text editor is set to load first. + if ( tinymce.$( '#wp-' + editor.id + '-wrap' ).hasClass( 'html-active' ) ) { + editor.hide(); + } + each( { c: 'JustifyCenter', r: 'JustifyRight', @@ -415,7 +421,7 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { } ); editor.addShortcut( 'meta+s', '', function() { - if ( typeof wp !== 'undefined' && wp.autosave ) { + if ( wp && wp.autosave ) { wp.autosave.server.triggerSave(); } } ); @@ -737,3 +743,5 @@ tinymce.PluginManager.add( 'wordpress', function( editor ) { _getEmbed: noop }; }); + +}( window.tinymce ));