Development

Changeset 7713

You must first sign up to be able to contribute.

Changeset 7713

Show
Ignore:
Timestamp:
03/02/08 14:28:53 (1 year ago)
Author:
fabien
Message:

enforced javascript and stylesheet positions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/plugins/sfCompat10Plugin/lib/helper/AssetHelper.php

    r5089 r7713  
    456456  $html = ''; 
    457457 
    458   foreach (array('first', '', 'last') as $position) 
     458  foreach ($response->getPositions() as $position) 
    459459  { 
    460460    foreach ($response->getJavascripts($position) as $files => $options) 
     
    505505  $html = ''; 
    506506 
    507   foreach (array('first', '', 'last') as $position) 
     507  foreach ($response->getPositions() as $position) 
    508508  { 
    509509    foreach ($response->getStylesheets($position) as $files => $options) 
  • branches/1.1/lib/response/sfWebResponse.class.php

    r6988 r7713  
    3030    $metas       = array(), 
    3131    $httpMetas   = array(), 
     32    $positions   = array('first', '', 'last'), 
    3233    $stylesheets = array(), 
    3334    $javascripts = array(), 
     
    4748  { 
    4849    parent::initialize($dispatcher, $parameters); 
     50 
     51    $this->javascripts = array_combine($this->positions, array_fill(0, count($this->positions), array())); 
     52    $this->stylesheets = array_combine($this->positions, array_fill(0, count($this->positions), array())); 
    4953 
    5054    $this->statusTexts = array( 
     
    538542 
    539543  /** 
     544   * Returns the available position names for stylesheets and javascripts in order. 
     545   * 
     546   * @return array An array of position names 
     547   */ 
     548  public function getPositions() 
     549  { 
     550    return $this->positions; 
     551  } 
     552 
     553  /** 
    540554   * Retrieves stylesheets for the current web response. 
    541555   * 
     
    550564      return $this->stylesheets; 
    551565    } 
     566 
     567    $this->validatePosition($position); 
    552568 
    553569    return isset($this->stylesheets[$position]) ? $this->stylesheets[$position] : array(); 
     
    563579  public function addStylesheet($css, $position = '', $options = array()) 
    564580  { 
    565     if (!isset($this->stylesheets[$position])) 
    566     { 
    567       $this->stylesheets[$position] = array(); 
    568     } 
     581    $this->validatePosition($position); 
    569582 
    570583    $this->stylesheets[$position][$css] = $options; 
     
    584597      return $this->javascripts; 
    585598    } 
     599 
     600    $this->validatePosition($position); 
    586601 
    587602    return isset($this->javascripts[$position]) ? $this->javascripts[$position] : array(); 
     
    597612  public function addJavascript($js, $position = '', $options = array()) 
    598613  { 
    599     if (!isset($this->javascripts[$position])) 
    600     { 
    601       $this->javascripts[$position] = array(); 
    602     } 
     614    $this->validatePosition($position); 
    603615 
    604616    $this->javascripts[$position][$js] = $options; 
     
    697709    list($this->content, $this->statusCode, $this->statusText, $this->parameterHolder, $this->cookies, $this->headerOnly, $this->headers, $this->metas, $this->httpMetas, $this->stylesheets, $this->javascripts, $this->slots) = $data; 
    698710  } 
     711 
     712  /** 
     713   * Validate a position name. 
     714   * 
     715   * @throws InvalidArgumentException if the position is not available 
     716   */ 
     717  protected function validatePosition($position) 
     718  { 
     719    if (!in_array($position, $this->positions, true)) 
     720    { 
     721      throw new InvalidArgumentException(sprintf('The position "%s" does not exist (available positions: %s).', $position, implode(', ', $this->positions))); 
     722    } 
     723  } 
    699724} 
  • branches/1.1/test/unit/response/sfWebResponseTest.php

    r6791 r7713  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(66, new lime_output_color()); 
     13$t = new lime_test(68, new lime_output_color()); 
    1414 
    1515class myWebResponse extends sfWebResponse 
     
    194194$t->is($stylesheets['bar'], array('media' => 'print'), '->addStylesheet() takes an array of parameters as its third argument'); 
    195195 
     196try 
     197{ 
     198  $response->addStylesheet('last', 'none'); 
     199  $t->fail('->addStylesheet() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 
     200} 
     201catch (InvalidArgumentException $e) 
     202{ 
     203  $t->pass('->addStylesheet() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 
     204} 
     205 
    196206// ->getStylesheets() 
    197207$t->diag('->getStylesheets()'); 
     
    212222$t->ok(array_key_exists('last_js', $response->getJavascripts('last')), '->addJavascript() takes a position as its second argument'); 
    213223 
     224try 
     225{ 
     226  $response->addJavascript('last_js', 'none'); 
     227  $t->fail('->addJavascript() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 
     228} 
     229catch (InvalidArgumentException $e) 
     230{ 
     231  $t->pass('->addJavascript() throws an InvalidArgumentException if the position is not first, the empty string, or last'); 
     232} 
     233 
    214234// ->getJavascripts() 
    215235$t->diag('->getJavascripts()'); 

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.