Skip to content

Commit

Permalink
Normalize the ?title= parameter
Browse files Browse the repository at this point in the history
Remove underscores and uppercase the first character of the 'title'
parameter, falling back to 'page' if there's no title.

Bug: T256345
  • Loading branch information
samwilson committed Jan 15, 2021
1 parent f5f0628 commit b436605
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/Controller/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,14 @@ public function home(
return $this->export( $request, $api, $fontProvider, $generatorSelector );
}

$title = $request->get( 'title' );
$font = $this->getFont( $request, $api->getLang(), $fontProvider );
$images = (bool)$request->get( 'images', true );
return $this->render( 'export.html.twig', [
'fonts' => $fontProvider->getAll(),
'font' => $font,
'formats' => GeneratorSelector::$formats,
'format' => $this->getFormat( $request ),
'title' => $title,
'title' => $this->getTitle( $request ),
'lang' => $api->getLang(),
'images' => $images,
'nocache' => $nocache,
Expand Down Expand Up @@ -198,6 +197,15 @@ private function getFormat( Request $request ): string {
return $format;
}

/**
* Get the page title from either the 'title' or 'page' parameters of the request.
*/
private function getTitle( Request $request ): string {
// It doesn't always make sense to fall back to the 'page' parameter, because that's what prompts the export,
// but for error pages it's useful.
return ucfirst( str_replace( '_', ' ', $request->get( 'title', $request->get( 'page' ) ) ) );
}

/**
* Error page handler, to always show the export form with any HTTP error message.
*
Expand All @@ -222,7 +230,7 @@ public function error( Request $request, Throwable $exception, Api $api, FontPro
'font' => $request->get( 'fonts' ),
'formats' => GeneratorSelector::$formats,
'format' => $this->getFormat( $request ),
'title' => $request->get( 'title', $request->get( 'page' ) ),
'title' => $this->getTitle( $request ),
'lang' => $api->getLang(),
'images' => true,
'messages' => [
Expand Down

0 comments on commit b436605

Please # to comment.