This commit is contained in:
Paolo A
2024-08-13 13:44:16 +00:00
parent 1bbb23088d
commit e796d76612
4001 changed files with 30101 additions and 40075 deletions

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
@@ -14,37 +16,38 @@
namespace League\CommonMark\Node;
use Dflydev\DotAccessData\Data;
use League\CommonMark\Exception\InvalidArgumentException;
abstract class Node
{
/**
* @var int
*/
protected $depth = 0;
/** @psalm-readonly */
public Data $data;
/**
* @var Node|null
*/
protected $parent;
/** @psalm-readonly-allow-private-mutation */
protected int $depth = 0;
/**
* @var Node|null
*/
protected $previous;
/** @psalm-readonly-allow-private-mutation */
protected ?Node $parent = null;
/**
* @var Node|null
*/
protected $next;
/** @psalm-readonly-allow-private-mutation */
protected ?Node $previous = null;
/**
* @var Node|null
*/
protected $firstChild;
/** @psalm-readonly-allow-private-mutation */
protected ?Node $next = null;
/**
* @var Node|null
*/
protected $lastChild;
/** @psalm-readonly-allow-private-mutation */
protected ?Node $firstChild = null;
/** @psalm-readonly-allow-private-mutation */
protected ?Node $lastChild = null;
public function __construct()
{
$this->data = new Data([
'attributes' => [],
]);
}
public function previous(): ?Node
{
@@ -61,25 +64,16 @@ abstract class Node
return $this->parent;
}
/**
* @param Node|null $node
*
* @return void
*/
protected function setParent(Node $node = null)
protected function setParent(?Node $node = null): void
{
$this->parent = $node;
$this->depth = ($node === null) ? 0 : $node->depth + 1;
$this->depth = $node === null ? 0 : $node->depth + 1;
}
/**
* Inserts the $sibling node after $this
*
* @param Node $sibling
*
* @return void
*/
public function insertAfter(Node $sibling)
public function insertAfter(Node $sibling): void
{
$sibling->detach();
$sibling->next = $this->next;
@@ -89,22 +83,18 @@ abstract class Node
}
$sibling->previous = $this;
$this->next = $sibling;
$this->next = $sibling;
$sibling->setParent($this->parent);
if (!$sibling->next && $sibling->parent) {
if (! $sibling->next && $sibling->parent) {
$sibling->parent->lastChild = $sibling;
}
}
/**
* Inserts the $sibling node before $this
*
* @param Node $sibling
*
* @return void
*/
public function insertBefore(Node $sibling)
public function insertBefore(Node $sibling): void
{
$sibling->detach();
$sibling->previous = $this->previous;
@@ -113,31 +103,23 @@ abstract class Node
$sibling->previous->next = $sibling;
}
$sibling->next = $this;
$sibling->next = $this;
$this->previous = $sibling;
$sibling->setParent($this->parent);
if (!$sibling->previous && $sibling->parent) {
if (! $sibling->previous && $sibling->parent) {
$sibling->parent->firstChild = $sibling;
}
}
/**
* @param Node $replacement
*
* @return void
*/
public function replaceWith(Node $replacement)
public function replaceWith(Node $replacement): void
{
$replacement->detach();
$this->insertAfter($replacement);
$this->detach();
}
/**
* @return void
*/
public function detach()
public function detach(): void
{
if ($this->previous) {
$this->previous->next = $this->next;
@@ -151,13 +133,16 @@ abstract class Node
$this->parent->lastChild = $this->previous;
}
$this->parent = null;
$this->next = null;
$this->parent = null;
$this->next = null;
$this->previous = null;
$this->depth = 0;
$this->depth = 0;
}
abstract public function isContainer(): bool;
public function hasChildren(): bool
{
return $this->firstChild !== null;
}
public function firstChild(): ?Node
{
@@ -175,19 +160,14 @@ abstract class Node
public function children(): iterable
{
$children = [];
for ($current = $this->firstChild; null !== $current; $current = $current->next) {
for ($current = $this->firstChild; $current !== null; $current = $current->next) {
$children[] = $current;
}
return $children;
}
/**
* @param Node $child
*
* @return void
*/
public function appendChild(Node $child)
public function appendChild(Node $child): void
{
if ($this->lastChild) {
$this->lastChild->insertAfter($child);
@@ -200,12 +180,8 @@ abstract class Node
/**
* Adds $child as the very first child of $this
*
* @param Node $child
*
* @return void
*/
public function prependChild(Node $child)
public function prependChild(Node $child): void
{
if ($this->firstChild) {
$this->firstChild->insertBefore($child);
@@ -218,14 +194,13 @@ abstract class Node
/**
* Detaches all child nodes of given node
*
* @return void
*/
public function detachChildren()
public function detachChildren(): void
{
foreach ($this->children() as $children) {
$children->setParent(null);
}
$this->firstChild = $this->lastChild = null;
}
@@ -233,17 +208,13 @@ abstract class Node
* Replace all children of given node with collection of another
*
* @param iterable<Node> $children
*
* @return $this
*/
public function replaceChildren(iterable $children)
public function replaceChildren(iterable $children): void
{
$this->detachChildren();
foreach ($children as $item) {
$this->appendChild($item);
}
return $this;
}
public function getDepth(): int
@@ -256,6 +227,11 @@ abstract class Node
return new NodeWalker($this);
}
public function iterator(int $flags = 0): NodeIterator
{
return new NodeIterator($this, $flags);
}
/**
* Clone the current node and its children
*
@@ -264,9 +240,9 @@ abstract class Node
public function __clone()
{
// Cloned nodes are detached from their parents, siblings, and children
$this->parent = null;
$this->parent = null;
$this->previous = null;
$this->next = null;
$this->next = null;
// But save a copy of the children since we'll need that in a moment
$children = $this->children();
$this->detachChildren();
@@ -276,4 +252,11 @@ abstract class Node
$this->appendChild(clone $child);
}
}
public static function assertInstanceOf(Node $node): void
{
if (! $node instanceof static) {
throw new InvalidArgumentException(\sprintf('Incompatible node type: expected %s, got %s', static::class, \get_class($node)));
}
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
@@ -14,27 +16,23 @@
namespace League\CommonMark\Node;
use League\CommonMark\Node\Block\AbstractBlock;
final class NodeWalker
{
/**
* @var Node
*/
private $root;
/** @psalm-readonly */
private Node $root;
/**
* @var Node|null
*/
private $current;
/** @psalm-readonly-allow-private-mutation */
private ?Node $current = null;
/**
* @var bool
*/
private $entering;
/** @psalm-readonly-allow-private-mutation */
private bool $entering;
public function __construct(Node $root)
{
$this->root = $root;
$this->current = $this->root;
$this->root = $root;
$this->current = $this->root;
$this->entering = true;
}
@@ -42,31 +40,29 @@ final class NodeWalker
* Returns an event which contains node and entering flag
* (entering is true when we enter a Node from a parent or sibling,
* and false when we reenter it from child)
*
* @return NodeWalkerEvent|null
*/
public function next(): ?NodeWalkerEvent
{
$current = $this->current;
$current = $this->current;
$entering = $this->entering;
if (null === $current) {
if ($current === null) {
return null;
}
if ($entering && $current->isContainer()) {
if ($entering && ($current instanceof AbstractBlock || $current->hasChildren())) {
if ($current->firstChild()) {
$this->current = $current->firstChild();
$this->current = $current->firstChild();
$this->entering = true;
} else {
$this->entering = false;
}
} elseif ($current === $this->root) {
$this->current = null;
} elseif (null === $current->next()) {
$this->current = $current->parent();
} elseif ($current->next() === null) {
$this->current = $current->parent();
$this->entering = false;
} else {
$this->current = $current->next();
$this->current = $current->next();
$this->entering = true;
}
@@ -75,15 +71,10 @@ final class NodeWalker
/**
* Resets the iterator to resume at the specified node
*
* @param Node $node
* @param bool $entering
*
* @return void
*/
public function resumeAt(Node $node, bool $entering = true)
public function resumeAt(Node $node, bool $entering = true): void
{
$this->current = $node;
$this->current = $node;
$this->entering = $entering;
}
}

View File

@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
@@ -16,23 +18,15 @@ namespace League\CommonMark\Node;
final class NodeWalkerEvent
{
/**
* @var Node
*/
private $node;
/** @psalm-readonly */
private Node $node;
/**
* @var bool
*/
private $isEntering;
/** @psalm-readonly */
private bool $isEntering;
/**
* @param Node $node
* @param bool $isEntering
*/
public function __construct(Node $node, $isEntering = true)
public function __construct(Node $node, bool $isEntering = true)
{
$this->node = $node;
$this->node = $node;
$this->isEntering = $isEntering;
}