Guide

SVG, raw HTML and URLs

#SVG

Write SVG in the view as you would in HTML. Holes, conditions and <For> rows inside <svg> work as elsewhere, and the browser gets SVG elements:

import { For } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

interface Point {
  id: string;
  x: number;
  y: number;
}

export default function Chart(props: { points: Point[] }): JSX.Element {
  let points = props.points;

  function add() {
    points = [...points, { id: `p${points.length}`, x: points.length * 10, y: 50 }];
  }

  return (
    <svg viewBox="0 0 100 100" width="200" on:click={add}>
      <title>Points</title>
      <For each={points} key={(point) => point.id}>
        {(point) => <circle cx={point.x} cy={point.y} r="2" />}
      </For>
      <foreignObject x="0" y="0" width="100" height="20">
        <p>{points.length > 9 && <strong>Full</strong>}</p>
      </foreignObject>
    </svg>
  );
}

{
  "points": [
    {
      "id": "p0",
      "x": 0,
      "y": 80
    },
    {
      "id": "p1",
      "x": 10,
      "y": 40
    },
    {
      "id": "p2",
      "x": 20,
      "y": 60
    }
  ]
}

#Raw HTML

The html attribute sets the content of an element from a string of markup. Its value must be a trusted(...) call, with trusted imported from idlip:

import { trusted } from 'idlip';
import type { JSX } from 'idlip/jsx-runtime';

export default function Post(props: { body: string }): JSX.Element {
  let body = props.body;

  function clear() {
    body = '';
  }

  return (
    <article>
      <div html={trusted(body)} />
      <button on:click={clear}>Clear</button>
    </article>
  );
}

{
  "body": "<p>A post with <strong>bold</strong> text.</p>"
}

trusted does not sanitize or escape anything. The call marks the place where you vouch for the markup, so pass only markup you wrote or cleaned with a sanitizer. When body changes, the element's content is replaced.

The compiler checks the use:

Rule id Cause
html-trusted html={value} where the value is not a call of the trusted export of idlip.
html-children An element with html that also has children.
html-element html on <script>, <style>, <textarea>, <title> or <noscript>.
srcdoc-trusted A bound srcdoc that is not trusted(...). A literal srcdoc is allowed.

trusted may be imported under another name. A local function named trusted does not count.

The page's Trusted Types policy is named idlip. Under a CSP with require-trusted-types-for 'script', allow it with trusted-types idlip.

#Bound URLs

A hole in a URL attribute is checked on the server and in the browser. The attributes are href, src, action, formaction, xlink:href, poster, cite, data, background, codebase, longdesc, manifest, ping and profile.

In development, the browser console warns each time a URL is dropped.

import type { JSX } from 'idlip/jsx-runtime';

export default function Profile(props: { site: string }): JSX.Element {
  let site = props.site;

  function reset() {
    site = 'https://example.com';
  }

  return (
    <p>
      <a href={site}>Website</a>
      <button on:click={reset}>Reset</button>
    </p>
  );
}

{
  "site": "https://example.com"
}

With site set to javascript:alert(1), the link renders with no href.

See idlip for trusted and idlip/server for the URL check.