Comments: Allow for `comment-reply.js` to be loaded in the HTML header.

Allows for themes or plugins setting the comment-reply JavaScript as a dependency of an HTML header script. This in turn causes `comment-reply.js` to be loaded early, requiring execution to be delayed.

Props pento, peterwilsoncc, jorbin for feedback.
Fixes #46280.


Built from https://develop.svn.wordpress.org/trunk@44794


git-svn-id: http://core.svn.wordpress.org/trunk@44626 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2019-03-05 01:52:51 +00:00
parent 9e8d0c14d3
commit 4094d3687a
3 changed files with 28 additions and 7 deletions

View File

@ -32,7 +32,7 @@ window.addComment = ( function( window ) {
* Check browser supports dataset.
* !! sets the variable to true if the property exists.
*/
var supportsDataset = !! document.body.dataset;
var supportsDataset = !! document.documentElement.dataset;
// For holding the cancel element.
var cancelElement;
@ -46,11 +46,24 @@ window.addComment = ( function( window ) {
// The mutation observer.
var observer;
// Initialise the events.
init();
if ( cutsTheMustard && document.readyState !== 'loading' ) {
ready();
} else if ( cutsTheMustard ) {
window.addEventListener( 'DOMContentLoaded', ready, false );
}
// Set up a MutationObserver to check for comments loaded late.
observeChanges();
/**
* Sets up object variables after the DOM is ready.
*
* @since 5.1.1
*/
function ready() {
// Initialise the events.
init();
// Set up a MutationObserver to check for comments loaded late.
observeChanges();
}
/**
* Add events to links classed .comment-reply-link.
@ -163,6 +176,14 @@ window.addComment = ( function( window ) {
postId = getDataAttribute( replyLink, 'postid'),
follow;
if ( ! commId || ! parentId || ! respondId || ! postId ) {
/*
* Theme or plugin defines own link via custom `wp_list_comments()` callback
* and calls `moveForm()` either directly or via a custom event hook.
*/
return;
}
/*
* Third party comments systems can hook into this function via the global scope,
* therefore the click event needs to reference the global scope.

View File

@ -1 +1 @@
window.addComment=function(a){function b(a){if(s&&(l=i(q.cancelReplyId),m=i(q.commentFormId),l)){l.addEventListener("touchstart",d),l.addEventListener("click",d);for(var b,f=c(a),g=0,h=f.length;g<h;g++)b=f[g],b.addEventListener("touchstart",e),b.addEventListener("click",e)}}function c(a){var b,c=q.commentReplyClass;return a&&a.childNodes||(a=p),b=p.getElementsByClassName?a.getElementsByClassName(c):a.querySelectorAll("."+c)}function d(a){var b=this,c=q.temporaryFormId,d=i(c);d&&n&&(i(q.parentIdFieldId).value="0",d.parentNode.replaceChild(n,d),b.style.display="none",a.preventDefault())}function e(b){var c,d=this,e=h(d,"belowelement"),f=h(d,"commentid"),g=h(d,"respondelement"),i=h(d,"postid");c=a.addComment.moveForm(e,f,g,i),!1===c&&b.preventDefault()}function f(){if(r){var a={childList:!0,subTree:!0};o=new r(g),o.observe(p.body,a)}}function g(a){for(var c=a.length;c--;)if(a[c].addedNodes.length)return void b()}function h(a,b){return t?a.dataset[b]:a.getAttribute("data-"+b)}function i(a){return p.getElementById(a)}function j(b,c,d,e){var f=i(b);n=i(d);var g,h,j,o=i(q.parentIdFieldId),r=i(q.postIdFieldId);if(f&&n&&o){k(n),e&&r&&(r.value=e),o.value=c,l.style.display="",f.parentNode.insertBefore(n,f.nextSibling),l.onclick=function(){return!1};try{for(var s=0;s<m.elements.length;s++)if(g=m.elements[s],h=!1,"getComputedStyle"in a?j=a.getComputedStyle(g):p.documentElement.currentStyle&&(j=g.currentStyle),(g.offsetWidth<=0&&g.offsetHeight<=0||"hidden"===j.visibility)&&(h=!0),"hidden"!==g.type&&!g.disabled&&!h){g.focus();break}}catch(t){}return!1}}function k(a){var b=q.temporaryFormId,c=i(b);c||(c=p.createElement("div"),c.id=b,c.style.display="none",a.parentNode.insertBefore(c,a))}var l,m,n,o,p=a.document,q={commentReplyClass:"comment-reply-link",cancelReplyId:"cancel-comment-reply-link",commentFormId:"commentform",temporaryFormId:"wp-temp-form-div",parentIdFieldId:"comment_parent",postIdFieldId:"comment_post_ID"},r=a.MutationObserver||a.WebKitMutationObserver||a.MozMutationObserver,s="querySelector"in p&&"addEventListener"in a,t=!!p.body.dataset;return b(),f(),{init:b,moveForm:j}}(window);
window.addComment=function(a){function b(){c(),g()}function c(a){if(t&&(m=j(r.cancelReplyId),n=j(r.commentFormId),m)){m.addEventListener("touchstart",e),m.addEventListener("click",e);for(var b,c=d(a),g=0,h=c.length;g<h;g++)b=c[g],b.addEventListener("touchstart",f),b.addEventListener("click",f)}}function d(a){var b,c=r.commentReplyClass;return a&&a.childNodes||(a=q),b=q.getElementsByClassName?a.getElementsByClassName(c):a.querySelectorAll("."+c)}function e(a){var b=this,c=r.temporaryFormId,d=j(c);d&&o&&(j(r.parentIdFieldId).value="0",d.parentNode.replaceChild(o,d),b.style.display="none",a.preventDefault())}function f(b){var c,d=this,e=i(d,"belowelement"),f=i(d,"commentid"),g=i(d,"respondelement"),h=i(d,"postid");e&&f&&g&&h&&(c=a.addComment.moveForm(e,f,g,h),!1===c&&b.preventDefault())}function g(){if(s){var a={childList:!0,subTree:!0};p=new s(h),p.observe(q.body,a)}}function h(a){for(var b=a.length;b--;)if(a[b].addedNodes.length)return void c()}function i(a,b){return u?a.dataset[b]:a.getAttribute("data-"+b)}function j(a){return q.getElementById(a)}function k(b,c,d,e){var f=j(b);o=j(d);var g,h,i,k=j(r.parentIdFieldId),p=j(r.postIdFieldId);if(f&&o&&k){l(o),e&&p&&(p.value=e),k.value=c,m.style.display="",f.parentNode.insertBefore(o,f.nextSibling),m.onclick=function(){return!1};try{for(var s=0;s<n.elements.length;s++)if(g=n.elements[s],h=!1,"getComputedStyle"in a?i=a.getComputedStyle(g):q.documentElement.currentStyle&&(i=g.currentStyle),(g.offsetWidth<=0&&g.offsetHeight<=0||"hidden"===i.visibility)&&(h=!0),"hidden"!==g.type&&!g.disabled&&!h){g.focus();break}}catch(t){}return!1}}function l(a){var b=r.temporaryFormId,c=j(b);c||(c=q.createElement("div"),c.id=b,c.style.display="none",a.parentNode.insertBefore(c,a))}var m,n,o,p,q=a.document,r={commentReplyClass:"comment-reply-link",cancelReplyId:"cancel-comment-reply-link",commentFormId:"commentform",temporaryFormId:"wp-temp-form-div",parentIdFieldId:"comment_parent",postIdFieldId:"comment_post_ID"},s=a.MutationObserver||a.WebKitMutationObserver||a.MozMutationObserver,t="querySelector"in q&&"addEventListener"in a,u=!!q.documentElement.dataset;return t&&"loading"!==q.readyState?b():t&&a.addEventListener("DOMContentLoaded",b,!1),{init:c,moveForm:k}}(window);

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.2-alpha-44793';
$wp_version = '5.2-alpha-44794';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.