Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageProxy: better DOMDocument HTML handling #215

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Frenzie marked this conversation as resolved.
Show resolved Hide resolved

return $output;
}

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