From 7f26cff2cc0dc8b626083e922a88ae4899696747 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Thu, 7 Jul 2016 20:26:36 +0100 Subject: [PATCH 1/1] OutputPage: Make use of WrappedStringList in headElement() Right now, getInlineHeadScripts(), buildCssLinks() and getExternalHeadScripts() all return WrappedString::join(). But because they don't know about each other and because they need to return strings (not arrays), headElement() has no way of merging them. WrappedStringList allows this array to be kept, whilst still being backward-compatible with code that calls these methods and assumig a string (since it will lazy-join the array if the object is treated like a string). To be used by I8b6c6a10d965e7396. Output is not changed in this commit. Merely refactoring. Change-Id: Iae08345473bd93cc0948d51b62c48aeb1ea460a3 --- includes/OutputPage.php | 54 ++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 15b70c8324..c667fb9363 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -23,6 +23,7 @@ use MediaWiki\Logger\LoggerFactory; use MediaWiki\Session\SessionManager; use WrappedString\WrappedString; +use WrappedString\WrappedStringList; /** * This class should be covered by a general architecture document which does @@ -2633,7 +2634,8 @@ class OutputPage extends ContextSource { $userdir = $this->getLanguage()->getDir(); $sitedir = $wgContLang->getDir(); - $ret = Html::htmlHeader( $sk->getHtmlElementAttributes() ); + $pieces = []; + $pieces[] = Html::htmlHeader( $sk->getHtmlElementAttributes() ); if ( $this->getHTMLTitle() == '' ) { $this->setHTMLTitle( $this->msg( 'pagetitle', $this->getPageTitle() )->inContentLanguage() ); @@ -2641,8 +2643,7 @@ class OutputPage extends ContextSource { $openHead = Html::openElement( 'head' ); if ( $openHead ) { - # Don't bother with the newline if $head == '' - $ret .= "$openHead\n"; + $pieces[] = $openHead; } if ( !Html::isXmlMimeType( $this->getConfig()->get( 'MimeType' ) ) ) { @@ -2654,25 +2655,25 @@ class OutputPage extends ContextSource { // Our XML declaration is output by Html::htmlHeader. // http://www.whatwg.org/html/semantics.html#attr-meta-http-equiv-content-type // http://www.whatwg.org/html/semantics.html#charset - $ret .= Html::element( 'meta', [ 'charset' => 'UTF-8' ] ) . "\n"; + $pieces[] = Html::element( 'meta', [ 'charset' => 'UTF-8' ] ); } - $ret .= Html::element( 'title', null, $this->getHTMLTitle() ) . "\n"; - $ret .= $this->getInlineHeadScripts() . "\n"; - $ret .= $this->buildCssLinks() . "\n"; - $ret .= $this->getExternalHeadScripts() . "\n"; + $pieces[] = Html::element( 'title', null, $this->getHTMLTitle() ); + $pieces[] = $this->getInlineHeadScripts(); + $pieces[] = $this->buildCssLinks(); + $pieces[] = $this->getExternalHeadScripts(); foreach ( $this->getHeadLinksArray() as $item ) { - $ret .= $item . "\n"; + $pieces[] = $item; } foreach ( $this->mHeadItems as $item ) { - $ret .= $item . "\n"; + $pieces[] = $item; } $closeHead = Html::closeElement( 'head' ); if ( $closeHead ) { - $ret .= "$closeHead\n"; + $pieces[] = $closeHead; } $bodyClasses = []; @@ -2701,9 +2702,9 @@ class OutputPage extends ContextSource { $sk->addToBodyAttributes( $this, $bodyAttrs ); Hooks::run( 'OutputPageBodyAttributes', [ $this, $sk, &$bodyAttrs ] ); - $ret .= Html::openElement( 'body', $bodyAttrs ) . "\n"; + $pieces[] = Html::openElement( 'body', $bodyAttrs ); - return $ret; + return WrappedStringList::join( "\n", $pieces ); } /** @@ -2904,7 +2905,7 @@ class OutputPage extends ContextSource { /** * Build html output from an array of links from makeResourceLoaderLink. * @param array $links - * @return string HTML + * @return string|WrappedStringList HTML */ protected static function getHtmlFromLoaderLinks( array $links ) { $html = []; @@ -2920,7 +2921,7 @@ class OutputPage extends ContextSource { // Filter out empty values $html = array_filter( $html, 'strlen' ); - if ( count( $states ) ) { + if ( $states ) { array_unshift( $html, ResourceLoader::makeInlineScript( ResourceLoader::makeLoaderStateScript( $states ) ) ); @@ -2940,25 +2941,23 @@ class OutputPage extends ContextSource { } /** - * tags to put in "". + * Inline "