All pastes #13920 Raw Edit

dom.php

public php v1 · immutable
#13920 ·published 2005-06-10 22:56 UTC
rendered paste body
<?php/** * This is the DOM layer to generate XHTML. * Inspired by the Wikiwyg DOM layer. *//* I decided to differ from the Wikiwyg version due mostly to the differences of PHP and JavaScript, the biggest one being that creating associative arrays in PHP are more than just a pair of braces.*/$domDocument = null;$domElementStack = array();$domCurElement =& $domDocument;$domNS = array();/** Initializes the page. */function initPage() {  global $domDocument, $domElementStack, $domCurElement;  $domDocument = new DOMDocument('1.0', 'UTF-8');  $domElementStack[] =& $domDocument;  $domCurElement =& $domDocument;}/** Adds a namespace */function addNS($name, $url) {  global $domNS;  $domNS[$name] = $url;}/** Splits a name into the namespace and name. * You can use code like this: *  list($ns, $tag) = splitName($name); */function splitName($name) {  $parts = explode(':', $name, 2);  $value = array();  if (isset($parts[1])) {    $value[0] = $parts[0];    $value[1] = $parts[1];  } else {    $value[0] = false;    $value[1] = $parts[0];  }    return $value;}function dumpstack() {  global $domElementStack, $domCurElement;  echo 'Stack {';  foreach($domElementStack as $key => $elem) {    echo "\t";    var_dump($domElementStack[$key]->nodeName);  }  echo '} ';  echo 'Cur:'."\t";  var_dump($domCurElement->nodeName);}/** The basic element class */class Element {  private $name, $ns, $lname, $attrs = array();  private $element, $node;    function __construct() {    $args = func_get_args();    if (is_array($args[0])) $args = $args[0];        #Phase 1: parse arguments    reset($args);    list($key, $arg) = each($args);    $tagname = $arg;    while (list($key, $arg) = each($args)) {      $aname = $arg;      list($key, $arg) = each($args);      $avalue = $arg;      $this->attrs[$aname] = $avalue;    }        #phase 1.5: place temporary variables into object vars    $this->name = $tagname;    list($this->ns, $this->lname) = splitName($this->name);        #Phase 2: create, initialize, and add DOMElement object    global $domCurElement, $domDocument, $domElementStack;    echo '';    $this->node =& $domDocument->createElement($this->name);    dumpstack();    $this->element =& $domCurElement->appendChild(&$this->node);    $domElementStack[] =& $this->element;    $GLOBAL['domCurElement'] =& end($domElementStack);    dumpstack();            foreach ($this->attrs as $name => $value) {      list($ns, $lname) = splitName($name);      $domCurElement->setAttribute($name, $value);    }    echo 'construct: '.count($domElementStack).'';  }    static function end() {    global $domCurElement, $domDocument, $domElementStack;    array_pop($domElementStack);    $domCurElement =& end($domElementStack);    echo 'end: '.count($domElementStack).PHP_EOL;  }    static function &add() {    $args = func_get_args();    return new Element($args);    echo 'add: '.count($domElementStack).PHP_EOL;  }             	}function &Element() {  $args = func_get_args();  $tag =& new Element($args);  Element::end();  return $tag;}require_once('xhtml-1.0s.php');?>