From 8e05089259bff94ad486133b5ec65316dd85439f Mon Sep 17 00:00:00 2001 From: "J. King" Date: Wed, 21 Jun 2023 16:13:08 -0400 Subject: [PATCH] Fix logic errors in VCP --- lib/Parser.php | 34 +++++++------ .../third-party/phpmf2/classic/hfeed.html | 13 +++++ .../third-party/phpmf2/classic/hfeed.json | 49 +++++++++++++++++++ .../classic/v2-properties-v1-roots.html | 8 +++ .../classic/v2-properties-v1-roots.json | 17 +++++++ 5 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 tests/cases/third-party/phpmf2/classic/hfeed.html create mode 100644 tests/cases/third-party/phpmf2/classic/hfeed.json create mode 100644 tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.html create mode 100644 tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.json diff --git a/lib/Parser.php b/lib/Parser.php index e4b3a61..667a827 100644 --- a/lib/Parser.php +++ b/lib/Parser.php @@ -508,7 +508,7 @@ class Parser { $hasE = $hasE ?: $prefix === "e"; $hasU = $hasU ?: $prefix === "u"; // parse the node for the property value - $value = $this->parseProperty($node, $prefix, $backcompat ? $types : [], $impliedDate); + $value = $this->parseProperty($node, $prefix, $backcompat ? $types : [], $impliedDate, (bool) $child); if ($prefix === "dt") { // keep track of the last seen date value to serve as an implied date $impliedDate = $value; @@ -694,11 +694,11 @@ class Parser { return $out; } - protected function parseProperty(\DOMElement $node, string $prefix, array $backcompatTypes, ?string $impliedDate) { + protected function parseProperty(\DOMElement $node, string $prefix, array $backcompatTypes, ?string $impliedDate, bool $isChild) { switch ($prefix) { case "p": # To parse an element for a p-x property value (whether explicit p-* or backcompat equivalent): - if ($text = $this->getValueClassPattern($node, $prefix, $backcompatTypes)) { + if (!$isChild && $text = $this->getValueClassPattern($node, $prefix, $backcompatTypes)) { # Parse the element for the Value Class Pattern. If a value is found, return it. return $text; } elseif (in_array($node->localName, ["abbr", "link"]) && $node->hasAttribute("title")) { @@ -743,7 +743,7 @@ class Parser { } elseif ($node->localName === "object" && $node->hasAttribute("data")) { # else if object.u-x[data], then get the data attribute $url = $node->getAttribute("data"); - } elseif ($url = $this->getValueClassPattern($node, $prefix, $backcompatTypes)) { + } elseif (!$isChild && $url = $this->getValueClassPattern($node, $prefix, $backcompatTypes)) { # else parse the element for the Value Class Pattern. If a value is found, get it // Nothing to do in this branch } elseif ($node->localName === "abbr" && $node->hasAttribute("title")) { @@ -765,7 +765,7 @@ class Parser { case "dt": // NOTE: Because we perform implied date resolution we don't blindly return data from nodes; returning is done below after checks # To parse an element for a dt-x property value (whether explicit dt-* or backcompat equivalent): - if ($date = $this->getValueClassPattern($node, $prefix, $backcompatTypes, $impliedDate)) { + if (!$isChild && $date = $this->getValueClassPattern($node, $prefix, $backcompatTypes, $impliedDate)) { # parse the element for the Value Class Pattern, including the date and time parsing rules. If a value is found, then return it. return $date; } elseif (in_array($node->localName, ["time", "ins", "del"]) && $node->hasAttribute("datetime")) { @@ -864,6 +864,8 @@ class Parser { # for any other element, use its inner-text. $candidate = $this->getCleanText($node, $prefix); } + } else { + continue; } if ($prefix !== "dt") { $skipChildren = true; @@ -884,16 +886,20 @@ class Parser { } } } - if ($prefix !== "dt") { - # if the microformats property expects a simple string, enumerated - # value, or telephone number, then the values extracted from the - # value elements should be concatenated without inserting - # additional characters or white-space. - return implode("", $out); + if ($out) { + if ($prefix !== "dt") { + # if the microformats property expects a simple string, enumerated + # value, or telephone number, then the values extracted from the + # value elements should be concatenated without inserting + # additional characters or white-space. + return implode("", $out); + } else { + # if the microformats property expects a datetime value, see the Date Time Parsing section. + // The rules for datetimes are dispersed elsewhere. All that's required here is to stitch parts together + return $this->stitchDate($out, $impliedDate); + } } else { - # if the microformats property expects a datetime value, see the Date Time Parsing section. - // The rules for datetimes are dispersed elsewhere. All that's required here is to stitch parts together - return $this->stitchDate($out, $impliedDate); + return null; } } diff --git a/tests/cases/third-party/phpmf2/classic/hfeed.html b/tests/cases/third-party/phpmf2/classic/hfeed.html new file mode 100644 index 0000000..a890ebf --- /dev/null +++ b/tests/cases/third-party/phpmf2/classic/hfeed.html @@ -0,0 +1,13 @@ +
+
+

Microformats are amazing

+

Published by W. Developer + on + +

In which I extoll the virtues of using microformats.

+ +
+

Blah blah blah

+
+
+
\ No newline at end of file diff --git a/tests/cases/third-party/phpmf2/classic/hfeed.json b/tests/cases/third-party/phpmf2/classic/hfeed.json new file mode 100644 index 0000000..562016b --- /dev/null +++ b/tests/cases/third-party/phpmf2/classic/hfeed.json @@ -0,0 +1,49 @@ +{ + "items": [ + { + "type": [ + "h-feed" + ], + "properties": {}, + "children": [ + { + "type": [ + "h-entry" + ], + "properties": { + "name": [ + "Microformats are amazing" + ], + "summary": [ + "In which I extoll the virtues of using microformats." + ], + "published": [ + "2013-06-13 12:00:00" + ], + "content": [ + { + "html": "

Blah blah blah

", + "value": "Blah blah blah" + } + ], + "author": [ + { + "type": [ + "h-card" + ], + "properties": { + "name": [ + "W. Developer" + ] + }, + "value": "W. Developer" + } + ] + } + } + ] + } + ], + "rels": {}, + "rel-urls": {} +} \ No newline at end of file diff --git a/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.html b/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.html new file mode 100644 index 0000000..e911455 --- /dev/null +++ b/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.html @@ -0,0 +1,8 @@ +
+

title

+ other content +
+
this is a test for indieweb post
Also on: + +
+
\ No newline at end of file diff --git a/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.json b/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.json new file mode 100644 index 0000000..dbed653 --- /dev/null +++ b/tests/cases/third-party/phpmf2/classic/v2-properties-v1-roots.json @@ -0,0 +1,17 @@ +{ + "items": [ + { + "type": [ + "h-feed" + ], + "id": "page", + "properties": { + } + } + ], + "rels": { + }, + "rel-urls": { + } + } + \ No newline at end of file