Skip to content

Commit

Permalink
Merge pull request #255 from gRegorLove/dev/issue155
Browse files Browse the repository at this point in the history
Fix hreview backcompat
  • Loading branch information
gRegorLove authored Mar 9, 2024
2 parents 2753c22 + 774a6f8 commit 103b34e
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 14 deletions.
26 changes: 21 additions & 5 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1637,12 +1637,31 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
foreach ( $item_and_hproduct as $tempEl ) {
if ( !$this->hasRootMf2($tempEl) ) {
$this->addMfClasses($tempEl, 'p-item h-product');
$this->backcompat($tempEl, 'vevent');
$this->backcompat($tempEl, 'hproduct');
$this->addUpgraded($tempEl, array('item', 'hproduct'));
}
}
}

$rel_self_bookmark = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@rel), " "), " self ") and contains(concat(" ", normalize-space(@rel), " "), " bookmark ")]', $el);

if ( $rel_self_bookmark->length ) {
foreach ( $rel_self_bookmark as $tempEl ) {
$this->addMfClasses($tempEl, 'u-url');
$this->addUpgraded($tempEl, array('self', 'bookmark'));
}
}

$reviewer_nodes = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " reviewer ")]', $el);

if ( $reviewer_nodes->length ) {
foreach ( $reviewer_nodes as $tempEl ) {
if ( !$this->hasRootMf2($tempEl) ) {
$this->addMfClasses($tempEl, 'p-author h-card');
}
}
}

$this->upgradeRelTagToCategory($el);
break;

Expand Down Expand Up @@ -2058,10 +2077,7 @@ public function query($expression, $context = null) {
'replace' => 'p-item h-item',
'context' => 'item'
),
'reviewer' => array(
'replace' => 'p-author h-card',
'context' => 'vcard',
),
# reviewer: see backcompat()
'dtreviewed' => array(
'replace' => 'dt-published'
),
Expand Down
59 changes: 57 additions & 2 deletions tests/Mf2/ClassicMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ public function testParsesFBerrimanClassicHEntry() {
$this->assertContains('txjs', $e['properties']['category']);
}

public function testParsesSnarfedOrgArticleCorrectly() {
/**
* This test appears to have never made assertions. When initially
* added it was only printing out the parsed results, probably for
* debugging. Left for reference in case assertions need to be added.
* @see https://github.com/microformats/php-mf2/commit/5fd2c961447f305f50f73e17bd8decf7ec77fa1d#diff-45371c4a595b936f718ab44eb3ff35c326e00a01f2f5cb3d327f34d03750b872
*/
/*public function testParsesSnarfedOrgArticleCorrectly() {
$input = file_get_contents(__DIR__ . '/snarfed.org.html');
$result = Mf2\parse($input, 'http://snarfed.org/2013-10-23_oauth-dropins');
}
}*/

public function testParsesHProduct() {
$input = <<<'EOT'
Expand Down Expand Up @@ -761,6 +767,7 @@ public function testParsesClassicHreview() {
</div>
<p>Visit date: <span>April 2005</span></p>
<p>Food eaten: <span>Florentine crepe</span></p>
<p>Permanent link for review: <a rel="self bookmark" href="http://example.com/crepe">http://example.com/crepe</a></p>
</div>
END;
$parser = new Parser($input);
Expand Down Expand Up @@ -946,6 +953,54 @@ public function testHReviewRelTag() {
$this->assertContains('Garçon', $output['items'][0]['properties']['category']);
}

public function testHReviewItemVevent()
{
$input = '<div class="hreview">
<span><span class="rating">5</span> out of 5 stars</span>
<span class="item vevent">
<span class="summary">IndieWebCamp 2014</span> -
<a href="https://indieweb.org/2014" class="url">indieweb.org/2014</a>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('item', $output['items'][0]['properties']);

# assert item type is h-event
$this->assertCount(1, $output['items'][0]['properties']['item'][0]['type']);
$this->assertEquals('h-event', $output['items'][0]['properties']['item'][0]['type'][0]);

# assert expected h-event properties
$properties = $output['items'][0]['properties']['item'][0]['properties'];
$this->assertArrayHasKey('name', $properties);
$this->assertArrayHasKey('url', $properties);
}

public function testHReviewItemHproduct()
{
$input = '<div class="hreview">
<span><span class="rating">4</span> out of 5 stars</span>
<span class="item hproduct">
<span class="fn">Widget</span> -
<a href="https://example.com/widget/" class="url">example.com/widget/</a>
</span>
</div>';
$parser = new Parser($input);
$output = $parser->parse();

$this->assertArrayHasKey('item', $output['items'][0]['properties']);

# assert item type is h-product
$this->assertCount(1, $output['items'][0]['properties']['item'][0]['type']);
$this->assertEquals('h-product', $output['items'][0]['properties']['item'][0]['type'][0]);

# assert expected h-product properties
$properties = $output['items'][0]['properties']['item'][0]['properties'];
$this->assertArrayHasKey('name', $properties);
$this->assertArrayHasKey('url', $properties);
}

/**
* Should return the last non-empty URL segment
* @see https://github.com/indieweb/php-mf2/issues/157
Expand Down
14 changes: 10 additions & 4 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,21 @@ public function testParseEWithWhitespace() {
* @group parseH
*/
public function testInvalidClassnamesContainingHAreIgnored() {
$input = '<div class="asdfgh-jkl"></div>';
$classname = 'asdfgh-jkl';
$input = sprintf('<div class="%s"></div>', $classname);
$parser = new Parser($input);
$output = $parser->parse();

// Look through $output for an item which indicate failure
// Look through parsed items for `type` matching the classname.
// There shouldn't be any
$matches = 0;
foreach ($output['items'] as $item) {
if (in_array('asdfgh-jkl', $item['type']))
$this->fail();
if (in_array($classname, $item['type'])) {
$matches++;
}
}

$this->assertEquals(0, $matches, sprintf('Class name "%s" should not have parsed as a root microformat', $classname));
}

public function testHtmlSpecialCharactersWorks() {
Expand Down
6 changes: 3 additions & 3 deletions tests/Mf2/URLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Mf2;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

class UrlTest extends TestCase {
class URLTest extends TestCase {
protected function set_up() {
date_default_timezone_set('Europe/London');
}
Expand Down Expand Up @@ -149,15 +149,15 @@ public function testResolvesProtocolRelativeUrlsCorrectly() {
}

/**
* @dataProvider testData
* @dataProvider dataProvider
*/
public function testReturnsUrlIfAbsolute($assert, $base, $url, $expected) {
$actual = mf2\resolveUrl($base, $url);

$this->assertEquals($expected, $actual, $assert);
}

public function testData() {
public function dataProvider() {
// seriously, please update to PHP 5.4 so I can use nice array syntax ;)
// fail message, base, url, expected
$cases = array(
Expand Down

0 comments on commit 103b34e

Please sign in to comment.