make sure that values like '0' are valid in select options master
authorRobert Kaiser <kairo@kairo.at>
Fri, 12 Apr 2024 23:33:47 +0000 (01:33 +0200)
committerRobert Kaiser <kairo@kairo.at>
Fri, 12 Apr 2024 23:33:47 +0000 (01:33 +0200)
classes/document.php-class
classes/useragent.php-class

index ed688b6e38b3c7dd50b38f41e68bc9ebdfeae3cf..c2f0fcfe0d9107d7baf897774dd79bda2d2f44f7 100755 (executable)
@@ -65,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
@@ -125,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
@@ -198,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
@@ -250,6 +262,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 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
   //
@@ -269,7 +284,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);
@@ -287,11 +302,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) {
@@ -343,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));
   }
@@ -388,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));
   }
@@ -418,7 +446,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));
     }
@@ -519,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');
@@ -648,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;
   }
 
@@ -659,6 +702,15 @@ 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.
@@ -739,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
@@ -798,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
@@ -885,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));
   }
@@ -930,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'));
@@ -1024,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
@@ -1083,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
@@ -1160,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));
   }
@@ -1205,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));
   }
index 00ceb3a7a1f56286ddf9c3a909c1bc1ecafcc3e8..190e539119befaa46f1f2b2c569170ba1111147a 100755 (executable)
@@ -1519,18 +1519,18 @@ class userAgent {
         elseif ($this->uadata['os'] == 'WinNT') { $this->uadata['os'] = 'Windows NT'; }
         elseif ($this->uadata['os'] == 'Win32') { $this->uadata['os'] = 'Windows (32bit)'; }
         elseif ($this->uadata['os'] == 'Win64') { $this->uadata['os'] = 'Windows (64bit)'; }
-        elseif (preg_match('/iPhone OS ([\d_]+)/i', $this->uadata['os'], $regs)) { $this->uadata['os'] = 'iOS '.str_replace('_', '.', $regs[1]); }
-        elseif (preg_match('/Mac ?OS ?X/i', $this->uadata['os'])) { $this->uadata['os'] = 'MacOS X'; }
-        elseif (preg_match('/Mac_P(ower|)PC/i', $this->uadata['os'])) { $this->uadata['os'] = 'MacOS'; }
+        elseif (preg_match('/iPhone OS ([\d_]+)/i', ($this->uadata['os'] ?? ''), $regs)) { $this->uadata['os'] = 'iOS '.str_replace('_', '.', $regs[1]); }
+        elseif (preg_match('/Mac ?OS ?X/i', ($this->uadata['os'] ?? ''))) { $this->uadata['os'] = 'MacOS X'; }
+        elseif (preg_match('/Mac_P(ower|)PC/i', ($this->uadata['os'] ?? ''))) { $this->uadata['os'] = 'MacOS'; }
         elseif (strpos($this->uadata['os'] ?? '', 'darwin') !== false) { $this->uadata['os'] = 'MacOS X'; }
         elseif (strpos($this->uadata['os'] ?? '', 'Darwin') !== false) { $this->uadata['os'] = 'MacOS X'; }
         elseif (strpos($this->uadata['os'] ?? '', 'apple') !== false) { $this->uadata['os'] = 'MacOS'; }
         elseif (strpos($this->uadata['os'] ?? '', 'Macintosh') !== false) { $this->uadata['os'] = 'MacOS'; }
-        elseif (preg_match('/(?:web|hpw)OS\/([0-9a-zA-Z\._+]+)/i', $this->uadata['os'], $regs)) { $this->uadata['os'] = 'webOS '.$regs[1]; }
-        elseif (preg_match('/Android \(Linux (.+)\)/i', $this->uadata['os'], $regs)) { $this->uadata['os'] = 'Android '.$regs[1]; }
+        elseif (preg_match('/(?:web|hpw)OS\/([0-9a-zA-Z\._+]+)/i', ($this->uadata['os'] ?? ''), $regs)) { $this->uadata['os'] = 'webOS '.$regs[1]; }
+        elseif (preg_match('/Android \(Linux (.+)\)/i', ($this->uadata['os'] ?? ''), $regs)) { $this->uadata['os'] = 'Android '.$regs[1]; }
         elseif (strpos($this->uadata['os'] ?? '', 'linux') !== false) { $this->uadata['os'] = 'Linux'; }
         elseif (preg_match('/SymbianOS[\/ ]([0-9a-zA-Z\._+]+)/i', $this->uastring, $regs)) { $this->uadata['os'] = 'SymbianOS '.$regs[1]; }
-        elseif (preg_match('/Symbian ?OS/i', $this->uadata['os'])) { $this->uadata['os'] = 'SymbianOS'; }
+        elseif (preg_match('/Symbian ?OS/i', ($this->uadata['os'] ?? ''))) { $this->uadata['os'] = 'SymbianOS'; }
 
         if (strpos($this->uadata['os'] ?? '', 'Win') !== false) { $this->uadata['platform'] = 'Windows'; }
         elseif (strpos($this->uadata['os'] ?? '', 'Mac') !== false) { $this->uadata['platform'] = 'Macintosh'; }