ssrAttribute
Renders one attribute as markup with a leading space, or "" when the attribute is absent: the helper the compiler's server output calls for a dynamic attribute.
Renders one attribute as markup with a leading space, or "" when the attribute is absent: the
helper the compiler's server output calls for a dynamic attribute. It applies the client's value
rules and URL check, so server and client agree on what is written.
null,undefinedandfalsegive""(exceptfalseonaria-*,data-*,draggable,spellcheckandcontenteditable, which is written as"false");truewrites=""(or"true"for those same names). Other values are written asString(value), HTML-escaped.- URL attributes whose value would run script are dropped, with the check of
isUnsafeUrlAttribute(including the SVG animation attributesvalues,from,to,by). on*names are dropped: event handlers never reach markup.- Accessors are called (untracked) until a plain value remains.
Import#
ts
import { ssrAttribute } from "@shadow-js/web/server";
Signature#
ts
function ssrAttribute(name: string, value: unknown, isBoolean = false): string
Parameters#
| Name | Description |
|---|---|
name |
Attribute name, matching [A-Za-z_:][A-Za-z0-9_:.-]*. It is validated before anything else, so an invalid name always throws. |
value |
The value, or an accessor of it. |
isBoolean |
true for a presence attribute such as disabled: a truthy value writes the bare name, a falsy one writes nothing. Default: false. |
Returns#
Markup such as ' title="a&b"', or "".
Throws#
- TypeError
INVALID_ATTRIBUTE_NAMEfor an invalid name, andUNRECOGNIZED_INSERT_VALUEwhen the value is a symbol.
Examples#
ts
import { ssrAttribute } from "@shadow-js/web/server";
ssrAttribute("title", "Tom & Jerry"); // ' title="Tom & Jerry"'
ssrAttribute("disabled", true, true); // " disabled"
ssrAttribute("href", "javascript:alert(1)"); // ""