class Components::SafeBuffer

Overview

SafeBuffer is the construction mechanism paired with SafeHTML the output contract. It is an accumulator with one load-bearing rule (proposal §3.2 point 3): appending a String always means text and is always HTML-escaped. There is no overload that takes a raw String and treats it as markup — that is exactly the "buffer that's sometimes raw" design the hardening proposal rejects, because it recreates the hole.

Structure comes from appending SafeHTML / Elements::HTMLElement / Elements::RawHTML values (already-safe markup, passed through verbatim), never from interpolating a String that happens to contain tags — buf << "<b>#{x}</b>" escapes the entire literal, producing a visible &lt;b&gt;... cosmetic bug the author sees and fixes. That is the intended failure mode (see docs/SAFE_HTML_V1.md).

Defined in:

components/safe/safe_buffer.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def <<(text : String) : self #

A plain String is TEXT. Always escaped. Deliberately the only overload that accepts String.


[View source]
def <<(safe : SafeHTML) : self #

Already-safe markup — passthrough, no re-escaping.


[View source]
def <<(el : Elements::HTMLElement) : self #

HTMLElement#render already escapes its own text children and attribute values internally (§3.1 of the proposal — this DSL is already safe-by-default), so its serialized form is trusted verbatim.


[View source]
def <<(raw_html : Elements::RawHTML) : self #

The explicit, existing "I mean it" raw wrapper — passthrough.


[View source]
def <<(component : Component) : self #

A nested Component renders through the same SafeHTML contract, so its output is trusted verbatim.


[View source]
def <<(other : Int | Float | Bool | Char) : self #

Scalars: escape their to_s form. Harmless for numbers/bools, and future-proofs against a scalar type whose to_s could ever contain markup-shaped characters.


[View source]
def to_safe_html : SafeHTML #

[View source]