From 471e40e8bbc317eba10cdd503811b4a04cac399d Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Thu, 23 Nov 2017 12:21:16 +0000 Subject: [PATCH] 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 --- .../ce/nodes/ve.ce.MWTransclusionNode.js | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js b/modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js index bce910fad3..cd5028bea5 100644 --- a/modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js +++ b/modules/ve-mw/ce/nodes/ve.ce.MWTransclusionNode.js @@ -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

s, and inline templates // will render strangely when wrapped in

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