Skip to content

Core\Html::fromSource

Turns HTML text that your program trusts into a Core\Html\Markup value.

Use it for HTML that is not written in your code as a text in quotes. Two examples are a mail template in a file of your project, or a piece of a page that you stored with Core\Html::toSource. The text is not checked or changed. If you call Core\Html::toSource and then fromSource, you get the same text.

The second argument is the reason: a short text that says why this HTML is safe. You write it in quotes, or as a const. A reason in a variable does not compile. An empty reason throws a LogicError.

Text from outside the program has the type tainted string. This is true for text from a visitor, a database, a cache or a queued job. A tainted text does not compile here. For HTML from a visitor, use Core\Html::sanitize. For HTML that your own program stored, call Core\Taint::assertTrusted first.

The examples below show a mail template from a file, a cached piece of a page, and a queued job that sends stored HTML.

Parameters

  • $sourcestringThe HTML text. A tainted string is not allowed. For HTML read back from a database, a cache or a session, call Core\Taint::assertTrusted first.
  • $reasonstringWhy this HTML is safe to put into a page, written for the next person who reads it. The program does not use the reason. An empty reason is not allowed.

Return value

Returns Core\Html\Markup. A Core\Html\Markup with exactly the bytes of $source. The text is not parsed, checked or changed. echo writes it into a page as it is, with no escaping. If you call toSource and then fromSource, you get the same text.

Errors

  • LogicError$reason is empty. Each call must say why the HTML is safe.

Examples

The examples below run with this configuration file.

nvs.toml
# The examples send mail through this block. `capture = true` keeps each message
# and sends nothing, so no mail leaves the computer.
[mail.shop]
capture = true
from = "shop@example.com"
from_name = "Example Shop"
# The program may send mail through the `shop` block.
[capabilities.mail]
send = ["shop"]

A mail template from a file

<?nvs
// Shows how to use a mail footer that is stored as HTML in a file of your project.
// `Core\IO::read` returns the file as a `string`. A file of your project is not `tainted`.
// `tainted` text comes from outside the program, such as from a visitor.
string $footer = Core\IO::read('templates/footer.html');
// `Core\Html::fromSource` returns the text as a `Core\Html\Markup` value.
// The second argument is the reason. It says why this HTML is safe.
Core\Html\Markup $trusted = Core\Html::fromSource($footer, 'the shop footer that our designers write');
// The html template escapes the name. The footer is a `Core\Html\Markup` value, so it is added exactly as it is.
tainted string $name = 'Anna <admin>';
Core\Html\Markup $body = html`<p>Welcome, {$name}!</p>{$trusted}`;
// This prints `<p>Welcome, Anna &lt;admin&gt;!</p><p class="footer">Example Shop, 1 Main Street</p>`.
echo $body, "\n";
Output
<p>Welcome, Anna &lt;admin&gt;!</p><p class="footer">Example Shop, 1 Main Street</p>

A cached piece of a page

<?nvs
// Shows how to keep a piece of a page in the cache and use it again later.
var $cache = Core\Cache::memory();
// The cache keeps plain values, such as a `string`. It cannot keep a `Core\Html\Markup` value.
// `Core\Html::toSource` returns the HTML as a `string`, and the cache keeps that.
tainted string $product = 'Mugs & Cups';
Core\Html\Markup $card = html`<li>{$product}</li>`;
$cache->put('card:12', Core\Html::toSource($card, 'keeping the product card in the cache'));
// A value from the cache is `tainted`: it came from outside this request.
// `Core\Taint::assertTrusted` returns it as a normal `string`. The second argument says who wrote it.
string $stored = Core\Taint::assertTrusted($cache->get('card:12') as string, 'only this program writes card keys');
// `Core\Html::fromSource` returns a `Core\Html\Markup` value with the same text.
Core\Html\Markup $again = Core\Html::fromSource($stored, 'the product card that this program made above');
// The text does not change, so `&amp;` stays `&amp;`. This prints `<li>Mugs &amp; Cups</li>`.
echo $again, "\n";
Output
<li>Mugs &amp; Cups</li>

A queued job that sends stored html

<?nvs
// Shows how a queued job sends an HTML mail that an editor wrote and the program stored.
// An editor wrote this newsletter on an admin page of the shop. The program saved it as text.
string $newsletter = '<h1>Spring sale</h1><p>All mugs cost <b>20% less</b> this week.</p>';
array<mixed> $job = ['email' => 'anna@example.com', 'html' => $newsletter];
// In a web app, a request saves the job with `Core\Queue::push('jobs/send-newsletter.nvs', { arguments: $job })`.
// A worker runs the job file later. Here `spawn script` runs the job file at once, with the same arguments.
var $run = spawn script 'jobs/send-newsletter.nvs' with(arguments: $job, output: 'capture');
// This prints `Newsletter sent to anna@example.com`.
echo (await $run)->output;
Output
Newsletter sent to anna@example.com