Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,279 @@
<?php
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Node;
abstract class Node
{
/**
* @var int
*/
protected $depth = 0;
/**
* @var Node|null
*/
protected $parent;
/**
* @var Node|null
*/
protected $previous;
/**
* @var Node|null
*/
protected $next;
/**
* @var Node|null
*/
protected $firstChild;
/**
* @var Node|null
*/
protected $lastChild;
public function previous(): ?Node
{
return $this->previous;
}
public function next(): ?Node
{
return $this->next;
}
public function parent(): ?Node
{
return $this->parent;
}
/**
* @param Node|null $node
*
* @return void
*/
protected function setParent(Node $node = null)
{
$this->parent = $node;
$this->depth = ($node === null) ? 0 : $node->depth + 1;
}
/**
* Inserts the $sibling node after $this
*
* @param Node $sibling
*
* @return void
*/
public function insertAfter(Node $sibling)
{
$sibling->detach();
$sibling->next = $this->next;
if ($sibling->next) {
$sibling->next->previous = $sibling;
}
$sibling->previous = $this;
$this->next = $sibling;
$sibling->setParent($this->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)
{
$sibling->detach();
$sibling->previous = $this->previous;
if ($sibling->previous) {
$sibling->previous->next = $sibling;
}
$sibling->next = $this;
$this->previous = $sibling;
$sibling->setParent($this->parent);
if (!$sibling->previous && $sibling->parent) {
$sibling->parent->firstChild = $sibling;
}
}
/**
* @param Node $replacement
*
* @return void
*/
public function replaceWith(Node $replacement)
{
$replacement->detach();
$this->insertAfter($replacement);
$this->detach();
}
/**
* @return void
*/
public function detach()
{
if ($this->previous) {
$this->previous->next = $this->next;
} elseif ($this->parent) {
$this->parent->firstChild = $this->next;
}
if ($this->next) {
$this->next->previous = $this->previous;
} elseif ($this->parent) {
$this->parent->lastChild = $this->previous;
}
$this->parent = null;
$this->next = null;
$this->previous = null;
$this->depth = 0;
}
abstract public function isContainer(): bool;
public function firstChild(): ?Node
{
return $this->firstChild;
}
public function lastChild(): ?Node
{
return $this->lastChild;
}
/**
* @return Node[]
*/
public function children(): iterable
{
$children = [];
for ($current = $this->firstChild; null !== $current; $current = $current->next) {
$children[] = $current;
}
return $children;
}
/**
* @param Node $child
*
* @return void
*/
public function appendChild(Node $child)
{
if ($this->lastChild) {
$this->lastChild->insertAfter($child);
} else {
$child->detach();
$child->setParent($this);
$this->lastChild = $this->firstChild = $child;
}
}
/**
* Adds $child as the very first child of $this
*
* @param Node $child
*
* @return void
*/
public function prependChild(Node $child)
{
if ($this->firstChild) {
$this->firstChild->insertBefore($child);
} else {
$child->detach();
$child->setParent($this);
$this->lastChild = $this->firstChild = $child;
}
}
/**
* Detaches all child nodes of given node
*
* @return void
*/
public function detachChildren()
{
foreach ($this->children() as $children) {
$children->setParent(null);
}
$this->firstChild = $this->lastChild = null;
}
/**
* Replace all children of given node with collection of another
*
* @param iterable<Node> $children
*
* @return $this
*/
public function replaceChildren(iterable $children)
{
$this->detachChildren();
foreach ($children as $item) {
$this->appendChild($item);
}
return $this;
}
public function getDepth(): int
{
return $this->depth;
}
public function walker(): NodeWalker
{
return new NodeWalker($this);
}
/**
* Clone the current node and its children
*
* WARNING: This is a recursive function and should not be called on deeply-nested node trees!
*/
public function __clone()
{
// Cloned nodes are detached from their parents, siblings, and children
$this->parent = null;
$this->previous = null;
$this->next = null;
// But save a copy of the children since we'll need that in a moment
$children = $this->children();
$this->detachChildren();
// The original children get cloned and re-added
foreach ($children as $child) {
$this->appendChild(clone $child);
}
}
}

View File

@@ -0,0 +1,89 @@
<?php
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Node;
final class NodeWalker
{
/**
* @var Node
*/
private $root;
/**
* @var Node|null
*/
private $current;
/**
* @var bool
*/
private $entering;
public function __construct(Node $root)
{
$this->root = $root;
$this->current = $this->root;
$this->entering = true;
}
/**
* 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;
$entering = $this->entering;
if (null === $current) {
return null;
}
if ($entering && $current->isContainer()) {
if ($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();
$this->entering = false;
} else {
$this->current = $current->next();
$this->entering = true;
}
return new NodeWalkerEvent($current, $entering);
}
/**
* 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)
{
$this->current = $node;
$this->entering = $entering;
}
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
* - (c) John MacFarlane
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Node;
final class NodeWalkerEvent
{
/**
* @var Node
*/
private $node;
/**
* @var bool
*/
private $isEntering;
/**
* @param Node $node
* @param bool $isEntering
*/
public function __construct(Node $node, $isEntering = true)
{
$this->node = $node;
$this->isEntering = $isEntering;
}
public function getNode(): Node
{
return $this->node;
}
public function isEntering(): bool
{
return $this->isEntering;
}
}