replace workaround for HTML markup with a sane way of doing it, via loadHTML()
[php-utility-classes.git] / classes / document.php-class
index 41e817a4d96b678740e1fa0ca82591a886af529c..dbad7436beab02d0b313c7cbd9bf5fe9cb9960bc 100755 (executable)
@@ -1,23 +1,7 @@
 <?php
-/* ***** BEGIN LICENSE BLOCK *****
- *
- * The contents of this file are subject to Austrian copyright reegulations
- * ("Urheberrecht"); you may not use this file except in compliance with
- * those laws.
- * This contents and any derived work, if it gets distributed in any way,
- * is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
- * either express or implied.
- *
- * The Original Code is KaiRo's extended DOM document classes.
- *
- * The Initial Developer of the Original Code is
- * KaiRo - Robert Kaiser.
- * Portions created by the Initial Developer are Copyright (C) 2010
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s): Robert Kaiser <kairo@kairo.at>
- *
- * ***** END LICENSE BLOCK ***** */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 class ExtendedDocument extends DOMDocument {
   // ExtendedDocument PHP class
@@ -27,8 +11,9 @@ class ExtendedDocument extends DOMDocument {
   //   CONSTRUCTOR
   //   construct a new DOM Document that uses our element definitions
   //
-  // static function initHTML5()
+  // static function initHTML5([$doc])
   //   initialize as an HTML5 document and return references to its basic elements.
+  //     If a $doc is handed over (an ExtendedDocument or a derived class), load the content into that document.
   //     returns an associative array with the following elements: 'html', 'head', 'title', 'body'
   //
   // public function appendElement($name, [$value])
@@ -136,14 +121,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
   //     NO return value!
   //
-  // public function appendJSFile($jsURL)
+  // public function appendJSFile($jsURL, [$defer], [$async])
   //   appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
   //     returns the new child
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this document (see there for params)
-  //     returns the new child
-  //
   // public function createElementLink($target, [$value])
   //   returns an ExtendedElement that is an HTML <a> with the given target (href) and (optional) value
   //
@@ -208,12 +189,9 @@ class ExtendedDocument extends DOMDocument {
   // public function createElementJS($jsdata)
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
   //
-  // public function createElementJSFile($jsURL)
+  // public function createElementJSFile($jsURL, [$defer], [$async])
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
-  //
-  // public function createCOMElement($module, $attributes)
-  //   returns an ExtendedElement that is in COM_NS namespace, with the given module as name and the
-  //     given name=>value array as attributes
+  //     $defer and $async are boolean attributes that set if the script should be executed after parsing but before onload, and if it should be loaded asynchronously.
 
   function __construct($version = '1.0', $encoding = 'utf-8') {
     // make sure the default DOMDocument constructor runs
@@ -222,8 +200,8 @@ class ExtendedDocument extends DOMDocument {
     $this->registerNodeClass('DOMDocumentFragment', 'ExtendedDocumentFragment');
   }
 
-  static function initHTML5() {
-    $doc = new ExtendedDocument();
+  static function initHTML5($doc = null) {
+    if (is_null($doc)) { $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.
@@ -315,17 +293,19 @@ class ExtendedDocument extends DOMDocument {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->createElementJSFile($jsdata));
-  }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->createCOMElement($module, $attributes));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->createElementJSFile($jsdata, $defer, $async));
   }
 
   public function appendHTMLMarkup($htmldata, $parentNode = null) {
     if (is_null($parentNode)) { $parentNode =& $this; }
-    // XXX: just a workaround for now!
-    $parentNode->appendChild($this->createCDATASection($htmldata));
+    // Use loadHTML() to parse and turn the markup into proper HTML.
+    $tmpdoc = new ExtendedDocument;
+    // The XML line is needed to tell the parser that we need UTF-8 parsing.
+    $tmpdoc->loadHTML('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><body>'.$htmldata.'</body></html>');
+    foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
+      $parentNode->appendChild($this->importNode($child, true));
+    }
   }
 
   public function appendXMLMarkup($xmldata, $parentNode = null) {
@@ -505,23 +485,19 @@ class ExtendedDocument extends DOMDocument {
     return $jselem;
   }
 
-  public function createElementJSFile($jsURL) {
+  public function createElementJSFile($jsURL, $defer = false, $async = false) {
     $jselem = $this->createElement('script');
     // Note: type can/should be left out for HTML5.
     $jselem->setAttribute('type', 'text/javascript');
+    if ($defer) {
+      $jselem->setAttribute('defer', '');
+    }
+    if ($async) {
+      $jselem->setAttribute('async', '');
+    }
     $jselem->setAttribute('src', $jsURL);
     return $jselem;
   }
-
-  public function createCOMElement($module, $attributes) {
-    $com_elem = $this->createElementNS(COM_NS, $module);
-    if (is_array($attributes) && count($attributes)) {
-      foreach ($attributes as $name=>$value) {
-        $com_elem->setAttribute($name, $value);
-      }
-    }
-    return $com_elem;
-  }
 }
 
 class ExtendedElement extends DOMElement {
@@ -633,12 +609,8 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
   //     NO return value!
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
-  //     returns the new child
-  //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this element (see there for params)
+  // public function appendJSFile($jsURL, [$defer], [$async])
+  //   appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
   //     returns the new child
 
   public function appendElement($name, $value = '') {
@@ -722,11 +694,8 @@ class ExtendedElement extends DOMElement {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
-  }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
   }
 }
 
@@ -839,12 +808,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
   //     NO return value!
   //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
-  //     returns the new child
-  //
-  // public function appendCOMElement($module, $attributes)
-  //   appends an ExtendedDocument::createCOMElement() as a child of this fragment (see there for params)
+  // public function appendJSFile($jsURL, [$defer], [$async])
+  //   appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
   //     returns the new child
 
   public function appendElement($name, $value = '') {
@@ -928,11 +893,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
-  }
-  public function appendCOMElement($module, $attributes) {
-    return $this->appendChild($this->ownerDocument->createCOMElement($module, $attributes));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
   }
 }
 ?>