make sure that values like '0' are valid in select options
[php-utility-classes.git] / classes / document.php-class
index d3dca7d7e66553df777c4160ab97b633f685553a..c2f0fcfe0d9107d7baf897774dd79bda2d2f44f7 100755 (executable)
@@ -16,6 +16,10 @@ class ExtendedDocument extends DOMDocument {
   //     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 loadHTML5($source) {
+  //   A version of loadHTML() - see DOMDocument documentation - that is made for loading HTML5 and not emitting warnings/errors for unknown elements.
+  //     returns true on success, false otherwise, just like loadHTML5.
+  //
   // public function appendElement($name, [$value])
   //   appends a DOMDocument::createElement() as a child of this document (see there for params)
   //     returns the new child
@@ -61,6 +65,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementInputRange() as a child of this document (see there for params)
   //     returns the new child
   //
+  // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputSearch() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputUrl() as a child of this document (see there for params)
   //     returns the new child
@@ -121,6 +129,10 @@ class ExtendedDocument extends DOMDocument {
   //   appends an ExtendedDocument::createElementLabel() as a child of this document (see there for params)
   //     returns the new child
   //
+  // public function appendElementDatalist([$id], [$options])
+  //   appends an ExtendedDocument::createElementDatalist() as a child of this document (see there for params)
+  //     returns the new child
+  //
   // public function appendText($text)
   //   appends a DOMDocument::createTextNode() as a child of this document (see there for params)
   //     returns the new child
@@ -150,9 +162,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)
@@ -165,8 +181,7 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <img> with the given src and alt attributes (set to '' by default)
   //
   // public function createElementForm($action, $method, $name)
-  //   returns an ExtendedElement that is an HTML <div> that is a child of an HTML <form>
-  //   with the given action, method, and name
+  //   returns an ExtendedElement that is an HTML <form> with the given action, method, and name
   //
   // public function createElementInputHidden($name, $value)
   //   returns an ExtendedElement that is an HTML <input> of type 'hidden' with the given name and value
@@ -191,6 +206,10 @@ class ExtendedDocument extends DOMDocument {
   //   returns an ExtendedElement that is an HTML <input> of type 'url' with the given name, maxlength, size,
   //   and optionally id and value
   //
+  // public function createElementInputSearch($name, $maxlength, $size, [$id], [$value])
+  //   returns an ExtendedElement that is an HTML <input> of type 'search' with the given name, maxlength, size,
+  //   and optionally id and value
+  //
   // public function createElementInputEmail($name, $maxlength, $size, [$id], [$value])
   //   returns an ExtendedElement that is an HTML <input> of type 'email' with the given name, maxlength, size,
   //   and optionally id and value
@@ -243,6 +262,12 @@ 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 createElementDatalist([$id], [$options])
+  //   returns an ExtendedElement that is an HTML <datalist> with optionally the given id and array of options (key => description)
+  //
+  // 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
   //
@@ -259,7 +284,7 @@ class ExtendedDocument extends DOMDocument {
 
   static function initHTML5($doc = null) {
     if (is_null($doc)) { $doc = new ExtendedDocument(); }
-    $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.
+    $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);
@@ -274,6 +299,35 @@ class ExtendedDocument extends DOMDocument {
                  'body' => $body);
   }
 
+  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)) {
+      // 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) {
+      // $error is a libXMLError, see https://www.php.net/manual/en/class.libxmlerror.php
+      // See http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors for error numbers
+      if ($error->code != 801) { // XML_HTML_UNKNOWN_TAG gets no output, should not exist for HTML5.
+        trigger_error($errseverity[$error->level].' loading HTML5: '.$error->message.' (code '.$error->code.'), line: '.$error->line, E_USER_WARNING);
+      }
+    }
+    libxml_clear_errors();
+    libxml_use_internal_errors(false);
+    return $result;
+  }
+
   public function appendElement($name, $value = '') {
     return $this->appendChild($this->createElement($name, $value));
   }
@@ -311,6 +365,9 @@ class ExtendedDocument extends DOMDocument {
   public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
     return $this->appendChild($this->createElementInputRange($name, $id, $min, $max, $step, $value));
   }
+  public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->createElementInputSearch($name, $maxlength, $size, $id, $value));
+  }
   public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->createElementInputUrl($name, $maxlength, $size, $id, $value));
   }
