add support for datalist element
authorRobert Kaiser <kairo@kairo.at>
Sun, 26 Feb 2023 01:41:20 +0000 (02:41 +0100)
committerRobert Kaiser <kairo@kairo.at>
Sun, 26 Feb 2023 01:41:20 +0000 (02:41 +0100)
classes/document.php-class

index ed688b6e38b3c7dd50b38f41e68bc9ebdfeae3cf..4490589210b157a29f6442c8ff8a1ce0c386d281 100755 (executable)
@@ -125,6 +125,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
@@ -250,6 +254,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
   //
@@ -388,6 +395,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));
   }
@@ -648,7 +658,9 @@ class ExtendedDocument extends DOMDocument {
 
   public function createElementOption($key, $desc, $selected = false) {
     $option = $this->createElement('option', $desc);
-    $option->setAttribute('value', $key);
+    if ($key) {
+      $option->setAttribute('value', $key);
+    }
     if ($selected) { $option->setAttribute('selected', ''); }
     return $option;
   }
@@ -659,6 +671,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.
@@ -798,6 +819,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
@@ -930,8 +955,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'));
@@ -1083,6 +1111,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
@@ -1205,6 +1237,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));
   }