diff --git a/src/Containers/DocumentTags/BodyTagInterface.php b/src/Containers/DocumentTags/BodyTagInterface.php index f88867b..aafe43a 100644 --- a/src/Containers/DocumentTags/BodyTagInterface.php +++ b/src/Containers/DocumentTags/BodyTagInterface.php @@ -2,8 +2,10 @@ namespace ByJoby\HTML\Containers\DocumentTags; +use ByJoby\HTML\ContentCategories\FlowContent; +use ByJoby\HTML\ContentCategories\SectioningRoot; use ByJoby\HTML\Tags\ContainerTagInterface; -interface BodyTagInterface extends ContainerTagInterface +interface BodyTagInterface extends ContainerTagInterface, SectioningRoot, FlowContent { } diff --git a/src/Containers/DocumentTags/TitleTagInterface.php b/src/Containers/DocumentTags/TitleTagInterface.php index a3d477b..115ec97 100644 --- a/src/Containers/DocumentTags/TitleTagInterface.php +++ b/src/Containers/DocumentTags/TitleTagInterface.php @@ -2,8 +2,9 @@ namespace ByJoby\HTML\Containers\DocumentTags; +use ByJoby\HTML\ContentCategories\MetadataContent; use ByJoby\HTML\Tags\ContentTagInterface; -interface TitleTagInterface extends ContentTagInterface +interface TitleTagInterface extends ContentTagInterface, MetadataContent { } diff --git a/src/ContentCategories/SectioningRoot.php b/src/ContentCategories/SectioningRoot.php new file mode 100644 index 0000000..4552771 --- /dev/null +++ b/src/ContentCategories/SectioningRoot.php @@ -0,0 +1,7 @@ + */ protected $tag_namespaces = [ '\\ByJoby\\HTML\\Html5\\Tags\\', + '\\ByJoby\\HTML\\Html5\\TextContentTags\\', '\\ByJoby\\HTML\\Html5\\ContentSectioningTags\\', '\\ByJoby\\HTML\\Html5\\DocumentTags\\', ]; diff --git a/src/Html5/TextContentTags/BlockquoteTag.php b/src/Html5/TextContentTags/BlockquoteTag.php new file mode 100644 index 0000000..a3569cc --- /dev/null +++ b/src/Html5/TextContentTags/BlockquoteTag.php @@ -0,0 +1,31 @@ +attributes()->string('cite'); + } + + public function setCite(null|string $cite): static + { + if (!$cite) $this->attributes()['cite'] = false; + else $this->attributes()['cite'] = $cite; + return $this; + } + + public function unsetCite(): static + { + unset($this->attributes()['cite']); + return $this; + } +} diff --git a/src/Html5/TextContentTags/DdTag.php b/src/Html5/TextContentTags/DdTag.php new file mode 100644 index 0000000..403d9cd --- /dev/null +++ b/src/Html5/TextContentTags/DdTag.php @@ -0,0 +1,10 @@ +addGroup(new ContainerGroup(function (NodeInterface $node): bool { + if ($node instanceof TagInterface) { + return $node->tag() != 'figcaption'; + } else { + return true; + } + })); + // figcaption tag group + $this->addGroup(ContainerGroup::ofTag('figcaption', 1)); + } + + public function reverseCaptionOrder(): static + { + $this->children = array_reverse($this->children); + return $this; + } +} diff --git a/src/Html5/TextContentTags/HrTag.php b/src/Html5/TextContentTags/HrTag.php new file mode 100644 index 0000000..d0cc42b --- /dev/null +++ b/src/Html5/TextContentTags/HrTag.php @@ -0,0 +1,12 @@ +attributes()['reversed'] = $reversed; + return $this; + } + + public function reversed(): bool + { + return !!$this->attributes()['reversed']; + } + + public function start(): null|int + { + if (isset($this->attributes['start'])) { + return intval($this->attributes()->string('start')); + } else { + return null; + } + } + + public function setStart(null|int $start): static + { + if (!$start) $this->attributes()['start'] = false; + else $this->attributes()['start'] = strval($start); + return $this; + } + + public function unsetStart(): static + { + unset($this->attributes()['start']); + return $this; + } + + public function type(): null|string + { + return $this->attributes()->string('type'); + } + + public function setType(null|string $type): static + { + if (!in_array($type, ['a', 'A', 'i', 'I', '1'])) { + $type = null; + } + if (!$type) { + $this->attributes()['type'] = false; + } else { + $this->attributes()['type'] = $type; + } + return $this; + } + + public function unsetType(): static + { + unset($this->attributes()['type']); + return $this; + } +} diff --git a/src/Html5/Tags/PTag.php b/src/Html5/TextContentTags/PTag.php similarity index 85% rename from src/Html5/Tags/PTag.php rename to src/Html5/TextContentTags/PTag.php index 3c7bdde..8369189 100644 --- a/src/Html5/Tags/PTag.php +++ b/src/Html5/TextContentTags/PTag.php @@ -1,6 +1,6 @@ children() as $i => $v) { + foreach ($this->children as $i => $v) { if ($v === $child) return $i; } } else { - foreach ($this->children() as $i => $v) { + foreach ($this->children as $i => $v) { if ($v == $child) return $i; } } diff --git a/src/Traits/GroupedContainerTrait.php b/src/Traits/GroupedContainerTrait.php index dc17f6c..0ec88e2 100644 --- a/src/Traits/GroupedContainerTrait.php +++ b/src/Traits/GroupedContainerTrait.php @@ -26,7 +26,7 @@ trait GroupedContainerTrait public function groups(): array { return array_filter( - $this->children(), + $this->children, function (NodeInterface $node) { return $node instanceof ContainerGroup; } @@ -102,4 +102,16 @@ trait GroupedContainerTrait } return $this; } + + public function children(): array + { + /** @var array */ + $children = []; + foreach ($this->groups() as $group) { + foreach ($group->children() as $child) { + $children[] = $child; + } + } + return $children; + } } diff --git a/tests/Html5/ParserTest.php b/tests/Html5/ParserTest.php index 2a59820..762fe6d 100644 --- a/tests/Html5/ParserTest.php +++ b/tests/Html5/ParserTest.php @@ -2,7 +2,7 @@ namespace ByJoby\HTML\Html5; -use ByJoby\HTML\Html5\Tags\DivTag; +use ByJoby\HTML\Html5\TextContentTags\DivTag; use ByJoby\HTML\Nodes\TextInterface; use PHPUnit\Framework\TestCase; diff --git a/tests/Html5/TextContentTags/BlockquoteTagTest.php b/tests/Html5/TextContentTags/BlockquoteTagTest.php new file mode 100644 index 0000000..b5b481c --- /dev/null +++ b/tests/Html5/TextContentTags/BlockquoteTagTest.php @@ -0,0 +1,13 @@ +assertAttributeHelperMethods('cite', BlockquoteTag::class); + } +} diff --git a/tests/Html5/TextContentTags/FigureTagTest.php b/tests/Html5/TextContentTags/FigureTagTest.php new file mode 100644 index 0000000..7d32272 --- /dev/null +++ b/tests/Html5/TextContentTags/FigureTagTest.php @@ -0,0 +1,22 @@ +addChild(new FigcaptionTag); + $figure->addChild('Some content'); + $this->assertInstanceOf(TextInterface::class, $figure->children()[0]); + $this->assertInstanceOf(FigcaptionTag::class, $figure->children()[1]); + $figure->reverseCaptionOrder(); + $this->assertInstanceOf(TextInterface::class, $figure->children()[1]); + $this->assertInstanceOf(FigcaptionTag::class, $figure->children()[0]); + } +} diff --git a/tests/Html5/TextContentTags/OlTagTest.php b/tests/Html5/TextContentTags/OlTagTest.php new file mode 100644 index 0000000..d083165 --- /dev/null +++ b/tests/Html5/TextContentTags/OlTagTest.php @@ -0,0 +1,26 @@ +assertBooleanAttributeHelperMethods('reversed', OlTag::class); + $this->assertAttributeHelperMethods('start', OlTag::class, 1, 1); + $this->assertAttributeHelperMethods('type', OlTag::class, 'a', 'a'); + $this->assertAttributeHelperMethods('type', OlTag::class, 'A', 'A'); + $this->assertAttributeHelperMethods('type', OlTag::class, 'i', 'i'); + $this->assertAttributeHelperMethods('type', OlTag::class, 'I', 'I'); + $this->assertAttributeHelperMethods('type', OlTag::class, '1', '1'); + } + + public function testInvalidType(): void + { + $ol = new OlTag; + $ol->setType('X'); + $this->assertNull($ol->type()); + } +}