Show
Ignore:
Timestamp:
07/01/08 09:53:34 (6 months ago)
Author:
david
Message:

merge [2519:2549/branches/0.11]

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/filter/AgaviFormPopulationFilter.class.php

    r2518 r2550  
    4747  const ENCODING_ISO_8859_1 = 'iso-8859-1'; 
    4848 
     49  /** 
     50   * @var        DOMDocument Our (X)HTML document. 
     51   */ 
    4952  protected $doc; 
     53 
     54  /** 
     55   * @var        DOMXPath Our XPath instance for the document. 
     56   */ 
    5057  protected $xpath; 
    51   protected $ns; 
     58 
     59  /** 
     60   * @var        string The XML NS prefix we're working on with XPath, including 
     61   *                    a colon (or empty string if document has no NS). 
     62   */ 
     63  protected $xmlnsPrefix = ''; 
    5264 
    5365  /** 
     
    151163      if($this->doc->documentElement && $this->doc->documentElement->namespaceURI) { 
    152164        $this->xpath->registerNamespace('html', $this->doc->documentElement->namespaceURI); 
    153         $this->ns = 'html:'; 
     165        $this->xmlnsPrefix = 'html:'; 
    154166      } else { 
    155         $this->ns = ''; 
     167        $this->xmlnsPrefix = ''; 
    156168      } 
    157169    } else { 
    158170      $this->doc->loadHTML($output); 
    159171      $this->xpath = new DomXPath($this->doc); 
    160       $this->ns = ''; 
     172      $this->xmlnsPrefix = ''; 
    161173    } 
    162174 
     
    187199 
    188200    $properXhtml = false; 
    189     foreach($this->xpath->query('//' . $this->ns . 'head/' . $this->ns . 'meta') as $meta) { 
     201    foreach($this->xpath->query(sprintf('//%1$shead/%1$smeta', $this->xmlnsPrefix)) as $meta) { 
    190202      if(strtolower($meta->getAttribute('http-equiv')) == 'content-type') { 
    191203        if($this->doc->encoding === null) { 
     
    221233    } 
    222234 
    223     $base = $this->xpath->query('/' . $this->ns . 'html/' . $this->ns . 'head/' . $this->ns . 'base[@href]'); 
     235    $base = $this->xpath->query(sprintf('/%1$shtml/%1$shead/%1$sbase[@href]', $this->xmlnsPrefix)); 
    224236    if($base->length) { 
    225237      $baseHref = $base->item(0)->getAttribute('href'); 
     
    234246      foreach(array_keys($populate) as $id) { 
    235247        if(is_string($id)) { 
    236           $query[] = '@id="' . $id . '"'; 
     248          $query[] = sprintf('@id="%s"', $id); 
    237249        } 
    238250      } 
    239251      if($query) { 
    240         $forms = $this->xpath->query('//' . $this->ns . 'form[' . implode(' or ', $query) . ']'); 
     252        $forms = $this->xpath->query(sprintf('//%1$sform[%2$s]', $this->xmlnsPrefix, implode(' or ', $query))); 
    241253      } 
    242254    } else { 
    243       $forms = $this->xpath->query('//' . $this->ns . 'form[@action]'); 
     255      $forms = $this->xpath->query(sprintf('//%1$sform[@action]', $this->xmlnsPrefix)); 
    244256    } 
    245257 
     
    276288 
    277289      // build the XPath query 
    278       $query = 'descendant::' . $this->ns . 'textarea[@name] | descendant::' . $this->ns . 'select[@name] | descendant::' . $this->ns . 'input[@name and (not(@type) or @type="text" or (@type="checkbox" and not(contains(@name, "[]"))) or (@type="checkbox" and contains(@name, "[]") and @value) or @type="radio" or @type="password" or @type="file"'; 
     290      $query = sprintf('descendant::%1$stextarea[@name] | descendant::%1$sselect[@name] | descendant::%1$sinput[@name and (not(@type) or @type="text" or (@type="checkbox" and not(contains(@name, "[]"))) or (@type="checkbox" and contains(@name, "[]") and @value) or @type="radio" or @type="password" or @type="file"', $this->xmlnsPrefix); 
    279291      if($cfg['include_hidden_inputs']) { 
    280292        $query .= ' or @type="hidden"'; 
     
    345357          $errorClassElements[] = $element; 
    346358          // all implicit labels 
    347           foreach($this->xpath->query('ancestor::' . $this->ns . 'label[not(@for)]', $element) as $label) { 
     359          foreach($this->xpath->query(sprintf('ancestor::%1$slabel[not(@for)]', $this->xmlnsPrefix), $element) as $label) { 
    348360            $errorClassElements[] = $label; 
    349361          } 
    350362          // and all explicit labels 
    351363          if(($id = $element->getAttribute('id')) != '') { 
    352             foreach($this->xpath->query('descendant::' . $this->ns . 'label[@for="' . $id . '"]', $form) as $label) { 
     364            foreach($this->xpath->query(sprintf('descendant::%1$slabel[@for="%2$s"]', $this->xmlnsPrefix, $id), $form) as $label) { 
    353365              $errorClassElements[] = $label; 
    354366            } 
     
    360372            foreach($cfg['error_class_map'] as $xpathExpression => $errorClassName) { 
    361373              // evaluate each xpath expression 
    362               $errorClassResults = $this->xpath->query(AgaviToolkit::expandVariables($xpathExpression, array('htmlnsPrefix' => $this->ns)), $errorClassElement); 
     374              $errorClassResults = $this->xpath->query(AgaviToolkit::expandVariables($xpathExpression, array('htmlnsPrefix' => $this->xmlnsPrefix)), $errorClassElement); 
    363375              if($errorClassResults && $errorClassResults->length) { 
    364376                // we have results. the xpath expressions are used to locale the actual elements we set the error class on - doesn't necessarily have to be the erroneous element or the label! 
     
    457469          // select elements 
    458470          // yes, we still use XPath because there could be OPTGROUPs 
    459           foreach($this->xpath->query('descendant::' . $this->ns . 'option', $element) as $option) { 
     471          foreach($this->xpath->query(sprintf('descendant::%1$soption', $this->xmlnsPrefix), $element) as $option) { 
    460472            $option->removeAttribute('selected'); 
    461473            if($p->hasParameter($pname) && ($option->getAttribute('value') === $value || ($multiple && is_array($value) && in_array($option->getAttribute('value'), $value)))) { 
     
    633645    $insertSuccessful = false; 
    634646    foreach($rules as $xpathExpression => $errorMessageInfo) { 
    635       $targets = $this->xpath->query(AgaviToolkit::expandVariables($xpathExpression, array('htmlnsPrefix' => $this->ns)), $element); 
     647      $targets = $this->xpath->query(AgaviToolkit::expandVariables($xpathExpression, array('htmlnsPrefix' => $this->xmlnsPrefix)), $element); 
    636648 
    637649      if(!$targets || !$targets->length) {