Browse Source

when interpreting JSON, don't require `value` for html values

pull/78/head v1.4.31
Aaron Parecki 5 years ago
parent
commit
e2780ba0a0
No known key found for this signature in database GPG Key ID: 276C2817346D6056
2 changed files with 97 additions and 2 deletions
  1. +3
    -2
      lib/XRay/Formats/Mf2.php
  2. +94
    -0
      tests/ParseTest.php

+ 3
- 2
lib/XRay/Formats/Mf2.php View File

@ -229,7 +229,7 @@ class Mf2 extends Format {
$content = $item['properties'][$property][0];
if(is_string($content)) {
$textContent = $content;
} elseif(!is_string($content) && is_array($content) && array_key_exists('value', $content)) {
} elseif(!is_string($content) && is_array($content) && array_key_exists('html', $content)) {
if(array_key_exists('html', $content)) {
// Only allow images in the content if there is no photo property set
if(isset($item['properties']['photo']))
@ -241,7 +241,8 @@ class Mf2 extends Format {
#$textContent = trim(str_replace("
","\r",$content['value']));
$textContent = trim(self::stripHTML($htmlContent));
} else {
$textContent = trim($content['value']);
if(isset($content['value']))
$textContent = trim($content['value']);
}
}

+ 94
- 0
tests/ParseTest.php View File

@ -74,6 +74,100 @@ class ParseTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
}
public function testContentFromJSONOnlyPlaintext() {
$mf2JSON = json_encode([
'items' => [[
'type' => ['h-entry'],
'properties' => [
'content' => [
'plaintext'
]
]
]]
]);
$xray = new \p3k\XRay();
$parsed = $xray->process(false, $mf2JSON);
$item = $parsed['data'];
$this->assertEquals('entry', $item['type']);
$this->assertEquals('plaintext', $item['content']['text']);
$this->assertArrayNotHasKey('html', $item['content']);
}
public function testHTMLContentFromJSONNoPlaintext() {
$mf2JSON = json_encode([
'items' => [[
'type' => ['h-entry'],
'properties' => [
'content' => [[
'html' => '<b>bold</b> <i>italic</i> text'
]]
]
]]
]);
$xray = new \p3k\XRay();
$parsed = $xray->process(false, $mf2JSON);
$item = $parsed['data'];
$this->assertEquals('entry', $item['type']);
$this->assertEquals('bold italic text', $item['content']['text']);
$this->assertEquals('<b>bold</b> <i>italic</i> text', $item['content']['html']);
}
public function testHTMLContentFromJSONEmptyTags() {
$mf2JSON = json_encode([
'items' => [[
'type' => ['h-entry'],
'properties' => [
'content' => [[
'html' => '<b></b><i></i>'
]]
]
]]
]);
$xray = new \p3k\XRay();
$parsed = $xray->process(false, $mf2JSON);
$item = $parsed['data'];
$this->assertEquals('entry', $item['type']);
$this->assertArrayNotHasKey('content', $item);
}
public function testHTMLContentFromJSONContentMismatch() {
$mf2JSON = json_encode([
'items' => [[
'type' => ['h-entry'],
'properties' => [
'content' => [[
'value' => 'foo',
'html' => '<b>bar</b>'
]]
]
]]
]);
$xray = new \p3k\XRay();
$parsed = $xray->process(false, $mf2JSON);
$item = $parsed['data'];
$this->assertEquals('entry', $item['type']);
$this->assertEquals('bar', $item['content']['text']);
$this->assertEquals('<b>bar</b>', $item['content']['html']);
}
public function testHTMLContentFromJSONNoHTML() {
$mf2JSON = json_encode([
'items' => [[
'type' => ['h-entry'],
'properties' => [
'content' => [[
'value' => 'foo',
]]
]
]]
]);
$xray = new \p3k\XRay();
$parsed = $xray->process(false, $mf2JSON);
$item = $parsed['data'];
$this->assertEquals('entry', $item['type']);
$this->assertArrayNotHasKey('content', $item);
}
public function testFindTargetLinkIsImage() {
$url = 'http://source.example.com/link-is-img';
$response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);

Loading…
Cancel
Save