Browse Source

Tests and fixes for templates

master
J. King 11 months ago
parent
commit
8264d3c864
  1. 13
      lib/Microformats/Parser.php
  2. 31
      tests/cases/mensbeam/default-settings/implied-prop.html
  3. 59
      tests/cases/mensbeam/default-settings/template.html
  4. 82
      tests/cases/mensbeam/default-settings/template.json

13
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)) {

31
tests/cases/mensbeam/default-settings/implied-prop.html

@ -1,31 +0,0 @@
<!--
This only tests a few cases not covered by standard tests. In particular we
need to ensure that templates are not used as valid elements.
-->
<div class="h-test">
<template>
<img src="ook" alt="eek">
</template>
Template be gone!
</div>
<div class="h-test">
<template>
<area src="ook" alt="eek">
</template>
Template be gone!
</div>
<div class="h-test">
<template>
<img src="ook" alt="eek">
</template>
Template be gone!
</div>
<div class="h-test">
<template>
<abbr title="eek">ook</abbr>
</template>
Template be gone!
</div>

59
tests/cases/mensbeam/default-settings/template.html

@ -0,0 +1,59 @@
<!--
This tests various cases where templates should be discounted from
processing.
-->
<!-- None of these implied properties should match -->
<div class="h-test">
<template>
<img alt="TEMPLATE!">
</template>
</div>
<div class="h-test">
<template>
<area alt="TEMPLATE!">
</template>
</div>
<div class="h-test">
<template>
<abbr title="TEMPLATE!"></abbr>
</template>
</div>
<div class="h-test">
<template>
<img src="TEMPLATE!">
</template>
</div>
<div class="h-test">
<template>
<object data="TEMPLATE!"></object>
</template>
</div>
<div class="h-test">
<template>
<a href="TEMPLATE!"></a>
</template>
</div>
<div class="h-test">
<template>
<area href="TEMPLATE!">
</template>
</div>
<!-- No properties should be found here -->
<template class="h-test">
<div class="p-name">TEMPLATE!</div>
</template>
<!-- This is not a microformat at all -->
<template>
<div class="h-test">
<div class="p-name">TEMPLATE!</div>
</div>
</template>

82
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": {}
}
Loading…
Cancel
Save