3 namespace League\HTMLToMarkdown;
5 class Element implements ElementInterface
13 * @var ElementInterface|null
17 public function __construct(\DOMNode $node)
25 public function isBlock()
27 switch ($this->getTagName()) {
53 public function isText()
55 return $this->getTagName() === '#text';
61 public function isWhitespace()
63 return $this->getTagName() === '#text' && trim($this->getValue()) === '';
69 public function getTagName()
71 return $this->node->nodeName;
77 public function getValue()
79 return $this->node->nodeValue;
83 * @return ElementInterface|null
85 public function getParent()
87 return new static($this->node->parentNode) ?: null;
93 public function hasChildren()
95 return $this->node->hasChildNodes();
99 * @return ElementInterface[]
101 public function getChildren()
104 /** @var \DOMNode $node */
105 foreach ($this->node->childNodes as $node) {
106 $ret[] = new static($node);
113 * @return ElementInterface|null
115 public function getNext()
117 if ($this->nextCached === null) {
118 $nextNode = $this->getNextNode($this->node);
119 if ($nextNode !== null) {
120 $this->nextCached = new static($nextNode);
124 return $this->nextCached;
128 * @param \DomNode $node
129 * @param bool $checkChildren
131 * @return \DomNode|null
133 private function getNextNode($node, $checkChildren = true)
135 if ($checkChildren && $node->firstChild) {
136 return $node->firstChild;
139 if ($node->nextSibling) {
140 return $node->nextSibling;
143 if ($node->parentNode) {
144 return $this->getNextNode($node->parentNode, false);
149 * @param string[]|string $tagNames
153 public function isDescendantOf($tagNames)
155 if (!is_array($tagNames)) {
156 $tagNames = array($tagNames);
159 for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) {
164 if (in_array($p->nodeName, $tagNames)) {
173 * @param string $markdown
175 public function setFinalMarkdown($markdown)
177 $markdown_node = $this->node->ownerDocument->createTextNode($markdown);
178 $this->node->parentNode->replaceChild($markdown_node, $this->node);
184 public function getChildrenAsString()
186 return $this->node->C14N();
192 public function getSiblingPosition()
196 // Loop through all nodes and find the given $node
197 foreach ($this->getParent()->getChildren() as $current_node) {
198 if (!$current_node->isWhitespace()) {
202 // TODO: Need a less-buggy way of comparing these
203 // Perhaps we can somehow ensure that we always have the exact same object and use === instead?
204 if ($this->equals($current_node)) {
215 public function getListItemLevel()
218 $parent = $this->getParent();
220 while ($parent !== null && $parent->node->parentNode) {
221 if ($parent->getTagName() === 'li') {
224 $parent = $parent->getParent();
231 * @param string $name
235 public function getAttribute($name)
237 if ($this->node instanceof \DOMElement) {
238 return $this->node->getAttribute($name);
245 * @param ElementInterface $element
249 public function equals(ElementInterface $element)
251 if ($element instanceof self) {
252 return $element->node === $this->node;
255 return $element === $this;