module

Slang::Runtime

Runtime helpers used by codegen-emitted templates.

The codegen emits direct << writes for the literal-and-static common case. These helpers handle the dynamic cases where a runtime value must be inspected before being emitted (presence-checked, bool-aware attribute rendering, splat iteration).

Constants

ATTRIBUTE_NAME_RE = /\A[a-zA-Z_][a-zA-Z0-9_-]*\z/

Matches attribute names accepted via splat.

EVENT_HANDLER_RE = /\Aon[a-z]+\z/i

Matches event-handler attribute names (onclick, onmouseover, ...).

URL_ATTRIBUTE_NAMES = ["href", "src", "action", "formaction", "data", "cite", "poster", "manifest", "xlink:href", "background", "longdesc", "usemap"] of ::String

HTML attribute names whose values are URLs.

Instance methods

emit(io : IO, value : Ktistec::SafeHTML) : Nil

Emits a value to the buffer in HTML data context.

Ktistec::SafeHTML is emitted raw; Ktistec::SafeJSON is emitted raw; any other value has .to_s applied and is HTML-escaped.

Source
emit(io : IO, value : Ktistec::SafeJSON) : Nil

Emits a value to the buffer in HTML data context.

Ktistec::SafeHTML is emitted raw; Ktistec::SafeJSON is emitted raw; any other value has .to_s applied and is HTML-escaped.

Source
emit(io : IO, value) : Nil

Emits a value to the buffer in HTML data context.

Ktistec::SafeHTML is emitted raw; Ktistec::SafeJSON is emitted raw; any other value has .to_s applied and is HTML-escaped.

Source
emit_attr(io : IO, name : String, value : Ktistec::SafeAttrValue) : Nil

Emits a single attribute for an unwrapped name=expr source form.

  • Ktistec::SafeAttrValue -> emit name="<value.to_s>" raw
  • true -> emit name
  • false -> omit entirely
  • else -> emit name="<HTML-escaped value.to_s>"

Ktistec::SafeHTML is intentionally not admitted raw here: markup like <em>x</em> is safe in HTML data slots but would render as visible text inside an attribute (the type lattice treats the two destinations as distinct). It falls through to the HTML-escape branch below.

Source
emit_attr(io : IO, name : String, value) : Nil

Emits a single attribute for an unwrapped name=expr source form.

  • Ktistec::SafeAttrValue -> emit name="<value.to_s>" raw
  • true -> emit name
  • false -> omit entirely
  • else -> emit name="<HTML-escaped value.to_s>"

Ktistec::SafeHTML is intentionally not admitted raw here: markup like <em>x</em> is safe in HTML data slots but would render as visible text inside an attribute (the type lattice treats the two destinations as distinct). It falls through to the HTML-escape branch below.

Source
emit_class(io : IO, literal_prefix : String, *dynamics : String | Nil) : Nil

Emits a merged class="..." attribute combining a literal prefix and any number of dynamic class sources.

Source
emit_comment(io : IO, value) : Nil

Emits a value to the buffer in HTML comment context.

HTML-escapes (covers & < > " ') and breaks any -- run with a numeric character reference, preventing early-close of the surrounding <!-- ... -->. No type dispatch: Ktistec::SafeHTML is not admitted raw, because its safety claim is HTML-data context, not comment context (markup containing -- would break out).

Source
emit_splat_attrs(io : IO, hash, skip_class : Bool) : Nil

Iterates a splat hash, emitting each key/value pair as an attribute. nil values are skipped.

When skip_class is true, the class key is suppressed -- the caller has already routed its value through emit_class.

Source
emit_url_attr(io : IO, name : String, value : Ktistec::SafeURI | Nil) : Nil

Emits a single URL attribute (href, src, ...).

Only Ktistec::SafeURI? is admitted. Plain String (or any other type) at a URL-attribute callsite fails to compile. nil is silently skipped.

Source