add functions to add a style element (should only be needed inside <head>)
[php-utility-classes.git] / classes / document.php-class
index 64acdbcbb798509278f96ad8c288480f22822e94..1933808002202d86d315fc2a553eedfb3d60122a 100755 (executable)
@@ -137,6 +137,11 @@ class ExtendedDocument extends DOMDocument {
   //   appends a DOMDocument::createComment() as a child of this document (see there for params)
   //     returns the new child
   //
+  // public function appendClonedElement($dom_element, [$deep])
+  //   appends a clone of the given DOMElement as a child of this document
+  //     the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
+  //     returns the new child
+  //
   // public function appendHTMLMarkup($htmldata, [$parentNode])
   //   appends a representation of the HTML data as children of the given parent node, by default this document
   //     NO return value!
@@ -145,9 +150,13 @@ class ExtendedDocument extends DOMDocument {
   //   appends a representation of the XML data as children of the given parent node, by default this document
   //     NO return value!
   //
+  // public function appendStyleElement($styledata)
+  //   appends an ExtendedDocument::createElementStyle() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // public function appendJSElement($jsdata)
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
-  //     NO return value!
+  //     returns the new child
   //
   // public function appendJSFile($jsURL, [$defer], [$async])
   //   appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
@@ -238,6 +247,9 @@ class ExtendedDocument extends DOMDocument {
   // public function createElementLabel($for_id, $value)
   //   returns an ExtendedElement that is an HTML <label> with the given 'for' and value
   //
+  // public function createElementStyle($styledata)
+  //   returns an ExtendedElement that is an HTML <style> of CSS type with the style data inside
+  //
   // public function createElementJS($jsdata)
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type with the JS data inside
   //
@@ -254,7 +266,7 @@ class ExtendedDocument extends DOMDocument {
 
   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.
+    $doc->loadHTML('<?xml version="1.0" encoding="utf-8"?><!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);
@@ -363,11 +375,17 @@ class ExtendedDocument extends DOMDocument {
   public function appendComment($comment_data) {
     return $this->appendChild($this->createComment($comment_data));
   }
+  public function appendClonedElement($dom_element, $deep = true) {
+    return $this->appendChild($dom_element->cloneNode($deep));
+  }
+  public function appendStyleElement($styledata) {
+    return $this->appendChild($this->createElementStyle($styledata));
+  }
   public function appendJSElement($jsdata) {
-    $this->appendChild($this->createElementJS($jsdata));
+    return $this->appendChild($this->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata, $defer = false, $async = false) {
-    return $this->appendChild($this->createElementJSFile($jsdata, $defer, $async));
+  public function appendJSFile($jsURL, $defer = false, $async = false) {
+    return $this->appendChild($this->createElementJSFile($jsURL, $defer, $async));
   }
 
   public function appendHTMLMarkup($htmldata, $parentNode = null) {
@@ -614,6 +632,14 @@ class ExtendedDocument extends DOMDocument {
     return $label;
   }
 
+  public function createElementStyle($styledata) {
+    $style_elem = $this->createElement('style');
+    // Note: type can/should be left out for HTML5.
+    $style_elem->setAttribute('type', 'text/css');
+    $style_elem->appendChild($this->createCDATASection($styledata));
+    return $style_elem;
+  }
+
   public function createElementJS($jsdata) {
     $jselem = $this->createElement('script');
     // Note: type can/should be left out for HTML5.
@@ -761,6 +787,11 @@ class ExtendedElement extends DOMElement {
   //   appends a DOMDocument::createComment() as a child of this element (see there for params)
   //     returns the new child
   //
+  // public function appendClonedElement($dom_element, [$deep])
+  //   appends a clone of the given DOMElement as a child of this element
+  //     the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
+  //     returns the new child
+  //
   // public function appendHTMLMarkup($htmldata)
   //   appends a representation of the HTML data as children of this element
   //     NO return value!
@@ -769,9 +800,13 @@ class ExtendedElement extends DOMElement {
   //   appends a representation of the XML data as children of this element
   //     NO return value!
   //
+  // public function appendStyleElement($styledata)
+  //   appends an ExtendedDocument::createElementStyle() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // public function appendJSElement($jsdata)
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
-  //     NO return value!
+  //     returns the new child
   //
   // public function appendJSFile($jsURL, [$defer], [$async])
   //   appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
@@ -880,17 +915,23 @@ class ExtendedElement extends DOMElement {
   public function appendComment($comment_data) {
     return $this->appendChild($this->ownerDocument->createComment($comment_data));
   }
+  public function appendClonedElement($dom_element, $deep = true) {
+    return $this->appendChild($dom_element->cloneNode($deep));
+  }
   public function appendHTMLMarkup($htmldata) {
     $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
   }
   public function appendXMLMarkup($xmldata) {
     $this->ownerDocument->appendXMLMarkup($xmldata, $this);
   }
+  public function appendStyleElement($styledata) {
+    return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
+  }
   public function appendJSElement($jsdata) {
-    $this->appendChild($this->ownerDocument->createElementJS($jsdata));
+    return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata, $defer = false, $async = false) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
+  public function appendJSFile($jsURL, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsURL, $defer, $async));
   }
   public function setClass($classname) {
     $this->setAttribute('class', $classname);
@@ -1031,6 +1072,11 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends a DOMDocument::createComment() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // public function appendClonedElement($dom_element, [$deep])
+  //   appends a clone of the given DOMElement as a child of this fragment
+  //     the boolean $deep specifies if the children are cloned as well (defaults to TRUE)
+  //     returns the new child
+  //
   // public function appendHTMLMarkup($htmldata)
   //   appends a representation of the HTML data as children of this fragment
   //     NO return value!
@@ -1039,9 +1085,13 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends a representation of the XML data as children of this fragment
   //     NO return value!
   //
+  // public function appendStyleElement($styledata)
+  //   appends an ExtendedDocument::createElementStyle() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // public function appendJSElement($jsdata)
   //   appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
-  //     NO return value!
+  //     returns the new child
   //
   // public function appendJSFile($jsURL, [$defer], [$async])
   //   appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
@@ -1140,17 +1190,23 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendComment($comment_data) {
     return $this->appendChild($this->ownerDocument->createComment($comment_data));
   }
+  public function appendClonedElement($dom_element, $deep = true) {
+    return $this->appendChild($dom_element->cloneNode($deep));
+  }
   public function appendHTMLMarkup($htmldata) {
     $this->ownerDocument->appendHTMLMarkup($htmldata, $this);
   }
   public function appendXMLMarkup($xmldata) {
     $this->ownerDocument->appendXMLMarkup($xmldata, $this);
   }
+  public function appendStyleElement($styledata) {
+    return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
+  }
   public function appendJSElement($jsdata) {
-    $this->appendChild($this->ownerDocument->createElementJS($jsdata));
+    return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata, $defer = false, $async = false) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
+  public function appendJSFile($jsURL, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsURL, $defer, $async));
   }
 }
 ?>