From c9df327994ba9753a0e0d709559a253aa72fb537 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Sat, 14 Dec 2013 04:00:43 +0100 Subject: [PATCH] mediawiki.action.edit.preview: Run hook on detached node Change-Id: Ie5b70c6681bab51e9ac7067b9293b4e2cccd0574 --- .../mediawiki.action.edit.preview.js | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/resources/mediawiki.action/mediawiki.action.edit.preview.js b/resources/mediawiki.action/mediawiki.action.edit.preview.js index c5cd61effa..664bf897c4 100644 --- a/resources/mediawiki.action/mediawiki.action.edit.preview.js +++ b/resources/mediawiki.action/mediawiki.action.edit.preview.js @@ -71,24 +71,45 @@ // TODO: This should use the action=parse API instead of loading the entire page, // although that requires figuring out how to convert that raw data into proper HTML. $previewDataHolder.load( targetUrl + ' ' + copySelectors.join( ',' ), postData, function () { - var i, $from; + var i, $from, $next, $parent; // Copy the contents of the specified elements from the loaded page to the real page. // Also copy their class attributes. for ( i = 0; i < copySelectors.length; i++ ) { $from = $previewDataHolder.find( copySelectors[i] ); - $( copySelectors[i] ) - .empty() - .append( $from.contents() ) - .attr( 'class', $from.attr( 'class' ) ); + if ( copySelectors[i] === '#wikiPreview' ) { + $next = $wikiPreview.next(); + // If there is no next node, use parent instead. + // Only query parent if needed, false otherwise. + $parent = !$next.length && $wikiPreview.parent(); + + $wikiPreview + .detach() + .empty() + .append( $from.contents() ) + .attr( 'class', $from.attr( 'class' ) ); + + mw.hook( 'wikipage.content' ).fire( $wikiPreview ); + + // Reattach + if ( $parent ) { + $parent.append( $wikiPreview ); + } else { + $next.before( $wikiPreview ); + } + + } else { + $( copySelectors[i] ) + .empty() + .append( $from.contents() ) + .attr( 'class', $from.attr( 'class' ) ); + } } // Deprecated: Use mw.hook instead $( mw ).trigger( 'LivePreviewDone', [copySelectors] ); - mw.hook( 'wikipage.content' ).fire( $wikiPreview ); - $spinner.remove(); $copyElements.animate( { opacity: 1 -- 2.20.1