Fix template rendering

* Look inside first child for autoGenerated (as reflists can be wrapped)
* Unwrap Parsoid sections
* Discard leading and trailing whitespace

Bug: T179618
Change-Id: Ib9ead28173360f0f1d5a4dc66c33a75d70ef34b3
This commit is contained in:
Ed Sanders 2017-11-23 12:21:16 +00:00
parent 267d60f61a
commit 471e40e8bb

View file

@ -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;
};