Skip to content

Commit 65da7d7

Browse files
committed
Merge branch 'rikvdh-master' into dev/3.0.0
2 parents 9ecb7e4 + 6aff01b commit 65da7d7

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed

src/PHPHtmlParser/Content.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,7 @@ public function getPosition(): int
6767
*/
6868
public function char(?int $char = null): string
6969
{
70-
$pos = $this->pos;
71-
if (!\is_null($char)) {
72-
$pos = $char;
73-
}
74-
75-
if (!isset($this->content[$pos])) {
76-
return '';
77-
}
78-
79-
return $this->content[$pos];
70+
return $this->content[$char ?? $this->pos] ?? '';
8071
}
8172

8273
/**

src/PHPHtmlParser/Dom.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ public function loadFromFile(string $file, array $options = []): Dom
193193
*/
194194
public function loadFromUrl(string $url, array $options = [], ?ClientInterface $client = null, ?RequestInterface $request = null): Dom
195195
{
196-
if (\is_null($client)) {
196+
if ($client === null) {
197197
$client = new Client();
198198
}
199-
if (\is_null($request)) {
199+
if ($request === null) {
200200
$request = new Request('GET', $url);
201201
}
202202

@@ -218,7 +218,7 @@ public function loadStr(string $str, array $option = []): Dom
218218
{
219219
$this->options = new Options();
220220
$this->options->setOptions($this->globalOptions)
221-
->setOptions($option);
221+
->setOptions($option);
222222

223223
$this->rawSize = \strlen($str);
224224
$this->raw = $str;
@@ -614,7 +614,7 @@ protected function parse(): void
614614
$this->root = new HtmlNode('root');
615615
$this->root->setHtmlSpecialCharsDecode($this->options->htmlSpecialCharsDecode);
616616
$activeNode = $this->root;
617-
while (!\is_null($activeNode)) {
617+
while ($activeNode!== null) {
618618
if ($activeNode && $activeNode->tag->name() === 'script'
619619
&& $this->options->get('cleanupInput') != true
620620
) {
@@ -636,7 +636,7 @@ protected function parse(): void
636636
$originalNode = $activeNode;
637637
while ($activeNode->getTag()->name() != $info['tag']) {
638638
$activeNode = $activeNode->getParent();
639-
if (\is_null($activeNode)) {
639+
if ($activeNode === null) {
640640
// we could not find opening tag
641641
$activeNode = $originalNode;
642642
$foundOpeningTag = false;
@@ -693,7 +693,7 @@ protected function parseTag(): array
693693
if ($this->content->fastForward(1)->char() == '/') {
694694
// end tag
695695
$tag = $this->content->fastForward(1)
696-
->copyByToken('slash', true);
696+
->copyByToken('slash', true);
697697
// move to end of tag
698698
$this->content->copyUntil('>');
699699
$this->content->fastForward(1);
@@ -721,8 +721,10 @@ protected function parseTag(): array
721721
$node->setHtmlSpecialCharsDecode($this->options->htmlSpecialCharsDecode);
722722

723723
// attributes
724-
while ($this->content->char() != '>' &&
725-
$this->content->char() != '/') {
724+
while (
725+
$this->content->char() != '>' &&
726+
$this->content->char() != '/'
727+
) {
726728
$space = $this->content->skipByToken('blank', true);
727729
if (empty($space)) {
728730
$this->content->fastForward(1);
@@ -742,7 +744,7 @@ protected function parseTag(): array
742744
$this->content->skipByToken('blank');
743745
if ($this->content->char() == '=') {
744746
$this->content->fastForward(1)
745-
->skipByToken('blank');
747+
->skipByToken('blank');
746748
switch ($this->content->char()) {
747749
case '"':
748750
$this->content->fastForward(1);
@@ -751,6 +753,7 @@ protected function parseTag(): array
751753
$moreString = $this->content->copyUntilUnless('"', '=>');
752754
$string .= $moreString;
753755
} while (!empty($moreString));
756+
$attr['value'] = $string;
754757
$this->content->fastForward(1);
755758
$node->getTag()->setAttribute($name, $string);
756759
break;
@@ -761,6 +764,7 @@ protected function parseTag(): array
761764
$moreString = $this->content->copyUntilUnless("'", '=>');
762765
$string .= $moreString;
763766
} while (!empty($moreString));
767+
$attr['value'] = $string;
764768
$this->content->fastForward(1);
765769
$node->getTag()->setAttribute($name, $string, false);
766770
break;
@@ -825,7 +829,7 @@ protected function detectCharset(): bool
825829
$encode->to($this->defaultCharset);
826830

827831
$enforceEncoding = $this->options->enforceEncoding;
828-
if (!\is_null($enforceEncoding)) {
832+
if ($enforceEncoding !== null) {
829833
// they want to enforce the given encoding
830834
$encode->from($enforceEncoding);
831835
$encode->to($enforceEncoding);

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public function __destruct()
105105
public function __get(string $key)
106106
{
107107
// check attribute first
108-
if (!\is_null($this->getAttribute($key))) {
108+
if ($this->getAttribute($key) !== null) {
109109
return $this->getAttribute($key);
110110
}
111111
switch (\strtolower($key)) {
@@ -177,7 +177,7 @@ public function getParent()
177177
public function setParent(InnerNode $parent): AbstractNode
178178
{
179179
// remove from old parent
180-
if (!\is_null($this->parent)) {
180+
if ($this->parent !== null) {
181181
if ($this->parent->id() == $parent->id()) {
182182
// already the parent
183183
return $this;
@@ -202,7 +202,7 @@ public function setParent(InnerNode $parent): AbstractNode
202202
*/
203203
public function delete()
204204
{
205-
if (!\is_null($this->parent)) {
205+
if ($this->parent !== null) {
206206
$this->parent->removeChild($this->id);
207207
}
208208
$this->parent->clear();
@@ -226,7 +226,7 @@ public function propagateEncoding(Encode $encode)
226226
*/
227227
public function isAncestor(int $id): bool
228228
{
229-
if (!\is_null($this->getAncestor($id))) {
229+
if ($this->getAncestor($id) !== null) {
230230
return true;
231231
}
232232

@@ -240,11 +240,10 @@ public function isAncestor(int $id): bool
240240
*/
241241
public function getAncestor(int $id)
242242
{
243-
if (!\is_null($this->parent)) {
243+
if ($this->parent !== null) {
244244
if ($this->parent->id() == $id) {
245245
return $this->parent;
246246
}
247-
248247
return $this->parent->getAncestor($id);
249248
}
250249
}
@@ -280,7 +279,7 @@ public function hasNextSibling(): bool
280279
*/
281280
public function nextSibling(): AbstractNode
282281
{
283-
if (\is_null($this->parent)) {
282+
if ($this->parent === null) {
284283
throw new ParentNotFoundException('Parent is not set for this node.');
285284
}
286285

@@ -295,7 +294,7 @@ public function nextSibling(): AbstractNode
295294
*/
296295
public function previousSibling(): AbstractNode
297296
{
298-
if (\is_null($this->parent)) {
297+
if ($this->parent === null) {
299298
throw new ParentNotFoundException('Parent is not set for this node.');
300299
}
301300

@@ -401,13 +400,13 @@ public function ancestorByTag(string $tag): AbstractNode
401400
// Start by including ourselves in the comparison.
402401
$node = $this;
403402

404-
while (!\is_null($node)) {
403+
do {
405404
if ($node->tag->name() == $tag) {
406405
return $node;
407406
}
408407

409408
$node = $node->getParent();
410-
}
409+
} while ($node !== null);
411410

412411
throw new ParentNotFoundException('Could not find an ancestor with "' . $tag . '" tag');
413412
}
@@ -419,13 +418,13 @@ public function ancestorByTag(string $tag): AbstractNode
419418
*
420419
* @return mixed|Collection|null
421420
*/
422-
public function find(string $selector, int $nth = null, bool $depthFirst = false)
421+
public function find(string $selector, ?int $nth = null, bool $depthFirst = false)
423422
{
424423
$selector = new Selector($selector, new SelectorParser());
425424
$selector->setDepthFirstFind($depthFirst);
426425
$nodes = $selector->find($this);
427426

428-
if (!\is_null($nth)) {
427+
if ($nth !== null) {
429428
// return nth-element or array
430429
if (isset($nodes[$nth])) {
431430
return $nodes[$nth];

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function innerHtml(): string
7777
return '';
7878
}
7979

80-
if (!\is_null($this->innerHtml)) {
80+
if ($this->innerHtml !== null) {
8181
// we already know the result.
8282
return $this->innerHtml;
8383
}
@@ -86,7 +86,7 @@ public function innerHtml(): string
8686
$string = '';
8787

8888
// continue to loop until we are out of children
89-
while (!\is_null($child)) {
89+
while ($child !== null) {
9090
if ($child instanceof TextNode) {
9191
$string .= $child->text();
9292
} elseif ($child instanceof HtmlNode) {
@@ -124,7 +124,7 @@ public function outerHtml(): string
124124
return $this->innerHtml();
125125
}
126126

127-
if (!\is_null($this->outerHtml)) {
127+
if ($this->outerHtml !== null) {
128128
// we already know the results.
129129
return $this->outerHtml;
130130
}
@@ -154,11 +154,11 @@ public function outerHtml(): string
154154
public function text(bool $lookInChildren = false): string
155155
{
156156
if ($lookInChildren) {
157-
if (!\is_null($this->textWithChildren)) {
157+
if ($this->textWithChildren !== null) {
158158
// we already know the results.
159159
return $this->textWithChildren;
160160
}
161-
} elseif (!\is_null($this->text)) {
161+
} elseif ($this->text !== null) {
162162
// we already know the results.
163163
return $this->text;
164164
}
@@ -170,7 +170,8 @@ public function text(bool $lookInChildren = false): string
170170
$node = $child['node'];
171171
if ($node instanceof TextNode) {
172172
$text .= $child['node']->text;
173-
} elseif ($lookInChildren &&
173+
} elseif (
174+
$lookInChildren &&
174175
$node instanceof HtmlNode
175176
) {
176177
$text .= $node->text($lookInChildren);
@@ -198,7 +199,7 @@ protected function clear(): void
198199
$this->text = null;
199200
$this->textWithChildren = null;
200201

201-
if (!\is_null($this->parent)) {
202+
if ($this->parent !== null) {
202203
$this->parent->clear();
203204
}
204205
}

tests/Node/ParentTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ public function testGetGreatAncestor()
302302
$parent->addChild($child);
303303
$child->addChild($child2);
304304
$ancestor = $child2->getAncestor($parent->id());
305+
$this->assertNotNull($ancestor);
305306
$this->assertEquals($parent->id(), $ancestor->id());
306307
}
307308

0 commit comments

Comments
 (0)