Re: proper use of validation icons

Shane McCarron wrote:
> 
> 
> Michael A. Peters wrote:
>>
>> Hardly means I'm clueless, simply means I need to adjust my code to 
>> check the q value.
>>
>> I got the code snippet off of a Firefox developer blog. Guess he's 
>> clueless too, as he didn't check the q value either.
>>
>> There's a difference between clueless and sloppy. Sloppy is probably a 
>> better description for my bug.
>>
>> Browsers that don't know how to properly parse xhtml (IE IE8) seem to 
>> prefer not to even send it as part of what they can handle. But no, I 
>> haven't tested all browsers. The main 4 gui get pages they properly 
>> render (all but IE getting xhtml), wget and links get pages they 
>> properly render. But yes, I'll fix the bug.
> Feel free to share your fixed code so others can benefit - it is in 
> everyone's interest to help ensure that XHTML documents are delivered 
> using the proper media types.
> 

This is what I came up with (php) - the functions actually do more than 
is necessary, but give me a function for getting q value of anything in 
the accept string.

Tested in Firefox and Opera and manual manipulation of a test string.

It can be done better, but I'm tired (just got back from vacation and 
now they are paving my street).

function getQValue($string) {
    $exp = explode(';',$string);
    if (sizeof($exp) == 1) {
       $q = 1;
       } else {
       $qarray = explode('=',$exp[1]);
       if (isset($qarray[1])) {
          $q = (0 + trim($qarray[1]));
          }
       }
    if (! isset($q)) {
       $q=0;
       }
    $return[] = trim($exp[0]);
    $return[] = $q;
    return $return;
    }

function getAccept($string) {
    $accept = explode(',',$string);
    for ($i=0; $i<sizeof($accept); $i++) {
       $httpAccept[] = getQValue($accept[$i]);
       }
    return $httpAccept;
    }

$testString = "text/html, application/xml;q=0.9, 
application/xhtml+xml;q=0, image/png, image/jpeg, image/gif, 
image//x-xbitmap, */*;q=0.1";
if (! isset($usexml)) {
    if (isset($_SERVER['HTTP_ACCEPT'])) {
       $httpAccept = getAccept($_SERVER['HTTP_ACCEPT']);
       //$httpAccept = getAccept($testString);
       for ($i=0; $i<sizeof($httpAccept); $i++) {
          if (strcmp($httpAccept[$i][0],"application/xhtml+xml") == 0) {
             $q = $httpAccept[$i][1];
             }
          }
       if (! isset($q)) {
          $q = 0;
          }
       if ($q > 0) {
          $usexml = 1;
          } else {
          $usexml = 0;
          }
       } else {
       $usexml = 0;
       }
    } // end if $usexml not previously explicity set
///

Received on Monday, 15 June 2009 17:36:40 UTC