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