From 0d5846976f56f476e9a2e28ac191be00abcfd955 Mon Sep 17 00:00:00 2001 From: Baptiste Langlade Date: Sat, 7 Jun 2025 11:37:53 +0200 Subject: [PATCH] update dependencies --- CHANGELOG.md | 21 ++ README.md | 15 +- composer.json | 6 +- src/Element/A.php | 116 ++------- src/Element/Base.php | 117 ++------- src/Element/Img.php | 117 ++------- src/Element/Link.php | 121 ++------- src/Element/Script.php | 104 ++------ src/Node/Document.php | 93 +++---- src/Reader/Reader.php | 37 ++- src/Translator.php | 236 ++++++++++++++++++ src/Translator/NodeTranslator/ATranslator.php | 63 ----- .../NodeTranslator/BaseTranslator.php | 57 ----- .../NodeTranslator/DocumentTranslator.php | 102 -------- .../NodeTranslator/ElementTranslator.php | 60 ----- .../NodeTranslator/ImgTranslator.php | 54 ---- .../NodeTranslator/LinkTranslator.php | 82 ------ .../NodeTranslator/ScriptTranslator.php | 60 ----- src/Translator/NodeTranslators.php | 49 ---- src/Visitor/Element.php | 103 ++++++-- src/Visitor/Elements.php | 87 +++++-- tests/Element/ATest.php | 28 +-- tests/Element/BaseTest.php | 19 +- tests/Element/ImgTest.php | 19 +- tests/Element/LinkTest.php | 19 +- tests/Element/ScriptTest.php | 20 +- tests/Node/DocumentTest.php | 69 ++--- tests/Reader/ReaderTest.php | 19 +- .../NodeTranslator/ATranslatorTest.php | 51 +--- .../NodeTranslator/BaseTranslatorTest.php | 51 +--- .../NodeTranslator/DocumentTranslatorTest.php | 51 ++-- .../NodeTranslator/ElementTranslatorTest.php | 142 ----------- .../NodeTranslator/ImgTranslatorTest.php | 51 +--- .../NodeTranslator/LinkTranslatorTest.php | 65 ++--- .../NodeTranslator/ScriptTranslatorTest.php | 57 ++--- tests/Translator/NodeTranslatorsTest.php | 38 --- tests/Visitor/BodyTest.php | 12 +- tests/Visitor/ElementTest.php | 12 +- tests/Visitor/ElementsTest.php | 9 +- tests/Visitor/HeadTest.php | 12 +- 40 files changed, 740 insertions(+), 1704 deletions(-) create mode 100644 src/Translator.php delete mode 100644 src/Translator/NodeTranslator/ATranslator.php delete mode 100644 src/Translator/NodeTranslator/BaseTranslator.php delete mode 100644 src/Translator/NodeTranslator/DocumentTranslator.php delete mode 100644 src/Translator/NodeTranslator/ElementTranslator.php delete mode 100644 src/Translator/NodeTranslator/ImgTranslator.php delete mode 100644 src/Translator/NodeTranslator/LinkTranslator.php delete mode 100644 src/Translator/NodeTranslator/ScriptTranslator.php delete mode 100644 src/Translator/NodeTranslators.php delete mode 100644 tests/Translator/NodeTranslator/ElementTranslatorTest.php delete mode 100644 tests/Translator/NodeTranslatorsTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ed261f5..8282ad1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,27 @@ ## [Unreleased] +### Added + +- `Innmind\Html\Translator` +- `Innmind\Html\Reader\Reader::new()` + +### Changed + +- Requires `innmind/xml:~8.0` +- Requires `innmind/filesystem:~8.1` +- `Innmind\Html\Element\*` classes now implement `Innmind\Xml\Element\Custom` +- `Innmind\Html\Node\Document` no longer implement any interface +- `Innmind\Html\Reader\Reader` now return an `Innmind\Immutable\Attempt` + +### Removed + +- `Innmind\Html\Translator\*` +- `Innmind\Html\Node\Document::content()` +- `Innmind\Html\Node\Document::toString()` +- `Innmind\Html\Reader\Reader::default()` +- `Innmind\Html\Reader\Reader::of()` + ### Fixed - PHP `8.4` deprecations diff --git a/README.md b/README.md index 137f699..64e40dd 100644 --- a/README.md +++ b/README.md @@ -17,16 +17,23 @@ composer require innmind/html ## Usage ```php -use Innmind\Html\Reader\Reader; -use Innmind\Xml\Node; +use Innmind\Html\{ + Reader\Reader, + Node\Document, +}; +use Innmind\Xml\{ + Node, + Element, + Element\Custom, +}; use Innmind\Filesystem\File\Content; use Innmind\Immutable\Maybe; -$read = Reader::default(); +$read = Reader::new(); $html = $read( Content::ofString(\file_get_contents('https://github.com/')), -); // Maybe +); // Maybe ``` ## Extract some elements of the tree diff --git a/composer.json b/composer.json index 5ad2d6f..548b2b1 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,10 @@ }, "require": { "php": "~8.2", - "innmind/xml": "~7.7", - "innmind/filesystem": "~7.1", + "innmind/xml": "~8.0", + "innmind/filesystem": "~8.1", + "innmind/immutable": "~5.16", + "innmind/validation": "~2.0", "symfony/dom-crawler": ">=6.3 <7.0.7", "innmind/url": "~4.0" }, diff --git a/src/Element/A.php b/src/Element/A.php index 43b4eb2..8af124f 100644 --- a/src/Element/A.php +++ b/src/Element/A.php @@ -5,26 +5,23 @@ use Innmind\Xml\{ Element, + Element\Name, + Element\Custom, Node, Attribute, }; use Innmind\Url\Url; -use Innmind\Immutable\{ - Set, - Sequence, - Maybe, - Map, -}; +use Innmind\Immutable\Sequence; /** * @psalm-immutable */ -final class A implements Element +final class A implements Custom { - private Element\Element $element; + private Element $element; private Url $href; - private function __construct(Url $href, Element\Element $element) + private function __construct(Url $href, Element $element) { $this->element = $element; $this->href = $href; @@ -33,18 +30,18 @@ private function __construct(Url $href, Element\Element $element) /** * @psalm-pure * - * @param Set|null $attributes - * @param Sequence|null $children + * @param Sequence|null $attributes + * @param Sequence|null $children */ public static function of( Url $href, - ?Set $attributes = null, + ?Sequence $attributes = null, ?Sequence $children = null, ): self { return new self( $href, - Element\Element::of( - 'a', + Element::of( + Name::of('a'), $attributes, $children, ), @@ -57,95 +54,10 @@ public function href(): Url } #[\Override] - public function name(): string - { - return 'a'; - } - - #[\Override] - public function attributes(): Map - { - return $this->element->attributes(); - } - - #[\Override] - public function attribute(string $name): Maybe - { - return $this->element->attribute($name); - } - - #[\Override] - public function removeAttribute(string $name): self - { - return new self( - $this->href, - $this->element->removeAttribute($name), - ); - } - - #[\Override] - public function addAttribute(Attribute $attribute): self - { - return new self( - $this->href, - $this->element->addAttribute($attribute), - ); - } - - #[\Override] - public function children(): Sequence - { - return $this->element->children(); - } - - #[\Override] - public function filterChild(callable $filter): self - { - return new self( - $this->href, - $this->element->filterChild($filter), - ); - } - - #[\Override] - public function mapChild(callable $map): self - { - return new self( - $this->href, - $this->element->mapChild($map), - ); - } - - #[\Override] - public function prependChild(Node $child): self - { - return new self( - $this->href, - $this->element->prependChild($child), - ); - } - - #[\Override] - public function appendChild(Node $child): self + public function normalize(): Element { - return new self( - $this->href, - $this->element->appendChild($child), + return $this->element->addAttribute( + Attribute::of('href', $this->href->toString()), ); } - - #[\Override] - public function content(): string - { - return $this->element->content(); - } - - #[\Override] - public function toString(): string - { - return $this - ->element - ->addAttribute(Attribute::of('href', $this->href->toString())) - ->toString(); - } } diff --git a/src/Element/Base.php b/src/Element/Base.php index c98c300..50f2de3 100644 --- a/src/Element/Base.php +++ b/src/Element/Base.php @@ -4,28 +4,23 @@ namespace Innmind\Html\Element; use Innmind\Xml\{ - Element\SelfClosingElement, Element, + Element\Name, + Element\Custom, Attribute, - Node, }; use Innmind\Url\Url; -use Innmind\Immutable\{ - Set, - Sequence, - Maybe, - Map, -}; +use Innmind\Immutable\Sequence; /** * @psalm-immutable */ -final class Base implements Element +final class Base implements Custom { - private SelfClosingElement $element; + private Element $element; private Url $href; - private function __construct(Url $href, SelfClosingElement $element) + private function __construct(Url $href, Element $element) { $this->element = $element; $this->href = $href; @@ -34,11 +29,14 @@ private function __construct(Url $href, SelfClosingElement $element) /** * @psalm-pure * - * @param Set|null $attributes + * @param Sequence|null $attributes */ - public static function of(Url $href, ?Set $attributes = null): self + public static function of(Url $href, ?Sequence $attributes = null): self { - return new self($href, SelfClosingElement::of('base', $attributes)); + return new self($href, Element::selfClosing( + Name::of('base'), + $attributes, + )); } public function href(): Url @@ -47,95 +45,10 @@ public function href(): Url } #[\Override] - public function name(): string - { - return 'base'; - } - - #[\Override] - public function attributes(): Map - { - return $this->element->attributes(); - } - - #[\Override] - public function attribute(string $name): Maybe - { - return $this->element->attribute($name); - } - - #[\Override] - public function removeAttribute(string $name): self - { - return new self( - $this->href, - $this->element->removeAttribute($name), - ); - } - - #[\Override] - public function addAttribute(Attribute $attribute): self - { - return new self( - $this->href, - $this->element->addAttribute($attribute), - ); - } - - #[\Override] - public function children(): Sequence - { - return $this->element->children(); - } - - #[\Override] - public function filterChild(callable $filter): self - { - return new self( - $this->href, - $this->element->filterChild($filter), - ); - } - - #[\Override] - public function mapChild(callable $map): self + public function normalize(): Element { - return new self( - $this->href, - $this->element->mapChild($map), + return $this->element->addAttribute( + Attribute::of('href', $this->href->toString()), ); } - - #[\Override] - public function prependChild(Node $child): self - { - return new self( - $this->href, - $this->element->prependChild($child), - ); - } - - #[\Override] - public function appendChild(Node $child): self - { - return new self( - $this->href, - $this->element->appendChild($child), - ); - } - - #[\Override] - public function content(): string - { - return $this->element->content(); - } - - #[\Override] - public function toString(): string - { - return $this - ->element - ->addAttribute(Attribute::of('href', $this->href->toString())) - ->toString(); - } } diff --git a/src/Element/Img.php b/src/Element/Img.php index 0bf4498..bfbd0e4 100644 --- a/src/Element/Img.php +++ b/src/Element/Img.php @@ -4,28 +4,23 @@ namespace Innmind\Html\Element; use Innmind\Xml\{ - Element\SelfClosingElement, Element, + Element\Name, + Element\Custom, Attribute, - Node, }; use Innmind\Url\Url; -use Innmind\Immutable\{ - Set, - Sequence, - Maybe, - Map, -}; +use Innmind\Immutable\Sequence; /** * @psalm-immutable */ -final class Img implements Element +final class Img implements Custom { - private SelfClosingElement $element; + private Element $element; private Url $src; - private function __construct(Url $src, SelfClosingElement $element) + private function __construct(Url $src, Element $element) { $this->element = $element; $this->src = $src; @@ -34,11 +29,14 @@ private function __construct(Url $src, SelfClosingElement $element) /** * @psalm-pure * - * @param Set|null $attributes + * @param Sequence|null $attributes */ - public static function of(Url $src, ?Set $attributes = null): self + public static function of(Url $src, ?Sequence $attributes = null): self { - return new self($src, SelfClosingElement::of('img', $attributes)); + return new self($src, Element::selfClosing( + Name::of('img'), + $attributes, + )); } public function src(): Url @@ -47,95 +45,10 @@ public function src(): Url } #[\Override] - public function name(): string - { - return 'img'; - } - - #[\Override] - public function attributes(): Map - { - return $this->element->attributes(); - } - - #[\Override] - public function attribute(string $name): Maybe - { - return $this->element->attribute($name); - } - - #[\Override] - public function removeAttribute(string $name): self - { - return new self( - $this->src, - $this->element->removeAttribute($name), - ); - } - - #[\Override] - public function addAttribute(Attribute $attribute): self - { - return new self( - $this->src, - $this->element->addAttribute($attribute), - ); - } - - #[\Override] - public function children(): Sequence - { - return $this->element->children(); - } - - #[\Override] - public function filterChild(callable $filter): self - { - return new self( - $this->src, - $this->element->filterChild($filter), - ); - } - - #[\Override] - public function mapChild(callable $map): self + public function normalize(): Element { - return new self( - $this->src, - $this->element->mapChild($map), + return $this->element->addAttribute( + Attribute::of('src', $this->src->toString()), ); } - - #[\Override] - public function prependChild(Node $child): self - { - return new self( - $this->src, - $this->element->prependChild($child), - ); - } - - #[\Override] - public function appendChild(Node $child): self - { - return new self( - $this->src, - $this->element->appendChild($child), - ); - } - - #[\Override] - public function content(): string - { - return $this->element->content(); - } - - #[\Override] - public function toString(): string - { - return $this - ->element - ->addAttribute(Attribute::of('src', $this->src->toString())) - ->toString(); - } } diff --git a/src/Element/Link.php b/src/Element/Link.php index a995361..536697d 100644 --- a/src/Element/Link.php +++ b/src/Element/Link.php @@ -4,25 +4,20 @@ namespace Innmind\Html\Element; use Innmind\Xml\{ - Element\SelfClosingElement, Element, + Element\Name, + Element\Custom, Attribute, - Node, }; use Innmind\Url\Url; -use Innmind\Immutable\{ - Set, - Sequence, - Maybe, - Map, -}; +use Innmind\Immutable\Sequence; /** * @psalm-immutable */ -final class Link implements Element +final class Link implements Custom { - private SelfClosingElement $element; + private Element $element; private Url $href; /** @var non-empty-string */ private string $relationship; @@ -33,7 +28,7 @@ final class Link implements Element private function __construct( Url $href, string $relationship, - SelfClosingElement $element, + Element $element, ) { $this->element = $element; $this->href = $href; @@ -44,17 +39,20 @@ private function __construct( * @psalm-pure * * @param non-empty-string $relationship - * @param Set|null $attributes + * @param Sequence|null $attributes */ public static function of( Url $href, string $relationship, - ?Set $attributes = null, + ?Sequence $attributes = null, ): self { return new self( $href, $relationship, - SelfClosingElement::of('link', $attributes), + Element::selfClosing( + Name::of('link'), + $attributes, + ), ); } @@ -72,102 +70,11 @@ public function relationship(): string } #[\Override] - public function name(): string - { - return 'link'; - } - - #[\Override] - public function attributes(): Map - { - return $this->element->attributes(); - } - - #[\Override] - public function attribute(string $name): Maybe - { - return $this->element->attribute($name); - } - - #[\Override] - public function removeAttribute(string $name): self - { - return new self( - $this->href, - $this->relationship, - $this->element->removeAttribute($name), - ); - } - - #[\Override] - public function addAttribute(Attribute $attribute): self - { - return new self( - $this->href, - $this->relationship, - $this->element->addAttribute($attribute), - ); - } - - #[\Override] - public function children(): Sequence - { - return $this->element->children(); - } - - #[\Override] - public function filterChild(callable $filter): self - { - return new self( - $this->href, - $this->relationship, - $this->element->filterChild($filter), - ); - } - - #[\Override] - public function mapChild(callable $map): self - { - return new self( - $this->href, - $this->relationship, - $this->element->mapChild($map), - ); - } - - #[\Override] - public function prependChild(Node $child): self - { - return new self( - $this->href, - $this->relationship, - $this->element->prependChild($child), - ); - } - - #[\Override] - public function appendChild(Node $child): self - { - return new self( - $this->href, - $this->relationship, - $this->element->appendChild($child), - ); - } - - #[\Override] - public function content(): string - { - return $this->element->content(); - } - - #[\Override] - public function toString(): string + public function normalize(): Element { return $this ->element ->addAttribute(Attribute::of('rel', $this->relationship)) - ->addAttribute(Attribute::of('href', $this->href->toString())) - ->toString(); + ->addAttribute(Attribute::of('href', $this->href->toString())); } } diff --git a/src/Element/Script.php b/src/Element/Script.php index 3028a42..05f3dcf 100644 --- a/src/Element/Script.php +++ b/src/Element/Script.php @@ -4,112 +4,40 @@ namespace Innmind\Html\Element; use Innmind\Xml\{ - Node, - Attribute, Element, - Node\Text, -}; -use Innmind\Immutable\{ - Set, - Sequence, - Maybe, - Map, + Element\Name, + Element\Custom, + Attribute, + Node, }; +use Innmind\Immutable\Sequence; /** * @psalm-immutable */ -final class Script implements Element +final class Script implements Custom { - private Element\Element $element; - - private function __construct(Element\Element $element) + private function __construct(private Element $element) { - $this->element = $element; } /** * @psalm-pure * - * @param Set|null $attributes + * @param Sequence|null $attributes */ - public static function of(Text $text, ?Set $attributes = null): self - { - /** @var Sequence */ - $children = Sequence::of($text); - - return new self(Element\Element::of('script', $attributes, $children)); - } - - #[\Override] - public function name(): string - { - return 'script'; - } - - #[\Override] - public function attributes(): Map - { - return $this->element->attributes(); - } - - #[\Override] - public function attribute(string $name): Maybe - { - return $this->element->attribute($name); - } - - #[\Override] - public function removeAttribute(string $name): self - { - return new self($this->element->removeAttribute($name)); - } - - #[\Override] - public function addAttribute(Attribute $attribute): self - { - return new self($this->element->addAttribute($attribute)); - } - - #[\Override] - public function children(): Sequence - { - return $this->element->children(); - } - - #[\Override] - public function filterChild(callable $filter): self - { - return new self($this->element->filterChild($filter)); - } - - #[\Override] - public function mapChild(callable $map): self - { - return new self($this->element->mapChild($map)); - } - - #[\Override] - public function prependChild(Node $child): self - { - return new self($this->element->prependChild($child)); - } - - #[\Override] - public function appendChild(Node $child): self - { - return new self($this->element->appendChild($child)); - } - - #[\Override] - public function content(): string + public static function of(Node $text, ?Sequence $attributes = null): self { - return $this->element->content(); + return new self(Element::of( + Name::of('script'), + $attributes, + Sequence::of($text), + )); } #[\Override] - public function toString(): string + public function normalize(): Element { - return $this->element->toString(); + return $this->element; } } diff --git a/src/Node/Document.php b/src/Node/Document.php index 3e7e29c..b6dbfe3 100644 --- a/src/Node/Document.php +++ b/src/Node/Document.php @@ -5,29 +5,29 @@ use Innmind\Xml\{ Node, - Node\Document\Type, - AsContent, -}; -use Innmind\Filesystem\File\{ - Content, - Content\Line, + Element, + Element\Custom, + Document\Type, + Format, + Document as XMLDocument, }; +use Innmind\Filesystem\File\Content; use Innmind\Immutable\{ Sequence, - Str, + Maybe, }; /** * @psalm-immutable */ -final class Document implements Node, AsContent +final class Document { private Type $type; - /** @var Sequence */ + /** @var Sequence */ private Sequence $children; /** - * @param Sequence|null $children + * @param Sequence|null $children */ private function __construct(Type $type, ?Sequence $children = null) { @@ -38,7 +38,7 @@ private function __construct(Type $type, ?Sequence $children = null) /** * @psalm-pure * - * @param Sequence|null $children + * @param Sequence|null $children */ public static function of(Type $type, ?Sequence $children = null): self { @@ -50,7 +50,9 @@ public function type(): Type return $this->type; } - #[\Override] + /** + * @return Sequence + */ public function children(): Sequence { return $this->children; @@ -61,7 +63,9 @@ public function hasChildren(): bool return !$this->children->empty(); } - #[\Override] + /** + * @param callable(Node|Element|Custom): bool $filter + */ public function filterChild(callable $filter): self { return new self( @@ -70,7 +74,9 @@ public function filterChild(callable $filter): self ); } - #[\Override] + /** + * @param callable(Node|Element|Custom): (Node|Element|Custom) $map + */ public function mapChild(callable $map): self { return new self( @@ -79,8 +85,7 @@ public function mapChild(callable $map): self ); } - #[\Override] - public function prependChild(Node $child): Node + public function prependChild(Node|Element|Custom $child): self { return new self( $this->type, @@ -88,8 +93,7 @@ public function prependChild(Node $child): Node ); } - #[\Override] - public function appendChild(Node $child): Node + public function appendChild(Node|Element|Custom $child): self { return new self( $this->type, @@ -97,42 +101,23 @@ public function appendChild(Node $child): Node ); } - #[\Override] - public function content(): string - { - $children = $this->children->map( - static fn(Node $child): string => $child->toString(), - ); - - return Str::of('')->join($children)->toString(); - } - - #[\Override] - public function toString(): string + public function asContent(Format $format = Format::pretty): Content { - return $this->type->toString()."\n".$this->content(); - } - - #[\Override] - public function asContent(): Content - { - /** - * @psalm-suppress MixedArgumentTypeCoercion - * @psalm-suppress UndefinedInterfaceMethod - * @psalm-suppress MixedMethodCall - */ - return Content::ofLines( - $this - ->children - ->flatMap( - static fn($child) => match ($child instanceof AsContent) { - true => $child->asContent()->lines(), - false => Content::ofString($child->toString())->lines(), - }, - ) - ->prepend( - Sequence::of(Line::of(Str::of($this->type->toString()))), - ), - ); + /** @var Maybe */ + $encoding = Maybe::nothing(); + $chunks = XMLDocument::of( + XMLDocument\Version::of(1, 0), + Maybe::just($this->type), + $encoding, + $this->children, + ) + ->asContent($format) + ->chunks() + ->map(static fn($chunk) => match ($chunk->startsWith('')) { + true => $chunk->drop(22), + false => $chunk, + }); + + return Content::ofChunks($chunks); } } diff --git a/src/Reader/Reader.php b/src/Reader/Reader.php index c51a01f..2e37dff 100644 --- a/src/Reader/Reader.php +++ b/src/Reader/Reader.php @@ -3,21 +3,23 @@ namespace Innmind\Html\Reader; -use Innmind\Html\Translator\NodeTranslators as HtmlNodeTranslators; +use Innmind\Html\{ + Node\Document, + Translator, +}; use Innmind\Xml\{ - Reader as ReaderInterface, Node, - Translator\Translator, - Translator\NodeTranslators, + Element, + Element\Custom, }; use Innmind\Filesystem\File\Content; -use Innmind\Immutable\Maybe; +use Innmind\Immutable\Attempt; use Symfony\Component\DomCrawler\Crawler; /** * @psalm-immutable */ -final class Reader implements ReaderInterface +final class Reader { private Translator $translate; @@ -26,15 +28,17 @@ private function __construct(Translator $translate) $this->translate = $translate; } - #[\Override] - public function __invoke(Content $html): Maybe + /** + * @return Attempt + */ + public function __invoke(Content $html): Attempt { /** @psalm-suppress ImpureMethodCall */ $firstNode = (new Crawler($html->toString(), useHtml5Parser: false))->getNode(0); if (!$firstNode instanceof \DOMNode) { - /** @var Maybe */ - return Maybe::nothing(); + /** @var Attempt */ + return Attempt::error(new \RuntimeException('Failed to parse html content')); } /** @psalm-suppress RedundantCondition */ @@ -45,19 +49,10 @@ public function __invoke(Content $html): Maybe return ($this->translate)($firstNode); } - public static function of(Translator $translate): self - { - return new self($translate); - } - - public static function default(): self + public static function new(): self { return new self( - Translator::of( - NodeTranslators::defaults()->merge( - HtmlNodeTranslators::defaults(), - ), - ), + Translator::new(), ); } } diff --git a/src/Translator.php b/src/Translator.php new file mode 100644 index 0000000..b585b20 --- /dev/null +++ b/src/Translator.php @@ -0,0 +1,236 @@ + + */ + public function __invoke(\DOMNode|\Dom\Node $node): Attempt + { + return $this + ->buildDocument($node) + ->recover(fn() => $this->child($node)); + } + + /** + * @psalm-pure + */ + public static function new(): self + { + return new self(XmlTranslator::of( + self::custom(...), + )); + } + + /** + * @psalm-suppress UndefinedClass Since the package still supports PHP 8.2 + * + * @return Attempt + */ + private function child(\DOMNode|\Dom\Node $node): Attempt + { + /** @var Attempt Psalm doesn't understand the filter */ + return ($this->translate)($node) + ->either() + ->filter( + Instance::of(Element::class) + ->or(Instance::of(Custom::class)) + ->or(Instance::of(Node::class)), + static fn() => new \RuntimeException('Invalid document node'), + ) + ->match( + Attempt::result(...), + Attempt::error(...), + ); + } + + /** + * @psalm-suppress UndefinedClass Since the package still supports PHP 8.2 + * @psalm-suppress MixedArgument + * @psalm-suppress MixedMethodCall + * @psalm-suppress UndefinedPropertyFetch + * + * @return Attempt + */ + private function buildDocument(\DOMNode|\Dom\Node $node): Attempt + { + /** @var Sequence */ + $children = Sequence::of(); + + return Maybe::just($node) + ->keep( + Instance::of(\DOMDocument::class)->or( + Instance::of(\Dom\Document::class), + ), + ) + ->attempt(static fn() => new \RuntimeException('Not a document')) + ->flatMap( + fn($document) => Sequence::of(...\array_values(\iterator_to_array($document->childNodes))) + ->keep( + Instance::of(\DOMNode::class)->or( + Instance::of(\Dom\Node::class), + ), + ) + ->exclude(static fn($child) => $child->nodeType === \XML_DOCUMENT_TYPE_NODE) + ->sink($children) + ->attempt( + fn($children, $child) => $this + ->child($child) + ->map($children), + ) + ->map(static fn($children) => Document::of( + Maybe::of($document->doctype) + ->flatMap(self::buildDoctype(...)) + ->match( + static fn($type) => $type, + static fn() => Type::of('html'), + ), + $children, + )), + ); + } + + /** + * @psalm-pure + * @psalm-suppress ImpurePropertyFetch + * @psalm-suppress UndefinedClass Since the package still supports PHP 8.2 + * + * @return Maybe + */ + private static function buildDoctype(\DOMDocumentType|\Dom\DocumentType $type): Maybe + { + /** @psalm-suppress MixedArgument */ + return Type::maybe( + $type->name, + $type->publicId, + $type->systemId, + ); + } + + /** + * @psalm-pure + * + * @return Maybe + */ + private static function custom(Element $element): Maybe + { + if ($element->name()->toString() === 'a') { + return $element + ->attribute('href') + ->map(static fn($attribute) => $attribute->value()) + ->flatMap(Url::maybe(...)) + ->map(static fn($href) => A::of( + $href, + $element->attributes()->exclude( + static fn($attribute) => $attribute->name() === 'href', + ), + $element->children(), + )); + } + + if ($element->name()->toString() === 'base') { + return $element + ->attribute('href') + ->map(static fn($attribute) => $attribute->value()) + ->flatMap(Url::maybe(...)) + ->map(static fn($href) => Base::of( + $href, + $element->attributes()->exclude( + static fn($attribute) => $attribute->name() === 'href', + ), + )); + } + + if ($element->name()->toString() === 'img') { + return $element + ->attribute('src') + ->map(static fn($attribute) => $attribute->value()) + ->flatMap(Url::maybe(...)) + ->map(static fn($src) => Img::of( + $src, + $element->attributes()->exclude( + static fn($attribute) => $attribute->name() === 'src', + ), + )); + } + + if ($element->name()->toString() === 'link') { + return $element + ->attribute('href') + ->map(static fn($attribute) => $attribute->value()) + ->flatMap(Url::maybe(...)) + ->map(static fn($href) => Link::of( + $href, + $element + ->attribute('rel') + ->map(static fn($attribute) => $attribute->value()) + ->keep(Is::string()->nonEmpty()->asPredicate()) + ->match( + static fn($rel) => $rel, + static fn() => 'related', + ), + $element->attributes()->exclude( + static fn($attribute) => \in_array( + $attribute->name(), + ['href', 'rel'], + true, + ), + ), + )); + } + + if ($element->name()->toString() === 'script') { + return Maybe::just(Script::of( + $element + ->children() + ->first() + ->keep(Instance::of(Node::class)) + ->match( + static fn($node) => Node::text($node->content()), + static fn() => Node::text(''), + ), + $element->attributes(), + )); + } + + /** @var Maybe */ + return Maybe::nothing(); + } +} diff --git a/src/Translator/NodeTranslator/ATranslator.php b/src/Translator/NodeTranslator/ATranslator.php deleted file mode 100644 index 521faa4..0000000 --- a/src/Translator/NodeTranslator/ATranslator.php +++ /dev/null @@ -1,63 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->filter(static fn(\DOMElement $node) => $node->tagName === 'a') - ->flatMap( - static fn(\DOMElement $node) => Attributes::of()($node)->flatMap( - static fn($attributes) => $attributes - ->find(static fn($attribute) => $attribute->name() === 'href') - ->flatMap(static fn($href) => Url::maybe($href->value())) - ->flatMap( - static fn($href) => Children::of($translate)($node)->map( - static fn($children) => A::of( - $href, - $attributes, - $children, - ), - ), - ), - ), - ); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } -} diff --git a/src/Translator/NodeTranslator/BaseTranslator.php b/src/Translator/NodeTranslator/BaseTranslator.php deleted file mode 100644 index c0ef3ba..0000000 --- a/src/Translator/NodeTranslator/BaseTranslator.php +++ /dev/null @@ -1,57 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->filter(static fn(\DOMElement $node) => $node->tagName === 'base') - ->flatMap( - static fn(\DOMElement $node) => Attributes::of()($node)->flatMap( - static fn($attributes) => $attributes - ->find(static fn($attribute) => $attribute->name() === 'href') - ->flatMap(static fn($href) => Url::maybe($href->value())) - ->map(static fn($href) => Base::of( - $href, - $attributes, - )), - ), - ); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } -} diff --git a/src/Translator/NodeTranslator/DocumentTranslator.php b/src/Translator/NodeTranslator/DocumentTranslator.php deleted file mode 100644 index e6e6d48..0000000 --- a/src/Translator/NodeTranslator/DocumentTranslator.php +++ /dev/null @@ -1,102 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMDocument) - ->flatMap( - fn(\DOMDocument $node) => $this - ->buildChildren($node->childNodes, $translate) - ->map(fn($children) => Document::of( - Maybe::of($node->doctype) - ->flatMap($this->buildDoctype(...)) - ->match( - static fn($type) => $type, - static fn() => Type::of('html'), - ), - $children, - )), - ); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } - - /** - * @return Maybe - */ - private function buildDoctype(\DOMDocumentType $type): Maybe - { - /** @psalm-suppress MixedArgument */ - return Type::maybe( - $type->name, - $type->publicId, - $type->systemId, - ); - } - - /** - * @return Maybe> - */ - private function buildChildren( - \DOMNodeList $nodes, - Translator $translate, - ): Maybe { - /** @var Maybe> */ - $children = Maybe::just(Sequence::of()); - - /** - * @psalm-suppress ImpureMethodCall - * @var \DOMNode $child - */ - foreach ($nodes as $child) { - if ($child->nodeType === \XML_DOCUMENT_TYPE_NODE) { - continue; - } - - $children = $children->flatMap( - static fn($children) => $translate($child)->map( - static fn($child) => ($children)($child), - ), - ); - } - - return $children; - } -} diff --git a/src/Translator/NodeTranslator/ElementTranslator.php b/src/Translator/NodeTranslator/ElementTranslator.php deleted file mode 100644 index 3529502..0000000 --- a/src/Translator/NodeTranslator/ElementTranslator.php +++ /dev/null @@ -1,60 +0,0 @@ - */ - private Map $translators; - - /** - * @param Map $translators - */ - private function __construct( - GenericTranslator $genericTranslator, - Map $translators, - ) { - $this->genericTranslator = $genericTranslator; - $this->translators = $translators; - } - - #[\Override] - public function __invoke( - \DOMNode $node, - Translator $translate, - ): Maybe { - /** @psalm-suppress ArgumentTypeCoercion */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->flatMap(fn(\DOMElement $node) => $this->translators->get($node->tagName)) - ->flatMap(static fn($translator) => $translator($node, $translate)) - ->otherwise(fn() => ($this->genericTranslator)($node, $translate)); - } - - /** - * @psalm-pure - * - * @param Map $translators - */ - public static function of( - GenericTranslator $genericTranslator, - Map $translators, - ): self { - return new self($genericTranslator, $translators); - } -} diff --git a/src/Translator/NodeTranslator/ImgTranslator.php b/src/Translator/NodeTranslator/ImgTranslator.php deleted file mode 100644 index bdb1e76..0000000 --- a/src/Translator/NodeTranslator/ImgTranslator.php +++ /dev/null @@ -1,54 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->filter(static fn(\DOMElement $node) => $node->tagName === 'img') - ->flatMap( - static fn(\DOMElement $node) => Attributes::of()($node)->flatMap( - static fn($attributes) => $attributes - ->find(static fn($attribute) => $attribute->name() === 'src') - ->flatMap(static fn($src) => Url::maybe($src->value())) - ->map(static fn($src) => Img::of($src, $attributes)), - ), - ); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } -} diff --git a/src/Translator/NodeTranslator/LinkTranslator.php b/src/Translator/NodeTranslator/LinkTranslator.php deleted file mode 100644 index 82ff01a..0000000 --- a/src/Translator/NodeTranslator/LinkTranslator.php +++ /dev/null @@ -1,82 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->filter(static fn(\DOMElement $node) => $node->tagName === 'link') - ->flatMap( - fn(\DOMElement $node) => Attributes::of()($node)->flatMap( - $this->build(...), - ), - ); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } - - /** - * @param Set $attributes - * - * @return Maybe - */ - private function build(Set $attributes): Maybe - { - /** @var non-empty-string */ - $rel = $attributes - ->find(static fn($attribute) => $attribute->name() === 'rel') - ->map(static fn($rel) => $rel->value()) - ->filter(static fn($rel) => $rel !== '') - ->match( - static fn($rel) => $rel, - static fn() => 'related', - ); - - return $attributes - ->find(static fn($attribute) => $attribute->name() === 'href') - ->flatMap(static fn($href) => Url::maybe($href->value())) - ->map(static fn($href) => Link::of( - $href, - $rel, - $attributes, - )); - } -} diff --git a/src/Translator/NodeTranslator/ScriptTranslator.php b/src/Translator/NodeTranslator/ScriptTranslator.php deleted file mode 100644 index 8b0f95f..0000000 --- a/src/Translator/NodeTranslator/ScriptTranslator.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ - return Maybe::just($node) - ->filter(static fn($node) => $node instanceof \DOMElement) - ->filter(static fn(\DOMElement $node) => $node->tagName === 'script') - ->flatMap(static fn(\DOMElement $node) => Attributes::of()($node)->map( - static fn($attributes) => Script::of( - Text::of( - Maybe::of($node->firstChild) - ->flatMap(static fn($node) => $translate($node)) - ->map(static fn($node) => $node->content()) - ->match( - static fn($content) => $content, - static fn() => '', - ), - ), - $attributes, - ), - )); - } - - /** - * @psalm-pure - */ - public static function of(): self - { - return new self; - } -} diff --git a/src/Translator/NodeTranslators.php b/src/Translator/NodeTranslators.php deleted file mode 100644 index 787ce1f..0000000 --- a/src/Translator/NodeTranslators.php +++ /dev/null @@ -1,49 +0,0 @@ - - */ - public static function defaults(): Map - { - /** - * @psalm-suppress InvalidArgument - * @var Map - */ - return Map::of( - [\XML_HTML_DOCUMENT_NODE, DocumentTranslator::of()], - [ - \XML_ELEMENT_NODE, - ElementTranslator::of( - GenericTranslator::of(), - Map::of( - ['a', ATranslator::of()], - ['base', BaseTranslator::of()], - ['img', ImgTranslator::of()], - ['link', LinkTranslator::of()], - ['script', ScriptTranslator::of()], - ), - ), - ], - ); - } -} diff --git a/src/Visitor/Element.php b/src/Visitor/Element.php index d99d671..7c6be14 100644 --- a/src/Visitor/Element.php +++ b/src/Visitor/Element.php @@ -3,11 +3,17 @@ namespace Innmind\Html\Visitor; +use Innmind\Html\Node\Document; use Innmind\Xml\{ Node, - Element as ElementInterface, + Element as Model, + Element\Custom, +}; +use Innmind\Immutable\{ + Maybe, + Sequence, + Predicate\Instance, }; -use Innmind\Immutable\Maybe; /** * @psalm-immutable @@ -26,24 +32,16 @@ private function __construct(string $name) } /** - * @return Maybe + * @return Maybe */ - public function __invoke(Node $node): Maybe + public function __invoke(Document|Node|Model|Custom $node): Maybe { - if ( - $node instanceof ElementInterface && - $node->name() === $this->name - ) { - return Maybe::just($node); - } - - /** @var Maybe */ - return $node->children()->reduce( - Maybe::nothing(), - function(Maybe $element, Node $child): Maybe { - return $element->otherwise(fn() => $this($child)); - }, - ); + return match (true) { + $node instanceof Document => $this->visitDocument($node), + $node instanceof Node => $this->visitNode($node), + $node instanceof Model => $this->visitElement($node), + $node instanceof Custom => $this->visitCustom($node), + }; } /** @@ -71,4 +69,73 @@ public static function body(): self { return new self('body'); } + + /** + * @return Maybe + */ + private function visitDocument(Document $document): Maybe + { + return $this->visit($document->children()); + } + + /** + * @return Maybe + */ + private function visitNode(Node $node): Maybe + { + /** @var Maybe */ + return Maybe::nothing(); + } + + /** + * @return Maybe + */ + private function visitElement(Model $element): Maybe + { + if ($element->name()->toString() === $this->name) { + return Maybe::just($element); + } + + return $this->visit($element->children()); + } + + /** + * @return Maybe + */ + private function visitCustom(Custom $element): Maybe + { + $normalized = $element->normalize(); + + if ($normalized->name()->toString() === $this->name) { + return Maybe::just($element); + } + + return $this->visit($normalized->children()); + } + + /** + * @param Sequence $children + * + * @return Maybe + */ + private function visit(Sequence $children): Maybe + { + /** @var Model|Custom|null */ + $found = null; + + $found = $children + ->sink($found) + ->until( + fn($found, $child, $continuation) => $this($child)->match( + static fn($found) => $continuation->stop($found), + static fn() => $continuation->continue($found), + ), + ); + + return Maybe::of($found)->keep( + Instance::of(Model::class)->or( + Instance::of(Custom::class), + ), + ); + } } diff --git a/src/Visitor/Elements.php b/src/Visitor/Elements.php index 129850f..03fdbdd 100644 --- a/src/Visitor/Elements.php +++ b/src/Visitor/Elements.php @@ -3,9 +3,11 @@ namespace Innmind\Html\Visitor; +use Innmind\Html\Node\Document; use Innmind\Xml\{ Node, Element, + Element\Custom, }; use Innmind\Immutable\Set; @@ -26,25 +28,16 @@ private function __construct(string $name) } /** - * @return Set + * @return Set */ - public function __invoke(Node $node): Set + public function __invoke(Document|Node|Element|Custom $node): Set { - /** @var Set */ - $elements = Set::of(); - - if ( - $node instanceof Element && - $node->name() === $this->name - ) { - $elements = ($elements)($node); - } - - /** @var Set */ - return $node->children()->reduce( - $elements, - fn(Set $elements, Node $child): Set => $elements->merge($this($child)), - ); + return match (true) { + $node instanceof Document => $this->visitDocument($node), + $node instanceof Node => $this->visitNode($node), + $node instanceof Element => $this->visitElement($node), + $node instanceof Custom => $this->visitCustom($node), + }; } /** @@ -56,4 +49,64 @@ public static function of(string $name): self { return new self($name); } + + /** + * @return Set + */ + private function visitDocument(Document $document): Set + { + return $document + ->children() + ->flatMap(fn($child) => $this($child)->unsorted()) + ->toSet(); + } + + /** + * @return Set + */ + private function visitNode(Node $node): Set + { + return Set::of(); + } + + /** + * @return Set + */ + private function visitElement(Element $element): Set + { + /** @var Set */ + $elements = Set::of(); + + if ($element->name()->toString() === $this->name) { + $elements = ($elements)($element); + } + + return $elements->merge( + $element + ->children() + ->flatMap(fn($child) => $this($child)->unsorted()) + ->toSet(), + ); + } + + /** + * @return Set + */ + private function visitCustom(Custom $element): Set + { + /** @var Set */ + $elements = Set::of(); + $normalized = $element->normalize(); + + if ($normalized->name()->toString() === $this->name) { + $elements = ($elements)($element); + } + + return $elements->merge( + $normalized + ->children() + ->flatMap(fn($child) => $this($child)->unsorted()) + ->toSet(), + ); + } } diff --git a/tests/Element/ATest.php b/tests/Element/ATest.php index 9a92d84..5d9b8d0 100644 --- a/tests/Element/ATest.php +++ b/tests/Element/ATest.php @@ -9,10 +9,7 @@ Node, }; use Innmind\Url\Url; -use Innmind\Immutable\{ - Set, - Sequence, -}; +use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ATest extends TestCase @@ -20,16 +17,16 @@ class ATest extends TestCase public function testInterface() { $this->assertInstanceOf( - Element::class, + Element\Custom::class, $a = A::of( $href = Url::of('http://example.com'), - Set::of(), - Sequence::of($child = Node\Text::of('')), + Sequence::of(), + Sequence::of($child = Node::text('')), ), ); - $this->assertSame('a', $a->name()); + $this->assertSame('a', $a->normalize()->name()->toString()); $this->assertSame($href, $a->href()); - $this->assertSame($child, $a->children()->first()->match( + $this->assertSame($child, $a->normalize()->children()->first()->match( static fn($node) => $node, static fn() => null, )); @@ -37,23 +34,26 @@ public function testInterface() public function testWithoutAttributes() { - $this->assertTrue( - A::of(Url::of('http://example.com'))->attributes()->empty(), + $this->assertFalse( + A::of(Url::of('http://example.com'))->normalize()->attributes()->empty(), ); } public function testWithoutChildren() { $this->assertTrue( - A::of(Url::of('http://example.com'))->children()->empty(), + A::of(Url::of('http://example.com'))->normalize()->children()->empty(), ); } public function testToString() { $this->assertSame( - '', - A::of(Url::of('http://example.com/'))->toString(), + ''."\n", + A::of(Url::of('http://example.com/')) + ->normalize() + ->asContent() + ->toString(), ); } } diff --git a/tests/Element/BaseTest.php b/tests/Element/BaseTest.php index 7a61311..277b794 100644 --- a/tests/Element/BaseTest.php +++ b/tests/Element/BaseTest.php @@ -6,7 +6,7 @@ use Innmind\Html\Element\Base; use Innmind\Xml\Element; use Innmind\Url\Url; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BaseTest extends TestCase @@ -14,28 +14,31 @@ class BaseTest extends TestCase public function testInterface() { $this->assertInstanceOf( - Element::class, + Element\Custom::class, $base = Base::of( $href = Url::of('http://example.com'), - Set::of(), + Sequence::of(), ), ); - $this->assertSame('base', $base->name()); + $this->assertSame('base', $base->normalize()->name()->toString()); $this->assertSame($href, $base->href()); } public function testWithoutAttributes() { - $this->assertTrue( - Base::of(Url::of('http://example.com'))->attributes()->empty(), + $this->assertFalse( + Base::of(Url::of('http://example.com'))->normalize()->attributes()->empty(), ); } public function testToString() { $this->assertSame( - '', - Base::of(Url::of('http://example.com/'))->toString(), + ''."\n", + Base::of(Url::of('http://example.com/')) + ->normalize() + ->asContent() + ->toString(), ); } } diff --git a/tests/Element/ImgTest.php b/tests/Element/ImgTest.php index 841d941..2ce2c9a 100644 --- a/tests/Element/ImgTest.php +++ b/tests/Element/ImgTest.php @@ -6,7 +6,7 @@ use Innmind\Html\Element\Img; use Innmind\Xml\Element; use Innmind\Url\Url; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ImgTest extends TestCase @@ -14,28 +14,31 @@ class ImgTest extends TestCase public function testInterface() { $this->assertInstanceOf( - Element::class, + Element\Custom::class, $img = Img::of( $src = Url::of('http://example.com'), - Set::of(), + Sequence::of(), ), ); - $this->assertSame('img', $img->name()); + $this->assertSame('img', $img->normalize()->name()->toString()); $this->assertSame($src, $img->src()); } public function testWithoutAttributes() { - $this->assertTrue( - Img::of(Url::of('http://example.com'))->attributes()->empty(), + $this->assertFalse( + Img::of(Url::of('http://example.com'))->normalize()->attributes()->empty(), ); } public function testToString() { $this->assertSame( - '', - Img::of(Url::of('http://example.com/'))->toString(), + ''."\n", + Img::of(Url::of('http://example.com/')) + ->normalize() + ->asContent() + ->toString(), ); } } diff --git a/tests/Element/LinkTest.php b/tests/Element/LinkTest.php index 2d36e60..e430aa3 100644 --- a/tests/Element/LinkTest.php +++ b/tests/Element/LinkTest.php @@ -6,7 +6,7 @@ use Innmind\Html\Element\Link; use Innmind\Xml\Element; use Innmind\Url\Url; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LinkTest extends TestCase @@ -14,33 +14,36 @@ class LinkTest extends TestCase public function testInterface() { $this->assertInstanceOf( - Element::class, + Element\Custom::class, $link = Link::of( $href = Url::of('http://example.com'), 'rel', - Set::of(), + Sequence::of(), ), ); - $this->assertSame('link', $link->name()); + $this->assertSame('link', $link->normalize()->name()->toString()); $this->assertSame($href, $link->href()); $this->assertSame('rel', $link->relationship()); } public function testWithoutAttributes() { - $this->assertTrue( + $this->assertFalse( Link::of( Url::of('http://example.com'), 'foo', - )->attributes()->empty(), + )->normalize()->attributes()->empty(), ); } public function testToString() { $this->assertSame( - '', - Link::of(Url::of('http://example.com'), 'foo')->toString(), + ''."\n", + Link::of(Url::of('http://example.com'), 'foo') + ->normalize() + ->asContent() + ->toString(), ); } } diff --git a/tests/Element/ScriptTest.php b/tests/Element/ScriptTest.php index 122704b..4355e29 100644 --- a/tests/Element/ScriptTest.php +++ b/tests/Element/ScriptTest.php @@ -6,10 +6,10 @@ use Innmind\Html\Element\Script; use Innmind\Xml\{ Element, - Node\Text, + Node, Attribute, }; -use Innmind\Immutable\Set; +use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ScriptTest extends TestCase @@ -17,24 +17,28 @@ class ScriptTest extends TestCase public function testInterface() { $script = Script::of( - Text::of('foo'), + Node::text('foo'), ); - $this->assertInstanceOf(Element::class, $script); - $this->assertSame('', $script->toString()); + $this->assertInstanceOf(Element\Custom::class, $script); + $script = $script->normalize(); + $this->assertSame( + ''."\n", + $script->asContent()->toString(), + ); $this->assertCount(1, $script->children()); } public function testWithAttributes() { $script = Script::of( - Text::of('foo'), - Set::of( + Node::text('foo'), + Sequence::of( $attribute = Attribute::of('foo', 'bar'), ), ); - $this->assertSame($attribute, $script->attributes()->get('foo')->match( + $this->assertSame($attribute, $script->normalize()->attribute('foo')->match( static fn($attribute) => $attribute, static fn() => null, )); diff --git a/tests/Node/DocumentTest.php b/tests/Node/DocumentTest.php index 5cfa802..8dd0bab 100644 --- a/tests/Node/DocumentTest.php +++ b/tests/Node/DocumentTest.php @@ -6,30 +6,22 @@ use Innmind\Html\Node\Document; use Innmind\Xml\{ Node, - Node\Document\Type, - Node\Text, - Element\Element, + Document\Type, + Element, + Element\Name, }; use Innmind\Immutable\Sequence; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DocumentTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - Node::class, - Document::of(Type::of('html')), - ); - } - public function testType() { $type = Type::of('html'); $this->assertSame( $type, - (Document::of($type))->type(), + Document::of($type)->type(), ); } @@ -45,7 +37,7 @@ public function testWithChildren() { $document = Document::of( Type::of('html'), - Sequence::of($child = Text::of('')), + Sequence::of($child = Node::text('')), ); $this->assertSame($child, $document->children()->first()->match( @@ -55,38 +47,22 @@ public function testWithChildren() $this->assertFalse($document->children()->empty()); } - public function testContent() - { - $document = Document::of( - Type::of('html'), - Sequence::of( - Element::of( - 'html', - null, - Sequence::of(Text::of('wat')), - ), - ), - ); - - $this->assertSame('wat', $document->content()); - } - public function testCast() { $document = Document::of( Type::of('html'), Sequence::of( Element::of( - 'html', + Name::of('html'), null, - Sequence::of(Text::of('wat')), + Sequence::of(Node::text('wat')), ), ), ); $this->assertSame( - ''."\n".'wat', - $document->toString(), + ''."\n".'wat'."\n", + $document->asContent()->toString(), ); } @@ -95,14 +71,14 @@ public function testPrependChild() $document = Document::of( Type::of('html'), Sequence::of( - Element::of('foo'), - Element::of('bar'), - Element::of('baz'), + Element::of(Name::of('foo')), + Element::of(Name::of('bar')), + Element::of(Name::of('baz')), ), ); $document2 = $document->prependChild( - $node = Text::of(''), + $node = Node::text(''), ); $this->assertNotSame($document, $document2); @@ -137,14 +113,14 @@ public function testAppendChild() $document = Document::of( Type::of('html'), Sequence::of( - Element::of('foo'), - Element::of('bar'), - Element::of('baz'), + Element::of(Name::of('foo')), + Element::of(Name::of('bar')), + Element::of(Name::of('baz')), ), ); $document2 = $document->appendChild( - $node = Text::of(''), + $node = Node::text(''), ); $this->assertNotSame($document, $document2); @@ -180,9 +156,13 @@ public function testAsContent() Type::of('html'), Sequence::of( Element::of( - 'html', + Name::of('html'), null, - Sequence::of(Text::of('wat')), + Sequence::of(Element::of( + Name::of('body'), + null, + Sequence::of(Node::text('wat')), + )), ), ), ); @@ -191,8 +171,9 @@ public function testAsContent() << - wat + wat + HTML, $document->asContent()->toString(), ); diff --git a/tests/Reader/ReaderTest.php b/tests/Reader/ReaderTest.php index 8c2406b..0b13289 100644 --- a/tests/Reader/ReaderTest.php +++ b/tests/Reader/ReaderTest.php @@ -7,10 +7,7 @@ Reader\Reader, Node\Document, }; -use Innmind\Xml\{ - Reader as ReaderInterface, - Node\Document as XmlDocument, -}; +use Innmind\Xml\Format; use Innmind\Filesystem\File\Content; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -20,15 +17,7 @@ class ReaderTest extends TestCase public function setUp(): void { - $this->read = Reader::default(); - } - - public function testInterface() - { - $this->assertInstanceOf( - ReaderInterface::class, - $this->read, - ); + $this->read = Reader::new(); } public function testReadSimple() @@ -59,7 +48,7 @@ public function testReadSimple() HTML; $this->assertInstanceOf(Document::class, $node); - $this->assertSame($expected, $node->toString()); + $this->assertSame($expected, $node->asContent(Format::inline)->toString()); } public function testReadFullPage() @@ -83,7 +72,7 @@ public function testReadScreenOnline() static fn() => null, ); - $this->assertInstanceOf(XmlDocument::class, $node); + $this->assertInstanceOf(Document::class, $node); } public function testReturnNothingWhenEmptyStream() diff --git a/tests/Translator/NodeTranslator/ATranslatorTest.php b/tests/Translator/NodeTranslator/ATranslatorTest.php index 1dc7cab..181d10f 100644 --- a/tests/Translator/NodeTranslator/ATranslatorTest.php +++ b/tests/Translator/NodeTranslator/ATranslatorTest.php @@ -4,54 +4,21 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\ATranslator, - Element\A, -}; -use Innmind\Xml\Translator\{ Translator, - NodeTranslators, - NodeTranslator, + Element\A, }; +use Innmind\Immutable\Predicate\Instance; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ATranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - ATranslator::of(), - ); - } - - public function testReturnNothingWhenNotExpectedElement() - { - $dom = new \DOMDocument; - $dom->loadHTML(''); - - $result = ATranslator::of()( - $dom->childNodes->item(1), - Translator::of( - NodeTranslators::defaults(), - ) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - public function testTranslate() { $dom = new \DOMDocument; $dom->loadHTML('foo'); - $a = ATranslator::of()( + $a = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($a) => $a, static fn() => null, @@ -59,8 +26,9 @@ public function testTranslate() $this->assertInstanceOf(A::class, $a); $this->assertSame('/', $a->href()->toString()); + $a = $a->normalize(); $this->assertCount(2, $a->attributes()); - $this->assertSame('whatever', $a->attributes()->get('class')->match( + $this->assertSame('whatever', $a->attribute('class')->match( static fn($attribute) => $attribute->value(), static fn() => null, )); @@ -72,12 +40,11 @@ public function testReturnNothingWhenMissingHrefAttribute() $dom = new \DOMDocument; $dom->loadHTML('foo'); - $result = ATranslator::of()( + $result = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - ); + ) + ->maybe() + ->keep(Instance::of(A::class)); $this->assertNull($result->match( static fn($node) => $node, diff --git a/tests/Translator/NodeTranslator/BaseTranslatorTest.php b/tests/Translator/NodeTranslator/BaseTranslatorTest.php index afea074..588bf74 100644 --- a/tests/Translator/NodeTranslator/BaseTranslatorTest.php +++ b/tests/Translator/NodeTranslator/BaseTranslatorTest.php @@ -4,54 +4,21 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\BaseTranslator, - Element\Base, -}; -use Innmind\Xml\Translator\{ Translator, - NodeTranslators, - NodeTranslator, + Element\Base, }; +use Innmind\Immutable\Predicate\Instance; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class BaseTranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - BaseTranslator::of(), - ); - } - - public function testReturnNothingWhenNotExpectedElement() - { - $dom = new \DOMDocument; - $dom->loadHTML(''); - - $result = BaseTranslator::of()( - $dom->childNodes->item(1), - Translator::of( - NodeTranslators::defaults(), - ) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - public function testTranslate() { $dom = new \DOMDocument; $dom->loadHTML(''); - $base = BaseTranslator::of()( + $base = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($base) => $base, static fn() => null, @@ -59,8 +26,9 @@ public function testTranslate() $this->assertInstanceOf(Base::class, $base); $this->assertSame('/', $base->href()->toString()); + $base = $base->normalize(); $this->assertCount(2, $base->attributes()); - $this->assertSame('_blank', $base->attributes()->get('target')->match( + $this->assertSame('_blank', $base->attribute('target')->match( static fn($attribute) => $attribute->value(), static fn() => null, )); @@ -71,12 +39,11 @@ public function testReturnNothingWhenMissingHrefAttribute() $dom = new \DOMDocument; $dom->loadHTML(''); - $result = BaseTranslator::of()( + $result = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - ); + ) + ->maybe() + ->keep(Instance::of(Base::class)); $this->assertNull($result->match( static fn($node) => $node, diff --git a/tests/Translator/NodeTranslator/DocumentTranslatorTest.php b/tests/Translator/NodeTranslator/DocumentTranslatorTest.php index 6c6f071..d80f3b4 100644 --- a/tests/Translator/NodeTranslator/DocumentTranslatorTest.php +++ b/tests/Translator/NodeTranslator/DocumentTranslatorTest.php @@ -4,34 +4,20 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\DocumentTranslator, + Translator, Node\Document, }; -use Innmind\Xml\{ - Translator\NodeTranslator, - Translator\Translator, - Translator\NodeTranslators, -}; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class DocumentTranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - DocumentTranslator::of(), - ); - } - public function testTranslate() { $document = new \DOMDocument; $document->loadHtml(''); - $node = DocumentTranslator::of()( + $node = Translator::new()( $document, - Translator::of(NodeTranslators::defaults()) )->match( static fn($node) => $node, static fn() => null, @@ -40,7 +26,16 @@ public function testTranslate() $this->assertInstanceOf(Document::class, $node); $this->assertSame('html', $node->type()->name()); $this->assertCount(1, $node->children()); - $this->assertSame('', $node->content()); + $this->assertSame( + << + + + + + HTML, + $node->asContent()->toString(), + ); } public function testTranslateWithoutDoctype() @@ -48,9 +43,8 @@ public function testTranslateWithoutDoctype() $document = new \DOMDocument; $document->loadHtml(''); - $node = DocumentTranslator::of()( + $node = Translator::new()( $document, - Translator::of(NodeTranslators::defaults()) )->match( static fn($node) => $node, static fn() => null, @@ -67,9 +61,8 @@ public function testTranslateWithoutChildren() $document = new \DOMDocument; $document->loadHtml(''); - $node = DocumentTranslator::of()( + $node = Translator::new()( $document, - Translator::of(NodeTranslators::defaults()) )->match( static fn($node) => $node, static fn() => null, @@ -77,20 +70,4 @@ public function testTranslateWithoutChildren() $this->assertTrue($node->children()->empty()); } - - public function testReturnNothingWhenInvalidNode() - { - $document = new \DOMDocument; - $document->loadXML(''); - - $result = DocumentTranslator::of()( - $document->childNodes->item(0), - Translator::of(NodeTranslators::defaults()) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } } diff --git a/tests/Translator/NodeTranslator/ElementTranslatorTest.php b/tests/Translator/NodeTranslator/ElementTranslatorTest.php deleted file mode 100644 index bd38d8b..0000000 --- a/tests/Translator/NodeTranslator/ElementTranslatorTest.php +++ /dev/null @@ -1,142 +0,0 @@ -expected = Node\Text::of(''); - $this->translate = ElementTranslator::of( - GenericTranslator::of(), - Map::of( - [ - 'bar', - new class implements NodeTranslator { - public function __invoke(\DOMNode $node, Translator $translate): Maybe - { - return Maybe::nothing(); - } - }, - ], - [ - 'baz', - new class($this->expected) implements NodeTranslator { - public function __construct( - private $expected, - ) { - } - - public function __invoke(\DOMNode $node, Translator $translate): Maybe - { - if ($node instanceof \DOMElement && $node->nodeName === 'baz') { - return Maybe::just($this->expected); - } - - return Maybe::nothing(); - } - }, - ], - ), - ); - } - - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - $this->translate, - ); - } - - public function testReturnNothingWhenInvalidNode() - { - $result = ElementTranslator::of( - GenericTranslator::of(), - Map::of(), - )( - new \DOMNode, - Translator::of( - NodeTranslators::defaults(), - ), - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - - public function testTranslateToGenericWhenNoSubTranslatorFound() - { - $dom = new \DOMDocument; - $dom->loadXML(''); - - $node = ($this->translate)( - $dom->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - )->match( - static fn($node) => $node, - static fn() => null, - ); - - $this->assertInstanceOf(SelfClosingElement::class, $node); - } - - public function testTranslateToGenericWhenSubTranslatorReturnsNothing() - { - $dom = new \DOMDocument; - $dom->loadXML(''); - - $node = ($this->translate)( - $dom->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - )->match( - static fn($node) => $node, - static fn() => null, - ); - - $this->assertInstanceOf(SelfClosingElement::class, $node); - } - - public function testTranslateViaSubTranslator() - { - $dom = new \DOMDocument; - $dom->loadXML(''); - - $node = ($this->translate)( - $dom->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - )->match( - static fn($node) => $node, - static fn() => null, - ); - - $this->assertSame($this->expected, $node); - } -} diff --git a/tests/Translator/NodeTranslator/ImgTranslatorTest.php b/tests/Translator/NodeTranslator/ImgTranslatorTest.php index 4cb531d..f1f6972 100644 --- a/tests/Translator/NodeTranslator/ImgTranslatorTest.php +++ b/tests/Translator/NodeTranslator/ImgTranslatorTest.php @@ -4,54 +4,21 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\ImgTranslator, - Element\Img, -}; -use Innmind\Xml\Translator\{ Translator, - NodeTranslators, - NodeTranslator, + Element\Img, }; +use Innmind\Immutable\Predicate\Instance; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ImgTranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - ImgTranslator::of(), - ); - } - - public function testReturnNothingWhenNotExpectedElement() - { - $dom = new \DOMDocument; - $dom->loadHTML(''); - - $result = ImgTranslator::of()( - $dom->childNodes->item(1), - Translator::of( - NodeTranslators::defaults(), - ) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - public function testTranslate() { $dom = new \DOMDocument; $dom->loadHTML('bar'); - $img = ImgTranslator::of()( + $img = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($img) => $img, static fn() => null, @@ -59,8 +26,9 @@ public function testTranslate() $this->assertInstanceOf(Img::class, $img); $this->assertSame('foo.png', $img->src()->toString()); + $img = $img->normalize(); $this->assertCount(2, $img->attributes()); - $this->assertSame('bar', $img->attributes()->get('alt')->match( + $this->assertSame('bar', $img->attribute('alt')->match( static fn($attribute) => $attribute->value(), static fn() => null, )); @@ -71,12 +39,11 @@ public function testReturnNothingWhenMissingHrefAttribute() $dom = new \DOMDocument; $dom->loadHTML(''); - $result = ImgTranslator::of()( + $result = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - ); + ) + ->maybe() + ->keep(Instance::of(Img::class)); $this->assertNull($result->match( static fn($node) => $node, diff --git a/tests/Translator/NodeTranslator/LinkTranslatorTest.php b/tests/Translator/NodeTranslator/LinkTranslatorTest.php index 8019774..c7038c6 100644 --- a/tests/Translator/NodeTranslator/LinkTranslatorTest.php +++ b/tests/Translator/NodeTranslator/LinkTranslatorTest.php @@ -4,54 +4,21 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\LinkTranslator, - Element\Link, -}; -use Innmind\Xml\Translator\{ Translator, - NodeTranslators, - NodeTranslator, + Element\Link, }; +use Innmind\Immutable\Predicate\Instance; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class LinkTranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - LinkTranslator::of(), - ); - } - - public function testReturnNothingWhenNotExpectedElement() - { - $dom = new \DOMDocument; - $dom->loadHTML(''); - - $result = LinkTranslator::of()( - $dom->childNodes->item(1), - Translator::of( - NodeTranslators::defaults(), - ) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - public function testTranslate() { $dom = new \DOMDocument; $dom->loadHTML(''); - $link = LinkTranslator::of()( + $link = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($link) => $link, static fn() => null, @@ -60,8 +27,9 @@ public function testTranslate() $this->assertInstanceOf(Link::class, $link); $this->assertSame('/', $link->href()->toString()); $this->assertSame('next', $link->relationship()); + $link = $link->normalize(); $this->assertCount(3, $link->attributes()); - $this->assertSame('fr', $link->attributes()->get('hreflang')->match( + $this->assertSame('fr', $link->attribute('hreflang')->match( static fn($attribute) => $attribute->value(), static fn() => null, )); @@ -72,11 +40,8 @@ public function testTranslateWithoutRelationship() $dom = new \DOMDocument; $dom->loadHTML(''); - $link = LinkTranslator::of()( + $link = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($link) => $link, static fn() => null, @@ -85,8 +50,13 @@ public function testTranslateWithoutRelationship() $this->assertInstanceOf(Link::class, $link); $this->assertSame('/', $link->href()->toString()); $this->assertSame('related', $link->relationship()); - $this->assertCount(2, $link->attributes()); - $this->assertSame('fr', $link->attributes()->get('hreflang')->match( + $link = $link->normalize(); + $this->assertCount(3, $link->attributes()); + $this->assertSame('fr', $link->attribute('hreflang')->match( + static fn($attribute) => $attribute->value(), + static fn() => null, + )); + $this->assertSame('related', $link->attribute('rel')->match( static fn($attribute) => $attribute->value(), static fn() => null, )); @@ -97,12 +67,11 @@ public function testReturnNothingWhenMissingHrefAttribute() $dom = new \DOMDocument; $dom->loadHTML(''); - $result = LinkTranslator::of()( + $result = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) - ); + ) + ->maybe() + ->keep(Instance::of(Link::class)); $this->assertNull($result->match( static fn($node) => $node, diff --git a/tests/Translator/NodeTranslator/ScriptTranslatorTest.php b/tests/Translator/NodeTranslator/ScriptTranslatorTest.php index e888623..7440e9d 100644 --- a/tests/Translator/NodeTranslator/ScriptTranslatorTest.php +++ b/tests/Translator/NodeTranslator/ScriptTranslatorTest.php @@ -4,65 +4,35 @@ namespace Tests\Innmind\Html\Translator\NodeTranslator; use Innmind\Html\{ - Translator\NodeTranslator\ScriptTranslator, - Element\Script, -}; -use Innmind\Xml\Translator\{ Translator, - NodeTranslators, - NodeTranslator, + Element\Script, }; use Innmind\BlackBox\PHPUnit\Framework\TestCase; class ScriptTranslatorTest extends TestCase { - public function testInterface() - { - $this->assertInstanceOf( - NodeTranslator::class, - ScriptTranslator::of(), - ); - } - - public function testReturnNothingWhenNotExpectedElement() - { - $dom = new \DOMDocument; - $dom->loadHTML(''); - - $result = ScriptTranslator::of()( - $dom->childNodes->item(1), - Translator::of( - NodeTranslators::defaults(), - ) - ); - - $this->assertNull($result->match( - static fn($node) => $node, - static fn() => null, - )); - } - public function testTranslate() { $dom = new \DOMDocument; $dom->loadHTML(''); - $script = ScriptTranslator::of()( + $script = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($script) => $script, static fn() => null, ); $this->assertInstanceOf(Script::class, $script); - $this->assertSame('var foo = 42;', $script->content()); + $script = $script->normalize(); + $this->assertSame( + ''."\n", + $script->asContent()->toString(), + ); $this->assertCount(1, $script->attributes()); $this->assertSame( 'text/javascript', - $script->attributes()->get('type')->match( + $script->attribute('type')->match( static fn($attribute) => $attribute->value(), static fn() => null, ), @@ -75,18 +45,19 @@ public function testTranslateWithoutCode() $dom = new \DOMDocument; $dom->loadHTML(''); - $script = ScriptTranslator::of()( + $script = Translator::new()( $dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), - Translator::of( - NodeTranslators::defaults(), - ) )->match( static fn($script) => $script, static fn() => null, ); $this->assertInstanceOf(Script::class, $script); - $this->assertSame('', $script->content()); + $script = $script->normalize(); + $this->assertSame( + ''."\n", + $script->asContent()->toString(), + ); $this->assertCount(0, $script->attributes()); $this->assertCount(1, $script->children()); } diff --git a/tests/Translator/NodeTranslatorsTest.php b/tests/Translator/NodeTranslatorsTest.php deleted file mode 100644 index a408783..0000000 --- a/tests/Translator/NodeTranslatorsTest.php +++ /dev/null @@ -1,38 +0,0 @@ -assertInstanceOf(Map::class, $defaults); - $this->assertCount(2, $defaults); - $this->assertInstanceOf( - DocumentTranslator::class, - $defaults->get(\XML_HTML_DOCUMENT_NODE)->match( - static fn($translator) => $translator, - static fn() => null, - ), - ); - $this->assertInstanceOf( - ElementTranslator::class, - $defaults->get(\XML_ELEMENT_NODE)->match( - static fn($translator) => $translator, - static fn() => null, - ), - ); - $this->assertEquals($defaults, NodeTranslators::defaults()); - } -} diff --git a/tests/Visitor/BodyTest.php b/tests/Visitor/BodyTest.php index 49d396e..bd807bf 100644 --- a/tests/Visitor/BodyTest.php +++ b/tests/Visitor/BodyTest.php @@ -8,8 +8,8 @@ Reader\Reader, }; use Innmind\Xml\{ - Element as ElementInterface, - Element\Element, + Element, + Element\Name, }; use Innmind\Filesystem\File\Content; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -20,7 +20,7 @@ class BodyTest extends TestCase public function setUp(): void { - $this->read = Reader::default(); + $this->read = Reader::new(); } public function testExtractBody() @@ -37,15 +37,15 @@ public function testExtractBody() static fn() => null, ); - $this->assertInstanceOf(ElementInterface::class, $body); - $this->assertSame('body', $body->name()); + $this->assertInstanceOf(Element::class, $body); + $this->assertSame('body', $body->name()->toString()); $this->assertFalse($body->children()->empty()); $this->assertFalse($body->attributes()->empty()); } public function testReturnNothingWhenBodyNotFound() { - $this->assertNull(Visitor\Element::body()(Element::of('head'))->match( + $this->assertNull(Visitor\Element::body()(Element::of(Name::of('head')))->match( static fn($node) => $node, static fn() => null, )); diff --git a/tests/Visitor/ElementTest.php b/tests/Visitor/ElementTest.php index a7e16c4..6cb16ef 100644 --- a/tests/Visitor/ElementTest.php +++ b/tests/Visitor/ElementTest.php @@ -8,8 +8,8 @@ Reader\Reader, }; use Innmind\Xml\{ - Element as ElementInterface, - Element\Element, + Element, + Element\Name, }; use Innmind\Filesystem\File\Content; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -20,7 +20,7 @@ class ElementTest extends TestCase public function setUp(): void { - $this->read = Reader::default(); + $this->read = Reader::new(); } public function testExtractElement() @@ -37,15 +37,15 @@ public function testExtractElement() static fn() => null, ); - $this->assertInstanceOf(ElementInterface::class, $h1); - $this->assertSame('h1', $h1->name()); + $this->assertInstanceOf(Element::class, $h1); + $this->assertSame('h1', $h1->name()->toString()); $this->assertFalse($h1->children()->empty()); $this->assertFalse($h1->attributes()->empty()); } public function testReturnNothingWhenElementNotFound() { - $this->assertNull(ElementFinder::of('foo')(Element::of('whatever'))->match( + $this->assertNull(ElementFinder::of('foo')(Element::of(Name::of('whatever')))->match( static fn($node) => $node, static fn() => null, )); diff --git a/tests/Visitor/ElementsTest.php b/tests/Visitor/ElementsTest.php index bb71bef..2d8cc0d 100644 --- a/tests/Visitor/ElementsTest.php +++ b/tests/Visitor/ElementsTest.php @@ -7,7 +7,10 @@ Visitor\Elements, Reader\Reader, }; -use Innmind\Xml\Element\Element; +use Innmind\Xml\{ + Element, + Element\Name, +}; use Innmind\Filesystem\File\Content; use Innmind\Immutable\Set; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -18,7 +21,7 @@ class ElementsTest extends TestCase public function setUp(): void { - $this->read = Reader::default(); + $this->read = Reader::new(); } public function testExtractElement() @@ -38,7 +41,7 @@ public function testExtractElement() public function testEmptySetWhenNoElementFound() { - $elements = Elements::of('foo')(Element::of('whatever')); + $elements = Elements::of('foo')(Element::of(Name::of('whatever'))); $this->assertInstanceOf(Set::class, $elements); $this->assertCount(0, $elements); diff --git a/tests/Visitor/HeadTest.php b/tests/Visitor/HeadTest.php index ef601de..2fca214 100644 --- a/tests/Visitor/HeadTest.php +++ b/tests/Visitor/HeadTest.php @@ -8,8 +8,8 @@ Reader\Reader, }; use Innmind\Xml\{ - Element as ElementInterface, - Element\Element, + Element, + Element\Name, }; use Innmind\Filesystem\File\Content; use Innmind\BlackBox\PHPUnit\Framework\TestCase; @@ -20,7 +20,7 @@ class HeadTest extends TestCase public function setUp(): void { - $this->read = Reader::default(); + $this->read = Reader::new(); } public function testExtractHead() @@ -37,15 +37,15 @@ public function testExtractHead() static fn() => null, ); - $this->assertInstanceOf(ElementInterface::class, $head); - $this->assertSame('head', $head->name()); + $this->assertInstanceOf(Element::class, $head); + $this->assertSame('head', $head->name()->toString()); $this->assertFalse($head->children()->empty()); $this->assertTrue($head->attributes()->empty()); } public function testReturnNothingWhenHeadNotFound() { - $this->assertNull(Visitor\Element::head()(Element::of('body'))->match( + $this->assertNull(Visitor\Element::head()(Element::of(Name::of('body')))->match( static fn($node) => $node, static fn() => null, ));