diff --git a/VisualEditor.php b/VisualEditor.php index 65e810c220..522a50c8ec 100644 --- a/VisualEditor.php +++ b/VisualEditor.php @@ -67,6 +67,11 @@ $wgResourceModules += array( 'jquery/jquery.multiSuggest.js' ), ), + 'jquery.visibleText' => $wgVisualEditorResourceTemplate + array( + 'scripts' => array( + 'jquery/jquery.visibleText.js' + ), + ), // Alias for backwards compat, safe to remove after 'ext.visualEditor.editPageInit' => $wgVisualEditorResourceTemplate + array( 'dependencies' => array( @@ -113,6 +118,7 @@ $wgResourceModules += array( 'jquery.byteLimit', 'jquery.client', 'jquery.placeholder', + 'jquery.visibleText', 'mediawiki.jqueryMsg', 'mediawiki.Title', 'mediawiki.Uri', diff --git a/modules/jquery/jquery.visibleText.js b/modules/jquery/jquery.visibleText.js new file mode 100644 index 0000000000..b49d75a0dd --- /dev/null +++ b/modules/jquery/jquery.visibleText.js @@ -0,0 +1,46 @@ +/** + * jQuery visibleText plugin 0.1.0 + * https://github.com/Krinkle/jquery-visibleText + * + * @author Timo Tijhof, 2012 + * @source This plugin is based on Sizzle.getText. + * Copyright 2012 jQuery Foundation and other contributors http://jquery.com/ + * @license MIT License + */ +(function ($) { + + /** + * @param {Array|jQuery|HTMLElement} elem + */ + var getVisibleText = $.getVisibleText = function (elem) { + var node, + i = 0, + ret = '', + nodeType = elem.nodeType; + + if (nodeType) { + if (nodeType === 1 || nodeType === 9 || nodeType === 11) { + // Traverse the children + for (elem = elem.firstChild; elem; elem = elem.nextSibling) { + ret += $.expr.filters.hidden(elem) ? + '' : + getVisibleText(elem); + } + } else if (nodeType === 3 || nodeType === 4) { + return elem.nodeValue; + } + } else { + + // If no nodeType, this is expected to be an array (or jQuery object) + for (; (node = elem[i]); i++) { + ret += getVisibleText(node); + } + } + return ret; + }; + + $.fn.visibleText = function () { + return getVisibleText(this); + }; + +}(jQuery)); diff --git a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js index 8ed546c90d..f71a010f1a 100644 --- a/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js +++ b/modules/ve/init/mw/targets/ve.init.mw.ViewPageTarget.js @@ -646,11 +646,7 @@ ve.init.mw.ViewPageTarget.prototype.setupToolbarEditNotices = function () { var key; this.$toolbarEditNotices.empty(); for ( key in this.editNotices ) { - this.$toolbarEditNotices.append( - $( '
' ) - .addClass( 've-init-mw-viewPageTarget-toolbar-editNotices-notice' ) - .attr( 'rel', key ).html( this.editNotices[key] ) - ); + this.$toolbarEditNotices.append( this.editNotices[key] ); } }; diff --git a/modules/ve/init/mw/ve.init.mw.Target.js b/modules/ve/init/mw/ve.init.mw.Target.js index fa6fe658c1..4e7509d97e 100644 --- a/modules/ve/init/mw/ve.init.mw.Target.js +++ b/modules/ve/init/mw/ve.init.mw.Target.js @@ -68,7 +68,9 @@ ve.inheritClass( ve.init.mw.Target, ve.EventEmitter ); * @emits loadError (null, message, null) */ ve.init.mw.Target.onLoad = function ( response ) { - var data = response.visualeditor; + var key, tmp, el, + data = response.visualeditor; + if ( !data && !response.error ) { ve.init.mw.Target.onLoadError.call( this, null, 'Invalid response in response from server', null @@ -81,7 +83,40 @@ ve.init.mw.Target.onLoad = function ( response ) { ); } else { this.dom = $( '
' ).html( data.content )[0]; - this.editNotices = data.notices; + + /** + * Don't show notices with no visible html (bug 43013). + */ + + // Since we're going to parse them, we might as well save these nodes + // so we don't have to parse them again later. + this.editNotices = {}; + + // This is a temporary container for parsed notices in the . + // We need the elements to be in the DOM in order for stylesheets to apply + // and jquery.visibleText to determine whether a node is visible. + tmp = document.createElement( 'div' ); + + // The following is essentially display none, but we can't use that + // since then then all descendants will be considered invisible too. + tmp.style.cssText = 'position: static; top: 0; width: 0; height: 0; border: 0; visibility: hidden'; + + document.body.appendChild( tmp ); + for ( key in data.notices ) { + el = $( '
' ) + .addClass( 've-init-mw-viewPageTarget-toolbar-editNotices-notice' ) + .attr( 'rel', key ) + .html( data.notices[key] ) + .get( 0 ); + + tmp.appendChild( el ); + if ( $.getVisibleText( el ).trim() !== '' ) { + this.editNotices[key] = el; + } + tmp.removeChild( el ); + } + document.body.removeChild( tmp ); + this.baseTimeStamp = data.basetimestamp; this.startTimeStamp = data.starttimestamp; // Everything worked, the page was loaded, continue as soon as the module is ready