Completed
Push — master ( ca9126...817989 )
by Colin
05:00 queued 15s
created

Element::isText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace League\HTMLToMarkdown;
4
5
class Element implements ElementInterface
6
{
7
    /**
8
     * @var \DOMNode
9
     */
10
    protected $node;
11
12
    /**
13
     * @var ElementInterface|null
14
     */
15
    private $nextCached;
16
17 75
    public function __construct(\DOMNode $node)
18
    {
19 75
        $this->node = $node;
20 75
    }
21
22
    /**
23
     * @return bool
24
     */
25 9
    public function isBlock()
26
    {
27 9
        switch ($this->getTagName()) {
28 9
            case 'blockquote':
29 9
            case 'body':
30 9
            case 'code':
31 9
            case 'div':
32 9
            case 'h1':
33 9
            case 'h2':
34 9
            case 'h3':
35 9
            case 'h4':
36 9
            case 'h5':
37 9
            case 'h6':
38 9
            case 'hr':
39 9
            case 'html':
40 9
            case 'li':
41 9
            case 'p':
42 9
            case 'ol':
43 9
            case 'ul':
44 3
                return true;
45 6
            default:
46 6
                return false;
47 6
        }
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isText()
54
    {
55
        return $this->getTagName() === '#text';
56
    }
57
58
    /**
59
     * @return bool
60
     */
61 6
    public function isWhitespace()
62
    {
63 6
        return $this->getTagName() === '#text' && trim($this->getValue()) === '';
64
    }
65
66
    /**
67
     * @return string
68
     */
69 75
    public function getTagName()
70
    {
71 75
        return $this->node->nodeName;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 63
    public function getValue()
78
    {
79 63
        return $this->node->nodeValue;
80
    }
81
82
    /**
83
     * @return ElementInterface|null
84
     */
85 6
    public function getParent()
86
    {
87 6
        return new static($this->node->parentNode) ?: null;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93 75
    public function hasChildren()
94
    {
95 75
        return $this->node->hasChildNodes();
96
    }
97
98
    /**
99
     * @return ElementInterface[]
100
     */
101 75
    public function getChildren()
102
    {
103 75
        $ret = array();
104
        /** @var \DOMNode $node */
105 75
        foreach ($this->node->childNodes as $node) {
106 75
            $ret[] = new static($node);
107 75
        }
108
109 75
        return $ret;
110
    }
111
112
    /**
113
     * @return ElementInterface|null
114
     */
115 9
    public function getNext()
116
    {
117 9
        if ($this->nextCached === null) {
118 9
            $nextNode = $this->getNextNode($this->node);
119 9
            if ($nextNode !== null) {
120 9
                $this->nextCached = new static($nextNode);
121 9
            }
122 9
        }
123
124 9
        return $this->nextCached;
125
    }
126
127
    /**
128
     * @param \DomNode $node
129
     *
130
     * @return \DomNode|null
131
     */
132 9
    private function getNextNode($node, $checkChildren = true)
133
    {
134 9
        if ($checkChildren && $node->firstChild) {
135
            return $node->firstChild;
136 9
        } elseif ($node->nextSibling) {
137 9
            return $node->nextSibling;
138
        } elseif ($node->parentNode) {
139
            return $this->getNextNode($node->parentNode, false);
140
        }
141
    }
142
143
    /**
144
     * @param string[]|string $tagNames
145
     *
146
     * @return bool
147
     */
148 75
    public function isDescendantOf($tagNames)
149
    {
150 75
        if (!is_array($tagNames)) {
151 3
            $tagNames = array($tagNames);
152 3
        }
153
154 75
        for ($p = $this->node->parentNode; $p !== false; $p = $p->parentNode) {
155 75
            if (is_null($p)) {
156 75
                return false;
157
            }
158
159 75
            if (in_array($p->nodeName, $tagNames)) {
160 18
                return true;
161
            }
162 75
        }
163
164
        return false;
165
    }
166
167
    /**
168
     * @param string $markdown
169
     */
170 75
    public function setFinalMarkdown($markdown)
171
    {
172 75
        $markdown_node = $this->node->ownerDocument->createTextNode($markdown);
173 75
        $this->node->parentNode->replaceChild($markdown_node, $this->node);
174 75
    }
175
176
    /**
177
     * @return string
178
     */
179 66
    public function getChildrenAsString()
180
    {
181 66
        return $this->node->C14N();
182
    }
183
184
    /**
185
     * @return int
186
     */
187 6
    public function getSiblingPosition()
188
    {
189 6
        $position = 0;
190
191
        // Loop through all nodes and find the given $node
192 6
        foreach ($this->getParent()->getChildren() as $current_node) {
193 6
            if (!$current_node->isWhitespace()) {
194 6
                $position++;
195 6
            }
196
197
            // TODO: Need a less-buggy way of comparing these
198
            // Perhaps we can somehow ensure that we always have the exact same object and use === instead?
199 6
            if ($this->equals($current_node)) {
200 6
                break;
201
            }
202 6
        }
203
204 6
        return $position;
205
    }
206
207
    /**
208
     * @return int
209
     */
210 6
    public function getListItemLevel()
211
    {
212 6
        $level = 0;
213 6
        $parent = $this->getParent();
214
215 6
        while ($parent !== null && $parent->node->parentNode) {
216 6
            if ($parent->getTagName() === 'li') {
217 3
                $level++;
218 3
            }
219 6
            $parent = $parent->getParent();
220 6
        }
221
222 6
        return $level;
223
    }
224
225
    /**
226
     * @param string $name
227
     *
228
     * @return string
229
     */
230 18
    public function getAttribute($name)
231
    {
232 18
        if ($this->node instanceof \DOMElement) {
233 18
            return $this->node->getAttribute($name);
234
        }
235
236
        return '';
237
    }
238
239
    /**
240
     * @param ElementInterface $element
241
     *
242
     * @return bool
243
     */
244 6
    public function equals(ElementInterface $element)
245
    {
246 6
        if ($element instanceof self) {
247 6
            return $element->node === $this->node;
248
        }
249
250
        return $element === $this;
251
    }
252
}
253