Skip to content

Commit

Permalink
Merge pull request #248 from mensbeam/newlines
Browse files Browse the repository at this point in the history
Apply space normalization in all XPath searches for classes
  • Loading branch information
gRegorLove authored Oct 11, 2023
2 parents 2d65354 + 8b3721c commit 7daaefd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Mf2/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public function resolveUrl($url) {
* @return string|null the parsed value or null if value-class or -title aren’t in use
*/
public function parseValueClassTitle(\DOMElement $e, $separator = '') {
$valueClassElements = $this->xpath->query('./*[contains(concat(" ", @class, " "), " value ")]', $e);
$valueClassElements = $this->xpath->query('./*[contains(concat(" ", normalize-space(@class), " "), " value ")]', $e);

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

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

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

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

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

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

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

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

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

foreach ($this->classicPropertyMap as $oldRoot => $properties) {
$newRoot = $this->classicRootMap[$oldRoot];
foreach ($properties as $old => $data) {
foreach ($xp->query('//*[contains(concat(" ", @class, " "), " ' . $oldRoot . ' ")]//*[contains(concat(" ", @class, " "), " ' . $old . ' ") and not(contains(concat(" ", @class, " "), " ' . $data['replace'] . ' "))]') as $el) {
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) {
$el->setAttribute('class', $el->getAttribute('class') . ' ' . $data['replace']);
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Mf2/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -898,5 +898,21 @@ public function testGetRootMfOnlyFindsValidElements() {
$this->assertEquals(1, count($rootEls));
$this->assertEquals('h-vendor123-name', $rootEls->item(0)->getAttribute('class'));
}

/**
* @see https://github.com/microformats/php-mf2/issues/245
*/
public function testNewlineBeforePrefix() {
$input = <<<EOT
<div class="h-entry">
<h1 class="post_title__text
p-name">Page Title</h1>
<p class="p-summary">A summary so the p-name won't be implied. This test demonstrates p-name is not being parsed.</p>
</div>
EOT;
$result = Mf2\parse($input);
$this->assertEquals('Page Title', $result['items'][0]['properties']['name'][0]);
$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]);
}
}

0 comments on commit 7daaefd

Please sign in to comment.