Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve backcompat parsing #111

Merged
merged 5 commits into from
Apr 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update backcompat handling for adr
  • Loading branch information
gRegorLove committed Apr 26, 2017
commit 6740428317cdfe4ba55171d529457371083ffd8e
21 changes: 4 additions & 17 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,19 +1317,6 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
// special handling for specific properties
switch ( $classname )
{
case 'vcard':
$adr = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " adr ")]', $el);

if ( $adr->length ) {
foreach ( $adr as $tempEl ) {
if ( !$this->hasRootMf2($tempEl) ) {
$this->backcompat($tempEl, 'adr');
$this->addMfClasses($tempEl, 'p-adr h-adr');
}
}
}
break;

case 'hreview':
$item_and_vcard = $this->xpath->query('.//*[contains(concat(" ", normalize-space(@class), " "), " item ") and contains(concat(" ", normalize-space(@class), " "), " vcard ")]', $el);

Expand Down Expand Up @@ -1405,7 +1392,7 @@ public function backcompat(DOMElement $el, $context = '', $isParentMf2 = false)
}
}

if ( empty($context) && isset($this->classicRootMap[$classname]) ) {
if ( empty($context) && isset($this->classicRootMap[$classname]) && !$this->hasRootMf2($el) ) {
$this->addMfClasses($el, $this->classicRootMap[$classname]);
}
}
Expand Down Expand Up @@ -1522,7 +1509,8 @@ public function query($expression, $context = null) {
'hresume' => 'h-resume',
'vevent' => 'h-event',
'hreview' => 'h-review',
'hproduct' => 'h-product'
'hproduct' => 'h-product',
'adr' => 'h-adr',
);

/**
Expand Down Expand Up @@ -1571,8 +1559,7 @@ public function query($expression, $context = null) {
'replace' => 'p-category'
),
'adr' => array(
'replace' => 'p-adr h-adr',
'context' => 'adr',
'replace' => 'p-adr',
),
'extended-address' => array(
'replace' => 'p-extended-address'
Expand Down
49 changes: 43 additions & 6 deletions tests/Mf2/ClassicMicroformatsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,59 @@ public function testRelBookmarkUrl() {
}


/**
* @see http://microformats.org/wiki/microformats2-parsing-issues#any_h-_root_class_name_overrides_and_stops_backcompat_root
*/
public function testMf2RootStopsBackcompatRoot() {
$input = '<div class="adr h-adr">
<div class="locality">MF1 adr locality</div>
<div class="p-locality">MF2 adr locality</div>
</div>';
$parser = new Parser($input);
$result = $parser->parse();

$this->assertCount(1, $result['items'][0]['type']);
$this->assertEquals('h-adr', $result['items'][0]['type'][0]);
$this->assertCount(1, $result['items'][0]['properties']['locality']);
$this->assertEquals('MF2 adr locality', $result['items'][0]['properties']['locality'][0]);
}


/**
* @see http://microformats.org/wiki/microformats2-parsing-issues#any_h-_root_class_name_overrides_and_stops_backcompat_root
*/
public function testMf2CustomRootStopsBackcompatRoot() {
$input = '<div class="adr h-acme-address">
<div class="locality">MF1 acme locality</div>
<div class="p-locality">MF2 acme locality</div>
</div>';
$parser = new Parser($input);
$result = $parser->parse();

$this->assertCount(1, $result['items'][0]['type']);
$this->assertEquals('h-acme-address', $result['items'][0]['type'][0]);
$this->assertCount(1, $result['items'][0]['properties']['locality']);
$this->assertEquals('MF2 acme locality', $result['items'][0]['properties']['locality'][0]);
}


/**
* @see http://microformats.org/wiki/microformats2-parsing-issues#uf2_children_on_backcompat_properties
*/
public function testMf2ChildrenOnBackcompatProperties()
{
public function testMf2ChildrenOnBackcompatProperties() {
$input = '<div class="vcard">
<div class="adr h-custom">
<div class="locality">MF1</div>
<div class="p-locality">MF2</div>
<div class="adr h-acme-some-acme-object">
<div class="locality">MF1 some acme locality</div>
<div class="p-locality">MF2 some acme locality</div>
</div>
</div>';
$parser = new Parser($input);
$result = $parser->parse();

$this->assertCount(1, $result['items'][0]['properties']['adr'][0]['type']);
$this->assertEquals('h-acme-some-acme-object', $result['items'][0]['properties']['adr'][0]['type'][0]);
$this->assertCount(1, $result['items'][0]['properties']['adr'][0]['properties']['locality']);
$this->assertEquals('MF2', $result['items'][0]['properties']['adr'][0]['properties']['locality'][0]);
$this->assertEquals('MF2 some acme locality', $result['items'][0]['properties']['adr'][0]['properties']['locality'][0]);
}


Expand Down