Make WordPress Core

Changeset 44751


Ignore:
Timestamp:
02/19/2019 02:00:21 AM (8 years ago)
Author:
pento
Message:

Comments: Fix backward compatibility regressions in comment reply JavaScript.

Adds a MutationObserver to comment-reply.js to allow for lazy-loaded comments to continue working without the need to re-initialize the comment form.

Merges [44748] to the 5.1 branch.

Fixes #46260.

Location:
branches/5.1
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.1

  • branches/5.1/src/js/_enqueues/lib/comment-reply.js

    r44388 r44751  
    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;
     
    4144        var respondElement;
    4245
     46        // The mutation observer.
     47        var observer;
     48
    4349        // Initialise the events.
    4450        init();
     51
     52        // Set up a MutationObserver to check for comments loaded late.
     53        observeChanges();
    4554
    4655        /**
     
    5867         */
    5968        function init( context ) {
    60                 if ( true !== cutsTheMustard ) {
     69                if ( ! cutsTheMustard ) {
    6170                        return;
    6271                }
     
    162171                if ( false === follow ) {
    163172                        event.preventDefault();
     173                }
     174        }
     175
     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                        }
    164211                }
    165212        }
Note: See TracChangeset for help on using the changeset viewer.