Ticket #25947: 25947.4.diff
| File 25947.4.diff, 15.6 KB (added by , 13 years ago) |
|---|
-
src/wp-admin/js/editor.js
1 /* global tinyMCE, tinyMCEPreInit, QTags, setUserSetting */ 1 2 2 3 var switchEditors = { 3 4 4 switchto: function(el) { 5 var aid = el.id, l = aid.length, id = aid.substr(0, l - 5), mode = aid.substr(l - 4); 5 switchto: function( el ) { 6 var aid = el.id, 7 l = aid.length, 8 id = aid.substr( 0, l - 5 ), 9 mode = aid.substr( l - 4 ); 6 10 7 this.go( id, mode);11 this.go( id, mode ); 8 12 }, 9 13 10 go: function( id, mode) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the "Text"editor tab.14 go: function( id, mode ) { // mode can be 'html', 'tmce', or 'toggle'; 'html' is used for the 'Text' editor tab. 11 15 id = id || 'content'; 12 16 mode = mode || 'toggle'; 13 17 14 var t = this, ed = tinyMCE.get( id), wrap_id, txtarea_el, dom = tinymce.DOM;18 var t = this, ed = tinyMCE.get( id ), wrap_id, txtarea_el, dom = tinyMCE.DOM; 15 19 16 wrap_id = 'wp-' +id+'-wrap';17 txtarea_el = dom.get( id);20 wrap_id = 'wp-' + id + '-wrap'; 21 txtarea_el = dom.get( id ); 18 22 19 if ( 'toggle' == mode ) {20 if ( ed && ! ed.isHidden() )23 if ( 'toggle' === mode ) { 24 if ( ed && ! ed.isHidden() ) { 21 25 mode = 'html'; 22 else26 } else { 23 27 mode = 'tmce'; 28 } 24 29 } 25 30 26 if ( 'tmce' == mode || 'tinymce'== mode ) {27 if ( ed && ! ed.isHidden() ) 31 if ( 'tmce' === mode || 'tinymce' === mode ) { 32 if ( ed && ! ed.isHidden() ) { 28 33 return false; 34 } 29 35 30 if ( typeof(QTags) != 'undefined' ) 31 QTags.closeAllTags(id); 36 if ( typeof( QTags ) !== 'undefined' ) { 37 QTags.closeAllTags( id ); 38 } 32 39 33 if ( tinyMCEPreInit.mceInit[ id] && tinyMCEPreInit.mceInit[id].wpautop )40 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) { 34 41 txtarea_el.value = t.wpautop( txtarea_el.value ); 42 } 35 43 36 44 if ( ed ) { 37 45 ed.show(); 38 46 } else { 39 ed = new tiny mce.Editor(id, tinyMCEPreInit.mceInit[id]);47 ed = new tinyMCE.Editor( id, tinyMCEPreInit.mceInit[ id ] ); 40 48 ed.render(); 41 49 } 42 50 43 dom.removeClass( wrap_id, 'html-active');44 dom.addClass( wrap_id, 'tmce-active');45 setUserSetting( 'editor', 'tinymce');51 dom.removeClass( wrap_id, 'html-active' ); 52 dom.addClass( wrap_id, 'tmce-active' ); 53 setUserSetting( 'editor', 'tinymce' ); 46 54 47 } else if ( 'html' == mode ) {55 } else if ( 'html' === mode ) { 48 56 49 if ( ed && ed.isHidden() ) 57 if ( ed && ed.isHidden() ) { 50 58 return false; 59 } 51 60 52 61 if ( ed ) { 53 62 ed.hide(); 54 63 } else { 55 // The TinyMCE instance doesn't exist, run the content through "pre_wpautop()"and show the textarea56 if ( tinyMCEPreInit.mceInit[ id] && tinyMCEPreInit.mceInit[id].wpautop )64 // The TinyMCE instance doesn't exist, run the content through 'pre_wpautop()' and show the textarea 65 if ( tinyMCEPreInit.mceInit[ id ] && tinyMCEPreInit.mceInit[ id ].wpautop ) { 57 66 txtarea_el.value = t.pre_wpautop( txtarea_el.value ); 67 } 58 68 59 dom.setStyles( txtarea_el, {'display': '', 'visibility': ''});69 dom.setStyles( txtarea_el, {'display': '', 'visibility': ''} ); 60 70 } 61 71 62 dom.removeClass( wrap_id, 'tmce-active');63 dom.addClass( wrap_id, 'html-active');64 setUserSetting( 'editor', 'html');72 dom.removeClass( wrap_id, 'tmce-active' ); 73 dom.addClass( wrap_id, 'html-active' ); 74 setUserSetting( 'editor', 'html' ); 65 75 } 66 76 return false; 67 77 }, 68 78 69 _wp_Nop : function(content) { 70 var blocklist1, blocklist2, preserve_linebreaks = false, preserve_br = false; 79 _wp_Nop: function( content ) { 80 var blocklist1, 81 blocklist2, 82 preserve_linebreaks = false, 83 preserve_br = false; 71 84 72 85 // Protect pre|script tags 73 if ( content.indexOf( '<pre') != -1 || content.indexOf('<script') != -1 ) {86 if ( content.indexOf( '<pre' ) !== -1 || content.indexOf( '<script' ) !== -1 ) { 74 87 preserve_linebreaks = true; 75 content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {76 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>');77 return a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>');88 content = content.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) { 89 a = a.replace( /<br ?\/?>(\r\n|\n)?/g, '<wp-temp-lb>' ); 90 return a.replace( /<\/?p( [^>]*)?>(\r\n|\n)?/g, '<wp-temp-lb>' ); 78 91 }); 79 92 } 80 93 81 94 // keep <br> tags inside captions and remove line breaks 82 if ( content.indexOf( '[caption') != -1 ) {95 if ( content.indexOf( '[caption' ) !== -1 ) { 83 96 preserve_br = true; 84 content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function(a) {85 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>').replace(/[\r\n\t]+/, '');97 content = content.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { 98 return a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ).replace( /[\r\n\t]+/, '' ); 86 99 }); 87 100 } 88 101 89 102 // Pretty it up for the source editor 90 103 blocklist1 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|div|h[1-6]|p|fieldset'; 91 content = content.replace( new RegExp('\\s*</('+blocklist1+')>\\s*', 'g'), '</$1>\n');92 content = content.replace( new RegExp('\\s*<((?:'+blocklist1+')(?: [^>]*)?)>', 'g'), '\n<$1>');104 content = content.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' ); 105 content = content.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); 93 106 94 107 // Mark </p> if it has any attributes. 95 content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>');108 content = content.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' ); 96 109 97 110 // Separate <div> containing <p> 98 content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n');111 content = content.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' ); 99 112 100 113 // Remove <p> and <br /> 101 content = content.replace( /\s*<p>/gi, '');102 content = content.replace( /\s*<\/p>\s*/gi, '\n\n');103 content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n');104 content = content.replace( /\s*<br ?\/?>\s*/gi, '\n');114 content = content.replace( /\s*<p>/gi, '' ); 115 content = content.replace( /\s*<\/p>\s*/gi, '\n\n' ); 116 content = content.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); 117 content = content.replace( /\s*<br ?\/?>\s*/gi, '\n' ); 105 118 106 119 // Fix some block element newline issues 107 content = content.replace( /\s*<div/g, '\n<div');108 content = content.replace( /<\/div>\s*/g, '</div>\n');109 content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n');110 content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption');120 content = content.replace( /\s*<div/g, '\n<div' ); 121 content = content.replace( /<\/div>\s*/g, '</div>\n' ); 122 content = content.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' ); 123 content = content.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' ); 111 124 112 125 blocklist2 = 'blockquote|ul|ol|li|table|thead|tbody|tfoot|tr|th|td|h[1-6]|pre|fieldset'; 113 content = content.replace( new RegExp('\\s*<((?:'+blocklist2+')(?: [^>]*)?)\\s*>', 'g'), '\n<$1>');114 content = content.replace( new RegExp('\\s*</('+blocklist2+')>\\s*', 'g'), '</$1>\n');115 content = content.replace( /<li([^>]*)>/g, '\t<li$1>');126 content = content.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); 127 content = content.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' ); 128 content = content.replace( /<li([^>]*)>/g, '\t<li$1>' ); 116 129 117 if ( content.indexOf( '<hr') != -1 ) {118 content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n');130 if ( content.indexOf( '<hr' ) !== -1 ) { 131 content = content.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' ); 119 132 } 120 133 121 if ( content.indexOf( '<object') != -1 ) {122 content = content.replace( /<object[\s\S]+?<\/object>/g, function(a){123 return a.replace( /[\r\n]+/g, '');134 if ( content.indexOf( '<object' ) !== -1 ) { 135 content = content.replace( /<object[\s\S]+?<\/object>/g, function( a ) { 136 return a.replace( /[\r\n]+/g, '' ); 124 137 }); 125 138 } 126 139 127 140 // Unmark special paragraph closing tags 128 content = content.replace( /<\/p#>/g, '</p>\n');129 content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1');141 content = content.replace( /<\/p#>/g, '</p>\n' ); 142 content = content.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' ); 130 143 131 144 // Trim whitespace 132 content = content.replace( /^\s+/, '');133 content = content.replace( /[\s\u00a0]+$/, '');145 content = content.replace( /^\s+/, '' ); 146 content = content.replace( /[\s\u00a0]+$/, '' ); 134 147 135 148 // put back the line breaks in pre|script 136 if ( preserve_linebreaks ) 137 content = content.replace(/<wp-temp-lb>/g, '\n'); 149 if ( preserve_linebreaks ) { 150 content = content.replace( /<wp-temp-lb>/g, '\n' ); 151 } 138 152 139 153 // and the <br> tags in captions 140 if ( preserve_br ) 141 content = content.replace(/<wp-temp-br([^>]*)>/g, '<br$1>'); 154 if ( preserve_br ) { 155 content = content.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' ); 156 } 142 157 143 158 return content; 144 159 }, 145 160 146 _wp_Autop : function(pee) {161 _wp_Autop: function(pee) { 147 162 var preserve_linebreaks = false, preserve_br = false, 148 163 blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|option|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|noscript|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; 149 164 150 if ( pee.indexOf( '<object') != -1 ) {151 pee = pee.replace( /<object[\s\S]+?<\/object>/g, function(a){152 return a.replace( /[\r\n]+/g, '');165 if ( pee.indexOf( '<object' ) !== -1 ) { 166 pee = pee.replace( /<object[\s\S]+?<\/object>/g, function( a ) { 167 return a.replace( /[\r\n]+/g, '' ); 153 168 }); 154 169 } 155 170 156 pee = pee.replace( /<[^<>]+>/g, function(a){157 return a.replace( /[\r\n]+/g, ' ');171 pee = pee.replace( /<[^<>]+>/g, function( a ){ 172 return a.replace( /[\r\n]+/g, ' ' ); 158 173 }); 159 174 160 175 // Protect pre|script tags 161 if ( pee.indexOf( '<pre') != -1 || pee.indexOf('<script') != -1 ) {176 if ( pee.indexOf( '<pre' ) !== -1 || pee.indexOf( '<script' ) !== -1 ) { 162 177 preserve_linebreaks = true; 163 pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {164 return a.replace( /(\r\n|\n)/g, '<wp-temp-lb>');178 pee = pee.replace( /<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function( a ) { 179 return a.replace( /(\r\n|\n)/g, '<wp-temp-lb>' ); 165 180 }); 166 181 } 167 182 168 183 // keep <br> tags inside captions and convert line breaks 169 if ( pee.indexOf( '[caption') != -1 ) {184 if ( pee.indexOf( '[caption' ) !== -1 ) { 170 185 preserve_br = true; 171 pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function(a) {186 pee = pee.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { 172 187 // keep existing <br> 173 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>');188 a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ); 174 189 // no line breaks inside HTML tags 175 a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function(b){176 return b.replace( /[\r\n\t]+/, ' ');190 a = a.replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( b ){ 191 return b.replace( /[\r\n\t]+/, ' ' ); 177 192 }); 178 193 // convert remaining line breaks to <br> 179 return a.replace( /\s*\n\s*/g, '<wp-temp-br />');194 return a.replace( /\s*\n\s*/g, '<wp-temp-br />' ); 180 195 }); 181 196 } 182 197 183 198 pee = pee + '\n\n'; 184 pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n');185 pee = pee.replace( new RegExp('(<(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), '\n$1');186 pee = pee.replace( new RegExp('(</(?:'+blocklist+')>)', 'gi'), '$1\n\n');187 pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n'); // hr is self closing block element188 pee = pee.replace( /\r\n|\r/g, '\n');189 pee = pee.replace( /\n\s*\n+/g, '\n\n');190 pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n');191 pee = pee.replace( /<p>\s*?<\/p>/gi, '');192 pee = pee.replace( new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");193 pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1');194 pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>');195 pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>');196 pee = pee.replace( new RegExp('<p>\\s*(</?(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), "$1");197 pee = pee.replace( new RegExp('(</?(?:'+blocklist+')(?: [^>]*)?>)\\s*</p>', 'gi'), "$1");198 pee = pee.replace( /\s*\n/gi, '<br />\n');199 pee = pee.replace( new RegExp('(</?(?:'+blocklist+')[^>]*>)\\s*<br />', 'gi'), "$1");200 pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1');201 pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]');199 pee = pee.replace( /<br \/>\s*<br \/>/gi, '\n\n' ); 200 pee = pee.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n$1' ); 201 pee = pee.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); 202 pee = pee.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element 203 pee = pee.replace( /\r\n|\r/g, '\n' ); 204 pee = pee.replace( /\n\s*\n+/g, '\n\n' ); 205 pee = pee.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' ); 206 pee = pee.replace( /<p>\s*?<\/p>/gi, ''); 207 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' ); 208 pee = pee.replace( /<p>(<li.+?)<\/p>/gi, '$1'); 209 pee = pee.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>'); 210 pee = pee.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>'); 211 pee = pee.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); 212 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' ); 213 pee = pee.replace( /\s*\n/gi, '<br />\n'); 214 pee = pee.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' ); 215 pee = pee.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); 216 pee = pee.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' ); 202 217 203 pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function(a, b, c) {204 if ( c.match( /<p( [^>]*)?>/) )218 pee = pee.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { 219 if ( c.match( /<p( [^>]*)?>/ ) ) { 205 220 return a; 221 } 206 222 207 223 return b + '<p>' + c + '</p>'; 208 224 }); 209 225 210 226 // put back the line breaks in pre|script 211 if ( preserve_linebreaks ) 212 pee = pee.replace(/<wp-temp-lb>/g, '\n'); 227 if ( preserve_linebreaks ) { 228 pee = pee.replace( /<wp-temp-lb>/g, '\n' ); 229 } 213 230 214 if ( preserve_br ) 215 pee = pee.replace(/<wp-temp-br([^>]*)>/g, '<br$1>'); 231 if ( preserve_br ) { 232 pee = pee.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' ); 233 } 216 234 217 235 return pee; 218 236 }, 219 237 220 pre_wpautop : function(content) {238 pre_wpautop: function( content ) { 221 239 var t = this, o = { o: t, data: content, unfiltered: content }, 222 q = typeof( jQuery) != 'undefined';240 q = typeof( jQuery ) !== 'undefined'; 223 241 224 if ( q ) 225 jQuery('body').trigger('beforePreWpautop', [o]); 226 o.data = t._wp_Nop(o.data); 227 if ( q ) 228 jQuery('body').trigger('afterPreWpautop', [o]); 242 if ( q ) { 243 jQuery( 'body' ).trigger( 'beforePreWpautop', [ o ] ); 244 } 229 245 246 o.data = t._wp_Nop( o.data ); 247 248 if ( q ) { 249 jQuery('body').trigger('afterPreWpautop', [ o ] ); 250 } 251 230 252 return o.data; 231 253 }, 232 254 233 wpautop : function(pee) {255 wpautop: function( pee ) { 234 256 var t = this, o = { o: t, data: pee, unfiltered: pee }, 235 q = typeof( jQuery) != 'undefined';257 q = typeof( jQuery ) !== 'undefined'; 236 258 237 if ( q ) 238 jQuery('body').trigger('beforeWpautop', [o]); 239 o.data = t._wp_Autop(o.data); 240 if ( q ) 241 jQuery('body').trigger('afterWpautop', [o]); 259 if ( q ) { 260 jQuery( 'body' ).trigger('beforeWpautop', [ o ] ); 261 } 242 262 263 o.data = t._wp_Autop( o.data ); 264 265 if ( q ) { 266 jQuery( 'body' ).trigger('afterWpautop', [ o ] ); 267 } 268 243 269 return o.data; 244 270 } 245 } 271 };