Skip to content

Core\Html

Makes HTML safe to put into a page. It escapes text, cleans untrusted HTML, writes attributes from a map with attributes, merges sets of them with mergeAttributes, and parses HTML documents.

Core\Html\Markup is the one type an HTTP request writes raw; every string written into a page is escaped. A Markup is made in three ways: an `html… template, whose text is trusted because it was written in the source and whose {$…} holes are escaped; Core\Html::escape, which escapes a string; and Core\Html::sanitize, which rebuilds an untrusted document with only its safe parts. + joins two Markup values and Core\Html::join joins a list; . on a Markup is a compile error. Core\Html::toSource is the only way back to a string, and it takes a written reason. Core\Html::fromSource turns HTML text that your program trusts into a Markup, such as a template file or a fragment it stored. It also takes a written reason, and a tainted text does not compile. Core\Html::parse reads any document onto a Core\Xml\Node` tree and never fails.

Core\Html::attributes writes a map of attributes for a tag. Every value is escaped and quoted, and an unsafe URL is left out. Core\Html::mergeAttributes merges several maps. A later value replaces an earlier one, and class and style are joined.

<?nvs
tainted string $name = '<script>alert(1)</script>';
string $bio = '<p>likes <b>tea</b></p><img src=x onerror=alert(1)>';
Core\Html\Markup $badge = html`<span class="badge">new</span>`;
Core\Html\Markup $line = html`<p>{$name} {$badge}</p>`;
Core\Html\Markup $safe = Core\Html::sanitize($bio);
echo $line + $safe, "\n";
echo Core\Html::join([$badge, $badge], html`, `), "\n";
echo Core\Html::toSource($badge, 'logging the fragment'), "\n";
var $attrs = Core\Html::mergeAttributes(['class' => 'btn', 'disabled' => true], ['class' => 'wide', 'disabled' => false]);
echo html`<button <?= Core\Html::attributes($attrs) ?>>Save</button>`, "\n";
echo html`<a <?= Core\Html::attributes(['href' => 'javascript:alert(1)', 'title' => 'A "quote"']) ?>>link</a>`, "\n";
Output
<p>&lt;script&gt;alert(1)&lt;/script&gt; <span class="badge">new</span></p><p>likes <b>tea</b></p><img src="x">
<span class="badge">new</span>, <span class="badge">new</span>
<span class="badge">new</span>
<button class="btn wide">Save</button>
<a title="A &quot;quote&quot;">link</a>

Members

9 members.

MemberSignature
escapeescape(string $text): Core\Html\Markup
attributesattributes(array<?string|int|uint|float|bool|array<?string>> $attributes, { allowAnyUrl?: bool }): Core\Html\Markup
mergeAttributesmergeAttributes(array<?string|int|uint|float|bool|array<?string>> ...$sets): array<?string|int|uint|float|bool|array<?string>>
joinjoin(array<Core\Html\Markup> $parts, Core\Html\Markup $separator): Core\Html\Markup
laterlater(callable(): mixed $fn, { placeholder?: Core\Html\Markup, error?: Core\Html\Markup, deadline?: Core\Time\Duration }): Core\Html\Markup
toSourcetoSource(Core\Html\Markup $markup, string $reason): string
fromSourcefromSource(string $source, string $reason): Core\Html\Markup
parseparse(string $document): Core\Xml\Node
sanitizesanitize(string $document): Core\Html\Markup

Example apps

These complete apps use this class. You can download and run each one.

  • Notes: A small notes app with SQLite, HTML pages, a JSON API, form checks and tests. Download it, run it and start your own app from it.