aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/league/html-to-markdown/src/Element.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/league/html-to-markdown/src/Element.php')
-rw-r--r--vendor/league/html-to-markdown/src/Element.php169
1 files changed, 74 insertions, 95 deletions
diff --git a/vendor/league/html-to-markdown/src/Element.php b/vendor/league/html-to-markdown/src/Element.php
index 80ae7a911..5407f1ffb 100644
--- a/vendor/league/html-to-markdown/src/Element.php
+++ b/vendor/league/html-to-markdown/src/Element.php
@@ -1,28 +1,28 @@
<?php
+declare(strict_types=1);
+
namespace League\HTMLToMarkdown;
class Element implements ElementInterface
{
- /**
- * @var \DOMNode
- */
+ /** @var \DOMNode */
protected $node;
- /**
- * @var ElementInterface|null
- */
+ /** @var ElementInterface|null */
private $nextCached;
+ /** @var \DOMNode|null */
+ private $previousSiblingCached;
+
public function __construct(\DOMNode $node)
{
$this->node = $node;
+
+ $this->previousSiblingCached = $this->node->previousSibling;
}
- /**
- * @return bool
- */
- public function isBlock()
+ public function isBlock(): bool
{
switch ($this->getTagName()) {
case 'blockquote':
@@ -46,50 +46,47 @@ class Element implements ElementInterface
}
}
- /**
- * @return bool
- */
- public function isText()
+ public function isText(): bool
{
return $this->getTagName() === '#text';
}
- /**
- * @return bool
- */
- public function isWhitespace()
+ public function isWhitespace(): bool
{
- return $this->getTagName() === '#text' && trim($this->getValue()) === '';
+ return $this->getTagName() === '#text' && \trim($this->getValue()) === '';
}
- /**
- * @return string
- */
- public function getTagName()
+ public function getTagName(): string
{
return $this->node->nodeName;
}
- /**
- * @return string
- */
- public function getValue()
+ public function getValue(): string
{
return $this->node->nodeValue;
}
- /**
- * @return ElementInterface|null
- */
- public function getParent()
+ public function hasParent(): bool
{
- return new static($this->node->parentNode) ?: null;
+ return $this->node->parentNode !== null;
}
- /**
- * @return bool
- */
- public function hasChildren()
+ public function getParent(): ?ElementInterface
+ {
+ return $this->node->parentNode ? new self($this->node->parentNode) : null;
+ }
+
+ public function getNextSibling(): ?ElementInterface
+ {
+ return $this->node->nextSibling !== null ? new self($this->node->nextSibling) : null;
+ }
+
+ public function getPreviousSibling(): ?ElementInterface
+ {
+ return $this->previousSiblingCached !== null ? new self($this->previousSiblingCached) : null;
+ }
+
+ public function hasChildren(): bool
{
return $this->node->hasChildNodes();
}
@@ -97,39 +94,29 @@ class Element implements ElementInterface
/**
* @return ElementInterface[]
*/
- public function getChildren()
+ public function getChildren(): array
{
- $ret = array();
- /** @var \DOMNode $node */
+ $ret = [];
foreach ($this->node->childNodes as $node) {
- $ret[] = new static($node);
+ $ret[] = new self($node);
}
return $ret;
}
- /**
- * @return ElementInterface|null
- */
- public function getNext()
+ public function getNext(): ?ElementInterface
{
if ($this->nextCached === null) {
$nextNode = $this->getNextNode($this->node);
if ($nextNode !== null) {
- $this->nextCached = new static($nextNode);
+ $this->nextCached = new self($nextNode);
}
}
return $this->nextCached;
}
- /**
- * @param \DomNode $node
- * @param bool $checkChildren
- *
- * @return \DomNode|null
- */
- private function getNextNode($node, $checkChildren = true)
+ private function getNextNode(\DomNode $node, bool $checkChildren = true): ?\DomNode
{
if ($checkChildren && $node->firstChild) {
return $node->firstChild;
@@ -142,25 +129,25 @@ class Element implements ElementInterface
if ($node->parentNode) {
return $this->getNextNode($node->parentNode, false);
}
+
+ return null;
}
/**
* @param string[]|string $tagNames
- *
- * @return bool
*/
- public function isDescendantOf($tagNames)
+ public function isDescendantOf($tagNames): bool
{
- if (!is_array($tagNames)) {
- $tagNames = array($tagNames);
+ if (! \is_array($tagNames)) {
+ $tagNames = [$tagNames];
}
for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) {
- if (is_null($p)) {
+ if ($p === null) {
return false;
}
- if (in_array($p->nodeName, $tagNames)) {
+ if (\in_array($p->nodeName, $tagNames, true)) {
return true;
}
}
@@ -168,39 +155,43 @@ class Element implements ElementInterface
return false;
}
- /**
- * @param string $markdown
- */
- public function setFinalMarkdown($markdown)
+ public function setFinalMarkdown(string $markdown): void
{
- $markdown_node = $this->node->ownerDocument->createTextNode($markdown);
- $this->node->parentNode->replaceChild($markdown_node, $this->node);
+ if ($this->node->ownerDocument === null) {
+ throw new \RuntimeException('Unowned node');
+ }
+
+ if ($this->node->parentNode === null) {
+ throw new \RuntimeException('Cannot setFinalMarkdown() on a node without a parent');
+ }
+
+ $markdownNode = $this->node->ownerDocument->createTextNode($markdown);
+ $this->node->parentNode->replaceChild($markdownNode, $this->node);
}
- /**
- * @return string
- */
- public function getChildrenAsString()
+ public function getChildrenAsString(): string
{
return $this->node->C14N();
}
- /**
- * @return int
- */
- public function getSiblingPosition()
+ public function getSiblingPosition(): int
{
$position = 0;
+ $parent = $this->getParent();
+ if ($parent === null) {
+ return $position;
+ }
+
// Loop through all nodes and find the given $node
- foreach ($this->getParent()->getChildren() as $current_node) {
- if (!$current_node->isWhitespace()) {
+ foreach ($parent->getChildren() as $currentNode) {
+ if (! $currentNode->isWhitespace()) {
$position++;
}
// TODO: Need a less-buggy way of comparing these
// Perhaps we can somehow ensure that we always have the exact same object and use === instead?
- if ($this->equals($current_node)) {
+ if ($this->equals($currentNode)) {
break;
}
}
@@ -208,30 +199,23 @@ class Element implements ElementInterface
return $position;
}
- /**
- * @return int
- */
- public function getListItemLevel()
+ public function getListItemLevel(): int
{
- $level = 0;
+ $level = 0;
$parent = $this->getParent();
- while ($parent !== null && $parent->node->parentNode) {
+ while ($parent !== null && $parent->hasParent()) {
if ($parent->getTagName() === 'li') {
$level++;
}
+
$parent = $parent->getParent();
}
return $level;
}
- /**
- * @param string $name
- *
- * @return string
- */
- public function getAttribute($name)
+ public function getAttribute(string $name): string
{
if ($this->node instanceof \DOMElement) {
return $this->node->getAttribute($name);
@@ -240,17 +224,12 @@ class Element implements ElementInterface
return '';
}
- /**
- * @param ElementInterface $element
- *
- * @return bool
- */
- public function equals(ElementInterface $element)
+ public function equals(ElementInterface $element): bool
{
if ($element instanceof self) {
return $element->node === $this->node;
}
- return $element === $this;
+ return false;
}
}