From 8264d3c8648af523b5db1c31fde67de5ae48c6ad Mon Sep 17 00:00:00 2001 From: "J. King" Date: Fri, 23 Jun 2023 21:12:31 -0400 Subject: [PATCH] Tests and fixes for templates --- lib/Microformats/Parser.php | 13 ++- .../default-settings/implied-prop.html | 31 ------- .../mensbeam/default-settings/template.html | 59 +++++++++++++ .../mensbeam/default-settings/template.json | 82 +++++++++++++++++++ 4 files changed, 151 insertions(+), 34 deletions(-) delete mode 100644 tests/cases/mensbeam/default-settings/implied-prop.html create mode 100644 tests/cases/mensbeam/default-settings/template.html create mode 100644 tests/cases/mensbeam/default-settings/template.json diff --git a/lib/Microformats/Parser.php b/lib/Microformats/Parser.php index 35c282f..fb99e66 100644 --- a/lib/Microformats/Parser.php +++ b/lib/Microformats/Parser.php @@ -355,9 +355,12 @@ class Parser { * @param \DOMElement $node The element to start searching from, including itself */ protected function getRootCandidates(\DOMElement $node): void { - $query = [".", ".//*[contains(concat(' ', normalize-space(@class)), ' h-')]"]; + $query = [ + "self::*[not(ancestor::template)]", + ".//*[contains(concat(' ', normalize-space(@class)), ' h-') and not(ancestor::template)]", + ]; foreach (array_keys(static::BACKCOMPAT_ROOTS) as $root) { - $query[] = ".//*[contains(concat(' ', normalize-space(@class), ' '), ' $root ')]"; + $query[] = ".//*[contains(concat(' ', normalize-space(@class), ' '), ' $root ') and not(ancestor::template)]"; } $query = implode("|", $query); $this->roots = iterator_to_array($this->xpath->query($query, $node)); @@ -585,6 +588,10 @@ class Parser { if ($this->options['lang'] && ($lang = $this->getLang($root))) { $out['lang'] = $lang; } + // stop here if the root is a template, as all children of templates must be ignored + if ($root->localName === "template") { + return $out; + } // keep track of deferred properties ("use Y if X is not defined") $deferred = []; // keep track of the implied date @@ -1446,7 +1453,7 @@ class Parser { * @param bool $considerChildren Whether or not child nodes are valid next nodes */ protected function nextElement(\DOMElement $node, \DOMElement $root, bool $considerChildren): ?\DOMElement { - if ($considerChildren && $node->hasChildNodes()) { + if ($considerChildren && $node->hasChildNodes() && $node->localName !== "template") { $node = $node->firstChild; $next = $node; } elseif ($node->isSameNode($root)) { diff --git a/tests/cases/mensbeam/default-settings/implied-prop.html b/tests/cases/mensbeam/default-settings/implied-prop.html deleted file mode 100644 index 94c3c18..0000000 --- a/tests/cases/mensbeam/default-settings/implied-prop.html +++ /dev/null @@ -1,31 +0,0 @@ - -
- - Template be gone! -
- -
- - Template be gone! -
- -
- - Template be gone! -
- -
- - Template be gone! -
diff --git a/tests/cases/mensbeam/default-settings/template.html b/tests/cases/mensbeam/default-settings/template.html new file mode 100644 index 0000000..cb1df8b --- /dev/null +++ b/tests/cases/mensbeam/default-settings/template.html @@ -0,0 +1,59 @@ + + + +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+ + + + + + diff --git a/tests/cases/mensbeam/default-settings/template.json b/tests/cases/mensbeam/default-settings/template.json new file mode 100644 index 0000000..35c5543 --- /dev/null +++ b/tests/cases/mensbeam/default-settings/template.json @@ -0,0 +1,82 @@ +{ + "items": [ + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": { + "name": [ + "" + ] + } + }, + { + "type": [ + "h-test" + ], + "properties": {} + } + ], + "rels": {}, + "rel-urls": {} +}