diff --git a/lib/XRay/Formats/Mf2.php b/lib/XRay/Formats/Mf2.php index bc25753..5eab18e 100644 --- a/lib/XRay/Formats/Mf2.php +++ b/lib/XRay/Formats/Mf2.php @@ -349,6 +349,7 @@ class Mf2 extends Format { $textContent = array_key_exists('value', $content) ? $content['value'] : null; } + $checkedname = $name; if($content) { // Trim ellipses from the name $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name); @@ -359,20 +360,29 @@ class Mf2 extends Format { // Check if the name is a prefix of the content if($contentCompare && $nameCompare && strpos($contentCompare, $nameCompare) === 0) { - $name = null; + $checkedname = null; } } - if($name) { - $data['name'] = $name; + if($checkedname) { + $data['name'] = $checkedname; } // If there is content, always return the plaintext content, and return HTML content if it's different if($content) { $content = self::parseHTMLValue('content', $item); - $data['content']['text'] = $content['text']; - if(isset($content['html'])) - $data['content']['html'] = $content['html']; + if($content['text']) { + $data['content']['text'] = $content['text']; + if(isset($content['html'])) + $data['content']['html'] = $content['html']; + } else { + // If the content text was blank because the img was removed and that was the only content, + // then put the name back as the name if it was previously set. + // See https://github.com/aaronpk/XRay/issues/57 + if($name) { + $data['name'] = $name; + } + } } } diff --git a/tests/SanitizeTest.php b/tests/SanitizeTest.php index e26b1bd..d8e31a1 100644 --- a/tests/SanitizeTest.php +++ b/tests/SanitizeTest.php @@ -222,7 +222,6 @@ class SanitizeTest extends PHPUnit_Framework_TestCase { $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); -print_r($data); $this->assertObjectHasAttribute('name', $data->data); $this->assertEquals('Oh, how well they know me! 🥃', $data->data->name); @@ -230,19 +229,35 @@ print_r($data); $this->assertEquals('https://cleverdevil.io/file/5bf2fa91c3d4c592f9978200923cb56e/thumb.jpg', $data->data->photo[0]); } - public function testPhotoWithDupeNameAndAlt() { + public function testPhotoWithDupeNameAndAlt1() { $url = 'http://sanitize.example/photo-with-dupe-name-alt'; $response = $this->parse(['url' => $url]); $body = $response->getContent(); $this->assertEquals(200, $response->getStatusCode()); $data = json_decode($body); -echo json_encode($data->data, JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES); $this->assertObjectHasAttribute('name', $data->data); - $this->assertEquals('Oh, how well they know me! 🥃', $data->data->name); + $this->assertEquals('Photo caption', $data->data->name); $this->assertObjectNotHasAttribute('content', $data->data); - $this->assertEquals('https://cleverdevil.io/file/5bf2fa91c3d4c592f9978200923cb56e/thumb.jpg', $data->data->photo[0]); + $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]); + } + + public function testPhotoWithDupeNameAndAlt2() { + // https://github.com/aaronpk/XRay/issues/57 + // This is simliar to adactio's markup + // https://adactio.com/notes/13301 + $url = 'http://sanitize.example/photo-with-dupe-name-alt-2'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body); + + $this->assertObjectHasAttribute('content', $data->data); + $this->assertEquals('Photo caption', $data->data->content->text); + $this->assertObjectNotHasAttribute('name', $data->data); + $this->assertEquals('http://sanitize.example/photo.jpg', $data->data->photo[0]); } public function testPhotosWithAlt() { diff --git a/tests/data/sanitize.example/photo-with-dupe-name-alt-2 b/tests/data/sanitize.example/photo-with-dupe-name-alt-2 new file mode 100644 index 0000000..c5ac1a9 --- /dev/null +++ b/tests/data/sanitize.example/photo-with-dupe-name-alt-2 @@ -0,0 +1,14 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + Test + + +

Photo caption Photo caption

+ +