Make WordPress Core

Changeset 41656


Ignore:
Timestamp:
10/01/2017 10:32:58 AM (7 years ago)
Author:
azaozz
Message:

Editor: Use editor.$ to improve removeSelectionMarker().

See #42029

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/js/editor.js

    r41655 r41656  
    509509
    510510
    511             removeSelectionMarker( editor, startNode );
    512             removeSelectionMarker( editor, endNode );
    513         }
    514 
    515         /**
    516          * @summary Remove selection marker with optional `<p>` parent.
    517          *
    518          * By default TinyMCE puts every inline node at the main level in a `<p>` wrapping tag.
    519          *
    520          * In the case with selection markers, when removed they leave an empty `<p>` behind,
    521          * which adds an empty paragraph line with `&nbsp;` when switched to Text mode.
    522          *
    523          * In order to prevent that the wrapping `<p>` needs to be removed when removing the
    524          * selection marker.
    525          *
    526          * @param {object} editor The TinyMCE Editor instance
    527          * @param {object} marker The marker to be removed from the editor DOM
    528          */
    529         function removeSelectionMarker( editor, marker ) {
    530             var markerParent = editor.$( marker ).parent();
    531 
    532             if (
    533                 ! markerParent.length ||
    534                 markerParent.prop('tagName').toLowerCase() !== 'p' ||
    535                 markerParent[0].childNodes.length > 1 ||
    536                 ! markerParent.prop('outerHTML').match(/^<p>/)
    537             ) {
    538                 /**
    539                  * The selection marker is not self-contained in a <p>.
    540                  * In this case only the selection marker is removed, since
    541                  * it will affect the content.
    542                  */
    543                 marker.remove();
    544             }
    545             else {
    546                 /**
    547                  * The marker is self-contained in an blank `<p>` tag.
    548                  *
    549                  * This is usually inserted by TinyMCE
    550                  */
    551                 markerParent.remove();
     511            removeSelectionMarker( startNode );
     512            removeSelectionMarker( endNode );
     513        }
     514
     515        /**
     516         * @summary Remove selection marker and the parent node if it is an empty paragraph.
     517         *
     518         * By default TinyMCE wraps loose inline tags in a `<p>`.
     519         * When removing selection markers an empty `<p>` may be left behind, remove it.
     520         *
     521         * @param {object} $marker The marker to be removed from the editor DOM, wrapped in an instnce of `editor.$`
     522         */
     523        function removeSelectionMarker( $marker ) {
     524            var $markerParent = $marker.parent();
     525
     526            $marker.remove();
     527
     528            //Remove empty paragraph left over after removing the marker.
     529            if ( $markerParent.is( 'p' ) && ! $markerParent.children().length && ! $markerParent.text() ) {
     530                $markerParent.remove();
    552531            }
    553532        }
Note: See TracChangeset for help on using the changeset viewer.