Update Composer packages

This commit is contained in:
Paul Nicoué 2022-12-19 14:56:05 +01:00
parent 0320235f6c
commit a8b68fb61b
378 changed files with 28466 additions and 28852 deletions

View file

@ -21,20 +21,9 @@ use Kirby\Toolkit\Str;
*/
class Element
{
/**
* @var array
*/
protected $marks;
protected array $marks;
protected DOMElement $node;
/**
* @var \DOMElement
*/
protected $node;
/**
* @param \DOMElement $node
* @param array $marks
*/
public function __construct(DOMElement $node, array $marks = [])
{
$this->marks = $marks;
@ -44,12 +33,8 @@ class Element
/**
* The returns the attribute value or
* the given fallback if the attribute does not exist
*
* @param string $attr
* @param string|null $fallback
* @return string|null
*/
public function attr(string $attr, string $fallback = null): ?string
public function attr(string $attr, string|null $fallback = null): string|null
{
if ($this->node->hasAttribute($attr)) {
return $this->node->getAttribute($attr) ?? $fallback;
@ -60,8 +45,6 @@ class Element
/**
* Returns a list of all child elements
*
* @return \DOMNodeList
*/
public function children(): DOMNodeList
{
@ -70,8 +53,6 @@ class Element
/**
* Returns an array with all class names
*
* @return array
*/
public function classList(): array
{
@ -80,20 +61,16 @@ class Element
/**
* Returns the value of the class attribute
*
* @return string|null
*/
public function className(): ?string
public function className(): string|null
{
return $this->attr('class');
}
/**
* Returns the original dom element
*
* @return \DOMElement
*/
public function element()
public function element(): DOMElement
{
return $this->node;
}
@ -101,9 +78,6 @@ class Element
/**
* Returns an array with all nested elements
* that could be found for the given query
*
* @param string $query
* @return array
*/
public function filter(string $query): array
{
@ -121,11 +95,8 @@ class Element
/**
* Tries to find a single nested element by
* query and otherwise returns null
*
* @param string $query
* @return \Kirby\Parsley\Element|null
*/
public function find(string $query)
public function find(string $query): Element|null
{
if ($result = $this->query($query)[0]) {
return new static($result);
@ -136,19 +107,14 @@ class Element
/**
* Returns the inner HTML of the element
*
* @param array|null $marks List of allowed marks
* @return string
*/
public function innerHtml(array $marks = null): string
public function innerHtml(array|null $marks = null): string
{
return (new Inline($this->node, $marks ?? $this->marks))->innerHtml();
}
/**
* Returns the contents as plain text
*
* @return string
*/
public function innerText(): string
{
@ -157,40 +123,31 @@ class Element
/**
* Returns the full HTML for the element
*
* @param array|null $marks
* @return string
*/
public function outerHtml(array $marks = null): string
public function outerHtml(array|null $marks = null): string
{
return $this->node->ownerDocument->saveHtml($this->node);
}
/**
* Searches nested elements
*
* @param string $query
* @return DOMNodeList|null
*/
public function query(string $query)
public function query(string $query): DOMNodeList|null
{
return (new DOMXPath($this->node->ownerDocument))->query($query, $this->node);
$path = new DOMXPath($this->node->ownerDocument);
return $path->query($query, $this->node);
}
/**
* Removes the element from the DOM
*
* @return void
*/
public function remove()
public function remove(): void
{
$this->node->parentNode->removeChild($this->node);
}
/**
* Returns the name of the element
*
* @return string
*/
public function tagName(): string
{

View file

@ -2,8 +2,10 @@
namespace Kirby\Parsley;
use DOMComment;
use DOMNode;
use DOMNodeList;
use DOMText;
use Kirby\Toolkit\Html;
/**
@ -20,20 +22,9 @@ use Kirby\Toolkit\Html;
*/
class Inline
{
/**
* @var string
*/
protected $html = '';
protected string $html = '';
protected array $marks = [];
/**
* @var array
*/
protected $marks = [];
/**
* @param \DOMNode $node
* @param array $marks
*/
public function __construct(DOMNode $node, array $marks = [])
{
$this->createMarkRules($marks);
@ -42,11 +33,8 @@ class Inline
/**
* Loads all mark rules
*
* @param array $marks
* @return array
*/
protected function createMarkRules(array $marks)
protected function createMarkRules(array $marks): array
{
foreach ($marks as $mark) {
$this->marks[$mark['tag']] = $mark;
@ -58,10 +46,6 @@ class Inline
/**
* Get all allowed attributes for a DOMNode
* as clean array
*
* @param DOMNode $node
* @param array $marks
* @return array
*/
public static function parseAttrs(DOMNode $node, array $marks = []): array
{
@ -83,10 +67,6 @@ class Inline
/**
* Parses all children and creates clean HTML
* for each of them.
*
* @param \DOMNodeList $children
* @param array $marks
* @return string
*/
public static function parseChildren(DOMNodeList $children, array $marks): string
{
@ -100,11 +80,8 @@ class Inline
/**
* Go through all child elements and create
* clean inner HTML for them
*
* @param DOMNode $node
* @return string|null
*/
public static function parseInnerHtml(DOMNode $node, array $marks = []): ?string
public static function parseInnerHtml(DOMNode $node, array $marks = []): string|null
{
$html = static::parseChildren($node->childNodes, $marks);
@ -123,19 +100,15 @@ class Inline
/**
* Converts the given node to clean HTML
*
* @param \DOMNode $node
* @param array $marks
* @return string|null
*/
public static function parseNode(DOMNode $node, array $marks = []): ?string
public static function parseNode(DOMNode $node, array $marks = []): string|null
{
if (is_a($node, 'DOMText') === true) {
if ($node instanceof DOMText) {
return Html::encode($node->textContent);
}
// ignore comments
if (is_a($node, 'DOMComment') === true) {
if ($node instanceof DOMComment) {
return null;
}
@ -165,8 +138,6 @@ class Inline
/**
* Returns the HTML contents of the element
*
* @return string
*/
public function innerHtml(): string
{

View file

@ -2,7 +2,10 @@
namespace Kirby\Parsley;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMText;
use Kirby\Parsley\Schema\Plain;
use Kirby\Toolkit\Dom;
@ -20,56 +23,18 @@ use Kirby\Toolkit\Dom;
*/
class Parsley
{
/**
* @var array
*/
protected $blocks = [];
protected array $blocks = [];
protected DOMDocument $doc;
protected Dom $dom;
protected array $inline = [];
protected array $marks = [];
protected array $nodes = [];
protected Schema $schema;
protected array $skip = [];
/**
* @var \DOMDocument
*/
protected $doc;
public static bool $useXmlExtension = true;
/**
* @var \Kirby\Toolkit\Dom
*/
protected $dom;
/**
* @var array
*/
protected $inline = [];
/**
* @var array
*/
protected $marks = [];
/**
* @var array
*/
protected $nodes = [];
/**
* @var \Kirby\Parsley\Schema
*/
protected $schema;
/**
* @var array
*/
protected $skip = [];
/**
* @var bool
*/
public static $useXmlExtension = true;
/**
* @param string $html
* @param \Kirby\Parsley\Schema|null $schema
*/
public function __construct(string $html, Schema $schema = null)
public function __construct(string $html, Schema|null $schema = null)
{
// fail gracefully if the XML extension is not installed
// or should be skipped
@ -110,8 +75,6 @@ class Parsley
/**
* Returns all detected blocks
*
* @return array
*/
public function blocks(): array
{
@ -120,9 +83,6 @@ class Parsley
/**
* Load all node rules from the schema
*
* @param array $nodes
* @return array
*/
public function createNodeRules(array $nodes): array
{
@ -136,9 +96,6 @@ class Parsley
/**
* Checks if the given element contains
* any other block level elements
*
* @param \DOMNode $element
* @return bool
*/
public function containsBlock(DOMNode $element): bool
{
@ -162,10 +119,8 @@ class Parsley
* if the type matches, or will be appended.
*
* The inline cache will be reset afterwards
*
* @return void
*/
public function endInlineBlock()
public function endInlineBlock(): void
{
if (empty($this->inline) === true) {
return;
@ -191,11 +146,8 @@ class Parsley
* Creates a fallback block type for the given
* element. The element can either be a element object
* or a simple HTML/plain text string
*
* @param \Kirby\Parsley\Element|string $element
* @return array|null
*/
public function fallback($element): ?array
public function fallback(Element|string $element): array|null
{
if ($fallback = $this->schema->fallback($element)) {
return $fallback;
@ -206,13 +158,10 @@ class Parsley
/**
* Checks if the given DOMNode is a block element
*
* @param DOMNode $element
* @return bool
*/
public function isBlock(DOMNode $element): bool
{
if (is_a($element, 'DOMElement') === false) {
if ($element instanceof DOMElement === false) {
return false;
}
@ -221,17 +170,14 @@ class Parsley
/**
* Checks if the given DOMNode is an inline element
*
* @param \DOMNode $element
* @return bool
*/
public function isInline(DOMNode $element): bool
{
if (is_a($element, 'DOMText') === true) {
if ($element instanceof DOMText) {
return true;
}
if (is_a($element, 'DOMElement') === true) {
if ($element instanceof DOMElement) {
// all spans will be treated as inline elements
if ($element->tagName === 'span') {
return true;
@ -252,11 +198,7 @@ class Parsley
return false;
}
/**
* @param array $block
* @return void
*/
public function mergeOrAppend(array $block)
public function mergeOrAppend(array $block): void
{
$lastIndex = count($this->blocks) - 1;
$lastItem = $this->blocks[$lastIndex] ?? null;
@ -274,9 +216,6 @@ class Parsley
/**
* Parses the given DOM node and tries to
* convert it to a block or a list of blocks
*
* @param \DOMNode $element
* @return void
*/
public function parseNode(DOMNode $element): bool
{
@ -339,9 +278,6 @@ class Parsley
return true;
}
/**
* @return bool
*/
public function useXmlExtension(): bool
{
if (static::$useXmlExtension !== true) {

View file

@ -18,11 +18,8 @@ class Schema
/**
* Returns the fallback block when no
* other block type can be detected
*
* @param \Kirby\Parsley\Element|string $element
* @return array|null
*/
public function fallback($element): ?array
public function fallback(Element|string $element): array|null
{
return null;
}
@ -30,8 +27,6 @@ class Schema
/**
* Returns a list of allowed inline marks
* and their parsing rules
*
* @return array
*/
public function marks(): array
{
@ -41,8 +36,6 @@ class Schema
/**
* Returns a list of allowed nodes and
* their parsing rules
*
* @return array
*/
public function nodes(): array
{
@ -52,8 +45,6 @@ class Schema
/**
* Returns a list of all elements that should be
* skipped and not be parsed at all
*
* @return array
*/
public function skip(): array
{

View file

@ -2,6 +2,8 @@
namespace Kirby\Parsley\Schema;
use DOMElement;
use DOMText;
use Kirby\Parsley\Element;
use Kirby\Toolkit\Str;
@ -19,10 +21,6 @@ use Kirby\Toolkit\Str;
*/
class Blocks extends Plain
{
/**
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function blockquote(Element $node): array
{
$citation = null;
@ -30,10 +28,14 @@ class Blocks extends Plain
// get all the text for the quote
foreach ($node->children() as $child) {
if (is_a($child, 'DOMText') === true) {
if ($child instanceof DOMText) {
$text[] = trim($child->textContent);
}
if (is_a($child, 'DOMElement') === true && $child->tagName !== 'footer') {
if (
$child instanceof DOMElement &&
$child->tagName !== 'footer'
) {
$text[] = (new Element($child))->innerHTML($this->marks());
}
}
@ -58,13 +60,10 @@ class Blocks extends Plain
/**
* Creates the fallback block type
* if no other block can be found
*
* @param \Kirby\Parsley\Element|string $element
* @return array|null
*/
public function fallback($element): ?array
public function fallback(Element|string $element): array|null
{
if (is_a($element, Element::class) === true) {
if ($element instanceof Element) {
$html = $element->innerHtml();
// wrap the inner HTML in a p tag if it doesn't
@ -94,9 +93,6 @@ class Blocks extends Plain
/**
* Converts a heading element to a heading block
*
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function heading(Element $node): array
{
@ -117,10 +113,6 @@ class Blocks extends Plain
];
}
/**
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function iframe(Element $node): array
{
$caption = null;
@ -163,10 +155,6 @@ class Blocks extends Plain
];
}
/**
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function img(Element $node): array
{
$caption = null;
@ -197,9 +185,6 @@ class Blocks extends Plain
/**
* Converts a list element to HTML
*
* @param \Kirby\Parsley\Element $node
* @return string
*/
public function list(Element $node): string
{
@ -209,9 +194,9 @@ class Blocks extends Plain
$innerHtml = '';
foreach ($li->children() as $child) {
if (is_a($child, 'DOMText') === true) {
if ($child instanceof DOMText) {
$innerHtml .= $child->textContent;
} elseif (is_a($child, 'DOMElement') === true) {
} elseif ($child instanceof DOMElement) {
$child = new Element($child);
if (in_array($child->tagName(), ['ul', 'ol']) === true) {
@ -231,8 +216,6 @@ class Blocks extends Plain
/**
* Returns a list of allowed inline marks
* and their parsing rules
*
* @return array
*/
public function marks(): array
{
@ -291,114 +274,79 @@ class Blocks extends Plain
* their parsing rules
*
* @codeCoverageIgnore
* @return array
*/
public function nodes(): array
{
return [
[
'tag' => 'blockquote',
'parse' => function (Element $node) {
return $this->blockquote($node);
}
'parse' => fn (Element $node) => $this->blockquote($node)
],
[
'tag' => 'h1',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'h2',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'h3',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'h4',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'h5',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'h6',
'parse' => function (Element $node) {
return $this->heading($node);
}
'parse' => fn (Element $node) => $this->heading($node)
],
[
'tag' => 'hr',
'parse' => function (Element $node) {
return [
'type' => 'line'
];
}
'parse' => fn (Element $node) => ['type' => 'line']
],
[
'tag' => 'iframe',
'parse' => function (Element $node) {
return $this->iframe($node);
}
'parse' => fn (Element $node) => $this->iframe($node)
],
[
'tag' => 'img',
'parse' => function (Element $node) {
return $this->img($node);
}
'parse' => fn (Element $node) => $this->img($node)
],
[
'tag' => 'ol',
'parse' => function (Element $node) {
return [
'content' => [
'text' => $this->list($node)
],
'type' => 'list',
];
}
'parse' => fn (Element $node) => [
'content' => [
'text' => $this->list($node)
],
'type' => 'list',
]
],
[
'tag' => 'pre',
'parse' => function (Element $node) {
return $this->pre($node);
}
'parse' => fn (Element $node) => $this->pre($node)
],
[
'tag' => 'table',
'parse' => function (Element $node) {
return $this->table($node);
}
'parse' => fn (Element $node) => $this->table($node)
],
[
'tag' => 'ul',
'parse' => function (Element $node) {
return [
'content' => [
'text' => $this->list($node)
],
'type' => 'list',
];
}
'parse' => fn (Element $node) => [
'content' => [
'text' => $this->list($node)
],
'type' => 'list',
]
],
];
}
/**
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function pre(Element $node): array
{
$language = 'text';
@ -421,10 +369,6 @@ class Blocks extends Plain
];
}
/**
* @param \Kirby\Parsley\Element $node
* @return array
*/
public function table(Element $node): array
{
return [

View file

@ -23,13 +23,10 @@ class Plain extends Schema
/**
* Creates the fallback block type
* if no other block can be found
*
* @param \Kirby\Parsley\Element|string $element
* @return array|null
*/
public function fallback($element): ?array
public function fallback(Element|string $element): array|null
{
if (is_a($element, Element::class) === true) {
if ($element instanceof Element) {
$text = $element->innerText();
} elseif (is_string($element) === true) {
$text = trim($element);
@ -52,8 +49,6 @@ class Plain extends Schema
/**
* Returns a list of all elements that
* should be skipped during parsing
*
* @return array
*/
public function skip(): array
{