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());