From 3bc791f34c4b71955b4a34d123c187958f29b7c2 Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Fri, 12 Sep 2008 23:15:52 +0000 Subject: typography tweaks --- system/helpers/typography_helper.php | 11 ++----- system/libraries/Typography.php | 57 +++++++++++------------------------- user_guide/libraries/typography.html | 26 +++++++++++++++- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/system/helpers/typography_helper.php b/system/helpers/typography_helper.php index 11acea1f7..5c5fffa80 100644 --- a/system/helpers/typography_helper.php +++ b/system/helpers/typography_helper.php @@ -60,16 +60,11 @@ if ( ! function_exists('nl2br_except_pre')) */ if ( ! function_exists('auto_typography')) { - function auto_typography($str, $allow_event_handlers = FALSE, $reduce_empty_lines = FALSE) + function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE) { - $CI =& get_instance(); - + $CI =& get_instance(); $CI->load->library('typography'); - - $CI->typography->allow_js_event_handlers($allow_event_handlers); - $CI->typography->reduce_empty_lines($reduce_empty_lines); - - return $CI->typography->auto_typography($str); + return $CI->typography->auto_typography($str, $strip_js_event_handlers, $reduce_linebreaks); } } diff --git a/system/libraries/Typography.php b/system/libraries/Typography.php index dd5f928d9..27604421d 100644 --- a/system/libraries/Typography.php +++ b/system/libraries/Typography.php @@ -35,12 +35,6 @@ class CI_Typography { // Tags we want the parser to completely ignore when splitting the string. var $ignore_elements = 'a|b|i|em|strong|span|img|li'; - // Whether to allow Javascript event handlers to be sumitted inside tags - var $allow_js_event_handlers = FALSE; - - // Whether to reduce more than two consecutive empty lines to a maximum of two - var $reduce_empty_lines = FALSE; - /** * Nothing to do here... * @@ -59,14 +53,20 @@ class CI_Typography { * - Converts three dots into ellipsis. * - Converts double dashes into em-dashes. * - Converts two spaces into entities + * + * @access public + * @param string + * @param bool whether to strip javascript event handlers for security + * @param bool whether to reduce more then two consecutive newlines to two + * @return string */ - function auto_typography($str) + function auto_typography($str, $strip_js_event_handlers = TRUE, $reduce_linebreaks = FALSE) { if ($str == '') { return ''; } - + // Standardize Newlines to make matching easier if (strpos($str, "\r") !== FALSE) { @@ -75,13 +75,13 @@ class CI_Typography { // Reduce line breaks. If there are more than two consecutive linebreaks // we'll compress them down to a maximum of two since there's no benefit to more. - if ($this->reduce_empty_lines == TRUE) + if ($reduce_linebreaks === TRUE) { $str = preg_replace("/\n\n+/", "\n\n", $str); } // Do we allow JavaScript event handlers? If not, we strip them from within all tags - if ($this->allow_js_event_handlers == FALSE) + if ($strip_js_event_handlers === TRUE) { $str = preg_replace("#<([^><]+?)([^a-z_\-]on\w*|xmlns)(\s*=\s*[^><]*)([><]*)#i", "<\\1\\4", $str); } @@ -140,7 +140,7 @@ class CI_Typography { } // Convert Newlines into

and
tags - $str .= $this->format_newlines($chunk); + $str .= $this->_format_newlines($chunk); } // Convert quotes, elipsis, and em-dashes @@ -168,7 +168,7 @@ class CI_Typography { ); // Do we need to reduce empty lines? - if ($this->reduce_empty_lines == TRUE) + if ($reduce_linebreaks === TRUE) { $table['#

\n*

#'] = ''; } @@ -191,6 +191,10 @@ class CI_Typography { * This function mainly converts double and single quotes * to curly entities, but it also converts em-dashes, * double spaces, and ampersands + * + * @access public + * @param string + * @return string */ function format_characters($str) { @@ -242,7 +246,7 @@ class CI_Typography { * Converts newline characters into either

tags or
* */ - function format_newlines($str) + function _format_newlines($str) { if ($str == '') { @@ -260,33 +264,6 @@ class CI_Typography { return '

'.$str.'

'; } - // -------------------------------------------------------------------- - - /** - * Allow JavaScript Event Handlers? - * - * For security reasons, by default we disallow JS event handlers - * - */ - function allow_js_event_handlers($val = FALSE) - { - $this->allow_js_event_handlers = ($val === FALSE) ? FALSE : TRUE; - } - - // -------------------------------------------------------------------- - - /** - * Reduce empty lines - * - * Sets a flag that tells the parser to reduce any instances of more than - * two consecutive linebreaks down to two - * - */ - function reduce_empty_lines($val = FALSE) - { - $this->reduce_empty_lines = ($val === FALSE) ? FALSE : TRUE; - } - // ------------------------------------------------------------------------ /** diff --git a/user_guide/libraries/typography.html b/user_guide/libraries/typography.html index 91b205bb2..359c590ce 100644 --- a/user_guide/libraries/typography.html +++ b/user_guide/libraries/typography.html @@ -89,7 +89,21 @@ the following formatting:

$string = $this->typography->auto_typography($string); -

Note: Typographic formatting can be processor intensive, particularly if you have a lot of content being formatted. +

Parameters

+ +

There are two optional parameters:

+ +
    +
  1. Strip JavaScript Event Handlers. Determines whether the parser should strip all JavaScript event handlers for security. Use bolean TRUE or FALSE.
  2. +
  3. Reduce Linebreaks. Determines whether the parser should reduce more then two consecutive linebreaks down to two. Use bolean TRUE or FALSE.
  4. +
+ +

By default the parser strips JS Event handlers and does not reduce line breaks. In other words, if no parameters are submitted, it is the same as doing this:

+ +$string = $this->typography->auto_typography($string, TRUE, FALSE); + + +

Note: Typographic formatting can be processor intensive, particularly if you have a lot of content being formatted. If you choose to use this function you may want to consider caching your pages.

@@ -97,9 +111,19 @@ If you choose to use this function you may want to consider

convert_characters()

+

This function is similiar to the auto_typography function above, except that it only does character conversion:

+ +

Usage example:

+$string = $this->typography->convert_characters($string);

nl2br_except_pre()

-- cgit v1.2.3-24-g4f1b