cvs: pear /HTML_Javascript Javascript.php package.xml

From: Date: Fri, 31 May 2002 20:32:14 +0000
Subject: cvs: pear /HTML_Javascript Javascript.php package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-4327@lists.php.net to get a copy of this message
tal Fri May 31 16:32:14 2002 EDT Added files: /pear/HTML_Javascript Javascript.php package.xml Log: Initial commit

Index: pear/HTML_Javascript/Javascript.php +++ pear/HTML_Javascript/Javascript.php <?php // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Tal Peer <tal@php.net> | // | | // +----------------------------------------------------------------------+ require_once('PEAR.php'); /** * A class for converting PHP variables into Javascript variables * * Usage: * * echo "<html><head><title>lala</title></head><body>"; * $js = new HTML_Javascript(); * echo $js->startScript(); * echo $js->convertString("Hello,\n My name is tal peer", 'foo', true); * echo $js->writeLine('foo',true); * $a = array('foo','bar','foobarism',1,98); * echo $js->convertArray($a, 'bar', true); * echo $js->writeLine('bar[0]', true); * echo $js->writeLine('bar[3]', true); * echo $js->endScript(); * echo "</body></html>"; * * @author Tal Peer <tal@php.net> * @package HTML * @version 0.9-dev * @access public */ class HTML_Javascript extends PEAR { /** * Used to determaine if a script has been started * * @var boolean $_started * @access private */ var $_started = false; /** * Constructor - creates a new HTML_Javascript object * * @access public */ function HTML_Javascript() { $this->PEAR(); } /** * Used to end the script (</script>) * * @return mixed PEAR_Error if no script has been started or the end tag for the script * @access public */ function endScript() { if ($this->_started) { $this->_started = false; return "</script>\n"; } else { return $this->raiseError('No script started'); } } /** * Used to terminate escape characters in strings, as javascript doesn't allow them * * @param string $str the string to be processed * @return mixed the processed string * @access public */ function escapeString($str) { $js_escape = array( "\r" => '\r', "\n" => '\n', "\t" => '\t', "'" => "\\'", '\\' => '\\\\' ); return strtr($str,$js_escape); } /** * Converts a PHP string into a JS string * * @access public * @param string $str the string to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted string */ function convertString($str, $varname, $global = false) { if (!$this->_started) { return $this->raiseError('No script started'); } $var = ''; $str = $this->escapeString($str); if($global) { $var = 'var '; } $var .= $varname.' = "'.$str.'"'; return $var."\n"; } /** * Converts a PHP variable into a JS variable * Note: you can safely provide strings or arrays as arguments for this function * * @access public * @param string $var the variable to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted variable */ function convertVar($var, $varname, $global = false) { if (!$this->_started) { return $this->raiseError('No script started'); } if (!(is_string($var) || is_array($var))) { $ret = ''; if ($global) { $ret = 'var '; } $ret .= $varname.' = '.$var; return $ret."\n"; } else { if (is_array($var)) { return $this->convertArray($var, $varname, $global); } elseif (is_string($var)) { return $this->convertString($var, $varname, $global); } else { $this->raiseError('Invalid variable'); } } } /** * Starts a new script * * @access public * @return mixed a PEAR_Error if a script has been already started or a string (HTML tag <script language="javascript">) */ function startScript() { if ($this->_started) { return $this->raiseError('Script already started'); } else { $this->_started = true; return "<script language=\"javascript\">\n"; } } /** * Converts a PHP array into a JS array * Note: support for multi-dimensional arrays is not yet implemented, DO NOT use them * * @access public * @param string $arr the array to convert * @param string $varname the variable name to declare * @param boolean $global if true, the JS var will be global * @return mixed a PEAR_Error if no script was started or the converted array */ function convertArray($arr, $varname, $global = false) { if (!$this->_started) { return $this->raiseError('No script started'); } $var = ''; if ($global) { $var = 'var '; } $var .= $varname.' = Array('; foreach ($arr as $key => $cell) { if ($key != 0) { $var .= ','; } if (is_string($cell)) { $cell = $this->escapeString($cell); $var .= '"'.$cell.'"'; } else { $var .= $cell; } } $var .= ')'; return $var."\n"; } /** * A wrapper for document.writeln * * @access public * @param string $str the string to output * @param boolean $var set to true if $str is a variable name * @return mixed PEAR_Error if no script was started or the processed string */ function write($str, $var = false) { if ($this->_started) { if ($var) { return 'document.writeln('.$str.')'."\n"; } else { return 'document.writeln("'.$str.'")'."\n"; } } else { return $this->raiseError('No script started'); } } /** * A wrapper for document.writeln with an addtional <br /> tag * * @access public * @param string $str the string to output * @param boolean $var set to true if $str is a variable name * @return mixed PEAR_Error if no script was started or the processed string */ function writeLine($str, $var = false) { if ($this->_started) { if ($var) { return 'document.writeln('.$str.'+"<br />")'."\n"; } else { return 'document.writeln("'.$str.'"+"<br />")'."\n"; } } else { return $this->raiseError('No script started'); } } /** * A wrapper for alert * * @access public * @param string $str the string to output * @param boolean $var set to true if $str is a variable name * @return mixed PEAR_Error if no script was started or the processed string */ function alert($str, $var = false) { if ($this->_started) { if ($var) { return 'alert('.$str.')'."\n"; } else { return 'alert("'.$str.'")'."\n"; } } else { return $this->raiseError('No script started'); } } } //TESTS echo "<html><head><title>lala</title></head><body>"; $js = new HTML_Javascript(); echo $js->startScript(); echo $js->convertString("Hello,\n My name is tal peer", 'foo', true); echo $js->writeLine('foo', true); $a = array('foo','bar','foobarism',1,98,$js); echo $js->convertArray($a, 'bar', true); echo $js->writeLine('bar', true); echo $js->endScript(); echo "</body></html>"; Index: pear/HTML_Javascript/package.xml +++ pear/HTML_Javascript/package.xml <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE package SYSTEM "../package.dtd"> <package version="0.9"> <name>HTML_Javascript</name> <summary>Provides methods to convert PHP variables into JS variables and basic JS operations.</summary> <description> HTML_Javascript provides two types of methods: * Methods for converting PHP variables into Javascript variables * Methods to perform some basic Javascript operations </description> <license>PHP License</license> <maintainers> <maintainer> <user>tal</user> <name>Tal Peer</name> <email>tal@php.net</email> <role>lead</role> </maintainer> </maintainers> <release> <version>0.9</version> <date>2002-05-31</date> <state>beta</state> <notes> First release. </notes> <filelist> <dir name="/" baseinstalldir="HTML"> <file role="php">Javascript.php</file> </dir> </filelist> </release> </package>
« previous php.pear.cvs (#4327) next »