Development

Changeset 7120

You must first sign up to be able to contribute.

Changeset 7120

Show
Ignore:
Timestamp:
01/21/08 09:13:59 (10 months ago)
Author:
fabien
Message:

added a way to mark template value as being safe for output

  • new decorator class sfOutputEscaperSafe to mark a value as being safe for output
  • added sfOutputEscaper::markClassesAsSafe() and sfOutputEscaper::markClassAsSafe() to mark classes as safe for ouptut
  • added sfForm class (and all its children) as being safe by default
  • updated CRUD templates to just echo the form
  • added a third argument to sfComponent::setVar() to add a safe value
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/action/sfComponent.class.php

    r6509 r7120  
    237237   * Sets a variable for the template. 
    238238   * 
    239    * @param  string The variable name 
    240    * @param  mixed  The variable value 
    241    */ 
    242   public function setVar($name, $value) 
    243   { 
    244     $this->varHolder->set($name, $value); 
     239   * If you add a safe value, the variable won't be output escaped 
     240   * by symfony, so this is your responsability to ensure that the 
     241   * value is escaped properly. 
     242   * 
     243   * @param string  The variable name 
     244   * @param mixed   The variable value 
     245   * @param Boolean true if the value is safe for output (false by default) 
     246   */ 
     247  public function setVar($name, $value, $safe = false) 
     248  { 
     249    $this->varHolder->set($name, $safe ? new sfOutputEscaperSafe($value) : $value); 
    245250  } 
    246251 
  • branches/1.1/lib/plugins/sfPropelPlugin/data/generator/sfPropelCrud/default/template/templates/editSuccess.php

    r6882 r7120  
    1818    <tbody> 
    1919<?php if (isset($this->params['non_verbose_templates']) && $this->params['non_verbose_templates']): ?> 
    20       [?php echo $this->getAttributeHolder()->isEscaped() ? $form->render(ESC_RAW) : $form ?] 
     20      [?php echo $form ?] 
    2121<?php else: ?> 
    2222 
  • branches/1.1/lib/view/escaper/sfOutputEscaper.class.php

    r7109 r7120  
    3333   */ 
    3434  protected $escapingMethod; 
     35 
     36  static protected $safeClasses = array(); 
    3537 
    3638  /** 
     
    101103      if ($value instanceof sfOutputEscaper) 
    102104      { 
    103         // avoid double decoration when passing values from action template to component/partial 
     105        // avoid double decoration 
    104106        $copy = clone $value; 
    105107 
     
    108110        return $copy; 
    109111      } 
    110       elseif ($value instanceof Traversable) 
     112      else if ($value instanceof Traversable) 
    111113      { 
    112114        return new sfOutputEscaperIteratorDecorator($escapingMethod, $value); 
     115      } 
     116      else if ($value instanceof sfOutputEscaperSafe) 
     117      { 
     118        // do not escape objects marked as safe 
     119        // return the original object 
     120        return $value->getValue(); 
     121      } 
     122      else if (self::isClassMarkedAsSafe(get_class($value))) 
     123      { 
     124        // the class or one of its children is marked as safe 
     125        // return the unescaped object 
     126        return $value; 
    113127      } 
    114128      else 
     
    123137 
    124138  /** 
     139   * Returns true if the class if marked as safe. 
     140   * 
     141   * @param  string  A class name 
     142   * 
     143   * @return Boolean true if the class if safe, false otherwise 
     144   */ 
     145  static public function isClassMarkedAsSafe($class) 
     146  { 
     147    if (in_array($class, self::$safeClasses)) 
     148    { 
     149      return true; 
     150    } 
     151 
     152    foreach (self::$safeClasses as $safeClass) 
     153    { 
     154      if (is_subclass_of($class, $safeClass)) 
     155      { 
     156        return true; 
     157      } 
     158    } 
     159 
     160    return false; 
     161  } 
     162 
     163  /** 
     164   * Marks an array of classes (and all its children) as being safe for output. 
     165   * 
     166   * @param array An array of class names 
     167   */ 
     168  static public function markClassesAsSafe(array $classes) 
     169  { 
     170    self::$safeClasses = array_unique(array_merge(self::$safeClasses, $classes)); 
     171  } 
     172 
     173  /** 
     174   * Marks a class (and all its children) as being safe for output. 
     175   * 
     176   * @param string A class name 
     177   */ 
     178  static public function markClassAsSafe($class) 
     179  { 
     180    self::markClassesAsSafe(array($class)); 
     181  } 
     182 
     183  /** 
    125184   * Returns the raw value associated with this instance. 
    126185   * 
  • branches/1.1/lib/view/sfView.class.php

    r6176 r7120  
    119119    } 
    120120 
     121    sfOutputEscaper::markClassAsSafe('sfForm'); 
     122 
    121123    $this->attributeHolder = false === sfConfig::get('sf_escaping_method') ? new sfViewParameterHolder() : new sfEscapedViewParameterHolder(); 
    122124    $this->attributeHolder->initialize($this->dispatcher, array(), array( 
  • branches/1.1/test/unit/view/escaper/sfOutputEscaperTest.php

    r7109 r7120  
    1515require_once(dirname(__FILE__).'/../../../../lib/view/escaper/sfOutputEscaperObjectDecorator.class.php'); 
    1616require_once(dirname(__FILE__).'/../../../../lib/view/escaper/sfOutputEscaperIteratorDecorator.class.php'); 
     17require_once(dirname(__FILE__).'/../../../../lib/view/escaper/sfOutputEscaperSafe.class.php'); 
    1718 
    1819require_once(dirname(__FILE__).'/../../../../lib/plugins/sfCompat10Plugin/lib/helper/EscapingHelper.php'); 
     
    2122sfConfig::set('sf_charset', 'UTF-8'); 
    2223 
    23 $t = new lime_test(18, new lime_output_color()); 
     24$t = new lime_test(21, new lime_output_color()); 
    2425 
    2526class OutputEscaperTestClass 
     
    3839    return $o->getTitle(); 
    3940  } 
     41} 
     42 
     43class OutputEscaperTestClassChild extends OutputEscaperTestClass 
     44{ 
    4045} 
    4146 
     
    7782$t->isa_ok(sfOutputEscaper::escape('esc_entities', new DirectoryIterator('.')), 'sfOutputEscaperIteratorDecorator', '::escape() returns a sfOutputEscaperIteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface'); 
    7883 
     84$t->diag('::escape() does not escape object marked as being safe'); 
     85$t->isa_ok(sfOutputEscaper::escape('esc_entities', new sfOutputEscaperSafe(new OutputEscaperTestClass())), 'OutputEscaperTestClass', '::escape() returns the original value if it is marked as being safe'); 
     86 
     87sfOutputEscaper::markClassAsSafe('OutputEscaperTestClass'); 
     88$t->isa_ok(sfOutputEscaper::escape('esc_entities', new OutputEscaperTestClass()), 'OutputEscaperTestClass', '::escape() returns the original value if the object class is marked as being safe'); 
     89$t->isa_ok(sfOutputEscaper::escape('esc_entities', new OutputEscaperTestClassChild()), 'OutputEscaperTestClassChild', '::escape() returns the original value if one of the object parent class is marked as being safe'); 
     90 
    7991$t->diag('::escape() cannot escape resources'); 
    8092$fh = fopen(__FILE__, 'r');