Browse Source

if removing the img results in empty content, put the name value back

closes #57
pull/60/head
Aaron Parecki 6 years ago
parent
commit
4d65b1ca1e
No known key found for this signature in database GPG Key ID: 276C2817346D6056
3 changed files with 50 additions and 11 deletions
  1. +16
    -6
      lib/XRay/Formats/Mf2.php
  2. +20
    -5
      tests/SanitizeTest.php
  3. +14
    -0
      tests/data/sanitize.example/photo-with-dupe-name-alt-2

+ 16
- 6
lib/XRay/Formats/Mf2.php View File

@ -349,6 +349,7 @@ class Mf2 extends Format {
$textContent = array_key_exists('value', $content) ? $content['value'] : null; $textContent = array_key_exists('value', $content) ? $content['value'] : null;
} }
$checkedname = $name;
if($content) { if($content) {
// Trim ellipses from the name // Trim ellipses from the name
$name = preg_replace('/ ?(\.\.\.|…)$/', '', $name); $name = preg_replace('/ ?(\.\.\.|…)$/', '', $name);
@ -359,20 +360,29 @@ class Mf2 extends Format {
// Check if the name is a prefix of the content // Check if the name is a prefix of the content
if($contentCompare && $nameCompare && strpos($contentCompare, $nameCompare) === 0) { 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 there is content, always return the plaintext content, and return HTML content if it's different
if($content) { if($content) {
$content = self::parseHTMLValue('content', $item); $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;
}
}
} }
} }

+ 20
- 5
tests/SanitizeTest.php View File

@ -222,7 +222,6 @@ class SanitizeTest extends PHPUnit_Framework_TestCase {
$body = $response->getContent(); $body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body); $data = json_decode($body);
print_r($data);
$this->assertObjectHasAttribute('name', $data->data); $this->assertObjectHasAttribute('name', $data->data);
$this->assertEquals('Oh, how well they know me! 🥃', $data->data->name); $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]); $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'; $url = 'http://sanitize.example/photo-with-dupe-name-alt';
$response = $this->parse(['url' => $url]); $response = $this->parse(['url' => $url]);
$body = $response->getContent(); $body = $response->getContent();
$this->assertEquals(200, $response->getStatusCode()); $this->assertEquals(200, $response->getStatusCode());
$data = json_decode($body); $data = json_decode($body);
echo json_encode($data->data, JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES);
$this->assertObjectHasAttribute('name', $data->data); $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->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() { public function testPhotosWithAlt() {

+ 14
- 0
tests/data/sanitize.example/photo-with-dupe-name-alt-2 View File

@ -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
<html>
<head>
<title>Test</title>
</head>
<body class="h-entry">
<p class="e-content p-name">Photo caption <img class="u-photo" src="/photo.jpg" alt="Photo caption"></p>
</body>
</html>

Loading…
Cancel
Save