mirror of
https://github.com/e107inc/e107.git
synced 2025-08-05 06:07:32 +02:00
Simplify e_form::get_attributes()
Reduce code duplication without changing behavior Introduce helper `e_form::attributes()` to generate HTML attributes
This commit is contained in:
@@ -848,7 +848,7 @@ class e_form
|
|||||||
|
|
||||||
|
|
||||||
//never allow id in format name-value for text fields
|
//never allow id in format name-value for text fields
|
||||||
return "<input type='".$type."' name='{$name}' value='{$value}' {$mlength} ".$this->get_attributes($options, $name). ' />';
|
return "<input type='".$type. "' name='" . $name . "' value='" . $value . "' " . $mlength . " " .$this->get_attributes($options, $name). ' />';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -3901,6 +3901,27 @@ var_dump($select_options);*/
|
|||||||
$this->_tabindex_counter = $reset;
|
$this->_tabindex_counter = $reset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build a series of HTML attributes from the provided array
|
||||||
|
*
|
||||||
|
* @param array $attributes Key-value pairs of HTML attributes. The value must not be HTML-encoded. If the value is
|
||||||
|
* boolean true, the value will be set to the key (e.g. `['required' => true]` becomes
|
||||||
|
* "required='required'").
|
||||||
|
* @return string The HTML attributes to concatenate inside an HTML tag
|
||||||
|
*/
|
||||||
|
private function attributes($attributes)
|
||||||
|
{
|
||||||
|
$stringifiedAttributes = [];
|
||||||
|
|
||||||
|
foreach ($attributes as $key => $value)
|
||||||
|
{
|
||||||
|
if ($value === true) $value = $key;
|
||||||
|
if (!empty($value)) $stringifiedAttributes[] = $key . "='" . htmlspecialchars($value) . "'";
|
||||||
|
}
|
||||||
|
|
||||||
|
return count($stringifiedAttributes) > 0 ? " ".implode(" ", $stringifiedAttributes) : "";
|
||||||
|
}
|
||||||
|
|
||||||
public function get_attributes($options, $name = '', $value = '')
|
public function get_attributes($options, $name = '', $value = '')
|
||||||
{
|
{
|
||||||
$ret = '';
|
$ret = '';
|
||||||
@@ -3909,7 +3930,7 @@ var_dump($select_options);*/
|
|||||||
{
|
{
|
||||||
if ($option !== 'other')
|
if ($option !== 'other')
|
||||||
{
|
{
|
||||||
$optval = htmlspecialchars(trim((string) $optval), ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
|
$optval = html_entity_decode(trim((string) $optval));
|
||||||
}
|
}
|
||||||
switch ($option)
|
switch ($option)
|
||||||
{
|
{
|
||||||
@@ -3919,30 +3940,30 @@ var_dump($select_options);*/
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case 'class':
|
case 'class':
|
||||||
if(!empty($optval))
|
|
||||||
{
|
|
||||||
$ret .= " class='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'size':
|
case 'size':
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " size='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'title':
|
case 'title':
|
||||||
if($optval)
|
case 'label':
|
||||||
{
|
case 'maxlength':
|
||||||
$ret .= " title='{$optval}'";
|
case 'wrap':
|
||||||
}
|
case 'autocomplete':
|
||||||
|
case 'pattern':
|
||||||
|
$ret .= $this->attributes([$option => $optval]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'label':
|
case 'readonly':
|
||||||
if($optval)
|
case 'multiple':
|
||||||
{
|
case 'selected':
|
||||||
$ret .= " label='{$optval}'";
|
case 'checked':
|
||||||
|
case 'disabled':
|
||||||
|
case 'required':
|
||||||
|
case 'autofocus':
|
||||||
|
$ret .= $this->attributes([$option => (bool) $optval]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'placeholder':
|
||||||
|
if($optval) {
|
||||||
|
$optval = deftrue($optval, $optval);
|
||||||
|
$ret .= $this->attributes([$option => $optval]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -3958,91 +3979,7 @@ var_dump($select_options);*/
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
++$this->_tabindex_counter;
|
++$this->_tabindex_counter;
|
||||||
$ret .= " tabindex='".$this->_tabindex_counter."'";
|
$ret .= $this->attributes([$option => $this->_tabindex_counter]);
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'readonly':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " readonly='readonly'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'multiple':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " multiple='multiple'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'selected':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " selected='selected'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'maxlength':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " maxlength='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'checked':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " checked='checked'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'disabled':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " disabled='disabled'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'required':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " required='required'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'autofocus':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " autofocus='autofocus'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'placeholder':
|
|
||||||
if($optval) {
|
|
||||||
$optval = deftrue($optval, $optval);
|
|
||||||
$ret .= " placeholder='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'wrap':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " wrap='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'autocomplete':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " autocomplete='{$optval}'";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'pattern':
|
|
||||||
if($optval)
|
|
||||||
{
|
|
||||||
$ret .= " pattern='{$optval}'";
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -4056,7 +3993,7 @@ var_dump($select_options);*/
|
|||||||
default:
|
default:
|
||||||
if(strpos($option,'data-') === 0)
|
if(strpos($option,'data-') === 0)
|
||||||
{
|
{
|
||||||
$ret .= ' ' .$option."='{$optval}'";
|
$ret .= $this->attributes([$option => $optval]);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user