Make WordPress Core

Ticket #46260: 46260.4.diff

File 46260.4.diff, 2.1 KB (added by peterwilsoncc, 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..fae92ee237 100644
    a b window.addComment = ( function( window ) { 
    2222                postIdFieldId     : 'comment_post_ID'
    2323        };
    2424
     25        // Cross browser MutationObserver.
     26        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
     27
    2528        // Check browser cuts the mustard.
    2629        var cutsTheMustard = 'querySelector' in document && 'addEventListener' in window;
    2730
    window.addComment = ( function( window ) { 
    4043        // The respond element.
    4144        var respondElement;
    4245
     46        // The mutation observer.
     47        var observer;
     48
    4349        // Initialise the events.
    4450        init();
    4551
     52        // Set up a MutationObserver to check for comments loaded late.
     53        observeChanges();
     54
    4655        /**
    4756         * Add events to links classed .comment-reply-link.
    4857         *
    window.addComment = ( function( window ) { 
    5766         * @param {HTMLElement} context The parent DOM element to search for links.
    5867         */
    5968        function init( context ) {
    60                 if ( true !== cutsTheMustard ) {
     69                if ( ! cutsTheMustard ) {
    6170                        return;
    6271                }
    6372
    window.addComment = ( function( window ) { 
    164173                }
    165174        }
    166175
     176        /**
     177         * Creates a mutation observer to check for newly inserted comments.
     178         *
     179         * @since 5.1.0
     180         */
     181        function observeChanges() {
     182                if ( ! MutationObserver ) {
     183                        return;
     184                }
     185
     186                var observerOptions = {
     187                        childList: true,
     188                        subTree: true
     189                };
     190
     191                observer = new MutationObserver( handleChanges );
     192                observer.observe( document.body, observerOptions );
     193        }
     194
     195        /**
     196         * Handles DOM changes, calling init() if any new nodes are added.
     197         *
     198         * @since 5.1.0
     199         *
     200         * @param {Array} mutationRecords Array of MutationRecord objects.
     201         */
     202        function handleChanges( mutationRecords ) {
     203                var i = mutationRecords.length;
     204
     205                while ( i-- ) {
     206                        // Call init() once if any record in this set adds nodes.
     207                        if ( mutationRecords[ i ].addedNodes.length ) {
     208                                init();
     209                                return;
     210                        }
     211                }
     212        }
     213
    167214        /**
    168215         * Backward compatible getter of data-* attribute.
    169216         *