Zend Framework

Zend_Dom_Query Fails Tests

Details

  • Type: Bug Bug
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.7.6
  • Fix Version/s: None
  • Component/s: Zend_Dom_Query
  • Labels:
    None

Description

Hello,

It seems Zend_Dom does not work. i have gone through the tests and it currently does not work as expected.

For example:
1. Providing bogus string as html

try {
            $this->query = new Zend_Dom_Query();
            $this->query->setDocument('some bogus');
            Zend_Debug::dump($this->query->getDocumentType());
            Zend_Debug::dump($this->query->getDocument());
            $matches = $this->query->query ( ".foo" );
            Zend_Debug::dump ( $matches );
} catch (Zend_Dom_Exception $e) {
            var_dump($e->getMessage());
            exit;
}

I expected the exception to have been triggered (as there is a test for this), however the following was returned:

string(7) "docHtml" // $this->query->getDocumentType()

string(10) "some bogus" // $this->query->getDocument()

object(Zend_Dom_Query_Result)#68 (7) {
  ["_count:protected"] => NULL
  ["_cssQuery:protected"] => string(4) ".foo"
  ["_document:protected"] => object(DOMDocument)#72 (0) {
  }
  ["_nodeList:protected"] => object(DOMNodeList)#71 (0) {
  }
  ["_position:protected"] => int(0)
  ["_xpath:protected"] => NULL
  ["_xpathQuery:protected"] => string(30) "//*[contains(@class, ' foo ')]"
}

2. Providing Xhtml

try {
            $this->query = new Zend_Dom_Query();
            $this->query->setDocument(file_get_contents('http://framework.zend.com/svn/framework/standard/trunk/tests/Zend/Dom/_files/sample.xhtml'));
            Zend_Debug::dump($this->query->getDocumentType());
            Zend_Debug::dump($this->query->getDocument());
            $matches = $this->query->query ( ".foo" );
            Zend_Debug::dump ( $matches );
} catch (Zend_Dom_Exception $e) {
            var_dump($e->getMessage());
            exit;
}

This should work but instead the following was returned:

string(8) "docXhtml"

string(6456) "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Sample HTML Content for DOM Queries</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <div id="header">
        <h1>
            <a href="#"><img src="img/icon.png" class="right" alt="Logo"
                height="79" width="80" border="0" /></a>
            Page Title<br />
            <span class="subtitle">SubTitle</span>
        </h1>
    </div>

    <ul id="topnav">
        <li class="current"><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Misc</a></li>
        <li><a href="#">Wiki</a></li>
    </ul>

    <div id="main">
        <div id="content">
            <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed at dui quis magna
            viverra sagittis. Donec sed nibh ut pede pretium ultricies. Mauris viverra
            tempus dui. Donec dolor risus, mollis sed, pulvinar id, faucibus ac, nisl. Duis
            eu erat at erat tempus euismod. Suspendisse sodales lacus vitae libero. Sed
            suscipit commodo enim. Pellentesque erat. Cras orci. Pellentesque non nunc. Cras
            diam libero, feugiat vel, accumsan et, laoreet mattis, purus. Pellentesque
            volutpat hendrerit justo. Cras vehicula. Vestibulum cursus gravida est.
            Curabitur pede. Mauris consectetuer rutrum dolor. Ut a turpis ac metus
            sollicitudin dignissim.</p>

            <p>Praesent blandit, nisi in egestas mattis, lectus tortor vehicula massa, commodo
            vehicula diam lacus consequat lectus. In vitae pede eget leo faucibus
            condimentum. Pellentesque nisi. Ut condimentum elit vitae mi. Morbi neque erat,
            mollis nec, volutpat sed, placerat sit amet, ligula. Morbi eleifend. Morbi
            auctor condimentum ipsum. In ac purus. Sed vitae magna. Sed volutpat vestibulum
            leo. Phasellus id nibh.</p>

            <p>Fusce blandit elementum leo. Proin id erat. In dignissim orci vulputate libero
            cursus volutpat. Ut vitae arcu non nulla sagittis laoreet. Suspendisse sed
            tortor ac risus placerat convallis. Donec nulla ipsum, tempus et, porttitor
            quis, blandit vel, tortor. Donec condimentum. Aliquam felis dui, consectetuer
            ultricies, euismod eu, pretium non, sem. Sed ultricies, tortor non vulputate
            tempor, urna leo sagittis libero, ut feugiat nulla tellus et ligula. Suspendisse
            eget est. Vestibulum interdum mi at felis. Fusce dictum. Fusce a enim at ipsum
            consectetuer molestie. Integer rhoncus. Cum sociis natoque penatibus et magnis
            dis parturient montes, nascetur ridiculus mus. Donec rhoncus mattis arcu.</p>

            <p>Proin id nisi. Vivamus eu risus. Vivamus et enim et turpis volutpat bibendum.
            Nunc nunc ipsum, semper sit amet, pellentesque in, venenatis sit amet, elit.
            Suspendisse potenti. Cras lacinia, nisl vel vulputate pellentesque, lacus tellus
            lobortis ipsum, sed pellentesque neque felis ac orci. Sed pellentesque.
            Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac
            turpis egestas. Etiam lacus enim, facilisis eu, suscipit ac, condimentum ut,
            ante. Aenean convallis mattis enim. Aliquam pretium. Etiam quam. Donec
            tincidunt. Pellentesque id ante ut orci gravida semper. Cum sociis natoque
            penatibus et magnis dis parturient montes, nascetur ridiculus mus. Fusce
            bibendum elit vel sem cursus ultrices. Nunc in purus. Ut consectetuer, nulla ac
            porta hendrerit, ligula eros blandit nulla, at ornare dui odio vel justo. Morbi
            tellus nulla, sodales eget, aliquet ac, varius vitae, pede. Pellentesque
            habitant morbi tristique senectus et netus et malesuada fames ac turpis
            egestas.</p>

            <p>Vestibulum urna. Proin hendrerit neque et tellus. Duis luctus congue sem.
            Aliquam ultricies orci nec enim. Nam velit eros, feugiat posuere, ultricies
            vitae, consequat nec, urna. In ipsum neque, porta sed, ornare nec, cursus et,
            mauris. Nam eget urna. Suspendisse venenatis nulla nec urna lacinia bibendum.
            Morbi tempor lobortis nisl. Nulla ut eros. Fusce id tortor ut diam vulputate
            consectetuer. Nulla vel augue. Nulla ac tellus sed orci pulvinar dictum. Proin
            lobortis. Fusce consequat auctor ante. Donec bibendum fringilla nunc. Donec
            viverra, urna ac auctor dapibus, augue ipsum tristique lacus, sed feugiat nibh
            nibh quis purus. Donec orci est, pharetra laoreet, laoreet id, mattis ut, odio.
            In lacus sem, rutrum tristique, dignissim ac, imperdiet ac, lacus. </p>
        </div>

        <div id="subnav">
            <div class="navblock">
                <form action="/login" method="post">
                    Username:<br />
                    <input name="username" id="username" type="text" value="" /><br />
                    Password:<br />
                    <input name="password" id="password" type="password" value="" /><br />
                    <input name="login" id="login" type="submit" value="Login" />
                </form>
            </div>

            <div class="navblock" dojoType="FilteringSelect">
                <ul>
                    <li class="foo" dojoType="foo bar">Item 1</li>
                    <li class="foo current">Item 2</li>
                    <li class="foo" dojoType="foo bar">Item 3</li>
                </ul>
            </div>
        </div>
    </div>

    <div id="footer">
        <div class="footerblock first">
            <p class="attribution">
            &copy; 2008 - present, <br />
            Zend Framework
            </p>
            <a href="/about">About</a><br />
            <a href="/contact">Contact</a><br />
        </div>

        <div class="footerblock last">
            <h4>More:</h4>
            <ul>
                 <li>Ohloh profile</li>
                 <li>Facebook</li>
                 <li>Plaxo Pulse</li>
                 <li>LinkedIn</li>
                 <li>del.icious</li>
                 <li>Twitter</li>
                 <li>SlideShare</li>
            </ul>
        </div>
    </div>
