technosophos / querypath

QueryPath is a PHP library for manipulating XML and HTML. It is designed to work not only with local files, but also with web services and database resources.
http://querypath.org
Other
823 stars 115 forks source link

Calling remove() after a find() query with no match #79

Open xprudhomme opened 12 years ago

xprudhomme commented 12 years ago

With previous QueryPath version (2.1.1), I was able to do the following :

        $initialXML = XmlUtil::XML_HEADER.'<root><aaa><bbb></bbb><ccc>ddd</ccc></aaa><ddd><eee><bbb/></eee></ddd></root>';

        $qpObject = qp($initialXML)->xpath("//eee");
        $res = $qpObject->remove()->top()->xml();
        var_dump($res);

        $res = $qpObject->xpath("//test")->remove()->top()->xml();
        var_dump($res);

Which result was:

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

But now with current version (2.1.2), here is what I got:

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

null

Basically, with the new version, if I use the remove method() after a find() query with no match, the result is not the same as before. The only workaround I found is:

        $qpObject = qp($initialXML)->xpath("//eee");
        $qpObject->remove();
        $res = $qpObject->top()->xml();
        var_dump($res);

        $qpObject->xpath("//test");
        $qpObject->remove();
        $res = $qpObject->top()->xml();
        var_dump($res);

Is this new behavior normal or is it a bug?

technosophos commented 12 years ago

In the last release, I was trying to mimic jQuery's behavior with remove(), which would return an empty set of matches when nothing matches the selector.

I'll have to think about whether or not I want to change this case.

Thanks for pointing it out. I'm going to leave it open as a bug until I decide what to do, though my gut instinct at the moment is to leave it as is.

Matt

TechnoSophos Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Wednesday, March 21, 2012 at 2:01 PM, Xavatar wrote:

With previous QueryPath version (2.1.1), I was able to do the following :

$initialXML = XmlUtil::XML_HEADER.'ddd';

$qpObject = qp($initialXML)->xpath("//eee"); $res = $qpObject->remove()->top()->xml(); var_dump($res);

$res = $qpObject->xpath("//test")->remove()->top()->xml(); var_dump($res);

Which result was:

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

But now with current version (2.1.2), here is what I got:

string '<?xml version="1.0" encoding="UTF-8"?><root><aaa><bbb/><ccc>ddd</ccc></aaa><ddd/></root> ' (length=90)

null

Basically, with the new version, if I use the remove method() after a find() query with no match, the result is not the same as before. The only workaround I found is:

$qpObject = qp($initialXML)->xpath("//eee"); $qpObject->remove(); $res = $qpObject->top()->xml(); var_dump($res);

$qpObject->xpath("//test"); $qpObject->remove(); $res = $qpObject->top()->xml(); var_dump($res);

Is this new behavior normal or is it a bug?


Reply to this email directly or view it on GitHub: https://github.com/technosophos/querypath/issues/79

xprudhomme commented 12 years ago

Thanks Matt.

I don't know if you mentioned this point in the release notes, but if not I think it could be a good idea, as it would allow us to change our old code to make it compatible with new versions.

Another idea here, would be to pass a second and optional parameter (boolean) to remove(), which default value would be TRUE for the default jQuery behavior, if set to FALSE then remove() would work as before.

But I'm not sure whether this is a good idea or not, as in my case I only use XPath selectors with find(), and never the CSS selectors with the remove() first parameter (which would lead to a use of the method such as : remove(null, FALSE); ).

-Xav-