Skip to content

Commit

Permalink
Use RemexHtml to properly remove thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardspec committed Dec 30, 2024
1 parent 8247ebe commit cc7be0f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
16 changes: 12 additions & 4 deletions includes/HtmlSanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function startDocument( $fragmentNamespace, $fragmentName ) {

/** @inheritDoc */
public function element( SerializerNode $parent, SerializerNode $node, $contents ) {
$typeof = $node->attrs['typeof'] ?? null;

switch ( $node->name ) {
// Remove everything outside the <body> tag.
case 'head':
Expand All @@ -56,18 +58,24 @@ public function element( SerializerNode $parent, SerializerNode $node, $contents
return $contents;

case 'img':
// Remove the image tags: in 99,9% of cases they are too wide
// to be included into the calendar.
// Not needed in MediaWiki 1.40+ (already removed with the <span> below).
// (only for MediaWiki 1.39) Remove the image tags.
// Not needed in MediaWiki 1.40+ (already removed with <span> or <figure> below).
return '';

case 'span':
if ( ( $node->attrs['typeof'] ?? '' ) === 'mw:File' ) {
if ( $typeof === 'mw:File' ) {
// Wrapper around the image.
return '';
}
break;

case 'figure':
if ( $typeof === 'mw:File/Thumb' ) {
// Wrapper around the thumbnail.
return '';
}
break;

// TODO: properly remove <div class="thumb"> with all contents (currently hidden by CSS).

case 'p':
Expand Down
19 changes: 16 additions & 3 deletions tests/phpunit/EventCalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,12 @@ public function testSnippetForCompressedRevision() {

/**
* Verify that unwanted parts of HTML (such as images) are removed from the snippet.
* @dataProvider dataProviderSnippetSanitizer
* @param bool $isThumb False to add an image to the page, true to add a thumbnail.
*/
public function testSnippetSanitizer() {
$filename = 'Testimage.png';
$pageText = "Expected snippet [[File:$filename]]";
public function testSnippetSanitizer( $isThumb ) {
$filename = 'Testimage' . ( $isThumb ? '1' : '2' ) . '.png';
$pageText = 'Expected snippet [[File:' . $filename . ( $isThumb ? '|thumb' : '' ) . ']]';
$expectedSnippet = '<p>Expected snippet</p>';

// Upload a test file, so that [[File:]] syntax would create an actual thumbnail, not a redlink.
Expand All @@ -800,6 +802,17 @@ public function testSnippetSanitizer() {
$this->assertSame( $expectedSnippet, $actualData[0]['title'] );
}

/**
* Provides datasets for testSnippetSanitizer().
* @return array
*/
public function dataProviderSnippetSanitizer() {
return [
'image (not a thumbnail)' => [ false ],
'thumbnail' => [ true ]
];
}

/**
* Verify that parameters like height=300 and aspectratio=1.5 are provided to JavaScript library.
* @dataProvider dataProviderOptionalAttributes
Expand Down

0 comments on commit cc7be0f

Please sign in to comment.