</body>
</html>
"

object(Zend_Dom_Query_Result)#86 (7) {
  ["_count:protected"] => NULL
  ["_cssQuery:protected"] => string(4) ".foo"
  ["_document:protected"] => object(DOMDocument)#72 (0) {
  }
  ["_nodeList:protected"] => object(DOMNodeList)#83 (0) {
  }
  ["_position:protected"] => int(0)
  ["_xpath:protected"] => NULL
  ["_xpathQuery:protected"] => string(30) "//*[contains(@class, ' foo ')]"
}

This is my first bug report, so please be gentle

Activity

Hide
Matthew Weier O'Phinney added a comment -

It may actually be working as expected.

First, DOMDocument is very forgiving about malformed HTML, and will parse basically anything. So, as far as Zend_Dom_Query is concerned, it received valid documents.

When you call $dom->query(), you always receive a result. The trick is to find out if the result contains any members. What I cannot tell from your debug statements is whether or not the NodeLists attached to the results contain anything. Can you do the following on each result you receive:

echo count($result);

If it's 0 in the first case, than it's doing fine. For the second case, I'd expect a value of 3.

Otherwise... I don't see anything operating incorrectly here – what were your expectations?

Show
Matthew Weier O'Phinney added a comment - It may actually be working as expected. First, DOMDocument is very forgiving about malformed HTML, and will parse basically anything. So, as far as Zend_Dom_Query is concerned, it received valid documents. When you call $dom->query(), you always receive a result. The trick is to find out if the result contains any members. What I cannot tell from your debug statements is whether or not the NodeLists attached to the results contain anything. Can you do the following on each result you receive:
echo count($result);
If it's 0 in the first case, than it's doing fine. For the second case, I'd expect a value of 3. Otherwise... I don't see anything operating incorrectly here – what were your expectations?
Hide
Mark Smith added a comment -

You are right.
When I do:

foreach ($matches as $result) {
       	Zend_Debug::dump($result);
       	print count($result);
}

The response is:

object(DOMElement)#76 (0) {
}

1

object(DOMElement)#77 (0) {
}

1

object(DOMElement)#78 (0) {
}

1

To be totally honest, I was confused with the lack of information in the response object.

I expect to recieve an attribute value or element value from each of the matched elements. Is this possible?

Show
Mark Smith added a comment - You are right. When I do:
foreach ($matches as $result) {
       	Zend_Debug::dump($result);
       	print count($result);
}
The response is:
object(DOMElement)#76 (0) {
}

1

object(DOMElement)#77 (0) {
}

1

object(DOMElement)#78 (0) {
}

1
To be totally honest, I was confused with the lack of information in the response object. I expect to recieve an attribute value or element value from each of the matched elements. Is this possible?
Hide
Mark Smith added a comment -

Sorry,

I have just Google'd the answer and came across the PHP manual which has all the information I need.

Many Thanks for all your help

Show
Mark Smith added a comment - Sorry, I have just Google'd the answer and came across the PHP manual which has all the information I need. Many Thanks for all your help

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: