add shorthand functions to set/add classes and set IDs on elements
[php-utility-classes.git] / classes / document.php-class
index 1f4d8a2c17af82857f4e06ae5a9138d1c2b20308..be9d1502ec1e58805329dc14b613f6fdce4f8b00 100755 (executable)
@@ -85,7 +85,7 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementTextArea() as a child of this document (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementSelect() as a child of this document (see there for params)
   //     returns the new child
   //
@@ -121,7 +121,7 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementJS() as a child of this document (see there for params)
   //     NO return value!
   //
   //   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
   //
   //   appends an ExtendedDocument::createElementJSFile() as a child of this document (see there for params)
   //     returns the new child
   //
@@ -175,9 +175,9 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
   //   and optionally id and value
   //
   //   returns an ExtendedElement that is an HTML <textarea> with the given name, columns, rows,
   //   and optionally id and value
   //
-  // public function createElementSelect($name, [$id], [$options], [$default])
+  // public function createElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
   //   returns an ExtendedElement that is an HTML <select> with the given name, and optionally id,
-  //   array of options (key => description) and key of the by-default selected entry
+  //   array of options (key => description) and key of the by-default selected entry (matched including type when strictmatch is true)
   //
   // public function createElementOption($key, $desc, [$selected])
   //   returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
   //
   // public function createElementOption($key, $desc, [$selected])
   //   returns an ExtendedElement that is an HTML <option> with the given key (value) and description (content)
@@ -189,8 +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 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
   //   returns an ExtendedElement that is an HTML <script> of JavaScript type linking to the file given by the URL
+  //     $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
 
   function __construct($version = '1.0', $encoding = 'utf-8') {
     // make sure the default DOMDocument constructor runs
@@ -271,8 +272,8 @@ class ExtendedDocument extends DOMDocument {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
   }
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->createElementOption($key, $desc, $selected));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->createElementOption($key, $desc, $selected));
@@ -292,14 +293,19 @@ class ExtendedDocument extends DOMDocument {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
   public function appendJSElement($jsdata) {
     $this->appendChild($this->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->createElementJSFile($jsdata));
+  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; }
   }
 
   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) {
   }
 
   public function appendXMLMarkup($xmldata, $parentNode = null) {
@@ -448,12 +454,12 @@ class ExtendedDocument extends DOMDocument {
     return $txtarea;
   }
 
     return $txtarea;
   }
 
-  public function createElementSelect($name, $id = null, $options = array(), $default = null) {
+  public function createElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
     $select = $this->createElement('select');
     $select->setAttribute('name', $name);
     if (!is_null($id)) { $select->setAttribute('id', $id); }
     foreach ($options as $key => $desc) {
     $select = $this->createElement('select');
     $select->setAttribute('name', $name);
     if (!is_null($id)) { $select->setAttribute('id', $id); }
     foreach ($options as $key => $desc) {
-      $select->appendElementOption($key, $desc, ($key == $default));
+      $select->appendElementOption($key, $desc, $strictmatch ? ($key === $default) : ($key == $default));
     }
     return $select;
   }
     }
     return $select;
   }
@@ -479,10 +485,16 @@ class ExtendedDocument extends DOMDocument {
     return $jselem;
   }
 
     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');
     $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;
   }
     $jselem->setAttribute('src', $jsURL);
     return $jselem;
   }
@@ -561,7 +573,7 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementTextArea() as a child of this element (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementSelect() as a child of this element (see there for params)
   //     returns the new child
   //
@@ -597,9 +609,19 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementJS() as a child of this element (see there for params)
   //     NO return value!
   //
   //   appends an ExtendedDocument::createElementJS() as a child of this element (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 element (see there for params)
   //     returns the new child
   //   appends an ExtendedDocument::createElementJSFile() as a child of this element (see there for params)
   //     returns the new child
+  //
+  // public function setClass($classname)
+  //   sets the 'class' attribute of the element to the given classname value
+  //
+  // public function addClass($classname)
+  //   adds the given classname value to the space-separated list in the 'class' attribute
+  //     returns the value of the 'class' attribute
+  //
+  // public function setID($elem_id)
+  //   sets the 'id' attribute of the element to the given elem_id value
 
   public function appendElement($name, $value = '') {
     return $this->appendChild($this->ownerDocument->createElement($name, $value));
 
   public function appendElement($name, $value = '') {
     return $this->appendChild($this->ownerDocument->createElement($name, $value));
@@ -655,8 +677,8 @@ class ExtendedElement extends DOMElement {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
@@ -682,8 +704,21 @@ class ExtendedElement extends DOMElement {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
+  }
+  public function setClass($classname) {
+    $this->setAttribute('class', $classname);
+  }
+  public function addClass($classname) {
+    $classval = $this->getAttribute('class');
+    if (strlen($classval)) { $classval .= ' '; }
+    $classval .= $classname;
+    $this->setClass($classval);
+    return $classval;
+  }
+  public function setID($elem_id) {
+    $this->setAttribute('id', $elem_id);
   }
 }
 
   }
 }
 
@@ -760,7 +795,7 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementTextArea() as a child of this fragment (see there for params)
   //     returns the new child
   //
-  // public function appendElementSelect($name, [$id], [$options], [$default])
+  // public function appendElementSelect($name, [$id], [$options], [$default], [$strictmatch])
   //   appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
   //     returns the new child
   //
   //   appends an ExtendedDocument::createElementSelect() as a child of this fragment (see there for params)
   //     returns the new child
   //
@@ -796,7 +831,7 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementJS() as a child of this fragment (see there for params)
   //     NO return value!
   //
   //   appends an ExtendedDocument::createElementJS() as a child of this fragment (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 fragment (see there for params)
   //     returns the new child
 
   //   appends an ExtendedDocument::createElementJSFile() as a child of this fragment (see there for params)
   //     returns the new child
 
@@ -854,8 +889,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
   public function appendTextArea($name, $columns, $rows, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementTextArea($name, $columns, $rows, $id, $value));
   }
-  public function appendElementSelect($name, $id = null, $options = array(), $default = null) {
-    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default));
+  public function appendElementSelect($name, $id = null, $options = array(), $default = null, $strictmatch = false) {
+    return $this->appendChild($this->ownerDocument->createElementSelect($name, $id, $options, $default, $strictmatch));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
   }
   public function appendElementOption($key, $desc, $selected = false) {
     return $this->appendChild($this->ownerDocument->createElementOption($key, $desc, $selected));
@@ -881,8 +916,8 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
   public function appendJSElement($jsdata) {
     $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
-  public function appendJSFile($jsdata) {
-    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata));
+  public function appendJSFile($jsdata, $defer = false, $async = false) {
+    return $this->appendChild($this->ownerDocument->createElementJSFile($jsdata, $defer, $async));
   }
 }
 ?>
   }
 }
 ?>