Changeset 2550 for trunk/src/filter/AgaviFormPopulationFilter.class.php
- Timestamp:
- 07/01/08 09:53:34 (6 months ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/filter/AgaviFormPopulationFilter.class.php
r2518 r2550 47 47 const ENCODING_ISO_8859_1 = 'iso-8859-1'; 48 48 49 /** 50 * @var DOMDocument Our (X)HTML document. 51 */ 49 52 protected $doc; 53 54 /** 55 * @var DOMXPath Our XPath instance for the document. 56 */ 50 57 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 = ''; 52 64 53 65 /** … … 151 163 if($this->doc->documentElement && $this->doc->documentElement->namespaceURI) { 152 164 $this->xpath->registerNamespace('html', $this->doc->documentElement->namespaceURI); 153 $this-> ns= 'html:';165 $this->xmlnsPrefix = 'html:'; 154 166 } else { 155 $this-> ns= '';167 $this->xmlnsPrefix = ''; 156 168 } 157 169 } else { 158 170 $this->doc->loadHTML($output); 159 171 $this->xpath = new DomXPath($this->doc); 160 $this-> ns= '';172 $this->xmlnsPrefix = ''; 161 173 } 162 174 … … 187 199 188 200 $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) { 190 202 if(strtolower($meta->getAttribute('http-equiv')) == 'content-type') { 191 203 if($this->doc->encoding === null) { … … 221 233 } 222 234 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)); 224 236 if($base->length) { 225 237 $baseHref = $base->item(0)->getAttribute('href'); … … 234 246 foreach(array_keys($populate) as $id) { 235 247 if(is_string($id)) { 236 $query[] = '@id="' . $id . '"';248 $query[] = sprintf('@id="%s"', $id); 237 249 } 238 250 } 239 251 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))); 241 253 } 242 254 } else { 243 $forms = $this->xpath->query( '//' . $this->ns . 'form[@action]');255 $forms = $this->xpath->query(sprintf('//%1$sform[@action]', $this->xmlnsPrefix)); 244 256 } 245 257 … … 276 288 277 289 // 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); 279 291 if($cfg['include_hidden_inputs']) { 280 292 $query .= ' or @type="hidden"'; … … 345 357 $errorClassElements[] = $element; 346 358 // 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) { 348 360 $errorClassElements[] = $label; 349 361 } 350 362 // and all explicit labels 351 363 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) { 353 365 $errorClassElements[] = $label; 354 366 } … … 360 372 foreach($cfg['error_class_map'] as $xpathExpression => $errorClassName) { 361 373 // 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); 363 375 if($errorClassResults && $errorClassResults->length) { 364 376 // 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! … … 457 469 // select elements 458 470 // 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) { 460 472 $option->removeAttribute('selected'); 461 473 if($p->hasParameter($pname) && ($option->getAttribute('value') === $value || ($multiple && is_array($value) && in_array($option->getAttribute('value'), $value)))) { … … 633 645 $insertSuccessful = false; 634 646 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); 636 648 637 649 if(!$targets || !$targets->length) {

