Core\Html::mergeAttributes
Core\Html::mergeAttributes(
array<?string|int|uint|float|bool|array<?string>> ...$sets,
): array<?string|int|uint|float|bool|array<?string>>Merges several maps of HTML attributes into one map.
Core\Html::mergeAttributes reads the maps in order. For most attributes, a later value replaces an earlier one. This is also true for false and null, so "disabled" => false removes disabled. The values of class and style are joined into one list. For these two, null adds nothing.
The result is a new map. Each attribute stays in the place where its name first appears. Pass the map to Core\Html::attributes to write it into a tag. That method also checks the attributes, and it throws an error on a name such as onclick.
Good to know: a map that contains a tainted value gives a tainted result. A tainted value came from outside the program, such as from a visitor.
Parameters
...$setsarray<?string|int|uint|float|bool|array<?string>>variadicThe maps to merge, in order. A laterfalseornullreplaces an earlier value, so"disabled" => falseremovesdisabled. Forclassandstyle,nulladds nothing.
Return value
Returns array<?string|int|uint|float|bool|array<?string>>. A new map. Each name is in the place where it first appears. class and style are lists. Pass the map to Core\Html::attributes to write it.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Html::mergeAttributes merges sets of attributes into one map. For each name, the value of the later set is used. class and style are joined, so a component can add its own classes to the classes of its caller. |
Examples
Default classes and the callers
<?nvs// Shows how a component adds the caller's attributes to its own default attributes.
class Card{ public static function write(string $text, array<string|int|uint|float|bool|null|array<?string>> $extra): Core\Html\Markup { array<string|int|uint|float|bool|null|array<?string>> $defaults = ['class' => 'card shadow', 'role' => 'note']; // The values of `class` are joined. The caller's `role` replaces the default `role`. var $attrs = Core\Html::mergeAttributes($defaults, $extra); return html`<div <?= Core\Html::attributes($attrs) ?>>{$text}</div>`; }}
// This prints `class="card shadow wide"` and the caller's `role` and `id`.echo Card::write('Welcome', ['class' => 'wide', 'role' => 'status', 'id' => 'hello']), "\n";
// With no extra attributes, the card has only its default attributes.echo Card::write('Plain', []), "\n";<div class="card shadow wide" role="status" id="hello">Welcome</div><div class="card shadow" role="note">Plain</div>The caller turns off disabled
<?nvs// Shows how a later map removes an attribute that an earlier map turned on.
array<string|int|uint|float|bool|null|array<?string>> $defaults = ['type' => 'submit', 'disabled' => true];
// `"disabled" => true` writes the bare name `disabled`.echo html`<button <?= Core\Html::attributes($defaults) ?>>Send</button>`, "\n";
// A later `false` replaces the earlier `true`, so `disabled` is not written.var $enabled = Core\Html::mergeAttributes($defaults, ['disabled' => false]);echo html`<button <?= Core\Html::attributes($enabled) ?>>Send</button>`, "\n";
// `null` also removes an attribute. Here `null` removes `type`.// `disabled` stays, because the later map does not change it.var $plain = Core\Html::mergeAttributes($defaults, ['type' => null]);echo html`<button <?= Core\Html::attributes($plain) ?>>Send</button>`, "\n";<button type="submit" disabled>Send</button><button type="submit">Send</button><button disabled>Send</button>A style from two lists
<?nvs// Shows how the `style` values of two maps are joined into one `style` attribute.
array<string|int|uint|float|bool|null|array<?string>> $base = ['style' => 'color: navy; margin: 0'];array<string|int|uint|float|bool|null|array<?string>> $extra = ['style' => ['font-weight: bold', null]];
// The result has a list for `style`. A `null` entry is left out.var $merged = Core\Html::mergeAttributes($base, $extra);
// `Core\Html::attributes` joins the list with `; `.// This prints `style="color: navy; margin: 0; font-weight: bold"`.echo html`<p <?= Core\Html::attributes($merged) ?>>Note</p>`, "\n";<p style="color: navy; margin: 0; font-weight: bold">Note</p>