From 67763ada8f4e8e01024a56ab5f73591f5522d7c1 Mon Sep 17 00:00:00 2001 From: Joby Elliott Date: Fri, 16 Dec 2022 11:58:23 -0700 Subject: [PATCH] reorganized html5 parsing, added some tags --- src/{Parser.php => AbstractParser.php} | 9 +-- src/Containers/GenericHtmlDocument.php | 64 ------------------- .../AbstractHeaderTag.php | 2 +- .../ContentSectioningTags/AddressTag.php | 12 ++++ .../ArticleTag.php | 5 +- .../AsideTag.php | 5 +- src/Html5/ContentSectioningTags/FooterTag.php | 12 ++++ src/Html5/ContentSectioningTags/H1Tag.php | 8 +++ src/Html5/ContentSectioningTags/H2Tag.php | 8 +++ src/Html5/ContentSectioningTags/H3Tag.php | 8 +++ src/Html5/ContentSectioningTags/H4Tag.php | 8 +++ src/Html5/ContentSectioningTags/H5Tag.php | 8 +++ src/Html5/ContentSectioningTags/H6Tag.php | 8 +++ src/Html5/ContentSectioningTags/HeaderTag.php | 12 ++++ src/Html5/ContentSectioningTags/MainTag.php | 12 ++++ .../NavTag.php | 5 +- .../SectionTag.php | 5 +- .../DocumentTags/BodyTag.php | 3 +- .../DocumentTags/Doctype.php | 3 +- .../DocumentTags/HeadTag.php | 4 +- .../DocumentTags/HtmlTag.php | 5 +- .../DocumentTags/TitleTag.php | 3 +- src/Html5/Html5Document.php | 61 +++++++++++++++++- src/Html5/Html5Parser.php | 19 ++++++ src/Html5/Tags/H1Tag.php | 10 --- src/Html5/Tags/H2Tag.php | 10 --- src/Html5/Tags/H3Tag.php | 10 --- src/Html5/Tags/H4Tag.php | 10 --- src/Html5/Tags/H5Tag.php | 10 --- src/Html5/Tags/H6Tag.php | 10 --- .../DocumentTags/HeadTagTest.php | 3 +- .../DocumentTags/TitleTagTest.php | 2 +- .../GenericHtmlDocumentTest.php | 6 +- tests/{ => Html5}/ParserTest.php | 16 ++--- 34 files changed, 217 insertions(+), 159 deletions(-) rename src/{Parser.php => AbstractParser.php} (96%) delete mode 100644 src/Containers/GenericHtmlDocument.php rename src/Html5/{AbstractTags => ContentSectioningTags}/AbstractHeaderTag.php (79%) create mode 100644 src/Html5/ContentSectioningTags/AddressTag.php rename src/Html5/{Tags => ContentSectioningTags}/ArticleTag.php (64%) rename src/Html5/{Tags => ContentSectioningTags}/AsideTag.php (64%) create mode 100644 src/Html5/ContentSectioningTags/FooterTag.php create mode 100644 src/Html5/ContentSectioningTags/H1Tag.php create mode 100644 src/Html5/ContentSectioningTags/H2Tag.php create mode 100644 src/Html5/ContentSectioningTags/H3Tag.php create mode 100644 src/Html5/ContentSectioningTags/H4Tag.php create mode 100644 src/Html5/ContentSectioningTags/H5Tag.php create mode 100644 src/Html5/ContentSectioningTags/H6Tag.php create mode 100644 src/Html5/ContentSectioningTags/HeaderTag.php create mode 100644 src/Html5/ContentSectioningTags/MainTag.php rename src/Html5/{Tags => ContentSectioningTags}/NavTag.php (64%) rename src/Html5/{Tags => ContentSectioningTags}/SectionTag.php (64%) rename src/{Containers => Html5}/DocumentTags/BodyTag.php (60%) rename src/{Containers => Html5}/DocumentTags/Doctype.php (66%) rename src/{Containers => Html5}/DocumentTags/HeadTag.php (87%) rename src/{Containers => Html5}/DocumentTags/HtmlTag.php (82%) rename src/{Containers => Html5}/DocumentTags/TitleTag.php (79%) create mode 100644 src/Html5/Html5Parser.php delete mode 100644 src/Html5/Tags/H1Tag.php delete mode 100644 src/Html5/Tags/H2Tag.php delete mode 100644 src/Html5/Tags/H3Tag.php delete mode 100644 src/Html5/Tags/H4Tag.php delete mode 100644 src/Html5/Tags/H5Tag.php delete mode 100644 src/Html5/Tags/H6Tag.php rename tests/{Containers => Html5}/DocumentTags/HeadTagTest.php (74%) rename tests/{Containers => Html5}/DocumentTags/TitleTagTest.php (90%) rename tests/{Containers => Html5}/GenericHtmlDocumentTest.php (93%) rename tests/{ => Html5}/ParserTest.php (88%) diff --git a/src/Parser.php b/src/AbstractParser.php similarity index 96% rename from src/Parser.php rename to src/AbstractParser.php index 8fd5a24..5db0afd 100644 --- a/src/Parser.php +++ b/src/AbstractParser.php @@ -20,13 +20,10 @@ use DOMElement; use DOMNode; use DOMText; -class Parser +abstract class AbstractParser { /** @var array */ - protected $tag_namespaces = [ - '\\ByJoby\\HTML\\Html5\\Tags\\', - '\\ByJoby\\HTML\\Containers\\DocumentTags\\' - ]; + protected $tag_namespaces = []; /** @var array> */ protected $tag_classes = []; @@ -41,7 +38,7 @@ class Parser protected $cdata_class = CData::class; /** @var class-string */ - protected $document_class = GenericHtmlDocument::class; + protected $document_class; /** @var class-string */ protected $fragment_class = Fragment::class; diff --git a/src/Containers/GenericHtmlDocument.php b/src/Containers/GenericHtmlDocument.php deleted file mode 100644 index 5e40d28..0000000 --- a/src/Containers/GenericHtmlDocument.php +++ /dev/null @@ -1,64 +0,0 @@ - */ - protected $doctype; - /** @var ContainerGroup */ - protected $html; - - public function __construct() - { - $this->doctype = ContainerGroup::ofClass(DoctypeInterface::class, 1); - $this->html = ContainerGroup::ofClass(HtmlTagInterface::class, 1); - $this->addGroup($this->doctype); - $this->addGroup($this->html); - $this->addChild(new Doctype); - $this->addChild(new HtmlTag); - } - - public function doctype(): DoctypeInterface - { - return $this->doctype->children()[0]; - } - - public function html(): HtmlTagInterface - { - return $this->html->children()[0]; - } - - public function head(): HeadTagInterface - { - return $this->html()->head(); - } - - public function body(): BodyTagInterface - { - return $this->html()->body(); - } - - public function __toString(): string - { - return implode( - PHP_EOL, - array_filter( - $this->groups(), - function (ContainerGroup $group) { - return !!$group->children(); - } - ) - ); - } -} diff --git a/src/Html5/AbstractTags/AbstractHeaderTag.php b/src/Html5/ContentSectioningTags/AbstractHeaderTag.php similarity index 79% rename from src/Html5/AbstractTags/AbstractHeaderTag.php rename to src/Html5/ContentSectioningTags/AbstractHeaderTag.php index e4614f4..05cf8db 100644 --- a/src/Html5/AbstractTags/AbstractHeaderTag.php +++ b/src/Html5/ContentSectioningTags/AbstractHeaderTag.php @@ -1,6 +1,6 @@ */ + protected $doctype; + /** @var ContainerGroup */ + protected $html; + + public function __construct() + { + $this->doctype = ContainerGroup::ofClass(DoctypeInterface::class, 1); + $this->html = ContainerGroup::ofClass(HtmlTagInterface::class, 1); + $this->addGroup($this->doctype); + $this->addGroup($this->html); + $this->addChild(new Doctype); + $this->addChild(new HtmlTag); + } + + public function doctype(): DoctypeInterface + { + return $this->doctype->children()[0]; + } + + public function html(): HtmlTagInterface + { + return $this->html->children()[0]; + } + + public function head(): HeadTagInterface + { + return $this->html()->head(); + } + + public function body(): BodyTagInterface + { + return $this->html()->body(); + } + + public function __toString(): string + { + return implode( + PHP_EOL, + array_filter( + $this->groups(), + function (ContainerGroup $group) { + return !!$group->children(); + } + ) + ); + } } diff --git a/src/Html5/Html5Parser.php b/src/Html5/Html5Parser.php new file mode 100644 index 0000000..0e9d1c1 --- /dev/null +++ b/src/Html5/Html5Parser.php @@ -0,0 +1,19 @@ + */ + protected $tag_namespaces = [ + '\\ByJoby\\HTML\\Html5\\Tags\\', + '\\ByJoby\\HTML\\Html5\\ContentSectioningTags\\', + '\\ByJoby\\HTML\\Html5\\DocumentTags\\', + ]; + + /** @var class-string */ + protected $document_class = Html5Document::class; +} diff --git a/src/Html5/Tags/H1Tag.php b/src/Html5/Tags/H1Tag.php deleted file mode 100644 index 05fed6c..0000000 --- a/src/Html5/Tags/H1Tag.php +++ /dev/null @@ -1,10 +0,0 @@ -assertInstanceOf(DoctypeInterface::class, $document->doctype()); $this->assertInstanceOf(HtmlTagInterface::class, $document->html()); diff --git a/tests/ParserTest.php b/tests/Html5/ParserTest.php similarity index 88% rename from tests/ParserTest.php rename to tests/Html5/ParserTest.php index 12aa71a..2a59820 100644 --- a/tests/ParserTest.php +++ b/tests/Html5/ParserTest.php @@ -1,16 +1,16 @@ parseFragment('foobar'); $this->assertInstanceOf(TextInterface::class, $fragment->children()[0]); $fragment = $parser->parseFragment('foobar
fizzbuzz
'); @@ -20,7 +20,7 @@ class ParserTest extends TestCase public function testAttributes() { - $parser = new Parser(); + $parser = new Html5Parser(); $fragment = $parser->parseFragment('
'); $this->assertEquals('foo', $fragment->children()[0]->id()); $this->assertEquals('b', $fragment->children()[0]->attributes()['a']); @@ -29,7 +29,7 @@ class ParserTest extends TestCase public function testStylesAndClasses() { - $parser = new Parser(); + $parser = new Html5Parser(); $fragment = $parser->parseFragment('
'); $this->assertEquals(['bar', 'foo'], $fragment->children()[0]->classes()->getArray()); $this->assertEquals(['background-color' => 'blue', 'color' => 'red'], $fragment->children()[0]->styles()->getArray()); @@ -37,7 +37,7 @@ class ParserTest extends TestCase public function testNesting() { - $parser = new Parser(); + $parser = new Html5Parser(); $fragment = $parser->parseFragment('

foobar

foo

'); $this->assertInstanceOf(DivTag::class, $fragment->children()[0]); $this->assertCount(2, $fragment->children()[0]->children()); @@ -46,14 +46,14 @@ class ParserTest extends TestCase public function testUnknownTags() { - $parser = new Parser(); + $parser = new Html5Parser(); $fragment = $parser->parseFragment('
'); $this->assertCount(1, $fragment->children()); } public function testParseDocument() { - $parser = new Parser(); + $parser = new Html5Parser(); $document = $parser->parseDocument('Title
foo
'); $this->assertEquals('Title', $document->html()->head()->title()->content()); $this->assertEquals('
' . PHP_EOL . 'foo' . PHP_EOL . '
', $document->body()->children()[0]->__toString());