Skip to content
Open
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
11 changes: 9 additions & 2 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,14 @@ static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
}

GET_NODE(sxe, node);
node = php_sxe_get_first_node_non_destructive(sxe, node);
if (sxe->iter.type == SXE_ITER_CHILD) {
/* Start at the first child rather than at the first element child, so that
* non-element nodes (e.g. PIs, comments) before it are not skipped. Like
* sxe_prop_is_empty(), only do so if there is a matching element child. */
node = php_sxe_get_first_node_non_destructive(sxe, node) ? node->children : NULL;
} else {
node = php_sxe_get_first_node_non_destructive(sxe, node);
}

if (node && sxe->iter.type != SXE_ITER_ATTRLIST) {
if (node->type == XML_ATTRIBUTE_NODE) {
Expand All @@ -1128,7 +1135,7 @@ static HashTable *sxe_get_prop_hash(zend_object *object, int is_debug) /* {{{ */
}

while (node) {
if (node->children != NULL || node->prev != NULL || node->next != NULL || xmlIsBlankNode(node)) {
if (node->children != NULL || node->prev != NULL || node->next != NULL || xmlIsBlankNode(node) || sxe->iter.type == SXE_ITER_CHILD) {
SKIP_TEXT(node);
} else {
if (node->type == XML_TEXT_NODE) {
Expand Down
74 changes: 74 additions & 0 deletions ext/simplexml/tests/gh12168.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--TEST--
GH-12168 (Mixing processing instructions and element nodes gives inconsistent results in SimpleXML)
--EXTENSIONS--
simplexml
--FILE--
<?php

$sxe = simplexml_load_string('<container><x/><?hello world?></container>');
var_dump($sxe->children());

$sxe = simplexml_load_string('<container><?hello world?><x/></container>');
var_dump($sxe->children());

foreach ([
'<c><!-- c --><x/></c>',
'<c><x/><!-- c --></c>',
'<c><?pi a?><x/><?pi b?><y/></c>',
'<c>text</c>',
] as $xml) {
$sxe = simplexml_load_string($xml);
echo $xml, ': ', json_encode(array_keys((array) $sxe->children())), "\n";
}

/* Non-element nodes only show up alongside a matching element, consistent with (bool) */
foreach ([
'<c><?pi a?></c>',
'<c><!-- c --></c>',
'<c>t<?pi a?></c>',
'<!DOCTYPE c [<!ENTITY e "v">]><c>&e;<x/></c>',
] as $xml) {
$sxe = simplexml_load_string($xml);
echo $xml, ': ', json_encode(array_keys((array) $sxe->children())), ' ', var_export((bool) $sxe->children(), true), "\n";
}

$sxe = simplexml_load_string('<c xmlns:a="urn:a"><?pi a?><y/><a:x/></c>');
var_dump(array_keys((array) $sxe->children('urn:a')));
$sxe = simplexml_load_string('<c xmlns:a="urn:a"><?pi a?><y/></c>');
var_dump(array_keys((array) $sxe->children('urn:a')), (bool) $sxe->children('urn:a'));

?>
--EXPECTF--
object(SimpleXMLElement)#%d (2) {
["x"]=>
object(SimpleXMLElement)#%d (0) {
}
["hello"]=>
object(SimpleXMLElement)#%d (0) {
}
}
object(SimpleXMLElement)#%d (2) {
["hello"]=>
object(SimpleXMLElement)#%d (0) {
}
["x"]=>
object(SimpleXMLElement)#%d (0) {
}
}
<c><!-- c --><x/></c>: ["comment","x"]
<c><x/><!-- c --></c>: ["x","comment"]
<c><?pi a?><x/><?pi b?><y/></c>: ["pi","x","y"]
<c>text</c>: []
<c><?pi a?></c>: [] false
<c><!-- c --></c>: [] false
<c>t<?pi a?></c>: [] false
<!DOCTYPE c [<!ENTITY e "v">]><c>&e;<x/></c>: ["e","x"] true
array(2) {
[0]=>
string(2) "pi"
[1]=>
string(1) "x"
}
array(0) {
}
bool(false)
Loading