require_once 'PEAR.php';
require_once 'HTML/Common.php';
// HTML/Page/Doctypes.php is required in _getDoctype()
+// HTML/Page/Namespaces.php is required in _getNamespace()
/**
* Base class for XHTML pages
* <code>
* // The initializing code can also be in in the form of an HTML
* // attr="value" string.
- * // Possible attributes are: charset, lineend, tab, doctype, language and cache
+ * // Possible attributes are: charset, mime, lineend, tab, doctype, namespace, language and cache
*
* $p = new HTML_Page(array (
*
* // for more details
* if ($error) {
* $p->setTitle("Error!");
- * $p->setBodyContent("<p>oops, we have an error: $error</p>");
+ * $p->setBodyContent("<p>Houston, we have a problem: $error</p>");
* $p->display();
* die;
* } // end error handling
*/
var $_metaTags = array( 'standard' => array ( 'Generator' => 'PEAR HTML_Page' ) );
+ /**
+ * Document mime type
+ *
+ * @var string
+ * @access private
+ */
+ var $_mime = 'text/html';
+
+ /**
+ * Document namespace
+ *
+ * @var string
+ * @access private
+ */
+ var $_namespace = '';
+
/**
* Array of linked scripts
*
*/
var $_scripts = array();
+ /**
+ * Array of scripts placed in the header
+ *
+ * @var array
+ * @access private
+ */
+ var $_script = array();
+
/**
* Array of linked scripts
*
* Possible attributes are:
* - general options:
* - "lineend" => "unix|win|mac" (Sets line ending style; defaults to unix.)
- * - "tab" => string (Sets line ending style; defaults to \t.)
+ * - "tab" => string (Sets line ending style; defaults to \t.)
* - "cache" => "false|true"
* - "charset" => charset string (Sets charset encoding; defaults to utf-8)
+ * - "mime" => mime encoding string (Sets document mime type; defaults to text/html)
* - XHTML specific:
- * - "doctype" => mixed (Sets XHTML doctype; defaults to XHTML 1.0 Transitional.)
+ * - "doctype" => string (Sets XHTML doctype; defaults to XHTML 1.0 Transitional.)
* - "language" => two letter language designation. (Defines global document language; defaults to "en".)
+ * - "namespace" => string (Sets document namespace; defaults to the W3C defined namespace.)
*
* @param mixed $attributes Associative array of table tag attributes
* or HTML attributes name="value" pairs
* @access public
*/
- function HTML_Page($attributes = "")
+ function HTML_Page($attributes = array())
{
$commonVersion = 1.7;
if (HTML_Common::apiVersion() < $commonVersion) {
$this->setLang($attributes['language']);
}
+ if (isset($attributes['mime'])) {
+ $this->setMimeEncoding($attributes['mime']);
+ }
+
+ if (isset($attributes['namespace'])) {
+ $this->setNamespace($attributes['namespace']);
+ }
+
+ if (isset($attributes['tab'])) {
+ $this->setTab($attributes['tab']);
+ }
+
if (isset($attributes['cache'])) {
$this->setCache($attributes['cache']);
}
*/
function _generateHead()
{
+ // close empty tags if XHTML
+ if ($this->_doctype['type'] == 'html'){
+ $tagEnd = '>';
+ } else {
+ $tagEnd = ' />';
+ }
// get line endings
$lnEnd = $this->_getLineEnd();
foreach ($this->_metaTags as $type => $tag) {
foreach ($tag as $name => $content) {
if ($type == 'http-equiv') {
- $strHtml .= $tab . "<meta http-equiv=\"$name\" content=\"$content\" />" . $lnEnd;
+ $strHtml .= $tab . "<meta http-equiv=\"$name\" content=\"$content\"" . $tagEnd . $lnEnd;
} elseif ($type == 'standard') {
- $strHtml .= $tab . "<meta name=\"$name\" content=\"$content\" />" . $lnEnd;
+ $strHtml .= $tab . "<meta name=\"$name\" content=\"$content\"" . $tagEnd . $lnEnd;
}
}
}
// Generate stylesheet links
foreach ($this->_styleSheets as $strStyleSheet) {
- $strHtml .= $tab . "<link rel=\"stylesheet\" href=\"$strStyleSheet\" type=\"text/css\" />" . $lnEnd;
+ $strHtml .= $tab . "<link rel=\"stylesheet\" href=\"$strStyleSheet\" type=\"text/css\"" . $tagEnd . $lnEnd;
}
// Generate stylesheet declarations
foreach ($this->_style as $type => $content) {
$strHtml .= $tab . '<style type="' . $type . '">' . $lnEnd;
- $strHtml .= $tab . $tab . '<!--' . $lnEnd;
+
+ // This is for full XHTML supporte.
+ if ($this->_mime == 'text/html' ) {
+ $strHtml .= $tab . $tab . '<!--' . $lnEnd;
+ } else {
+ $strHtml .= $tab . $tab . '<![CDATA[' . $lnEnd;
+ }
+
if (is_object($content)) {
// first let's propagate line endings and tabs for other HTML_Common-based objects
if (method_exists($content, "toString")) {
$strHtml .= $content->toString() . $lnEnd;
} else {
- return PEAR::raiseError('Error: Body content object does not support method toString().',
+ return PEAR::raiseError('Error: Style content object does not support method toString().',
0,PEAR_ERROR_TRIGGER);
}
} else {
$strHtml .= $content . $lnEnd;
}
- $strHtml .= $tab . $spacer . "-->" . $lnEnd;
- $strHtml .= $tab . "</style>" . $lnEnd;
+
+ // See above note
+
+ if ($this->_mime == 'text/html' ) {
+ $strHtml .= $tab . $tab . '-->' . $lnEnd;
+ } else {
+ $strHtml .= $tab . $tab . ']]>' . $lnEnd;
+ }
+ $strHtml .= $tab . '</style>' . $lnEnd;
}
// Generate script file links
$strHtml .= $tab . "<script type=\"$strType\" src=\"$strSrc\"></script>" . $lnEnd;
}
+ // Generate script declarations
+ foreach ($this->_script as $type => $content) {
+ $strHtml .= $tab . '<script type="' . $type . '">' . $lnEnd;
+
+ // This is for full XHTML support.
+ if ($this->_mime == 'text/html' ) {
+ $strHtml .= $tab . $tab . '<!--' . $lnEnd;
+ } else {
+ $strHtml .= $tab . $tab . '<![CDATA[' . $lnEnd;
+ }
+
+ if (is_object($content)) {
+
+ // first let's propagate line endings and tabs for other HTML_Common-based objects
+ if (is_subclass_of($content, "html_common")) {
+ $content->setTab($tab);
+ $content->setTabOffset(3);
+ $content->setLineEnd($lnEnd);
+ }
+
+ // now let's get a string from the object
+ if (method_exists($content, "toString")) {
+ $strHtml .= $content->toString() . $lnEnd;
+ } else {
+ return PEAR::raiseError('Error: Script content object does not support method toString().',
+ 0,PEAR_ERROR_TRIGGER);
+ }
+
+ } else {
+ $strHtml .= $content . $lnEnd;
+ }
+
+ // See above note
+ if ($this->_mime == 'text/html' ) {
+ $strHtml .= $tab . $tab . '-->' . $lnEnd;
+ } else {
+ $strHtml .= $tab . $tab . ']]>' . $lnEnd;
+ }
+ $strHtml .= $tab . '</script>' . $lnEnd;
+ }
+
// Close tag
$strHtml .= '</head>' . $lnEnd;
/**
* Returns the doctype declaration
*
- * @return string
+ * @return mixed
* @access private
*/
function _getDoctype()
{
require('HTML/Page/Doctypes.php');
- $type = $this->_doctype['type'];
- $version = $this->_doctype['version'];
- $variant = $this->_doctype['variant'];
+ if (isset($this->_doctype['type'])) {
+ $type = $this->_doctype['type'];
+ }
+
+ if (isset($this->_doctype['version'])) {
+ $version = $this->_doctype['version'];
+ }
+
+ if (isset($this->_doctype['variant'])) {
+ $variant = $this->_doctype['variant'];
+ }
$strDoctype = '';
- if ($variant != '') {
+ if (isset($variant)) {
if (isset($doctype[$type][$version][$variant][0])) {
foreach ( $doctype[$type][$version][$variant] as $string) {
$strDoctype .= $string.$this->_getLineEnd();
}
}
- } elseif ($version != '') {
+ } elseif (isset($version)) {
if (isset($doctype[$type][$version][0])) {
foreach ( $doctype[$type][$version] as $string) {
$strDoctype .= $string.$this->_getLineEnd();
$strDoctype = $this->_getDoctype();
}
}
- } elseif ($type != '') {
+ } elseif (isset($type)) {
if (isset($default[$type][0])){
$this->_doctype = $this->_parseDoctypeString($default[$type][0]);
$strDoctype = $this->_getDoctype();
} // end func _getDoctype
+ /**
+ * Retrieves the document namespace
+ *
+ * @return mixed
+ * @access private
+ */
+ function _getNamespace()
+ {
+ require('HTML/Page/Namespaces.php');
+
+ if (isset($this->_doctype['type'])) {
+ $type = $this->_doctype['type'];
+ }
+
+ if (isset($this->_doctype['version'])) {
+ $version = $this->_doctype['version'];
+ }
+
+ if (isset($this->_doctype['variant'])) {
+ $variant = $this->_doctype['variant'];
+ }
+
+ $strNamespace = '';
+
+ if (isset($variant)){
+ if (isset($namespace[$type][$version][$variant][0]) && is_string($namespace[$type][$version][$variant][0])) {
+ $strNamespace = $namespace[$type][$version][$variant][0];
+ } elseif (isset($namespace[$type][$version][0]) && is_string($namespace[$type][$version][0]) ) {
+ $strNamespace = $namespace[$type][$version][0];
+ } elseif (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) {
+ $strNamespace = $namespace[$type][0];
+ }
+ } elseif (isset($version)) {
+ if (isset($namespace[$type][$version][0]) && is_string($namespace[$type][$version][0]) ) {
+ $strNamespace = $namespace[$type][$version][0];
+ } elseif (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) {
+ $strNamespace = $namespace[$type][0];
+ }
+ } else {
+ if (isset($namespace[$type][0]) && is_string($namespace[$type][0]) ) {
+ $strNamespace = $namespace[$type][0];
+ }
+ }
+
+
+ if ($strNamespace) {
+ return $strNamespace;
+ } else {
+ return PEAR::raiseError('Error: "'.$this->getDoctypeString().'" does not have a default namespace. Use setNamespace() to define your namespace.',
+ 0,PEAR_ERROR_TRIGGER);
+ }
+
+ } // end func _getNamespace
+
/**
* Parses a doctype declaration like "XHTML 1.0 Strict" to an array
*
$split = explode(' ',strtolower($string));
$elements = count($split);
- $array = array('type'=>$split[0],'version'=>$split[1],'variant'=>$split[2]);
+ if (isset($split[2])){
+ $array = array('type'=>$split[0],'version'=>$split[1],'variant'=>$split[2]);
+ } elseif (isset($split[1])){
+ $array = array('type'=>$split[0],'version'=>$split[1]);
+ } else {
+ $array = array('type'=>$split[0]);
+ }
return $array;
} // end func _parseDoctypeString
/**
* Adds a linked script to the page
*
- * @param string $url URL to the linked style sheet
+ * @param string $url URL to the linked script
* @param string $type Type of script. Defaults to 'text/javascript'
* @access public
*/
$this->_scripts[$url] = $type;
} // end func addScript
+ /**
+ * Adds a script to the page.
+ * Content can be a string or an object with a toString method.
+ * Defaults to text/javascript.
+ *
+ * @access public
+ * @param mixed $content Script (may be passed as a reference)
+ * @param string $type Scripting mime (defaults to 'text/javascript')
+ * @return void
+ */
+ function addScriptDeclaration($content, $type = 'text/javascript')
+ {
+ $this->_script[strtolower($type)] =& $content;
+ } // end func addScriptDeclaration
+
/**
* Adds a linked stylesheet to the page
*
*/
function addStyleDeclaration($content, $type = 'text/css')
{
- $this->_style[$type] =& $content;
+ $this->_style[strtolower($type)] =& $content;
} // end func addStyleDeclaration
/**
}
} // end func setMetaData
+ /**
+ * Sets an http-equiv Content-Type meta tag
+ *
+ * @access public
+ * @returns void
+ */
+ function setMetaContentType()
+ {
+ $this->setMetaData('Content-Type', $this->_mime . '; charset=' . $this->_charset , true );
+ } // end func setMetaContentType
+
/**
* Easily sets or alters a refresh meta tag.
* If no $url is passed, "self" is presupposed, and the appropriate URL
$this->setMetaData("Refresh", "$time; url=$url", true);
} // end func setMetaRefresh
+ /**
+ * Sets the document MIME encoding that is sent to the browser.
+ *
+ * @param string $type
+ * @access public
+ * @returns void
+ */
+ function setMimeEncoding($type = 'text/html')
+ {
+ $this->_mime = strtolower($type);
+ } // end func setMimeEncoding
+
+ /**
+ * Sets the document namespace
+ *
+ * @param string $namespace Optional. W3C namespaces are used by default.
+ * @access public
+ * @returns void
+ */
+ function setNamespace($namespace = '')
+ {
+ if (isset($namespace)){
+ $this->_namespace = $namespace;
+ } else {
+ $this->_namespace = $this->_getNamespace();
+ }
+ } // end func setTitle
+
/**
* Sets the title of the page
*
// get the doctype declaration
$strDoctype = $this->_getDoctype();
- //
+ // This determines how the doctype is declared
if ($this->_simple) {
+
$strHtml = '<html>' . $lnEnd;
+
} elseif ($this->_doctype['type'] == 'xhtml') {
+
+ // get the namespace if not already set
+ if (!$this->_namespace){
+ $this->_namespace = $this->_getNamespace();
+ }
+
$strHtml = '<?xml version="1.0" encoding="' . $this->_charset . '"?>' . $lnEnd;
$strHtml .= $strDoctype . $lnEnd;
- $strHtml .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.$this->_language.'">';
+ $strHtml .= '<html xmlns="' . $this->_namespace . '" xml:lang="' . $this->_language . '">' . $lnEnd;
+
} else {
+
$strHtml = $strDoctype . $lnEnd;
$strHtml .= '<html>' . $lnEnd;
+
}
+
$strHtml .= $this->_generateHead();
$strHtml .= $this->_generateBody();
$strHtml .= '</html>';
}
// set character encoding
- header("Content-Type: text/html; charset=" . $this->_charset);
+ header('Content-Type: ' . $this->_mime . '; charset=' . $this->_charset);
$strHtml = $this->toHTML();
print $strHtml;