Browse Source

More template fixes

master
J. King 1 year ago
parent
commit
5b1579d90b
  1. 6
      lib/Microformats/Parser.php
  2. 36
      tests/cases/StandardTest.php
  3. 23
      tests/cases/mensbeam/default-settings/template.html
  4. 20
      tests/cases/mensbeam/default-settings/template.json
  5. 6
      tests/cases/mensbeam/simpletrim-true/template.html
  6. 16
      tests/cases/mensbeam/simpletrim-true/template.json

6
lib/Microformats/Parser.php

@ -281,7 +281,7 @@ class Parser {
} }
} }
# parse all hyperlink (<a> <area> <link>) elements for rel microformats, adding to the JSON rels & rel-urls hashes accordingly # parse all hyperlink (<a> <area> <link>) elements for rel microformats, adding to the JSON rels & rel-urls hashes accordingly
foreach ($this->xpath->query(".//a[@rel][@href]|.//area[@rel][@href]|.//link[@rel][@href]", $root) as $link) { foreach ($this->xpath->query(".//a[@rel and @href and not(ancestor::template)]|.//area[@rel and @href and not(ancestor::template)]|.//link[@rel and @href and not(ancestor::template)]", $root) as $link) {
# To parse a hyperlink element (e.g. a or link) for rel # To parse a hyperlink element (e.g. a or link) for rel
# microformats: use the following algorithm or an algorithm that # microformats: use the following algorithm or an algorithm that
# produces equivalent results: # produces equivalent results:
@ -600,10 +600,6 @@ class Parser {
if ($this->options['lang'] && ($lang = $this->getLang($root))) { if ($this->options['lang'] && ($lang = $this->getLang($root))) {
$out['lang'] = $lang; $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") // keep track of deferred properties ("use Y if X is not defined")
$deferred = []; $deferred = [];
// keep track of the implied date // keep track of the implied date

36
tests/cases/StandardTest.php

@ -65,24 +65,26 @@ class StandardTest extends \PHPUnit\Framework\TestCase {
public function provideStandardTests(): \Generator { public function provideStandardTests(): \Generator {
// the standard tests // the standard tests
yield from $this->provideTestList([\MensBeam\Microformats\BASE."vendor-bin/phpunit/vendor/mf2/tests/tests/"], ['simpleTrim' => true]); yield from $this->provideTestList(\MensBeam\Microformats\BASE."vendor-bin/phpunit/vendor/mf2/tests/tests/", ['simpleTrim' => true]);
// tests from php-mf2 // tests from php-mf2
yield from $this->provideTestList([\MensBeam\Microformats\BASE."tests/cases/third-party/"], []); yield from $this->provideTestList(\MensBeam\Microformats\BASE."tests/cases/third-party/", []);
// tests from our own corpus // tests from our own corpus
yield from $this->provideTestList([\MensBeam\Microformats\BASE."tests/cases/mensbeam/default-settings/"], []); yield from $this->provideTestList(\MensBeam\Microformats\BASE."tests/cases/mensbeam/default-settings/", []);
yield from $this->provideTestList([\MensBeam\Microformats\BASE."tests/cases/mensbeam/lang-true/"], ['lang' => true]); yield from $this->provideTestList(\MensBeam\Microformats\BASE."tests/cases/mensbeam/lang-true/", ['lang' => true]);
yield from $this->provideTestList(\MensBeam\Microformats\BASE."tests/cases/mensbeam/simpletrim-true/", ['simpleTrim' => true]);
} }
protected function provideTestList(array $tests, ?array $options = null): \Generator { protected function provideTestList(string $set, ?array $options = null): \Generator {
foreach ($tests as $base) { $base = strtr(\MensBeam\Microformats\BASE."tests/cases/", "\\", "/");
$base = strtr($base, "\\", "/"); if (strpos(strtr($set, "\\", "/"), $base,) !== 0) {
foreach (new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($base )), '/\.json$/') as $file) { $base = strtr($set, "\\", "/");
$path = $file->getPathname(); }
$path = preg_replace('/\.json$/', '', $path); foreach (new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($set)), '/\.json$/') as $file) {
$name = strtr($path, "\\", "/"); $path = $file->getPathname();
$name = str_replace(strtr($base, "\\", "/"), "", $name); $path = preg_replace('/\.json$/', '', $path);
yield $name => [$name, $path, $options]; $name = strtr($path, "\\", "/");
} $name = str_replace($base, "", $name);
yield $name => [$name, $path, $options];
} }
} }
@ -118,9 +120,9 @@ class StandardTest extends \PHPUnit\Framework\TestCase {
$this->fixDates($exp['items'][0]['properties']['bday']); $this->fixDates($exp['items'][0]['properties']['bday']);
$this->fixDates($exp['items'][0]['properties']['rev']); $this->fixDates($exp['items'][0]['properties']['rev']);
break; break;
case "phpmf2/classic/fberriman": case "third-party/phpmf2/classic/fberriman":
case "phpmf2/classic/mixedroots2": case "third-party/phpmf2/classic/mixedroots2":
case "phpmf2/classic/hentry-tag": case "third-party/phpmf2/classic/hentry-tag":
$this->fixDates($exp['items'][0]['properties']['published']); $this->fixDates($exp['items'][0]['properties']['published']);
break; break;

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

@ -46,14 +46,33 @@
</template> </template>
</div> </div>
<!-- No properties should be found here --> <!-- This is not a microformat -->
<template class="h-test"> <template class="h-test">
<div class="p-name">TEMPLATE!</div> <div class="p-name">TEMPLATE!</div>
</template> </template>
<!-- This is not a microformat at all --> <!-- This is not a microformat, either -->
<template> <template>
<div class="h-test"> <div class="h-test">
<div class="p-name">TEMPLATE!</div> <div class="p-name">TEMPLATE!</div>
</div> </div>
</template> </template>
<!-- The links here should not appear in the global rel microformat list -->
<div class="h-test">
<span class="p-name">TEST</span>
<template>
<a rel="TEMPLATE!" href="a"></a>
<area rel="TEMPLATE!" href="a">
<link rel="TEMPLATE!" href="a">
</template>
</div>
<!-- Templates also should not appear in textContent -->
<div class="h-test">
<span class="p-name">
I am
<template>NOT</template>
the King!
</span>
</div>

20
tests/cases/mensbeam/default-settings/template.json

@ -69,6 +69,26 @@
"" ""
] ]
} }
},
{
"type": [
"h-test"
],
"properties": {
"name": [
"TEST"
]
}
},
{
"type": [
"h-test"
],
"properties": {
"name": [
"I am the King!"
]
}
} }
], ],
"rels": {}, "rels": {},

6
tests/cases/mensbeam/simpletrim-true/template.html

@ -0,0 +1,6 @@
<!-- Templates should not appear in textContent -->
<div class="h-test">
<span class="p-name">
I am <template>NOT </template>the King!
</span>
</div>

16
tests/cases/mensbeam/simpletrim-true/template.json

@ -0,0 +1,16 @@
{
"items": [
{
"type": [
"h-test"
],
"properties": {
"name": [
"I am the King!"
]
}
}
],
"rels": {},
"rel-urls": {}
}
Loading…
Cancel
Save