@@ -356,6 +413,9 @@ class ExtendedDocument extends DOMDocument {
   public function appendLabel($for_id, $value) {
     return $this->appendChild($this->createElementLabel($for_id, $value));
   }
+  public function appendElementDatalist($id = null, $options = array()) {
+    return $this->appendChild($this->createElementDatalist($id, $options));
+  }
   public function appendText($text) {
     return $this->appendChild($this->createTextNode($text));
   }
@@ -371,6 +431,9 @@ class ExtendedDocument extends DOMDocument {
   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) {
     return $this->appendChild($this->createElementJS($jsdata));
   }
@@ -380,10 +443,10 @@ class ExtendedDocument extends DOMDocument {
 
   public function appendHTMLMarkup($htmldata, $parentNode = null) {
     if (is_null($parentNode)) { $parentNode =& $this; }
-    // Use loadHTML() to parse and turn the markup into proper HTML.
+    // 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->loadHTML('<?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));
     }
@@ -404,7 +467,7 @@ class ExtendedDocument extends DOMDocument {
   public function createElement($name, $value = '') {
     // Adding the $value in DOMDocument's createElement does NOT escape it, so override it and use appendText to support that.
     $aelem = parent::createElement($name);
-    if (strlen($value)) { $aelem->appendText($value); }
+    if (strlen($value ?? '')) { $aelem->appendText($value); }
     return $aelem;
   }
 
@@ -484,6 +547,17 @@ class ExtendedDocument extends DOMDocument {
     return $rgfield;
   }
 
+  public function createElementInputSearch($name, $maxlength, $size, $id = null, $value = null) {
+    $urlfield = $this->createElement('input');
+    $urlfield->setAttribute('type', 'search');
+    if (!is_null($id)) { $urlfield->setAttribute('id', $id); }
+    $urlfield->setAttribute('name', $name);
+    $urlfield->setAttribute('maxlength', $maxlength);
+    $urlfield->setAttribute('size', $size);
+    if (!is_null($value)) { $urlfield->setAttribute('value', $value); }
+    return $urlfield;
+  }
+
   public function createElementInputUrl($name, $maxlength, $size, $id = null, $value = null) {
     $urlfield = $this->createElement('input');
     $urlfield->setAttribute('type', 'url');
@@ -518,23 +592,25 @@ class ExtendedDocument extends DOMDocument {
   }
 
   public function createElementInputDate($name, $id = null, $min = null, $max = null, $value = null) {
-    $telfield = $this->createElement('input');
-    $telfield->setAttribute('type', 'date');
-    if (!is_null($id)) { $telfield->setAttribute('id', $id); }
-    if (!is_null($min)) { $rgfield->setAttribute('min', $min); }
-    if (!is_null($max)) { $rgfield->setAttribute('max', $max); }
-    if (!is_null($value)) { $telfield->setAttribute('value', $value); }
-    return $telfield;
+    $dtfield = $this->createElement('input');
+    $dtfield->setAttribute('type', 'date');
+    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', $value); }
+    return $dtfield;
   }
 
   public function createElementInputTime($name, $id = null, $min = null, $max = null, $value = null) {
-    $telfield = $this->createElement('input');
-    $telfield->setAttribute('type', 'time');
-    if (!is_null($id)) { $telfield->setAttribute('id', $id); }
-    if (!is_null($min)) { $rgfield->setAttribute('min', $min); }
-    if (!is_null($max)) { $rgfield->setAttribute('max', $max); }
-    if (!is_null($value)) { $telfield->setAttribute('value', $value); }
-    return $telfield;
+    $timefield = $this->createElement('input');
+    $timefield->setAttribute('type', 'time');
+    if (!is_null($id)) { $timefield->setAttribute('id', $id); }
+    $timefield->setAttribute('name', $name);
+    if (!is_null($min)) { $timefield->setAttribute('min', $min); }
+    if (!is_null($max)) { $timefield->setAttribute('max', $max); }
+    if (!is_null($value)) { $timefield->setAttribute('value', $value); }
+    return $timefield;
   }
 
   public function createElementInputColor($name, $id = null, $value = null) {
@@ -611,8 +687,12 @@ class ExtendedDocument extends DOMDocument {
 
   public function createElementOption($key, $desc, $selected = false) {
     $option = $this->createElement('option', $desc);
-    $option->setAttribute('value', $key);
-    if ($selected) { $option->setAttribute('selected', ''); }
+    if (is_numeric($key) || is_string($key)) {
+      $option->setAttribute('value', $key);
+    }
+    if ($selected) {
+      $option->setAttribute('selected', '');
+    }
     return $option;
   }
 
@@ -622,6 +702,23 @@ class ExtendedDocument extends DOMDocument {
     return $label;
   }
 
+  public function createElementDatalist($id = null, $options = array()) {
+    $select = $this->createElement('datalist');
+    if (!is_null($id)) { $select->setAttribute('id', $id); }
+    foreach ($options as $key => $desc) {
+      $select->appendElementOption($key, $desc);
+    }
+    return $select;
+  }
+
+  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.
@@ -694,6 +791,10 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementInputRange() as a child of this element (see there for params)
   //     returns the new child
   //
+  // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputSearch() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputUrl() as a child of this element (see there for params)
   //     returns the new child
@@ -753,6 +854,10 @@ class ExtendedElement extends DOMElement {
   //   appends an ExtendedDocument::createElementLabel() as a child of this element (see there for params)
   //     returns the new child
   //
+  // public function appendElementDatalist([$id], [$options])
+  //   appends an ExtendedDocument::createElementDatalist() as a child of this element (see there for params)
+  //     returns the new child
+  //
   // public function appendText($text)
   //   appends a DOMDocument::createTextNode() as a child of this element (see there for params)
   //     returns the new child
@@ -782,6 +887,10 @@ 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)
   //     returns the new child
@@ -836,6 +945,9 @@ class ExtendedElement extends DOMElement {
   public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputRange($name, $id, $min, $max, $step, $value));
   }
+  public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputSearch($name, $maxlength, $size, $id, $value));
+  }
   public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputUrl($name, $maxlength, $size, $id, $value));
   }
@@ -881,8 +993,11 @@ class ExtendedElement extends DOMElement {
   public function appendLabel($for_id, $value) {
     return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
   }
+  public function appendElementDatalist($id = null, $options = array()) {
+    return $this->appendChild($this->ownerDocument->createElementDatalist($id, $options));
+  }
   public function appendText($text) {
-    return $this->appendChild($this->ownerDocument->createTextNode($text));
+    return $this->appendChild($this->ownerDocument->createTextNode($text ?? ''));
   }
   public function appendLinebreak() {
     return $this->appendChild($this->ownerDocument->createElement('br'));
@@ -902,6 +1017,9 @@ class ExtendedElement extends DOMElement {
   public function appendXMLMarkup($xmldata) {
     $this->ownerDocument->appendXMLMarkup($xmldata, $this);
   }
+  public function appendStyleElement($styledata) {
+    return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
+  }
   public function appendJSElement($jsdata) {
     return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }
@@ -972,6 +1090,10 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementInputRange() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // public function appendInputSearch($name, $maxlength, $size, [$id], [$value])
+  //   appends an ExtendedDocument::createElementInputSearch() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
   // public function appendInputUrl($name, $maxlength, $size, [$id], [$value])
   //   appends an ExtendedDocument::createElementInputUrl() as a child of this fragment (see there for params)
   //     returns the new child
@@ -1031,6 +1153,10 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   //   appends an ExtendedDocument::createElementLabel() as a child of this fragment (see there for params)
   //     returns the new child
   //
+  // public function appendElementDatalist([$id], [$options])
+  //   appends an ExtendedDocument::createElementDatalist() as a child of this fragment (see there for params)
+  //     returns the new child
+  //
   // public function appendText($text)
   //   appends a DOMDocument::createTextNode() as a child of this fragment (see there for params)
   //     returns the new child
@@ -1060,6 +1186,10 @@ 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)
   //     returns the new child
@@ -1104,6 +1234,9 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendInputRange($name, $id, $min, $max, $step = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputRange($name, $id, $min, $max, $step, $value));
   }
+  public function appendInputSearch($name, $maxlength, $size, $id = null, $value = null) {
+    return $this->appendChild($this->ownerDocument->createElementInputSearch($name, $maxlength, $size, $id, $value));
+  }
   public function appendInputUrl($name, $maxlength, $size, $id = null, $value = null) {
     return $this->appendChild($this->ownerDocument->createElementInputUrl($name, $maxlength, $size, $id, $value));
   }
@@ -1149,6 +1282,9 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendLabel($for_id, $value) {
     return $this->appendChild($this->ownerDocument->createElementLabel($for_id, $value));
   }
+  public function appendElementDatalist($id = null, $options = array()) {
+    return $this->appendChild($this->ownerDocument->createElementDatalist($id, $options));
+  }
   public function appendText($text) {
     return $this->appendChild($this->ownerDocument->createTextNode($text));
   }
@@ -1170,6 +1306,9 @@ class ExtendedDocumentFragment extends DOMDocumentFragment {
   public function appendXMLMarkup($xmldata) {
     $this->ownerDocument->appendXMLMarkup($xmldata, $this);
   }
+  public function appendStyleElement($styledata) {
+    return $this->appendChild($this->ownerDocument->createElementStyle($styledata));
+  }
   public function appendJSElement($jsdata) {
     return $this->appendChild($this->ownerDocument->createElementJS($jsdata));
   }