Browse Source

Add support for relative images in github readmes, fixes #532

Jordi Boggiano 9 years ago
parent
commit
a37dc97966
1 changed files with 9 additions and 1 deletions
  1. 9 1
      src/Packagist/WebBundle/Package/Updater.php

+ 9 - 1
src/Packagist/WebBundle/Package/Updater.php

@@ -421,7 +421,7 @@ class Updater
             $dom = new \DOMDocument();
             $dom->loadHTML('<?xml encoding="UTF-8">' . $readme);
 
-            // Links can not be trusted
+            // Links can not be trusted, mark them nofollow and convert relative to absolute links
             $links = $dom->getElementsByTagName('a');
             foreach ($links as $link) {
                 $link->setAttribute('rel', 'nofollow');
@@ -432,6 +432,14 @@ class Updater
                 }
             }
 
+            // convert relative to absolute images
+            $images = $dom->getElementsByTagName('img');
+            foreach ($images as $img) {
+                if (false === strpos($img->getAttribute('src'), '//')) {
+                    $img->setAttribute('src', 'https://raw.github.com/'.$owner.'/'.$repo.'/HEAD/'.$img->getAttribute('src'));
+                }
+            }
+
             // remove first title as it's usually the project name which we don't need
             if ($dom->getElementsByTagName('h1')->length) {
                 $first = $dom->getElementsByTagName('h1')->item(0);