From 14014b8f766b1c6af7c67eb2b902144510d25e69 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Sun, 23 Oct 2016 20:22:48 +0200 Subject: [PATCH] add a static function that will initialize a new document as a basic HTML5 template --- include/cbsm/util/document.php-class | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/include/cbsm/util/document.php-class b/include/cbsm/util/document.php-class index c267623..b708d1c 100755 --- a/include/cbsm/util/document.php-class +++ b/include/cbsm/util/document.php-class @@ -27,11 +27,9 @@ class ExtendedDocument extends DOMDocument { // 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) @@ -205,6 +203,23 @@ class ExtendedDocument extends DOMDocument { $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment'); } + static function initHTML5() { + $doc = new ExtendedDocument(); + $doc->loadHTML(''); // 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)); } -- 2.43.0