<?php

/**
 * Goweb Sitemanager 5 Framework
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */

goweb::loadClass('Zend_View_Helper_Abstract');

/**
 * Helper escapes a string so it can be safely echo'ed out as Javascript
 *
 * @version    5.1.3
 * @category   Goweb
 * @package    Goweb_View
 * @subpackage Helper
 * @link       http://mavrick.id.au/programming/2008/ultimate-php-escape-string/
 * @author     I&D <dev@goweb.pt>, Pedro Pinto <ppinto@goweb.pt>
 * @copyright  2010 (c) Goweb (http://www.goweb.pt)
 */
class Goweb_View_Helper_EscapeJs extends Zend_View_Helper_Abstract{
    /**
     * Escapes a string so it can be safely echo'ed out as Javascript
     *
     * @param String $string
     * @return String
     */
    public function EscapeJs($string) {
        $string = html_entity_decode($string, null, CHARSET);
        $string = str_replace(array('\\', "'"), array("\\\\", "\\'"), $string);
        $string = preg_replace('#([\x00-\x1F])#e', '"\x" . sprintf("%02x", ord("\1"))', $string);
        return $string;
    }
}