Skip to content

Commit ef7329a

Browse files
authored
Merge pull request #193 from Zegnat/implied-name-refactor
Refactor implied name to match photo and url
2 parents 3b9e49f + f12f2d6 commit ef7329a

File tree

1 file changed

+39
-47
lines changed

1 file changed

+39
-47
lines changed

Mf2/Parser.php

+39-47
Original file line numberDiff line numberDiff line change
@@ -1020,56 +1020,48 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10201020
$this->elementPrefixParsed($em, 'e');
10211021
}
10221022

1023-
// Imply 'name' only under specific conditions
1024-
$imply_name = (!$has_nested_mf && !array_key_exists('name', $return) && !$is_backcompat && !in_array('p-', $prefixes) && !in_array('e-', $prefixes));
1025-
1026-
if ($imply_name) {
1027-
try {
1028-
// Look for img @alt
1029-
if (($e->tagName == 'img' or $e->tagName == 'area') and $e->getAttribute('alt') != '') {
1030-
throw new Exception($e->getAttribute('alt'));
1031-
}
1032-
1033-
if ($e->tagName == 'abbr' and $e->hasAttribute('title')) {
1034-
throw new Exception($e->getAttribute('title'));
1035-
}
1036-
1037-
// Look for nested img @alt
1038-
foreach ($this->xpath->query('./img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
1039-
$emNames = mfNamesFromElement($em, 'h-');
1040-
if (empty($emNames) && $em->getAttribute('alt') != '') {
1041-
throw new Exception($em->getAttribute('alt'));
1042-
}
1043-
}
1044-
1045-
// Look for nested area @alt
1046-
foreach ($this->xpath->query('./area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
1047-
$emNames = mfNamesFromElement($em, 'h-');
1048-
if (empty($emNames) && $em->getAttribute('alt') != '') {
1049-
throw new Exception($em->getAttribute('alt'));
1050-
}
1051-
}
1052-
1053-
// Look for double nested img @alt
1054-
foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/img[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
1055-
$emNames = mfNamesFromElement($em, 'h-');
1056-
if (empty($emNames) && $em->getAttribute('alt') != '') {
1057-
throw new Exception($em->getAttribute('alt'));
1058-
}
1059-
}
1060-
1061-
// Look for double nested img @alt
1062-
foreach ($this->xpath->query('./*[count(preceding-sibling::*)+count(following-sibling::*)=0]/area[count(preceding-sibling::*)+count(following-sibling::*)=0]', $e) as $em) {
1063-
$emNames = mfNamesFromElement($em, 'h-');
1064-
if (empty($emNames) && $em->getAttribute('alt') != '') {
1065-
throw new Exception($em->getAttribute('alt'));
1023+
// Do we need to imply a name property?
1024+
// if no explicit "name" property, and no other p-* or e-* properties, and no nested microformats,
1025+
if (!array_key_exists('name', $return) && !in_array('p-', $prefixes) && !in_array('e-', $prefixes) && !$has_nested_mf && !$is_backcompat) {
1026+
$name = false;
1027+
// img.h-x[alt] or area.h-x[alt]
1028+
if (($e->tagName === 'img' || $e->tagName === 'area') && $e->hasAttribute('alt')) {
1029+
$name = $e->getAttribute('alt');
1030+
// abbr.h-x[title]
1031+
} elseif ($e->tagName === 'abbr' && $e->hasAttribute('title')) {
1032+
$name = $e->getAttribute('title');
1033+
} else {
1034+
$xpaths = array(
1035+
// .h-x>img:only-child[alt]:not([alt=""]):not[.h-*]
1036+
'./img[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and @alt and string-length(@alt) != 0]',
1037+
// .h-x>area:only-child[alt]:not([alt=""]):not[.h-*]
1038+
'./area[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and @alt and string-length(@alt) != 0]',
1039+
// .h-x>abbr:only-child[title]:not([title=""]):not[.h-*]
1040+
'./abbr[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and @title and string-length(@title) != 0]',
1041+
// .h-x>:only-child:not[.h-*]>img:only-child[alt]:not([alt=""]):not[.h-*]
1042+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(*) = 1]/img[not(contains(concat(" ", @class), " h-")) and @alt and string-length(@alt) != 0]',
1043+
// .h-x>:only-child:not[.h-*]>area:only-child[alt]:not([alt=""]):not[.h-*]
1044+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(*) = 1]/area[not(contains(concat(" ", @class), " h-")) and @alt and string-length(@alt) != 0]',
1045+
// .h-x>:only-child:not[.h-*]>abbr:only-child[title]:not([title=""]):not[.h-*]
1046+
'./*[not(contains(concat(" ", @class), " h-")) and count(../*) = 1 and count(*) = 1]/abbr[not(contains(concat(" ", @class), " h-")) and @title and string-length(@title) != 0]'
1047+
);
1048+
foreach ($xpaths as $xpath) {
1049+
$nameElement = $this->xpath->query($xpath, $e);
1050+
if ($nameElement !== false && $nameElement->length === 1) {
1051+
$nameElement = $nameElement->item(0);
1052+
if ($nameElement->tagName === 'img' || $nameElement->tagName === 'area') {
1053+
$name = $nameElement->getAttribute('alt');
1054+
} else {
1055+
$name = $nameElement->getAttribute('title');
1056+
}
1057+
break;
10661058
}
10671059
}
1068-
1069-
throw new Exception($this->textContent($e, true));
1070-
} catch (Exception $exc) {
1071-
$return['name'][] = unicodeTrim($exc->getMessage());
10721060
}
1061+
if ($name === false) {
1062+
$name = $this->textContent($e, true);
1063+
}
1064+
$return['name'][] = unicodeTrim($name);
10731065
}
10741066

10751067
// Check for u-photo

0 commit comments

Comments
 (0)