Skip to content

Commit

Permalink
ImageProxy: better DOMDocument HTML handling
Browse files Browse the repository at this point in the history
With the method in FreshRSS#212 undesired transformations could sometimes be effected.
  • Loading branch information
Frenzie committed Mar 5, 2024
1 parent b4afee6 commit 64f4dd1
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions xExtension-ImageProxy/extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function swapUris(string $content): string {

$doc = new DOMDocument();
libxml_use_internal_errors(true); // prevent tag soup errors from showing
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$doc->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'));
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
if ($img->hasAttribute('src')) {
Expand All @@ -136,7 +136,14 @@ public static function swapUris(string $content): string {
}
}

return $doc->saveHTML() ?: '';
$body = $doc->getElementsByTagName('body')->item(0);

$output = $doc->saveHTML($body);

$output = preg_replace('/^<body>/', '', $output);
$output = preg_replace('/<\/body>$/', '', $output);

return $output;
}

public static function setImageProxyHook(FreshRSS_Entry $entry): FreshRSS_Entry {
Expand Down

0 comments on commit 64f4dd1

Please sign in to comment.