Changeset 5834
- Timestamp:
- 11/04/07 13:29:07 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfHamlViewPlugin/branches/1.0/lib/HamlParser.class.php
r5827 r5834 185 185 private function _parseLine($line) 186 186 { 187 $this->_current_line ++;187 $this->_current_line += 1; 188 188 $indent = strspn($line, self::TOKEN_INDENT); 189 189 $line = trim($line, self::TOKEN_INDENT); … … 257 257 } 258 258 259 if (!ctype_digit(trim($value, " \"'")) && $this->_eval_syntax("return ".trim($value).";") !== false) { 259 if (!ctype_digit(trim($value, " \"'")) && $this->_eval_syntax("return ".trim($value).";") !== false) 260 { 260 261 $value = "[?php \$_haml_attr = '".htmlspecialchars(trim($value), ENT_QUOTES)."'; "; 261 262 $value .= "echo eval('return '.stripcslashes(htmlspecialchars_decode(html_entity_decode(\$_haml_attr), ENT_QUOTES)).';');?]"; … … 290 291 if (!empty($content)) 291 292 { 292 $content = $this->_createPHPEcho($content , $indent);293 $content = $this->_createPHPEcho($content); 293 294 } else { 294 295 $this->_syntax_error('If you use the "=" operator you need to have content after it.'); … … 321 322 } else if (isset($this->_parents[$key])) { 322 323 $parent = $this->_parents[$key]; 324 } 325 if (is_object($parent)) 326 { 327 $type = (is_object($parent->attributes))?$parent->attributes->getNamedItem('type'):null; 328 if (!is_null($type) && $type->value == 'instruction') { 329 $parent = null; 330 } 323 331 } 324 332 $key -= intval(self::INDENT); … … 373 381 } 374 382 375 $content = "<?php ".trim($content).$terminator." ?>"; 376 $node = $this->_createTextNode($content); 383 $content = trim($content).$terminator; 384 $attributes = array(array('key' =>'type', 'value' => 'instruction')); 385 $node = $this->_createTag('php', $attributes, $content); 377 386 378 387 return $node; … … 384 393 if (empty($content)) return false; 385 394 386 $node = $this->_createPHPEcho($content , $indent);395 $node = $this->_createPHPEcho($content); 387 396 $parent = $this->_getParent($indent); 388 397 if ($parent instanceof DOMCharacterData) … … 400 409 } 401 410 402 private function _createPHPEcho($content, $indent = 0) 403 { 404 $content = trim($content); 405 406 if ($this->_isXHTML()) { 407 $content = '<?php ob_start(); echo '.$content.' ; $content = ob_get_clean(); haml_echo($content, '.$indent.'); ?>'; 408 } else { 409 $content = '<?php echo '.$content.'; ?>'; 410 } 411 private function _createPHPEcho($content) 412 { 413 $content = '<?php echo '.trim($content).';?>'; 414 411 415 return $this->_createTextNode($content); 412 416 } 413 417 414 private function _addTag($tag, $attributes = array(), $content = null, $indent) 415 { 416 // handle special case for %javascript 418 private function _addTag($tag, $attributes = array(), $content = null, $indent = 0) 419 { 420 $node = $this->_createTag($tag, $attributes, $content); 421 422 $parent = $this->_getParent($indent); 423 if (!($parent instanceof DOMCharacterData)) 424 { 425 $node = $parent->appendChild($node); 426 $this->_setParent($node, $indent); 427 } 428 } 429 430 private function _createTag($tag, $attributes = array(), $content = null) 431 { 432 // handle special cases for %javascript and %style 417 433 if ($tag == 'javascript') 418 434 { … … 457 473 } 458 474 459 $parent = $this->_getParent($indent); 460 if (!($parent instanceof DOMCharacterData)) 461 { 462 $node = $parent->appendChild($node); 463 $this->_setParent($node, $indent); 464 } 475 return $node; 465 476 } 466 477 … … 483 494 if ($parent instanceof DOMCharacterData) 484 495 { 485 if ($parent->substringData(0, 22) == '<?php ob_start(); echo') { 486 $parent->insertData($parent->length - 54 - (($indent>=10)?2:1), $orig_content.' '); 487 } else if ($parent->substringData(0, 10) == '<?php echo') { 488 $parent->insertData($parent->length - 3 - (($indent>=10)?2:1), $orig_content.' '); 496 if ($parent->substringData(0, 10) == '<?php echo') { 497 $parent->insertData($parent->length - 3, $orig_content); 489 498 } 490 499 else … … 585 594 $source = substr_replace($source, '', $start, $end - $start + 3); 586 595 } 587 $source = str_replace(array(" <![CDATA[", " <![CDATA[", "<![CDATA[", "]]>"), '', $source); 588 } 596 } 597 598 $patterns = array( 599 '/(\]\]\>)?\s*\<\/php\>\s*\<php type\=\"(instruction|echo)\"\>\s*(\<\!\[CDATA\[)?/i', 600 '/\<php type\=\"(instruction|echo)\"\>\s*(\<\!\[CDATA\[)?/i', '/(\]\]\>)?\s*\<\/php\>/i', 601 '/\[\?php\s/i', '/\?\]/i', 602 '/\<(.*)\>\s*\<\!\[CDATA\[(.*)\]\]\>\s*\<\/(.*)\>/i', 603 '/\<\!\[CDATA\[/i', '/\]\]\>/i' 604 ); 605 $replacements = array( 606 '', 607 '<?php ', '?>', 608 '<?php ', '?>', 609 '<$1>$2</$3>', 610 '', '' 611 ); 612 $source = preg_replace($patterns, $replacements, $source); 589 613 590 614 $source = ltrim($source, self::TOKEN_LINE); 591 615 $source = str_replace(array('%5B', '%20', '%24', '%5D'), array('[', ' ', '$', ']'), $source); 592 $source = str_replace(array('[?php', '?]'), array('<?php', '?>'), $source);593 616 594 617 foreach (self::$_closed_tags as $tag) plugins/sfHamlViewPlugin/branches/1.0/tests/unit/HamlParserTest.php
r5827 r5834 203 203 204 204 $h->setDoctype('XHTML 1.0 Strict'); 205 $t->is($h->parse('%h1= $test', 'php'), '<h1> '."\n".'<?php ob_start(); echo $test ; $content = ob_get_clean(); haml_echo($content, 0); ?>'."\n".'</h1>');205 $t->is($h->parse('%h1= $test', 'php'), '<h1><?php echo $test;?></h1>'); 206 206 207 207 $h->setDoctype('HTML 4.01 Strict'); 208 $t->is($h->parse('%h1= $test', 'php'), '<h1><?php echo $test; ?></h1>');208 $t->is($h->parse('%h1= $test', 'php'), '<h1><?php echo $test;?></h1>');