Browse Source

More template fixes

master
J. King 11 months 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
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
# microformats: use the following algorithm or an algorithm that
# produces equivalent results:
@ -600,10 +600,6 @@ 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

36
tests/cases/StandardTest.php

@ -65,24 +65,26 @@ class StandardTest extends \PHPUnit\Framework\TestCase {
public function provideStandardTests(): \Generator {
// 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
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
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/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/simpletrim-true/", ['simpleTrim' => true]);
}
protected function provideTestList(array $tests, ?array $options = null): \Generator {
foreach ($tests as $base) {
$base = strtr($base, "\\", "/");
foreach (new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($base )), '/\.json$/') as $file) {
$path = $file->getPathname();
$path = preg_replace('/\.json$/', '', $path);
$name = strtr($path, "\\", "/");
$name = str_replace(strtr($base, "\\", "/"), "", $name);
yield $name => [$name, $path, $options];
}
protected function provideTestList(string $set, ?array $options = null): \Generator {
$base = strtr(\MensBeam\Microformats\BASE."tests/cases/", "\\", "/");
if (strpos(strtr($set, "\\", "/"), $base,) !== 0) {
$base = strtr($set, "\\", "/");
}
foreach (new \RegexIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($set)), '/\.json$/') as $file) {
$path = $file->getPathname();
$path = preg_replace('/\.json$/', '', $path);
$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']['rev']);
break;
case "phpmf2/classic/fberriman":
case "phpmf2/classic/mixedroots2":
case "phpmf2/classic/hentry-tag":
case "third-party/phpmf2/classic/fberriman":
case "third-party/phpmf2/classic/mixedroots2":
case "third-party/phpmf2/classic/hentry-tag":
$this->fixDates($exp['items'][0]['properties']['published']);
break;

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

@ -46,14 +46,33 @@
</template>
</div>
<!-- No properties should be found here -->
<!-- This is not a microformat -->
<template class="h-test">
<div class="p-name">TEMPLATE!</div>
</template>
<!-- This is not a microformat at all -->
<!-- This is not a microformat, either -->
<template>
<div class="h-test">
<div class="p-name">TEMPLATE!</div>
</div>
</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": {},

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