]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Refactor and rebase #380: Firefox reader view links 522/head
authorArthurHoaro <arthur@hoa.ro>
Thu, 24 Mar 2016 18:01:40 +0000 (19:01 +0100)
committerArthurHoaro <arthur@hoa.ro>
Sat, 26 Mar 2016 15:26:23 +0000 (16:26 +0100)
Fixes #366
Closes #380

application/Url.php
tests/Url/UrlTest.php

index e7c3a4cc4ae50291bd9e1cccf3dc55bd47899940..af38c4d9155ed4eb856691c941f26f11714b77be 100644 (file)
@@ -118,24 +118,41 @@ class Url
      */
     public function __construct($url)
     {
-        $this->parts = parse_url(trim($url));
+        $url = self::cleanupUnparsedUrl(trim($url));
+        $this->parts = parse_url($url);
 
         if (!empty($url) && empty($this->parts['scheme'])) {
             $this->parts['scheme'] = 'http';
         }
     }
 
+    /**
+     * Clean up URL before it's parsed.
+     * ie. handle urlencode, url prefixes, etc.
+     *
+     * @param string $url URL to clean.
+     *
+     * @return string cleaned URL.
+     */
+    protected static function cleanupUnparsedUrl($url)
+    {
+        return self::removeFirefoxAboutReader($url);
+    }
 
-    private function removeFirefoxAboutReader($input){
-      $output_array = [];
-      preg_match("%^about://reader\?url=(.*)%", $input, $output_array);
-      if(!empty($output_array)){
-        $extractedUrl = preg_replace("%^about://reader\?url=(.*)%", "$1", $input);
-        $url = urldecode($extractedUrl);
-      }else{
-        $url = $input;
-      }
-      return $url;
+    /**
+     * Remove Firefox Reader prefix if it's present.
+     *
+     * @param string $input url
+     *
+     * @return string cleaned url
+     */
+    protected static function removeFirefoxAboutReader($input)
+    {
+        $firefoxPrefix = 'about://reader?url=';
+        if (startsWith($input, $firefoxPrefix)) {
+            return urldecode(ltrim($input, $firefoxPrefix));
+        }
+        return $input;
     }
     
     /**
@@ -200,8 +217,7 @@ class Url
     {
         $this->cleanupQuery();
         $this->cleanupFragment();
-        $url = $this->toString();
-        return $this->removeFirefoxAboutReader($url);
+        return $this->toString();
     }
 
     /**
index 425327ed02b37b7f1ccb638208ddf404ed7b16d5..a64a73eae183e82672e3647c8a8249cdfa26c189 100644 (file)
@@ -128,6 +128,13 @@ class UrlTest extends PHPUnit_Framework_TestCase
             self::$baseUrl.'?my=stuff&is=kept#again',
             $url->cleanup()
         );
+
+        // test firefox reader url
+        $url = new Url(
+            'about://reader?url=' . urlencode(self::$baseUrl .'?my=stuff&is=kept')
+        );
+        $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
+
     }
 
     /**