Skip to content

Add tidyNode::getNextSibling() and tidyNode::getPreviousSibling() #15047

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ PHP NEWS
. Fix references in request_parse_body() options array. (nielsdos)
. Add RoundingMode enum. (timwolla, saki)

- Tidy:
. Add tidyNode::getNextSibling() and tidyNode::getPreviousSibling().
(nielsdos)

- XSL:
. Fix trampoline leak in xpath callables. (nielsdos)

Expand Down
3 changes: 3 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,9 @@ PHP 8.4 UPGRADE NOTES
array_any().
RFC: https://wiki.php.net/rfc/array_find

- Tidy:
. Added tidyNode::getNextSibling() and tidyNode::getPreviousSibling().

- XMLReader:
. Added XMLReader::fromStream(), XMLReader::fromUri(), XMLReader::fromString().
RFC: https://wiki.php.net/rfc/xmlreader_writer_streams
Expand Down
52 changes: 52 additions & 0 deletions ext/tidy/tests/sibling_nodes.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--TEST--
getPreviousSibling() and getNextSibling()
--EXTENSIONS--
tidy
--FILE--
<?php

$tidy = tidy_parse_string(<<<HTML
<!DOCTYPE html>
<html>
<body>
<div>first</div>
<!-- second -->
<div>third</div>
</body>
</html>
HTML);
$body = $tidy->body();

function format($str) {
if (is_null($str)) return $str;
return trim($str);
}

foreach ($body->child as $i => $child) {
echo "=== From the perspective of child $i ===\n";
echo "Previous: ";
var_dump(format($child->getPreviousSibling()?->value));
echo "Next: ";
var_dump(format($child->getNextSibling()?->value));
}

echo "=== html element has only the doctype as sibling ===\n";
echo "Previous: ";
var_dump(format($tidy->html()->getPreviousSibling()?->value));
echo "Next: ";
var_dump(format($tidy->html()->getNextSibling()?->value));

?>
--EXPECT--
=== From the perspective of child 0 ===
Previous: NULL
Next: string(15) "<!-- second -->"
=== From the perspective of child 1 ===
Previous: string(16) "<div>first</div>"
Next: string(16) "<div>third</div>"
=== From the perspective of child 2 ===
Previous: string(15) "<!-- second -->"
Next: NULL
=== html element has only the doctype as sibling ===
Previous: string(15) "<!DOCTYPE html>"
Next: NULL
26 changes: 21 additions & 5 deletions ext/tidy/tidy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1603,18 +1603,34 @@ PHP_METHOD(tidyNode, isPhp)
/* {{{ Returns the parent node if available or NULL */
PHP_METHOD(tidyNode, getParent)
{
TidyNode parent_node;
TIDY_FETCH_ONLY_OBJECT;

parent_node = tidyGetParent(obj->node);
if(parent_node) {
TidyNode parent_node = tidyGetParent(obj->node);
if (parent_node) {
tidy_create_node_object(return_value, obj->ptdoc, parent_node);
} else {
ZVAL_NULL(return_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just lost here ... not necessary anymore right ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You never need to set the return value to IS_NULL because the VM & JIT do it prior to the call.
I removed it to reduce the number of lines that are very very similar.

}
}
/* }}} */

PHP_METHOD(tidyNode, getPreviousSibling)
{
TIDY_FETCH_ONLY_OBJECT;

TidyNode previous_node = tidyGetPrev(obj->node);
if (previous_node) {
tidy_create_node_object(return_value, obj->ptdoc, previous_node);
}
}

PHP_METHOD(tidyNode, getNextSibling)
{
TIDY_FETCH_ONLY_OBJECT;

TidyNode next_node = tidyGetNext(obj->node);
if (next_node) {
tidy_create_node_object(return_value, obj->ptdoc, next_node);
}
}

/* {{{ __constructor for tidyNode. */
PHP_METHOD(tidyNode, __construct)
Expand Down
4 changes: 4 additions & 0 deletions ext/tidy/tidy.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -1004,4 +1004,8 @@ public function isAsp(): bool {}
public function isPhp(): bool {}

public function getParent(): ?tidyNode {}

public function getPreviousSibling(): ?tidyNode {}

public function getNextSibling(): ?tidyNode {}
}
10 changes: 9 additions & 1 deletion ext/tidy/tidy_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading