Development

Changeset 5834

You must first sign up to be able to contribute.

Changeset 5834

Show
Ignore:
Timestamp:
11/04/07 13:29:07 (1 year ago)
Author:
kupokomapa
Message:

[sfHamlViewPlugin] Improved the formatting of the resulting XHTML.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/sfHamlViewPlugin/branches/1.0/lib/HamlParser.class.php

    r5827 r5834  
    185185  private function _parseLine($line) 
    186186  { 
    187     $this->_current_line++
     187    $this->_current_line += 1
    188188    $indent = strspn($line, self::TOKEN_INDENT); 
    189189    $line = trim($line, self::TOKEN_INDENT); 
     
    257257          } 
    258258 
    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          { 
    260261            $value  = "[?php \$_haml_attr = '".htmlspecialchars(trim($value), ENT_QUOTES)."'; "; 
    261262            $value .= "echo eval('return '.stripcslashes(htmlspecialchars_decode(html_entity_decode(\$_haml_attr), ENT_QUOTES)).';');?]"; 
     
    290291          if (!empty($content))  
    291292          { 
    292             $content = $this->_createPHPEcho($content, $indent); 
     293            $content = $this->_createPHPEcho($content); 
    293294          } else { 
    294295            $this->_syntax_error('If you use the "=" operator you need to have content after it.'); 
     
    321322      } else if (isset($this->_parents[$key])) { 
    322323        $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        } 
    323331      } 
    324332      $key -= intval(self::INDENT); 
     
    373381    } 
    374382     
    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); 
    377386     
    378387    return $node; 
     
    384393    if (empty($content)) return false; 
    385394     
    386     $node = $this->_createPHPEcho($content, $indent); 
     395    $node = $this->_createPHPEcho($content); 
    387396    $parent = $this->_getParent($indent); 
    388397    if ($parent instanceof DOMCharacterData) 
     
    400409  } 
    401410   
    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     
    411415    return $this->_createTextNode($content); 
    412416  } 
    413417   
    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 
    417433    if ($tag == 'javascript') 
    418434    { 
     
    457473    } 
    458474     
    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; 
    465476  } 
    466477 
     
    483494    if ($parent instanceof DOMCharacterData) 
    484495    { 
    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); 
    489498      } 
    490499      else 
     
    585594        $source = substr_replace($source, '', $start, $end - $start + 3); 
    586595      } 
    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); 
    589613 
    590614    $source = ltrim($source, self::TOKEN_LINE); 
    591615    $source = str_replace(array('%5B', '%20', '%24', '%5D'), array('[', ' ', '$', ']'), $source); 
    592     $source = str_replace(array('[?php', '?]'), array('<?php', '?>'), $source); 
    593616 
    594617    foreach (self::$_closed_tags as $tag) 
  • plugins/sfHamlViewPlugin/branches/1.0/tests/unit/HamlParserTest.php

    r5827 r5834  
    203203 
    204204$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>'); 
    206206 
    207207$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>');