diff --git a/src/wp-admin/js/editor.js b/src/wp-admin/js/editor.js
index e3fbaab..e50799c 100644
|
a
|
b
|
|
| 1 | 1 | |
| 2 | 2 | ( function( $ ) { |
| | 3 | /** |
| | 4 | * @summary Creates the tinyMCE editors. |
| | 5 | * |
| | 6 | * Creates the tinyMCE editor and binds all events used for switching |
| | 7 | * from visual to text mode. |
| | 8 | * |
| | 9 | * @since 3.7.9 |
| | 10 | * |
| | 11 | * @class |
| | 12 | */ |
| 3 | 13 | function SwitchEditors() { |
| 4 | 14 | var tinymce, $$, |
| 5 | 15 | exports = {}; |
| 6 | 16 | |
| | 17 | /** |
| | 18 | * @summary Initializes the event binding for switching editors. |
| | 19 | * |
| | 20 | * @since 3.7.10 |
| | 21 | * |
| | 22 | * @returns {void} |
| | 23 | */ |
| 7 | 24 | function init() { |
| 8 | 25 | if ( ! tinymce && window.tinymce ) { |
| 9 | 26 | tinymce = window.tinymce; |
| 10 | 27 | $$ = tinymce.$; |
| 11 | 28 | |
| | 29 | /** |
| | 30 | * @summary Handles onclick events for the editor buttons. |
| | 31 | * |
| | 32 | * @since 3.7.10 |
| | 33 | * |
| | 34 | * Handles an onclick event on the document. |
| | 35 | * Switches the editor between visual and text, |
| | 36 | * if the clicked element has the 'wp-switch-editor' class. |
| | 37 | * If the class name is switch-html switches to the HTML editor, |
| | 38 | * if the class name is switch-tmce |
| | 39 | * switches to the TMCE editor. |
| | 40 | * |
| | 41 | * @returns {void} |
| | 42 | */ |
| 12 | 43 | $$( document ).on( 'click', function( event ) { |
| 13 | 44 | var id, mode, |
| 14 | 45 | target = $$( event.target ); |
| … |
… |
|
| 22 | 53 | } |
| 23 | 54 | } |
| 24 | 55 | |
| | 56 | /** |
| | 57 | * @summary Retrieves the height of the toolbar based on the container the |
| | 58 | * editor is placed in. |
| | 59 | * |
| | 60 | * @since 3.7.2 |
| | 61 | * |
| | 62 | * @param {Object} editor The tinyMCE editor. |
| | 63 | * @returns {number} If the height is between 10 and 200 return the height, |
| | 64 | * else return 30. |
| | 65 | */ |
| 25 | 66 | function getToolbarHeight( editor ) { |
| 26 | 67 | var node = $$( '.mce-toolbar-grp', editor.getContainer() )[0], |
| 27 | 68 | height = node && node.clientHeight; |
| … |
… |
|
| 33 | 74 | return 30; |
| 34 | 75 | } |
| 35 | 76 | |
| | 77 | /** |
| | 78 | * @summary Switches the editor between visual and text. |
| | 79 | * |
| | 80 | * @since 2.5 |
| | 81 | * |
| | 82 | * @memberof switchEditors |
| | 83 | * |
| | 84 | * @param {string} id The id of the editor you want to change the editor mode for. |
| | 85 | * If no id is given, it defaults to content. |
| | 86 | * @param {string} mode The mode you want to switch to. |
| | 87 | * If an undefined mode is given, it defaults to toggle. |
| | 88 | * @returns {void} |
| | 89 | */ |
| 36 | 90 | function switchEditor( id, mode ) { |
| 37 | 91 | id = id || 'content'; |
| 38 | 92 | mode = mode || 'toggle'; |
| … |
… |
|
| 43 | 97 | $textarea = $$( '#' + id ), |
| 44 | 98 | textarea = $textarea[0]; |
| 45 | 99 | |
| | 100 | // Toggles the mode between visual and textual representation. |
| 46 | 101 | if ( 'toggle' === mode ) { |
| 47 | 102 | if ( editor && ! editor.isHidden() ) { |
| 48 | 103 | mode = 'html'; |
| … |
… |
|
| 51 | 106 | } |
| 52 | 107 | } |
| 53 | 108 | |
| | 109 | |
| | 110 | // If the mode is tmce or tinymce, show the editor. |
| 54 | 111 | if ( 'tmce' === mode || 'tinymce' === mode ) { |
| | 112 | |
| | 113 | /* |
| | 114 | * If the editor isn't hidden we are already in tmce mode |
| | 115 | * and we don't need to switch. |
| | 116 | * Returns false to stop event bubbling. |
| | 117 | */ |
| 55 | 118 | if ( editor && ! editor.isHidden() ) { |
| 56 | 119 | return false; |
| 57 | 120 | } |
| 58 | 121 | |
| | 122 | // Closes the QuickTags toolbars if they are visible. |
| 59 | 123 | if ( typeof( window.QTags ) !== 'undefined' ) { |
| 60 | 124 | window.QTags.closeAllTags( id ); |
| 61 | 125 | } |
| … |
… |
|
| 65 | 129 | if ( editor ) { |
| 66 | 130 | editor.show(); |
| 67 | 131 | |
| 68 | | // No point resizing the iframe in iOS |
| | 132 | // Don't resize the iframe in iOS. |
| 69 | 133 | if ( ! tinymce.Env.iOS && editorHeight ) { |
| 70 | 134 | toolbarHeight = getToolbarHeight( editor ); |
| 71 | 135 | editorHeight = editorHeight - toolbarHeight + 14; |
| 72 | 136 | |
| 73 | | // height cannot be under 50 or over 5000 |
| | 137 | // Height must be between 50 and 5000. |
| 74 | 138 | if ( editorHeight > 50 && editorHeight < 5000 ) { |
| 75 | 139 | editor.theme.resizeTo( null, editorHeight ); |
| 76 | 140 | } |
| … |
… |
|
| 83 | 147 | $textarea.attr( 'aria-hidden', true ); |
| 84 | 148 | window.setUserSetting( 'editor', 'tinymce' ); |
| 85 | 149 | |
| | 150 | // Hides the editor if mode is html. |
| 86 | 151 | } else if ( 'html' === mode ) { |
| | 152 | |
| | 153 | /* |
| | 154 | * If the editor is hidden we are already in html mode and |
| | 155 | * we don't need to switch. |
| | 156 | * Returns false to stop event bubbling. |
| | 157 | */ |
| 87 | 158 | if ( editor && editor.isHidden() ) { |
| 88 | 159 | return false; |
| 89 | 160 | } |
| 90 | 161 | |
| 91 | 162 | if ( editor ) { |
| | 163 | |
| | 164 | // Don't resize the iframe in iOS. |
| 92 | 165 | if ( ! tinymce.Env.iOS ) { |
| 93 | 166 | iframe = editor.iframeElement; |
| 94 | 167 | editorHeight = iframe ? parseInt( iframe.style.height, 10 ) : 0; |
| … |
… |
|
| 97 | 170 | toolbarHeight = getToolbarHeight( editor ); |
| 98 | 171 | editorHeight = editorHeight + toolbarHeight - 14; |
| 99 | 172 | |
| 100 | | // height cannot be under 50 or over 5000 |
| | 173 | // Height must be between 50 and 5000. |
| 101 | 174 | if ( editorHeight > 50 && editorHeight < 5000 ) { |
| 102 | 175 | textarea.style.height = editorHeight + 'px'; |
| 103 | 176 | } |
| … |
… |
|
| 106 | 179 | |
| 107 | 180 | editor.hide(); |
| 108 | 181 | } else { |
| 109 | | // The TinyMCE instance doesn't exist, show the textarea |
| | 182 | // The TinyMCE instance doesn't exist, show the textarea. |
| 110 | 183 | $textarea.css({ 'display': '', 'visibility': '' }); |
| 111 | 184 | } |
| 112 | 185 | |
| … |
… |
|
| 116 | 189 | } |
| 117 | 190 | } |
| 118 | 191 | |
| 119 | | // Replace paragraphs with double line breaks |
| | 192 | /** |
| | 193 | * @summary Replaces all paragraphs with double line breaks. |
| | 194 | * |
| | 195 | * Replaces all paragraphs with double line breaks. Taking into account |
| | 196 | * the elements where the <p> should be preserved. |
| | 197 | * Unifies all whitespaces. |
| | 198 | * Adds indenting with tabs to li, dt and dd elements. |
| | 199 | * Trims whitespaces from beginning and end of the html input. |
| | 200 | * |
| | 201 | * @since 3.7.10 |
| | 202 | * |
| | 203 | * @memberof switchEditors |
| | 204 | * |
| | 205 | * @param {string} html The content from the editor. |
| | 206 | * @return {string} The formatted html string. |
| | 207 | */ |
| 120 | 208 | function removep( html ) { |
| 121 | 209 | var blocklist = 'blockquote|ul|ol|li|dl|dt|dd|table|thead|tbody|tfoot|tr|th|td|h[1-6]|fieldset', |
| 122 | 210 | blocklist1 = blocklist + '|div|p', |
| … |
… |
|
| 129 | 217 | return ''; |
| 130 | 218 | } |
| 131 | 219 | |
| 132 | | // Preserve script and style tags. |
| | 220 | /* |
| | 221 | * Protects script and style tags by replacing them with <wp-preserve>. |
| | 222 | * Pushes matches into the preserve array. |
| | 223 | */ |
| 133 | 224 | if ( html.indexOf( '<script' ) !== -1 || html.indexOf( '<style' ) !== -1 ) { |
| 134 | 225 | html = html.replace( /<(script|style)[^>]*>[\s\S]*?<\/\1>/g, function( match ) { |
| 135 | 226 | preserve.push( match ); |
| … |
… |
|
| 137 | 228 | } ); |
| 138 | 229 | } |
| 139 | 230 | |
| 140 | | // Protect pre tags. |
| | 231 | /* |
| | 232 | * Protects pre tags by replacing all newlines and |
| | 233 | * <br> tags with <wp-line-break>. |
| | 234 | */ |
| 141 | 235 | if ( html.indexOf( '<pre' ) !== -1 ) { |
| 142 | 236 | preserve_linebreaks = true; |
| 143 | 237 | html = html.replace( /<pre[^>]*>[\s\S]+?<\/pre>/g, function( a ) { |
| … |
… |
|
| 147 | 241 | }); |
| 148 | 242 | } |
| 149 | 243 | |
| 150 | | // keep <br> tags inside captions and remove line breaks |
| | 244 | /* |
| | 245 | * Keeps <br> tags inside captions and remove line breaks by replacing |
| | 246 | * them with <wp-temp-br>. |
| | 247 | */ |
| 151 | 248 | if ( html.indexOf( '[caption' ) !== -1 ) { |
| 152 | 249 | preserve_br = true; |
| 153 | 250 | html = html.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { |
| … |
… |
|
| 155 | 252 | }); |
| 156 | 253 | } |
| 157 | 254 | |
| 158 | | // Pretty it up for the source editor |
| | 255 | // Formats the text to be readable in the source editor. |
| 159 | 256 | html = html.replace( new RegExp( '\\s*</(' + blocklist1 + ')>\\s*', 'g' ), '</$1>\n' ); |
| 160 | 257 | html = html.replace( new RegExp( '\\s*<((?:' + blocklist1 + ')(?: [^>]*)?)>', 'g' ), '\n<$1>' ); |
| 161 | 258 | |
| 162 | | // Mark </p> if it has any attributes. |
| | 259 | // Marks </p> if it has any attributes. |
| 163 | 260 | html = html.replace( /(<p [^>]+>.*?)<\/p>/g, '$1</p#>' ); |
| 164 | 261 | |
| 165 | | // Separate <div> containing <p> |
| | 262 | // If the content of a container starts with a paragraph, replace the <p> tag with 2 newlines. |
| 166 | 263 | html = html.replace( /<div( [^>]*)?>\s*<p>/gi, '<div$1>\n\n' ); |
| 167 | 264 | |
| 168 | | // Remove <p> and <br /> |
| | 265 | // Removes <p> and </p> tags. |
| 169 | 266 | html = html.replace( /\s*<p>/gi, '' ); |
| 170 | 267 | html = html.replace( /\s*<\/p>\s*/gi, '\n\n' ); |
| | 268 | |
| | 269 | // Removes white spaces between newlines. u00a0 is a no breaking space. |
| 171 | 270 | html = html.replace( /\n[\s\u00a0]+\n/g, '\n\n' ); |
| | 271 | |
| | 272 | // Removes <br> tags. |
| 172 | 273 | html = html.replace( /\s*<br ?\/?>\s*/gi, '\n' ); |
| 173 | 274 | |
| 174 | | // Fix some block element newline issues |
| | 275 | /* |
| | 276 | * Fixes some block element newline issues. |
| | 277 | * Replaces white spaces with newlines in combination with <div> tags. |
| | 278 | */ |
| 175 | 279 | html = html.replace( /\s*<div/g, '\n<div' ); |
| 176 | 280 | html = html.replace( /<\/div>\s*/g, '</div>\n' ); |
| | 281 | |
| | 282 | // Replaces white spaces with newlines in combination with [caption] shortcodes. |
| 177 | 283 | html = html.replace( /\s*\[caption([^\[]+)\[\/caption\]\s*/gi, '\n\n[caption$1[/caption]\n\n' ); |
| | 284 | |
| | 285 | /* |
| | 286 | * Limits the newlines in combination with [caption]'s to a maximum of |
| | 287 | * two consecutive occurrences. |
| | 288 | * . |
| | 289 | */ |
| 178 | 290 | html = html.replace( /caption\]\n\n+\[caption/g, 'caption]\n\n[caption' ); |
| 179 | 291 | |
| | 292 | /* |
| | 293 | * Replaces white spaces with newlines in combination with |
| | 294 | * all elements listed in blocklist2. |
| | 295 | */ |
| 180 | 296 | html = html.replace( new RegExp('\\s*<((?:' + blocklist2 + ')(?: [^>]*)?)\\s*>', 'g' ), '\n<$1>' ); |
| 181 | 297 | html = html.replace( new RegExp('\\s*</(' + blocklist2 + ')>\\s*', 'g' ), '</$1>\n' ); |
| | 298 | |
| | 299 | // Adds indentation by adding a tab in front of <li>, <dt> and <dd> tags. |
| 182 | 300 | html = html.replace( /<((li|dt|dd)[^>]*)>/g, ' \t<$1>' ); |
| 183 | 301 | |
| | 302 | // Replaces white spaces with newlines in combination with <select> and <option> tags. |
| 184 | 303 | if ( html.indexOf( '<option' ) !== -1 ) { |
| 185 | 304 | html = html.replace( /\s*<option/g, '\n<option' ); |
| 186 | 305 | html = html.replace( /\s*<\/select>/g, '\n</select>' ); |
| 187 | 306 | } |
| 188 | 307 | |
| | 308 | // Replaces white spaces with 2 newlines in combination with <hr> tags. |
| 189 | 309 | if ( html.indexOf( '<hr' ) !== -1 ) { |
| 190 | 310 | html = html.replace( /\s*<hr( [^>]*)?>\s*/g, '\n\n<hr$1>\n\n' ); |
| 191 | 311 | } |
| 192 | 312 | |
| | 313 | // Removes newlines in <object> tags. |
| 193 | 314 | if ( html.indexOf( '<object' ) !== -1 ) { |
| 194 | 315 | html = html.replace( /<object[\s\S]+?<\/object>/g, function( a ) { |
| 195 | 316 | return a.replace( /[\r\n]+/g, '' ); |
| 196 | 317 | }); |
| 197 | 318 | } |
| 198 | 319 | |
| 199 | | // Unmark special paragraph closing tags |
| | 320 | // Unmarks special paragraph closing tags. |
| 200 | 321 | html = html.replace( /<\/p#>/g, '</p>\n' ); |
| | 322 | |
| | 323 | |
| | 324 | // Adds a new line before <p> tags when there is content inside the paragraph |
| 201 | 325 | html = html.replace( /\s*(<p [^>]+>[\s\S]*?<\/p>)/g, '\n$1' ); |
| 202 | 326 | |
| 203 | | // Trim whitespace |
| | 327 | /* |
| | 328 | * Removes whitespaces at the start and end of a string. |
| | 329 | * u00a0 is a no breaking space. |
| | 330 | */ |
| 204 | 331 | html = html.replace( /^\s+/, '' ); |
| 205 | 332 | html = html.replace( /[\s\u00a0]+$/, '' ); |
| 206 | 333 | |
| 207 | | // put back the line breaks in pre|script |
| | 334 | // Replaces <wp-line-break> tags with a newline. |
| 208 | 335 | if ( preserve_linebreaks ) { |
| 209 | 336 | html = html.replace( /<wp-line-break>/g, '\n' ); |
| 210 | 337 | } |
| 211 | 338 | |
| 212 | | // and the <br> tags in captions |
| | 339 | // Restores the <wp-temp-br> with <br> tags. |
| 213 | 340 | if ( preserve_br ) { |
| 214 | 341 | html = html.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' ); |
| 215 | 342 | } |
| 216 | 343 | |
| 217 | | // Put back preserved tags. |
| | 344 | // Restores preserved tags. |
| 218 | 345 | if ( preserve.length ) { |
| 219 | 346 | html = html.replace( /<wp-preserve>/g, function() { |
| 220 | 347 | return preserve.shift(); |
| … |
… |
|
| 224 | 351 | return html; |
| 225 | 352 | } |
| 226 | 353 | |
| 227 | | // Similar to `wpautop()` in formatting.php |
| | 354 | /** |
| | 355 | * @summary Adds paragraph tags to the text. |
| | 356 | * |
| | 357 | * Adds paragraph tags to the text taking into account block level elements. |
| | 358 | * Normalizes the whitespaces and newlines. |
| | 359 | * |
| | 360 | * Similar to `wpautop()` in formatting.php. |
| | 361 | * |
| | 362 | * @since 3.7.10 |
| | 363 | * |
| | 364 | * @memberof switchEditors |
| | 365 | * |
| | 366 | * @param {string} text The text input. |
| | 367 | * @returns {string} The formatted text. |
| | 368 | */ |
| 228 | 369 | function autop( text ) { |
| 229 | 370 | var preserve_linebreaks = false, |
| 230 | 371 | preserve_br = false, |
| | 372 | |
| | 373 | // A list containing all block level elements. |
| 231 | 374 | blocklist = 'table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre' + |
| 232 | 375 | '|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section' + |
| 233 | 376 | '|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary'; |
| 234 | 377 | |
| 235 | | // Normalize line breaks |
| | 378 | // Normalizes line breaks. |
| 236 | 379 | text = text.replace( /\r\n|\r/g, '\n' ); |
| 237 | 380 | |
| | 381 | // If there are no newlines, return the text. |
| 238 | 382 | if ( text.indexOf( '\n' ) === -1 ) { |
| 239 | 383 | return text; |
| 240 | 384 | } |
| 241 | 385 | |
| 242 | 386 | if ( text.indexOf( '<object' ) !== -1 ) { |
| | 387 | |
| | 388 | // If there are multiple newlines in an <object>, remove them. |
| 243 | 389 | text = text.replace( /<object[\s\S]+?<\/object>/g, function( a ) { |
| 244 | 390 | return a.replace( /\n+/g, '' ); |
| 245 | 391 | }); |
| 246 | 392 | } |
| 247 | 393 | |
| | 394 | // Replaces all new lines and tabs with spaces inside tags. |
| 248 | 395 | text = text.replace( /<[^<>]+>/g, function( a ) { |
| 249 | 396 | return a.replace( /[\n\t ]+/g, ' ' ); |
| 250 | 397 | }); |
| 251 | 398 | |
| 252 | | // Protect pre|script tags |
| | 399 | // Protects <pre> and <script> tags by replacing them with <wp-line-break>. |
| 253 | 400 | if ( text.indexOf( '<pre' ) !== -1 || text.indexOf( '<script' ) !== -1 ) { |
| 254 | 401 | preserve_linebreaks = true; |
| 255 | 402 | text = text.replace( /<(pre|script)[^>]*>[\s\S]*?<\/\1>/g, function( a ) { |
| … |
… |
|
| 257 | 404 | }); |
| 258 | 405 | } |
| 259 | 406 | |
| 260 | | // keep <br> tags inside captions and convert line breaks |
| | 407 | // Keeps <br> tags inside captions. |
| 261 | 408 | if ( text.indexOf( '[caption' ) !== -1 ) { |
| 262 | 409 | preserve_br = true; |
| | 410 | |
| | 411 | // Replaces all white spaces and <br> tags with <wp-temp-br>. |
| 263 | 412 | text = text.replace( /\[caption[\s\S]+?\[\/caption\]/g, function( a ) { |
| 264 | | // keep existing <br> |
| | 413 | |
| | 414 | // Protects <br> tags by converting them to <wp-temp-br> tags. |
| 265 | 415 | a = a.replace( /<br([^>]*)>/g, '<wp-temp-br$1>' ); |
| 266 | | // no line breaks inside HTML tags |
| | 416 | |
| | 417 | // Replaces all new lines and tabs with spaces inside HTML tags. |
| 267 | 418 | a = a.replace( /<[^<>]+>/g, function( b ) { |
| | 419 | |
| | 420 | // Replaces newlines and tabs with a space. |
| 268 | 421 | return b.replace( /[\n\t ]+/, ' ' ); |
| 269 | 422 | }); |
| 270 | | // convert remaining line breaks to <br> |
| | 423 | |
| | 424 | // Converts remaining line breaks to <wp-temp-br />. |
| 271 | 425 | return a.replace( /\s*\n\s*/g, '<wp-temp-br />' ); |
| 272 | 426 | }); |
| 273 | 427 | } |
| 274 | 428 | |
| | 429 | // Appends 2 newlines at the end of the text. |
| 275 | 430 | text = text + '\n\n'; |
| | 431 | |
| | 432 | /* |
| | 433 | * Replaces a <br> tag followed by 1 or more spaces |
| | 434 | * and another <br> tag with 2 newlines. |
| | 435 | */ |
| 276 | 436 | text = text.replace( /<br \/>\s*<br \/>/gi, '\n\n' ); |
| | 437 | |
| | 438 | /* |
| | 439 | * Replaces a block level element open tag with 2 newlines |
| | 440 | * followed by the captured block level element. |
| | 441 | */ |
| 277 | 442 | text = text.replace( new RegExp( '(<(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '\n\n$1' ); |
| | 443 | |
| | 444 | /* |
| | 445 | * Replaces a block level element closing tag with the captured |
| | 446 | * block level element followed by 2 newlines. |
| | 447 | */ |
| 278 | 448 | text = text.replace( new RegExp( '(</(?:' + blocklist + ')>)', 'gi' ), '$1\n\n' ); |
| 279 | | text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); // hr is self closing block element |
| 280 | | text = text.replace( /\s*<option/gi, '<option' ); // No <p> or <br> around <option> |
| | 449 | |
| | 450 | // Adds 2 newlines to a <hr> tag. <hr> is a self closing block element. |
| | 451 | text = text.replace( /<hr( [^>]*)?>/gi, '<hr$1>\n\n' ); |
| | 452 | |
| | 453 | // Removes the spaces before an <option> tag. |
| | 454 | text = text.replace( /\s*<option/gi, '<option' ); |
| | 455 | |
| | 456 | // Removes the spaces after an <option> closing tag. |
| 281 | 457 | text = text.replace( /<\/option>\s*/gi, '</option>' ); |
| | 458 | |
| | 459 | // Removes the spaces between two newlines. |
| 282 | 460 | text = text.replace( /\n\s*\n+/g, '\n\n' ); |
| | 461 | |
| | 462 | // Converts 2 newlines to a paragraph and a single newline. |
| 283 | 463 | text = text.replace( /([\s\S]+?)\n\n/g, '<p>$1</p>\n' ); |
| | 464 | |
| | 465 | // Removes empty paragraphs. |
| 284 | 466 | text = text.replace( /<p>\s*?<\/p>/gi, ''); |
| | 467 | |
| | 468 | // Removes spaces and <p> tags around block level elements. |
| 285 | 469 | text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' ); |
| | 470 | |
| | 471 | // Removes <p> tags around li elements. |
| 286 | 472 | text = text.replace( /<p>(<li.+?)<\/p>/gi, '$1'); |
| | 473 | |
| | 474 | // Removes spaces and <p> tags from blockquotes. |
| 287 | 475 | text = text.replace( /<p>\s*<blockquote([^>]*)>/gi, '<blockquote$1><p>'); |
| | 476 | |
| | 477 | // Places the <blockquote> outside of the paragraph. |
| 288 | 478 | text = text.replace( /<\/blockquote>\s*<\/p>/gi, '</p></blockquote>'); |
| | 479 | |
| | 480 | // Removes spaces at the start and <p> tags from block level elements. |
| 289 | 481 | text = text.replace( new RegExp( '<p>\\s*(</?(?:' + blocklist + ')(?: [^>]*)?>)', 'gi' ), '$1' ); |
| | 482 | |
| | 483 | // Removes spaces at the end and <p> tags from block level elements. |
| 290 | 484 | text = text.replace( new RegExp( '(</?(?:' + blocklist + ')(?: [^>]*)?>)\\s*</p>', 'gi' ), '$1' ); |
| 291 | 485 | |
| 292 | | // Remove redundant spaces and line breaks after existing <br /> tags |
| | 486 | // Removes spaces and newlines after a <br> tag. |
| 293 | 487 | text = text.replace( /(<br[^>]*>)\s*\n/gi, '$1' ); |
| 294 | 488 | |
| 295 | | // Create <br /> from the remaining line breaks |
| | 489 | // Replaces spaces followed by a newline with a <br> tag followed by a new line. |
| 296 | 490 | text = text.replace( /\s*\n/g, '<br />\n'); |
| 297 | 491 | |
| | 492 | // Removes <br> tag that follows a block element directly, ignoring spaces. |
| 298 | 493 | text = text.replace( new RegExp( '(</?(?:' + blocklist + ')[^>]*>)\\s*<br />', 'gi' ), '$1' ); |
| | 494 | |
| | 495 | /* |
| | 496 | * Removes a br tag preceding white spaces followed by a |
| | 497 | * <p>, <li>, <div>, <dl>, <dd>, <dt>, <th>, <pre>, <td>, <ul>, or <ol> tag. |
| | 498 | */ |
| 299 | 499 | text = text.replace( /<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)/gi, '$1' ); |
| | 500 | |
| | 501 | // Removes white spaces, <p> and <br> tags in captions. |
| 300 | 502 | text = text.replace( /(?:<p>|<br ?\/?>)*\s*\[caption([^\[]+)\[\/caption\]\s*(?:<\/p>|<br ?\/?>)*/gi, '[caption$1[/caption]' ); |
| 301 | 503 | |
| | 504 | /** |
| | 505 | * @summary Makes sure there is a paragraph open tag for a close tag. |
| | 506 | * |
| | 507 | * @since 3.5.2 |
| | 508 | * |
| | 509 | * Makes sure there is a paragraph open tag when there is a paragraph close tag |
| | 510 | * in a div, th, td, form, fieldset or dd element. |
| | 511 | * @param {string} a The complete match. |
| | 512 | * @param {string} b The first capture group. |
| | 513 | * @param {string} c The second capture group. |
| | 514 | * @returns {string} The string in paragraph tags. |
| | 515 | */ |
| 302 | 516 | text = text.replace( /(<(?:div|th|td|form|fieldset|dd)[^>]*>)(.*?)<\/p>/g, function( a, b, c ) { |
| | 517 | |
| | 518 | /* |
| | 519 | * Checks if the matched group has a p open tag in it. If so, we don't need to |
| | 520 | * enclose it with a paragraph. |
| | 521 | */ |
| 303 | 522 | if ( c.match( /<p( [^>]*)?>/ ) ) { |
| 304 | 523 | return a; |
| 305 | 524 | } |
| 306 | 525 | |
| | 526 | /* |
| | 527 | * If there is no p open tag in the matched string, |
| | 528 | * add it and return the string including p tags. |
| | 529 | */ |
| 307 | 530 | return b + '<p>' + c + '</p>'; |
| 308 | 531 | }); |
| 309 | 532 | |
| 310 | | // put back the line breaks in pre|script |
| | 533 | // Restores the line breaks in <pre> and <script> tags. |
| 311 | 534 | if ( preserve_linebreaks ) { |
| 312 | 535 | text = text.replace( /<wp-line-break>/g, '\n' ); |
| 313 | 536 | } |
| 314 | 537 | |
| | 538 | // Restores the <br> tags in captions. |
| 315 | 539 | if ( preserve_br ) { |
| 316 | 540 | text = text.replace( /<wp-temp-br([^>]*)>/g, '<br$1>' ); |
| 317 | 541 | } |
| … |
… |
|
| 319 | 543 | return text; |
| 320 | 544 | } |
| 321 | 545 | |
| 322 | | // Add old events |
| | 546 | /** |
| | 547 | * @summary Modifies the data when a switch is made from HTML to text. |
| | 548 | * |
| | 549 | * Modifies the data when a switch is made. |
| | 550 | * Remove the <p> tags from text. |
| | 551 | * Returns the modified text. |
| | 552 | * Adds a trigger on beforePreWpautop and afterPreWpautop. |
| | 553 | * |
| | 554 | * @since 3.5.2 |
| | 555 | * |
| | 556 | * @memberof switchEditors |
| | 557 | * |
| | 558 | * @param {String} html The content from the visual editor. |
| | 559 | * @returns {String} the modified text. |
| | 560 | */ |
| 323 | 561 | function pre_wpautop( html ) { |
| 324 | 562 | var obj = { o: exports, data: html, unfiltered: html }; |
| 325 | 563 | |
| … |
… |
|
| 336 | 574 | return obj.data; |
| 337 | 575 | } |
| 338 | 576 | |
| | 577 | /** |
| | 578 | * @summary Modifies the data when a switch is made from text to HTML. |
| | 579 | * |
| | 580 | * Modifies the data when a switch is made. Runs autop to add p tags from text. |
| | 581 | * Returns the modified text. Adds a trigger on beforeWpautop and afterWpautop. |
| | 582 | * |
| | 583 | * @since 3.5.2 |
| | 584 | * |
| | 585 | * @memberof switchEditors |
| | 586 | * |
| | 587 | * @param {String} text The content from the text editor. |
| | 588 | * @returns {String} the modified text. |
| | 589 | */ |
| 339 | 590 | function wpautop( text ) { |
| 340 | 591 | var obj = { o: exports, data: text, unfiltered: text }; |
| 341 | 592 | |
| … |
… |
|
| 352 | 603 | return obj.data; |
| 353 | 604 | } |
| 354 | 605 | |
| | 606 | // Binds the init function to be run when the document is loaded. |
| 355 | 607 | if ( $ ) { |
| 356 | 608 | $( document ).ready( init ); |
| 357 | 609 | } else if ( document.addEventListener ) { |
| | 610 | |
| | 611 | // Uses the addEventListener to bind the init event on document load. |
| 358 | 612 | document.addEventListener( 'DOMContentLoaded', init, false ); |
| 359 | 613 | window.addEventListener( 'load', init, false ); |
| | 614 | |
| 360 | 615 | } else if ( window.attachEvent ) { |
| | 616 | |
| | 617 | // Uses the addEvent to bind the init event on document load. |
| 361 | 618 | window.attachEvent( 'onload', init ); |
| 362 | 619 | document.attachEvent( 'onreadystatechange', function() { |
| 363 | 620 | if ( 'complete' === document.readyState ) { |
| … |
… |
|
| 366 | 623 | } ); |
| 367 | 624 | } |
| 368 | 625 | |
| | 626 | /* |
| | 627 | * Makes sure the window.wp object exists so autop and removep |
| | 628 | * can be bound to it. |
| | 629 | */ |
| 369 | 630 | window.wp = window.wp || {}; |
| 370 | 631 | window.wp.editor = window.wp.editor || {}; |
| 371 | 632 | window.wp.editor.autop = wpautop; |
| … |
… |
|
| 382 | 643 | return exports; |
| 383 | 644 | } |
| 384 | 645 | |
| | 646 | /** |
| | 647 | * @namespace {SwitchEditors} switchEditors |
| | 648 | * Expose the switch editors to be used globally. |
| | 649 | */ |
| 385 | 650 | window.switchEditors = new SwitchEditors(); |
| 386 | 651 | }( window.jQuery )); |