Skip to content

Commit 320b509

Browse files
committed
Improve attribute value handling
Changed: - Replaced `array_map()` with `foreach` to maintain support for array keys while allowing easy exclusion of attributes. Fixed: - Superfluous spaces by ignoring NULL values. - Support for boolean values from callable values. - Support for numeric (integer and float) values not always rendering.
1 parent af13e66 commit 320b509

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

Function.HTML-Build-Attributes.php

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,58 @@ function html_build_attributes(array $attr, callable $callback = null)
1515
return '';
1616
}
1717

18-
$html = array_map(
19-
function ($val, $key) use ($callback) {
20-
if (is_bool($val)) {
21-
return ($val ? $key : '');
22-
} elseif (isset($val)) {
23-
if ($val instanceof Closure) {
24-
$val = $val();
25-
} elseif ($val instanceof JsonSerializable) {
26-
$val = json_encode(
27-
$val->jsonSerialize(),
28-
(JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
29-
);
30-
} elseif (is_callable([ $val, 'toArray' ])) {
31-
$val = $val->toArray();
32-
} elseif (is_callable([ $val, '__toString' ])) {
33-
$val = strval($val);
34-
}
18+
$html = [];
19+
foreach ($attr as $key => $val) {
20+
if (is_null($val)) {
21+
continue;
22+
}
3523

36-
if (is_array($val)) {
37-
if (function_exists('is_blank')) {
38-
$filter = function ($var) {
39-
return !is_blank($var);
40-
};
41-
} else {
42-
$filter = function ($var) {
43-
return !empty($var) || is_numeric($var);
44-
};
45-
}
46-
$val = implode(' ', array_filter($val, $filter));
47-
}
24+
if (is_object($val)) {
25+
if ($val instanceof Closure) {
26+
$val = $val();
27+
} elseif ($val instanceof JsonSerializable) {
28+
$val = json_encode(
29+
$val->jsonSerialize(),
30+
(JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE)
31+
);
32+
} elseif (is_callable([ $val, 'toArray' ])) {
33+
$val = $val->toArray();
34+
} elseif (is_callable([ $val, '__toString' ])) {
35+
$val = strval($val);
36+
}
37+
}
4838

49-
if (is_callable($callback)) {
50-
$val = call_user_func($callback, $val);
51-
} elseif (function_exists('esc_attr')) {
52-
$val = esc_attr($val);
53-
} else {
54-
$val = htmlspecialchars($val, ENT_QUOTES);
55-
}
39+
if (is_bool($val)) {
40+
$html[] = ($val ? $key : '');
41+
continue;
42+
}
5643

57-
if (is_string($val)) {
58-
return sprintf('%1$s="%2$s"', $key, $val);
59-
}
44+
if (is_array($val)) {
45+
if (function_exists('is_blank')) {
46+
$filter = function ($var) {
47+
return !is_blank($var);
48+
};
49+
} else {
50+
$filter = function ($var) {
51+
return !empty($var) || is_numeric($var);
52+
};
6053
}
61-
},
62-
$attr,
63-
array_keys($attr)
64-
);
54+
$val = implode(' ', array_filter($val, $filter));
55+
}
56+
57+
if (is_callable($callback)) {
58+
$val = call_user_func($callback, $val);
59+
} elseif (function_exists('esc_attr')) {
60+
$val = esc_attr($val);
61+
} else {
62+
$val = htmlspecialchars($val, ENT_QUOTES);
63+
}
64+
65+
if (is_string($val) || is_numeric($val)) {
66+
$html[] = sprintf('%1$s="%2$s"', $key, $val);
67+
continue;
68+
}
69+
}
6570

6671
return implode(' ', $html);
6772
}

0 commit comments

Comments
 (0)