Skip to content

Commit 8b3721c

Browse files
committed
Apply space normalization in prefix XPath searches
1 parent 2d65354 commit 8b3721c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

‎Mf2/Parser.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ public function resolveUrl($url) {
602602
* @return string|null the parsed value or null if value-class or -title aren’t in use
603603
*/
604604
public function parseValueClassTitle(\DOMElement $e, $separator = '') {
605-
$valueClassElements = $this->xpath->query('./*[contains(concat(" ", @class, " "), " value ")]', $e);
605+
$valueClassElements = $this->xpath->query('./*[contains(concat(" ", normalize-space(@class), " "), " value ")]', $e);
606606

607607
if ($valueClassElements->length !== 0) {
608608
// Process value-class stuff
@@ -614,7 +614,7 @@ public function parseValueClassTitle(\DOMElement $e, $separator = '') {
614614
return unicodeTrim($val);
615615
}
616616

617-
$valueTitleElements = $this->xpath->query('./*[contains(concat(" ", @class, " "), " value-title ")]', $e);
617+
$valueTitleElements = $this->xpath->query('./*[contains(concat(" ", normalize-space(@class), " "), " value-title ")]', $e);
618618

619619
if ($valueTitleElements->length !== 0) {
620620
// Process value-title stuff
@@ -702,7 +702,7 @@ public function parseU(\DOMElement $u) {
702702
*/
703703
public function parseDT(\DOMElement $dt, &$dates = array(), &$impliedTimezone = null) {
704704
// Check for value-class pattern
705-
$valueClassChildren = $this->xpath->query('./*[contains(concat(" ", @class, " "), " value ") or contains(concat(" ", @class, " "), " value-title ")]', $dt);
705+
$valueClassChildren = $this->xpath->query('./*[contains(concat(" ", normalize-space(@class), " "), " value ") or contains(concat(" ", normalize-space(@class), " "), " value-title ")]', $dt);
706706
$dtValue = false;
707707

708708
if ($valueClassChildren->length > 0) {
@@ -978,7 +978,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
978978
}
979979

980980
// Handle p-*
981-
foreach ($this->xpath->query('.//*[contains(concat(" ", @class) ," p-")]', $e) as $p) {
981+
foreach ($this->xpath->query('.//*[contains(concat(" ", normalize-space(@class)) ," p-")]', $e) as $p) {
982982
// element is already parsed
983983
if ($this->isElementParsed($p, 'p')) {
984984
continue;
@@ -1003,7 +1003,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10031003
}
10041004

10051005
// Handle u-*
1006-
foreach ($this->xpath->query('.//*[contains(concat(" ", @class)," u-")]', $e) as $u) {
1006+
foreach ($this->xpath->query('.//*[contains(concat(" ", normalize-space(@class))," u-")]', $e) as $u) {
10071007
// element is already parsed
10081008
if ($this->isElementParsed($u, 'u')) {
10091009
continue;
@@ -1028,7 +1028,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10281028
$temp_dates = array();
10291029

10301030
// Handle dt-*
1031-
foreach ($this->xpath->query('.//*[contains(concat(" ", @class), " dt-")]', $e) as $dt) {
1031+
foreach ($this->xpath->query('.//*[contains(concat(" ", normalize-space(@class)), " dt-")]', $e) as $dt) {
10321032
// element is already parsed
10331033
if ($this->isElementParsed($dt, 'dt')) {
10341034
continue;
@@ -1063,7 +1063,7 @@ public function parseH(\DOMElement $e, $is_backcompat = false, $has_nested_mf =
10631063
}
10641064

10651065
// Handle e-*
1066-
foreach ($this->xpath->query('.//*[contains(concat(" ", @class)," e-")]', $e) as $em) {
1066+
foreach ($this->xpath->query('.//*[contains(concat(" ", normalize-space(@class))," e-")]', $e) as $em) {
10671067
// element is already parsed
10681068
if ($this->isElementParsed($em, 'e')) {
10691069
continue;
@@ -1755,15 +1755,15 @@ public function convertLegacy() {
17551755

17561756
// replace all roots
17571757
foreach ($this->classicRootMap as $old => $new) {
1758-
foreach ($xp->query('//*[contains(concat(" ", @class, " "), " ' . $old . ' ") and not(contains(concat(" ", @class, " "), " ' . $new . ' "))]') as $el) {
1758+
foreach ($xp->query('//*[contains(concat(" ", normalize-space(@class), " "), " ' . $old . ' ") and not(contains(concat(" ", normalize-space(@class), " "), " ' . $new . ' "))]') as $el) {
17591759
$el->setAttribute('class', $el->getAttribute('class') . ' ' . $new);
17601760
}
17611761
}
17621762

17631763
foreach ($this->classicPropertyMap as $oldRoot => $properties) {
17641764
$newRoot = $this->classicRootMap[$oldRoot];
17651765
foreach ($properties as $old => $data) {
1766-
foreach ($xp->query('//*[contains(concat(" ", @class, " "), " ' . $oldRoot . ' ")]//*[contains(concat(" ", @class, " "), " ' . $old . ' ") and not(contains(concat(" ", @class, " "), " ' . $data['replace'] . ' "))]') as $el) {
1766+
foreach ($xp->query('//*[contains(concat(" ", normalize-space(@class), " "), " ' . $oldRoot . ' ")]//*[contains(concat(" ", normalize-space(@class), " "), " ' . $old . ' ") and not(contains(concat(" ", normalize-space(@class), " "), " ' . $data['replace'] . ' "))]') as $el) {
17671767
$el->setAttribute('class', $el->getAttribute('class') . ' ' . $data['replace']);
17681768
}
17691769
}

‎tests/Mf2/ParserTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -898,5 +898,21 @@ public function testGetRootMfOnlyFindsValidElements() {
898898
$this->assertEquals(1, count($rootEls));
899899
$this->assertEquals('h-vendor123-name', $rootEls->item(0)->getAttribute('class'));
900900
}
901+
902+
/**
903+
* @see https://github.com/microformats/php-mf2/issues/245
904+
*/
905+
public function testNewlineBeforePrefix() {
906+
$input = <<<EOT
907+
<div class="h-entry">
908+
<h1 class="post_title__text
909+
p-name">Page Title</h1>
910+
<p class="p-summary">A summary so the p-name won't be implied. This test demonstrates p-name is not being parsed.</p>
911+
</div>
912+
EOT;
913+
$result = Mf2\parse($input);
914+
$this->assertEquals('Page Title', $result['items'][0]['properties']['name'][0]);
915+
$this->assertEquals('A summary so the p-name won\'t be implied. This test demonstrates p-name is not being parsed.', $result['items'][0]['properties']['summary'][0]);
916+
}
901917
}
902918

0 commit comments

Comments
 (0)