add a static function that will initialize a new document as a basic HTML5 template
authorRobert Kaiser <kairo@kairo.at>
Sun, 23 Oct 2016 18:22:48 +0000 (20:22 +0200)
committerRobert Kaiser <kairo@kairo.at>
Sun, 23 Oct 2016 18:22:48 +0000 (20:22 +0200)
include/cbsm/util/document.php-class

index c267623a5f26831a85b21a281399d066cb576c42..b708d1cb3a32c779575016b40beb70d0f2587c18 100755 (executable)
@@ -27,11 +27,9 @@ class ExtendedDocument extends DOMDocument {
   //   CONSTRUCTOR
   //   construct a new DOM Document that uses our element definitions
   //
   //   CONSTRUCTOR
   //   construct a new DOM Document that uses our element definitions
   //
-  // private $xmheader
-  //   the default XML header
-  //
-  // private $xhtmldtype
-  //   the XHTML doctype to use by default
+  // static function initHTML5()
+  //   initialize as an HTML5 document and return references to its basic elements.
+  //     returns an associative array with the following elements: 'html', 'head', 'title', 'body'
   //
   // function appendElement($name, [$value])
   //   appends a DOMDocument::createElement() as a child of this document (see there for params)
   //
   // function appendElement($name, [$value])
   //   appends a DOMDocument::createElement() as a child of this document (see there for params)
@@ -205,6 +203,23 @@ class ExtendedDocument extends DOMDocument {
     $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
   }
 
     $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
   }
 
+  static function initHTML5() {
+    $doc = new ExtendedDocument();
+    $doc->loadHTML('<!DOCTYPE html><html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
+
+    // Created basic HTML document structure.
+    $root = $doc->getElementsByTagName('html')->item(0);
+    $head = $root->appendElement('head');
+    $title = $head->appendElement('title');
+    $body = $root->appendElement('body');
+
+    return array('document' => $doc,
+                 'html' => $root,
+                 'head' => $head,
+                 'title' => $title,
+                 'body' => $body);
+  }
+
   function appendElement($name, $value = '') {
     return $this->appendChild($this->createElement($name, $value));
   }
   function appendElement($name, $value = '') {
     return $this->appendChild($this->createElement($name, $value));
   }