Make WordPress Core

Ticket #46260: 46260.3.diff

File 46260.3.diff, 1.6 KB (added by pento, 6 years ago)
  • src/js/_enqueues/lib/comment-reply.js

    diff --git a/src/js/_enqueues/lib/comment-reply.js b/src/js/_enqueues/lib/comment-reply.js
    index 71f997328e..01eac4cdbd 100644
    a b window.addComment = ( function( window ) { 
    4040        // The respond element.
    4141        var respondElement;
    4242
     43        // The mutation observer.
     44        var observer;
     45
    4346        // Initialise the events.
    4447        init();
    4548
     49        // Set up a MutationObserver to check for comments loaded late.
     50        observeChanges();
     51
    4652        /**
    4753         * Add events to links classed .comment-reply-link.
    4854         *
    window.addComment = ( function( window ) { 
    164170                }
    165171        }
    166172
     173        /**
     174         * Creates a mutation observer to check for newly inserted comments.
     175         */
     176        function observeChanges() {
     177                var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
     178                var observerOptions = {
     179                        childList: true,
     180                        subTree: true,
     181                };
     182
     183                if ( ! MutationObserver ) {
     184                        return;
     185                }
     186
     187                observer = new MutationObserver( handleChanges );
     188                observer.observe( document.body, observerOptions );
     189        }
     190
     191        /**
     192         * Handles DOM changes, calling init() if any new nodes are added.
     193         *
     194         * @since 5.1.0
     195         *
     196         * @param {Array} mutationRecords Array of MutationRecord objects.
     197         */
     198        function handleChanges( mutationRecords ) {
     199                var i = mutationRecords.length;
     200
     201                while ( i-- ) {
     202                        // Call init() once if any record in this set adds nodes.
     203                        if ( mutationRecords[ i ].addedNodes.length ) {
     204                                init();
     205                                return;
     206                        }
     207                }
     208        }
     209
    167210        /**
    168211         * Backward compatible getter of data-* attribute.
    169212         *