php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66359 Corrupt implementation of Traversable in DOMNodeList, when created by DOMXPath
Submitted: 2013-12-27 11:30 UTC Modified: 2018-11-28 17:24 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: r dot wilczek at web-appz dot de Assigned: cmb (profile)
Status: Closed Package: DOM XML related
PHP Version: 5.5.7 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: r dot wilczek at web-appz dot de
New email:
PHP Version: OS:

 

 [2013-12-27 11:30 UTC] r dot wilczek at web-appz dot de
Description:
------------
A DOMNodeList created by evaluating a DOMXPath is not correctly traversable.
Equivalent instances of DOMNodeList created by getElementsByTagName() etc. do not have this problem.

The example below creates equivalent nodelists and shows the incorrect iteration of the one created by evaluating an XPath-expression.

Test script:
---------------
function printList(\DOMNodeList $list)
{
    foreach ($list as $outer) {
        /* @var $outer \DOMNode */
        echo 'outer: ' . $outer->nodeValue . PHP_EOL;
        foreach ($list as $inner) {
            /* @var $inner \DOMNode */
            echo ' inner: ' . $inner->nodeValue . PHP_EOL;
        }
    }
}

/**
 * @return DOMNodeList
 */
function getByXpath(\DOMDocument $dom)
{
    return (new \DOMXPath($dom))->evaluate('item');
}
/**
 * @return DOMNodeList
 */
function getByTagName(\DOMDocument $dom)
{
    return $dom->getElementsByTagName('item');
}

$xml = <<< XML
<root>
  <item>1</item>
  <item>2</item>
  <item>3</item>
</root>
XML;

$dom = new \DOMDocument;
$dom->loadXml($xml);

printList(getByTagName($dom));
echo '-------------' . PHP_EOL;
printList(getByXpath($dom));



Expected result:
----------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3
-------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3

Actual result:
--------------
outer: 1
 inner: 1
 inner: 2
 inner: 3
outer: 2
 inner: 1
 inner: 2
 inner: 3
outer: 3
 inner: 1
 inner: 2
 inner: 3
-------------
outer: 1
 inner: 1
 inner: 2
 inner: 3


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-30 16:35 UTC] jpauli@php.net
DomXpath and DomDocument don't set the node to the same type.
Iterator uses the type to dispatch a different iteration method (http://lxr.php.net/xref/PHP_5_5/ext/dom/dom_iterators.c#203)

DomDocument iterator creation : http://lxr.php.net/xref/PHP_5_5/ext/dom/php_dom.c#1114
DomXpath iterator creation :
http://lxr.php.net/xref/PHP_5_5/ext/dom/xpath.c#dom_xpath_iter
 [2014-01-30 20:03 UTC] r dot wilczek at web-appz dot de
And is that expected behaviour, or would you confirm this as a bug?
 [2015-03-30 10:20 UTC] r dot wilczek at web-appz dot de
Is there anybody paying attention to this? I cannot see, why the fact, that DOMXPath and DOMDocument use different iterators, justifies itself.
 [2018-11-28 17:24 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2018-11-28 17:24 UTC] cmb@php.net
Fixed as of PHP 7.3.0[1].  Closing as duplicate of bug #75451.

[1] <https://3v4l.org/FXAuR>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 19:01:29 2024 UTC