muzidudu / phpquery

Automatically exported from code.google.com/p/phpquery
0 stars 0 forks source link

val() strange thing happen #96

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. having the DOM <DOM><select name="section"><option
value="-1">Niveau</option><option value="1">6°</option><option
value="2">5°</option><option selected="selected"
value="3">4°</option><option selected="selected" 
value="4">3°</option></select>
2. doing that $mySelect->val(3)
3.

What is the expected output? What do you see instead?
It should put selected attribute on the option having the value "3". It do
it weel, but add the seleted attribute to the line having the value "4" too
! considering tha the html of this last option is "3°"...

What version of the product are you using? On what operating system?
the latest dev version

Please provide any additional information below.

Original issue reported on code.google.com by nicolas....@gmail.com on 20 Jan 2009 at 10:09

GoogleCodeExporter commented 8 years ago
This happens because of 2 things:
1. Bad unicode string comparison in PHP
2. Using val() on select elements - selected will be options with attribute 
"value"
OR options with TEXT equal to val() parameter

Now those two reasons in combination has been selecting last option (which text
without unicode was same as requested value). There is no clean fix, but there 
are
couple of workarounds:
1. Compare strings and then their lengths
2. Convert strings to HTML Entities and then compare
3. Binary safe compare ?

Ive chosen 1st one and it's available in r331.

Original comment by tobiasz....@gmail.com on 21 Jan 2009 at 4:39

GoogleCodeExporter commented 8 years ago
Thanks tobiaz ;) I'll try it :).
Hope you're alright :).

Original comment by nicolas....@gmail.com on 21 Jan 2009 at 4:44

GoogleCodeExporter commented 8 years ago
It seems to be not fixed for me :(.
It always do the same.

An idea : the text between the <option/> tag should be use only if value isn't
define... nop ?

Original comment by nicolas....@gmail.com on 22 Jan 2009 at 8:30

GoogleCodeExporter commented 8 years ago
Testing code:
$doc = phpQuery::newDocument('<select name="section"><option
value="-1">Niveau</option><option value="1">6°</option><option
value="2">5°</option><option
value="3">4°</option><option value="4">3°</option></select>');
print $doc['select']->val(3);

Please also test:
print strlen('3°');

What is your PHP version ?

> An idea : the text between the <option/> tag should be use only if value isn't
> define... nop ?
phpQuery does it just like jQuery.

Original comment by tobiasz....@gmail.com on 22 Jan 2009 at 9:03

GoogleCodeExporter commented 8 years ago
Your testing code work for me.

print strlen('3°'); return "3".

Note that I use info from database. In my database I store info "encoded", so I 
have
"3°" instead of "3°"... but after inserting it in the doc with phpQuery using
something like

$listeSection_elements  =   $DOM_arbre->find('select[name="section"]');
if
($listeSection_elements->length()==1&&isset($section_infos)&&is_array($section_i
nfos->resultat))
{
    foreach ($section_infos->resultat as $section)
    {
        $listeSection_elements->append('<option
value="'.$section['id'].'">'.$section['libelle'].'</option>');
    }
}

I use PHP 5.2.6.

Disucss the point about the use of the value or the text on the HTML channel, 
I'm now
sure that, in a form, if an option have a value attribute it will be use 
instead of
text as submited value.
So I could say that if jQuery don't do that... jQuery is false :x.

Original comment by nicolas....@gmail.com on 22 Jan 2009 at 10:11

GoogleCodeExporter commented 8 years ago
Again, using 3° in this code:
$doc = phpQuery::newDocument('<select name="section"><option
value="-1">Niveau</option><option value="1">6°</option><option
value="2">5°</option><option
value="3">4°</option><option value="4">3°</option></select>');
print $doc['select']->val(3)->end();
works for me.

Could you do strlen($section['libelle']) inside this foreach ?

> So I could say that if jQuery don't do that... jQuery is false :x.
Personally i was terrified when i found this out (jQuery-1.3.js lines 467-468). 
Never
suspected such behavior from val(), but being honest i never trusted it fully ;)
From project's point of view, there should be strict main API behavior 
compatibility,
which can bring various benefits.

You can file a ticket against this in jQuery bugtracker:
http://dev.jquery.com/newticket/

Original comment by tobiasz....@gmail.com on 22 Jan 2009 at 1:05

GoogleCodeExporter commented 8 years ago
The code is working for me too :s.

Doing a strlen($section['libelle']) inside this foreach it gives me "6" as 
result.

I post on bug tracker here http://dev.jquery.com/ticket/3953

Original comment by nicolas....@gmail.com on 22 Jan 2009 at 1:37

GoogleCodeExporter commented 8 years ago
The problem is in this code :
<php>
$optionValue = $option->attr('value');
                        $optionText = $option->text();
                        $optionTextLenght = mb_strlen($optionText);
                        foreach($_val as $v)
                            if ($optionValue == $v)
                                $selected = true;
                            else if ($optionText == $v && $optionTextLenght == mb_strlen($v))
                                $selected = true;
</php>

If the option is
<dom>
<option value="3">3°</option>
</dom>

Doing : <php>$myoption->text()</php> return "3"... not "3°" ! only html() 
return
"3°".

Original comment by nicolas....@gmail.com on 26 Jan 2009 at 3:06

GoogleCodeExporter commented 8 years ago
Interesting... fixed in jQuery ^^ ! http://dev.jquery.com/ticket/3104

Original comment by nicolas....@gmail.com on 28 Jan 2009 at 8:52

GoogleCodeExporter commented 8 years ago

Original comment by tobiasz....@gmail.com on 11 Feb 2009 at 8:58

GoogleCodeExporter commented 8 years ago
Fixed same way as in jQuery in r354.

Original comment by tobiasz....@gmail.com on 16 Feb 2009 at 5:03

GoogleCodeExporter commented 8 years ago
Fabulous ! Thanks :).

Original comment by nicolas....@gmail.com on 17 Feb 2009 at 7:58