Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Node>
); // Maybe<Document|Element|Custom|Node>
```

## Extract some elements of the tree
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
116 changes: 14 additions & 102 deletions src/Element/A.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,18 +30,18 @@ private function __construct(Url $href, Element\Element $element)
/**
* @psalm-pure
*
* @param Set<Attribute>|null $attributes
* @param Sequence<Node>|null $children
* @param Sequence<Attribute>|null $attributes
* @param Sequence<Node|Element|Custom>|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,
),
Expand All @@ -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();
}
}
117 changes: 15 additions & 102 deletions src/Element/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,11 +29,14 @@ private function __construct(Url $href, SelfClosingElement $element)
/**
* @psalm-pure
*
* @param Set<Attribute>|null $attributes
* @param Sequence<Attribute>|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
Expand All @@ -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();
}
}
Loading