add code for datetime-local input elements
[php-utility-classes.git] / classes / document.php-class
index 211ae44116cd35bd7ca3e74328591b94cad33576..c48415fd22e66aa26f09143a33c6e351d1639891 100755 (executable)
@@ -89,6 +89,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementInputTime() as a child of this document (see there for params)
   //     returns the new child
   //
+  // public function appendInputDateTime($name, [$id], [$min], [$max], [$value])
+  //   appends an ExtendedDocument::createElementInputDateTime() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // public function appendInputColor($name, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputColor() as a child of this document (see there for params)
   //     returns the new child
@@ -226,6 +230,10 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <input> of type 'time' with the given name,
   //   and optionally id, min, max, and value
   //
+  // public function createElementInputDateTime($name, [$id], [$min], [$max], [$value])
+  //   returns an ExtendedElement that is an HTML <input> of type 'datetime-local' with the given name,
+  //   and optionally id, min, max, and value
+  //
   // public function createElementInputColor($name, [$id], [$value])
   //   returns an ExtendedElement that is an HTML <input> of type 'color' with the given name,
   //   and optionally id and value
@@ -284,7 +292,7 @@ class ExtendedDocument extends DOMDocument {
 
   static function initHTML5($doc = null) {
     if (is_null($doc)) { $doc = new ExtendedDocument(); }
-    $doc->loadHTML5('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html></html>'); // this seems to be the only way to get the DOCTYPE set properly.
+    $doc->loadHTML5('<?xml version="1.0" encoding="utf-8"?>'."\n".'<!DOCTYPE html>'."\n".'<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);
@@ -302,11 +310,18 @@ class ExtendedDocument extends DOMDocument {
   public function loadHTML5($source) {
     // Do our own handling of DOMDocument error reporting so we can ignore "unknown tags" which are usually fine in HTML5.
     libxml_use_internal_errors(true);
-    if (!preg_match('/^\s*<?xml /', $source)) {
+    if (!preg_match('/^\s*<\?xml /', $source)) {
       // Add an XML declaration to force DOMDocument into UTF-8 mode.
       $source = '<?xml version="1.0" encoding="utf-8"?>'."\n".$source;
     }
     $result = $this->loadHTML($source);
+    // Set encoding directly a,d remove any processing node that isn't the first node
+    $this->encoding = 'utf-8';
+    foreach ($this->childNodes as $i => $child) {
+      if ($i && $child->nodeType == XML_PI_NODE) {
+        $this->removeChild($child);
+      }
+    }
     // Handle DOMDocument loading errors, throw away warnings on unknown tags as HTML5 allows all kinds.
     $errseverity = array(LIBXML_ERR_WARNING => 'Warning', LIBXML_ERR_ERROR => 'Error', LIBXML_ERR_FATAL => 'Fatal');
     foreach (libxml_get_errors() as $error) {
@@ -376,6 +391,9 @@ class ExtendedDocument extends DOMDocument {
   public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
     return $this->appendChild($this->createElementInputTime($name, $id, $min, $max, $value));
   }
+  public function appendInputDateTime($name, $id = null, $min = null, $max = null, $value = null) {
+    return $this->appendChild($this->createElementInputDateTime($name, $id, $min, $max, $value));
+  }
   public function appendInputColor($name, $id = null, $value = null) {
     return $this->appendChild($this->createElementInputColor($name, $id, $value));
   }
@@ -439,7 +457,7 @@ class ExtendedDocument extends DOMDocument {
     // Use loadHTML5() 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->loadHTML5('<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><body>'.$htmldata.'</body></html>');
+    $tmpdoc->loadHTML5('<?xml version="1.0" encoding="utf-8"?>'."\n".'<!DOCTYPE html>'."\n".'<html><body>'.$htmldata.'</body></html>');
     foreach ($tmpdoc->getElementsByTagName('body')->item(0)->childNodes as $child) {
       $parentNode->appendChild($this->importNode($child, true));
     }
@@ -606,6 +624,17 @@ class ExtendedDocument extends DOMDocument {
     return $timefield;
   }
 
+  public function createElementInputDateTime($name, $id = null, $min = null, $max = null, $value = null) {
+    $dtfield = $this->createElement('input');
+    $dtfield->setAttribute('type', 'datetime-local');
+    if (!is_null($id)) { $dtfield->setAttribute('id', $id); }
+    $dtfield->setAttribute('name', $name);
+    if (!is_null($min)) { $dtfield->setAttribute('min', $min); }
+    if (!is_null($max)) { $dtfield->setAttribute('max', $max); }
+    if (!is_null($value)) { $dtfield->setAttribute('value', str_replace(' ', 'T', $value, 1)); }
+    return $dtfield;
+  }
+
   public function createElementInputColor($name, $id = null, $value = null) {
     $colfield = $this->createElement('input');
     $colfield->setAttribute('type', 'color');
@@ -680,10 +709,12 @@ class ExtendedDocument extends DOMDocument {
 
   public function createElementOption($key, $desc, $selected = false) {
     $option = $this->createElement('option', $desc);
-    if ($key) {
+    if (is_numeric($key) || is_string($key)) {
       $option->setAttribute('value', $key);
     }
-    if ($selected) { $option->setAttribute('selected', ''); }
+    if ($selected) {
+      $option->setAttribute('selected', '');
+    }
     return $option;
   }
 
@@ -806,6 +837,10 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementInputTime() as a child of this element (see there for params)
   //     returns the new child
   //
+  // public function appendInputDateTime($name, [$id], [$min], [$max], [$value])
+  //   appends an ExtendedDocument::createElementInputDateTime() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // public function appendInputColor($name, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputColor() as a child of this element (see there for params)
   //
@@ -954,6 +989,9 @@ class ExtendedElement extends DOMElement {
   public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputTime($name, $id, $min, $max, $value));
   }
+  public function appendInputDateTime($name, $id = null, $min = null, $max = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputDateTime($name, $id, $min, $max, $value));
+  }
   public function appendInputColor($name, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputColor($name, $id, $value));
   }
@@ -1105,6 +1143,10 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementInputTime() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // public function appendInputDateTime($name, [$id], [$min], [$max], [$value])
+  //   appends an ExtendedDocument::createElementInputDateTime() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
   // public function appendInputColor($name, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputColor() as a child of this fragment (see there for params)
   //
@@ -1243,6 +1285,9 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendInputTime($name, $id = null, $min = null, $max = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputTime($name, $id, $min, $max, $value));
   }
+  public function appendInputDateTime($name, $id = null, $min = null, $max = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputDateTime($name, $id, $min, $max, $value));
+  }
   public function appendInputColor($name, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputColor($name, $id, $value));
   }