Commit c05ccc5d authored by Taylor Otwell's avatar Taylor Otwell

moved entities method to html class and added encoding configuration option.

parent a77b6a97
...@@ -29,6 +29,18 @@ return array( ...@@ -29,6 +29,18 @@ return array(
'language' => 'en', 'language' => 'en',
/*
|--------------------------------------------------------------------------
| Application Character Encoding
|--------------------------------------------------------------------------
|
| This default character encoding used by your application. This is the
| character encoding that will be used by the Str, Text, and Form classes.
|
*/
'encoding' => 'UTF-8',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Timezone | Application Timezone
......
...@@ -22,12 +22,12 @@ class Form { ...@@ -22,12 +22,12 @@ class Form {
$action = URL::to($action); $action = URL::to($action);
$attributes['action'] = $action; $attributes['action'] = HTML::entities($action);
$attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST'; $attributes['method'] = ($method == 'GET' or $method == 'POST') ? $method : 'POST';
if ( ! array_key_exists('accept-charset', $attributes)) if ( ! array_key_exists('accept-charset', $attributes))
{ {
$attributes['accept-charset'] = 'UTF-8'; $attributes['accept-charset'] = Config::get('application.encoding');
} }
$html = '<form'.HTML::attributes($attributes).'>'; $html = '<form'.HTML::attributes($attributes).'>';
...@@ -142,7 +142,7 @@ class Form { ...@@ -142,7 +142,7 @@ class Form {
*/ */
public static function button($value, $attributes = array()) public static function button($value, $attributes = array())
{ {
return '<button'.HTML::attributes($attributes).'>'.$value.'</button>'.PHP_EOL; return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
} }
/** /**
...@@ -221,7 +221,7 @@ class Form { ...@@ -221,7 +221,7 @@ class Form {
$attributes['cols'] = 50; $attributes['cols'] = 50;
} }
return '<textarea'.HTML::attributes($attributes).'>'.Str::entities($value).'</textarea>'.PHP_EOL; return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
} }
/** /**
...@@ -243,10 +243,10 @@ class Form { ...@@ -243,10 +243,10 @@ class Form {
{ {
$option_attributes = array(); $option_attributes = array();
$option_attributes['value'] = $value; $option_attributes['value'] = HTML::entities($value);
$option_attributes['selected'] = ($value == $selected) ? 'selected' : null; $option_attributes['selected'] = ($value == $selected) ? 'selected' : null;
$html_options[] = '<option'.HTML::attributes($option_attributes).'>'.$display.'</option>'; $html_options[] = '<option'.HTML::attributes($option_attributes).'>'.HTML::entities($display).'</option>';
} }
return '<select'.HTML::attributes($attributes).'>'.implode('', $html_options).'</select>'.PHP_EOL; return '<select'.HTML::attributes($attributes).'>'.implode('', $html_options).'</select>'.PHP_EOL;
......
...@@ -2,6 +2,17 @@ ...@@ -2,6 +2,17 @@
class HTML { class HTML {
/**
* Convert HTML characters to entities.
*
* @param string $value
* @return string
*/
public static function entities($value)
{
return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
}
/** /**
* Generate a JavaScript reference. * Generate a JavaScript reference.
* *
...@@ -10,7 +21,7 @@ class HTML { ...@@ -10,7 +21,7 @@ class HTML {
*/ */
public static function script($url) public static function script($url)
{ {
return '<script type="text/javascript" src="'.trim(URL::to($url), '.js').'.js"></script>'.PHP_EOL; return '<script type="text/javascript" src="'.trim(static::entities(URL::to($url)), '.js').'.js"></script>'.PHP_EOL;
} }
/** /**
...@@ -21,7 +32,7 @@ class HTML { ...@@ -21,7 +32,7 @@ class HTML {
*/ */
public static function style($url, $media = 'all') public static function style($url, $media = 'all')
{ {
return '<link href="'.trim(URL::to($url), '.css').'.css" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL; return '<link href="'.trim(static::entities(URL::to($url)), '.css').'.css" rel="stylesheet" type="text/css" media="'.$media.'" />'.PHP_EOL;
} }
/** /**
...@@ -35,7 +46,7 @@ class HTML { ...@@ -35,7 +46,7 @@ class HTML {
*/ */
public static function link($url, $title, $attributes = array(), $https = false) public static function link($url, $title, $attributes = array(), $https = false)
{ {
return '<a href="'.URL::to($url, $https).'"'.static::attributes($attributes).'>'.Str::entities($title).'</a>'; return '<a href="'.static::entities(URL::to($url, $https)).'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
} }
/** /**
...@@ -71,7 +82,7 @@ class HTML { ...@@ -71,7 +82,7 @@ class HTML {
$title = $email; $title = $email;
} }
return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.static::attributes($attributes).'>'.$title.'</a>'; return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
} }
/** /**
...@@ -95,8 +106,8 @@ class HTML { ...@@ -95,8 +106,8 @@ class HTML {
*/ */
public static function image($url, $alt = '', $attributes = array()) public static function image($url, $alt = '', $attributes = array())
{ {
$attributes['alt'] = Str::entities($alt); $attributes['alt'] = static::entities($alt);
return '<img src="'.URL::to($url).'"'.static::attributes($attributes).' />'; return '<img src="'.static::entities(URL::to($url)).'"'.static::attributes($attributes).' />';
} }
/** /**
...@@ -164,7 +175,7 @@ class HTML { ...@@ -164,7 +175,7 @@ class HTML {
foreach ($list as $key => $value) foreach ($list as $key => $value)
{ {
$html .= '<li>'.Str::entities($value).'</li>'; $html .= '<li>'.static::entities($value).'</li>';
} }
return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>'; return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
...@@ -184,7 +195,7 @@ class HTML { ...@@ -184,7 +195,7 @@ class HTML {
{ {
if ( ! is_null($value)) if ( ! is_null($value))
{ {
$html[] = $key.'="'.Str::entities($value).'"'; $html[] = $key.'="'.static::entities($value).'"';
} }
} }
......
...@@ -2,13 +2,6 @@ ...@@ -2,13 +2,6 @@
class Str { class Str {
/**
* The default encoding.
*
* @var string
*/
private static $encoding = 'UTF-8';
/** /**
* Convert HTML characters to entities. * Convert HTML characters to entities.
* *
...@@ -17,7 +10,7 @@ class Str { ...@@ -17,7 +10,7 @@ class Str {
*/ */
public static function entities($value) public static function entities($value)
{ {
return htmlentities($value, ENT_QUOTES, static::$encoding, false); return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
} }
/** /**
...@@ -28,7 +21,7 @@ class Str { ...@@ -28,7 +21,7 @@ class Str {
*/ */
public static function lower($value) public static function lower($value)
{ {
return function_exists('mb_strtolower') ? mb_strtolower($value, static::$encoding) : strtolower($value); return function_exists('mb_strtolower') ? mb_strtolower($value, Config::get('application.encoding')) : strtolower($value);
} }
/** /**
...@@ -39,7 +32,7 @@ class Str { ...@@ -39,7 +32,7 @@ class Str {
*/ */
public static function upper($value) public static function upper($value)
{ {
return function_exists('mb_strtoupper') ? mb_strtoupper($value, static::$encoding) : strtoupper($value); return function_exists('mb_strtoupper') ? mb_strtoupper($value, Config::get('application.encoding')) : strtoupper($value);
} }
/** /**
...@@ -50,7 +43,7 @@ class Str { ...@@ -50,7 +43,7 @@ class Str {
*/ */
public static function title($value) public static function title($value)
{ {
return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, static::$encoding) : ucwords(strtolower($value)); return (function_exists('mb_convert_case')) ? mb_convert_case($value, MB_CASE_TITLE, Config::get('application.encoding')) : ucwords(strtolower($value));
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment