mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-29 00:30:44 +00:00
Merge "Fix template rendering"
This commit is contained in:
commit
65a27b48ce
|
@ -89,21 +89,53 @@ ve.ce.MWTransclusionNode.static.getDescription = function ( model ) {
|
|||
* @return {HTMLElement[]} Filtered rendered nodes
|
||||
*/
|
||||
ve.ce.MWTransclusionNode.static.filterRendering = function ( contentNodes ) {
|
||||
// Filter out auto-generated items, e.g. reference lists
|
||||
contentNodes = contentNodes.filter( function ( node ) {
|
||||
var dataMw = node.nodeType === Node.ELEMENT_NODE &&
|
||||
var whitespaceRegex, wrapper;
|
||||
|
||||
if ( !contentNodes.length ) {
|
||||
return;
|
||||
}
|
||||
|
||||
whitespaceRegex = new RegExp( '^[' + ve.dm.Converter.static.whitespaceList + ']+$' );
|
||||
wrapper = contentNodes[ 0 ].ownerDocument.createElement( 'div' );
|
||||
|
||||
// Unwrap Parsoid sections (which probably shouldn't exist: T181226)
|
||||
contentNodes.forEach( function ( node ) { wrapper.appendChild( node ); } );
|
||||
ve.unwrapParsoidSections( wrapper );
|
||||
contentNodes = Array.prototype.slice.call( wrapper.childNodes );
|
||||
|
||||
function isAutoGenerated( node ) {
|
||||
var dataMw = node &&
|
||||
node.nodeType === Node.ELEMENT_NODE &&
|
||||
node.hasAttribute( 'data-mw' ) &&
|
||||
JSON.parse( node.getAttribute( 'data-mw' ) );
|
||||
if ( dataMw && dataMw.autoGenerated ) {
|
||||
|
||||
return dataMw && dataMw.autoGenerated;
|
||||
}
|
||||
|
||||
// Filter out auto-generated items, e.g. reference lists
|
||||
contentNodes = contentNodes.filter( function ( node ) {
|
||||
// HACK: Also check first-child as auto-generated is applied inside ref wrapper (T181230)
|
||||
if ( isAutoGenerated( node ) || isAutoGenerated( node.childNodes[ 0 ] ) ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} );
|
||||
|
||||
function isWhitespaceNode( node ) {
|
||||
return node && node.nodeType === Node.TEXT_NODE && !!node.data.match( whitespaceRegex );
|
||||
}
|
||||
|
||||
while ( isWhitespaceNode( contentNodes[ 0 ] ) ) {
|
||||
contentNodes.shift();
|
||||
}
|
||||
while ( isWhitespaceNode( contentNodes[ contentNodes.length - 1 ] ) ) {
|
||||
contentNodes.pop();
|
||||
}
|
||||
// HACK: if $content consists of a single paragraph, unwrap it.
|
||||
// We have to do this because the parser wraps everything in <p>s, and inline templates
|
||||
// will render strangely when wrapped in <p>s.
|
||||
if ( contentNodes.length === 1 && contentNodes[ 0 ].nodeName.toLowerCase() === 'p' ) {
|
||||
contentNodes = Array.prototype.slice.apply( contentNodes[ 0 ].childNodes );
|
||||
contentNodes = Array.prototype.slice.call( contentNodes[ 0 ].childNodes );
|
||||
}
|
||||
return contentNodes;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue