From 916a8e1aacc8197b18d3898fb7a8897c27406a06 Mon Sep 17 00:00:00 2001 From: peter279k Date: Sun, 24 Jun 2018 15:48:10 +0800 Subject: [PATCH 1/2] Fix issue #66 --- src/Purl/Fragment.php | 7 ++++++- tests/Purl/Test/UrlTest.php | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Purl/Fragment.php b/src/Purl/Fragment.php index 9d4bb4e..96706b0 100644 --- a/src/Purl/Fragment.php +++ b/src/Purl/Fragment.php @@ -109,7 +109,12 @@ public function __toString() : string protected function doInitialize() : void { if ($this->fragment !== null) { - $this->data = array_merge($this->data, parse_url($this->fragment)); + $data = parse_url($this->fragment); + if ($data === false) { + $data = ['path' => $this->fragment]; + } + + $this->data = array_merge($this->data, $data); } foreach ($this->data as $key => $value) { diff --git a/tests/Purl/Test/UrlTest.php b/tests/Purl/Test/UrlTest.php index 6344756..23ee2d0 100644 --- a/tests/Purl/Test/UrlTest.php +++ b/tests/Purl/Test/UrlTest.php @@ -362,6 +362,10 @@ public function testRelativeUrl() : void $url = new Url('/events'); $url->query->set('param1', 'value1'); $this->assertEquals('/events?param1=value1', (string) $url); + + // test fragment with colon + $url = new Url('http://example.com/#hello:123'); + $this->assertEquals('http://example.com/', (string) $url); } } From bcb853cd2e4119bccac6ce40feb94d41e3dcdf22 Mon Sep 17 00:00:00 2001 From: peter279k Date: Tue, 26 Jun 2018 02:40:13 +0800 Subject: [PATCH 2/2] Rebuild the hashbang with colon --- src/Purl/Fragment.php | 9 ++++++++- tests/Purl/Test/UrlTest.php | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Purl/Fragment.php b/src/Purl/Fragment.php index 96706b0..ccd9d11 100644 --- a/src/Purl/Fragment.php +++ b/src/Purl/Fragment.php @@ -7,6 +7,8 @@ use function array_merge; use function parse_url; use function sprintf; +use function strpos; +use function substr; /** * Fragment represents the part of a Url after the hashmark (#). @@ -109,8 +111,13 @@ public function __toString() : string protected function doInitialize() : void { if ($this->fragment !== null) { + $pos = strpos($this->fragment, ':', 1); + $colon = ''; + if ($pos !== false) { + $colon = substr($this->fragment, $pos); + } $data = parse_url($this->fragment); - if ($data === false) { + if ($colon !== '') { $data = ['path' => $this->fragment]; } diff --git a/tests/Purl/Test/UrlTest.php b/tests/Purl/Test/UrlTest.php index 23ee2d0..6fe671b 100644 --- a/tests/Purl/Test/UrlTest.php +++ b/tests/Purl/Test/UrlTest.php @@ -365,7 +365,7 @@ public function testRelativeUrl() : void // test fragment with colon $url = new Url('http://example.com/#hello:123'); - $this->assertEquals('http://example.com/', (string) $url); + $this->assertEquals('http://example.com/#hello:123', (string) $url); } }