# richtext

A rich text editor as an element, for writing mail and documents: bold,
italic, code, links, headings, lists, quotes and pictures, with a toolbar.
Its document is **a value the program holds**, not the page's DOM: the page
keeps it in state, sends it, makes mail of it (`richtext.html`,
`richtext.text`) and can apply edits made elsewhere (`richtext.apply`).
Several people can edit one document at once, through a server that keeps
their edits sealed and only orders them ([below](#several-people-at-once)).
Written from nothing (`runtime/page/richtext.mote`, part `RX`; the document's
own `runtime/shared/richtext.mote`, part `RXM`; the merge,
`runtime/shared/richmerge.mote`, part `RXO`); a page that does not use it
does not carry it.

```mote
// docs: whole
state doc: json = richtext.from("Hello Ada,\n\nThe plans are attached.")

column gap=12 padding=16
  richtext value=doc placeholder="Write your message…" aria-label="Message" height=280
  text richtext.text(doc) white-space=pre-wrap color=dim
```

    richtext value=doc pictures=shown placeholder="…" readonly=locked toolbar=false tools="bold italic | bullet" edit=sent picture=attach input=typed change=left
    richtext value=doc log=entries send=post

It is a `<div>`: size and dress it with styles on the element (`height`,
`flex`, `border`, `border-radius`, `background`). With none it is at least
180px high, with a 1px `line` border and 8px corners; the text scrolls
inside it. `aria-label` on the element names the text field. `focus("id")`
with the element's id puts the caret in it. The gallery is
`examples/kit/richtext.mote`, a mail composer.

## Attributes

| attribute | type | default | what it does |
|---|---|---|---|
| `value` | json | an empty paragraph | The document ([below](#the-document)). Written back on every edit when it is state (or reaches into it). When the page assigns a document that differs, the editor takes it, keeps the caret in the block it was in, lets go of its undo history, and raises no event. `none` is an empty document. A document that cannot be written back, with no `input=`, `change=` or `edit=` to hear the edits, is refused (`H400`). |
| `pictures` | json | none | Where to show each picture from, by its ref: `{ref: address}` (a `bytes.url()`, a `data:` address, a picture of the back end's). A picture the editor was handed as a file shows without it. One with neither shows its `alt` in a dashed box. |
| `placeholder` | string | none | Shown, dim, while the document is one empty paragraph. |
| `readonly` | bool | `false` | The same view; the text can be selected and copied, not changed. The toolbar is hidden; links open (in a new tab) when pressed. |
| `toolbar` | bool | `true` | `toolbar=false` hides it. The keys still work. |
| `tools` | string | `"bold italic link \| heading bullet number quote \| picture"` | The toolbar's buttons, in order, `\|` between groups: `bold` `italic` `code` `link` `heading` (heading 2) `h1` `h2` `h3` `paragraph` `bullet` `number` `quote` `picture` `undo` `redo`. Another word, written out, is refused (`H401`). |
| `log` | json[] | none | The list of edits a server keeps for this document, in its order, which others add to too: the editor takes the entries after the last it took ([below](#several-people-at-once)). Given with `send=`, or refused (`H500`). |

## Events

| event | when | the handler is given |
|---|---|---|
| `input` | every edit (typing, a paste, undo, a format) | the document |
| `change` | the editor is left, if the document is not what it was when it was entered | the document |
| `edit` | every edit | its operations, `json[]` ([below](#operations)): what to send to another copy |
| `picture` | a picture is put in as a file (the toolbar's picture button, a paste, a drop) | `{ref, name, type, data: bytes}` |
| `send` | with `log=`: this editor's edits are to be stored in the list (one unit at a time) | a unit, `{by, n, parent, ops}` ([below](#several-people-at-once)) |

```mote
// docs: whole
shape Attached
  ref: string
  name: string
  type: string
  data: bytes

state doc: json = none
state attached: Attached[] = []
state sent = 0

action took(p: Attached)
  attached.push(p)

action edited(ops: json[])
  sent += ops.length

column gap=8 padding=16
  richtext value=doc picture=took edit=edited aria-label="Message" height=240
  text `${attached.length} pictures, ${sent} operations` color=dim
```

A value the page assigns raises none of them. `key`, `focus`, `blur`,
`paste` and the pointer events are the element's own.

**A picture put in** gets a block at once, with a new `ref`, shown from the
file (the editor makes it an address of its own). The page is handed the
file's bytes under that ref, once they are read: keep them, as the mail's
inline attachment. (An address made with `bytes.url()` lasts as long as the
bytes do: keep the bytes, not only the address.)

## Keys

`Mod` is Cmd on a Mac, Ctrl elsewhere.

| key | does |
|---|---|
| Mod+B, Mod+I, Mod+E | Bold, italic, code: over the selection (off if all of it has it already), or, with nothing selected, for what is typed next (until the caret moves). |
| Mod+K | The link field, over the selection, or for the link the caret is in. Enter applies, Escape closes. An address with no scheme becomes `https://…` (`ada@example.com`: `mailto:`); one other than `http`, `https`, `mailto` or `tel` is refused. With nothing selected, the address is put in as linked text. |
| Mod+Alt+1, 2, 3 / Mod+Alt+0 | Heading 1, 2, 3 / paragraph. |
| Mod+Shift+8 / Mod+Shift+7 | A bulleted / numbered list (off again if every block is one). |
| Mod+Shift+9 | Quote (off again if every block is quoted). |
| Mod+] / Mod+[ | A list item a level deeper / back (six levels). |
| Mod+Z, Shift+Mod+Z / Mod+Y | Undo, redo: the editor's own history. Letters typed one after another within a second and a half are one step. |
| Enter | A new paragraph (after a heading: a paragraph). In an empty list item: a level back, then out of the list; in an empty quoted line: a quote less. |
| Shift+Enter | A line break inside the block. |
| Backspace at a block's start | A list item a level back, then a paragraph; a heading a paragraph; a quote a quote less; then joins the block before. Before a picture: takes the picture. |
| Delete at a block's end | Joins the block after (a picture after it is taken). |
| `# `, `## `, `### ` at a paragraph's start | Heading 1, 2, 3. |
| `- `, `* `, `+ ` / `1. ` | A bulleted / numbered list item. |
| `> ` | Quoted. |
| Tab, Shift+Tab | **Leave the editor**, as from any field: nothing holds the keyboard. Lists are indented with Mod+]. |
| Alt+F10 | To the toolbar. There the arrow keys, Home and End move between its buttons (one of them is a stop for Tab), Enter or Space presses one, Escape goes back to the text. |

The markdown-style beginnings are a step of their own in the history: Undo
straight after one gives back what was typed. Mod+Click on a link opens it
in a new tab; a plain press puts the caret in it.

## The document

```text
doc   = {blocks: [block, …]}                     never empty: one empty paragraph at least
block = {id, kind, text, marks, level?, list?, quote?, ref?, alt?, width?, height?}
  kind    "paragraph" | "heading" (level 1–3) | "item" (list "bullet" | "number", level 0–5)
          | "picture" (ref, alt, width?, height?; its text is "")
  quote   how deeply it is quoted (0, or left out, to 9): any block, a list item or a heading too
  text    "\n" where a line breaks inside the block (Shift+Enter)
  marks   [{kind: "bold" | "italic" | "code" | "link", from, to, href?}]
          ranges of the text in UTF-16 units, sorted by kind and then from,
          none touching another of its kind
  id      a block's own, for the life of the document (12 letters and digits, made at random)
```

A list is a run of `item` blocks; a quote is a run of blocks with a
`quote`. So a reply quotes lists and headings as they were, at any depth,
and the document stays a flat list a program can walk.

Any value is made into a document before it is used: what is not a block
is left out, a block with no `id` is given one, a mark out of the text is
cut to it, a link to anything but `http`, `https`, `mailto` or `tel` is
dropped. So the page may write one by hand:

```mote
// docs: whole
state doc: json = {blocks: [{kind: "heading", level: 2, text: "Plans"}, {kind: "item", list: "number", text: "Draw them", marks: [{kind: "bold", from: 0, to: 4}]}, {kind: "paragraph", text: "Thanks", quote: 1}]}

richtext value=doc aria-label="Plans" height=200
```

## Mail of it

| call | gives |
|---|---|
| `richtext.html(doc)`, `richtext.html(doc, {pictures: {ref: src}})` | Mail-safe HTML: `p`, `h1`–`h3`, `ul`, `ol`, `li`, `blockquote`, `strong`, `em`, `code`, `a`, `br`, `img`, each with its styles inline (no class, no stylesheet, nothing fetched but the pictures). A picture's `src` is the address given for its ref, or `cid:<ref>`. |
| `richtext.text(doc)` | Plain text, a line for each block (`\n` between lines). |
| `richtext.from(text)` | A document of plain text: a paragraph a line; a line that begins with `>` is quoted, once for each. |
| `richtext.quote(doc)` | The document quoted once more, with new ids: the start of a reply. |
| `richtext.apply(doc, ops)` | The document with the operations of an edit done to it ([below](#operations)). |
| `richtext.pictures(doc)` | The refs of its pictures, in order, each once: the inline attachments a mail needs. |

None of them waits. They are the same in a page, in a worker (an `off fn`)
and in a `server fn`: one fn of the runtime's, sent along.

The HTML, for a mail client:

- Paragraphs have no margin, as in most mail: an empty paragraph is a blank
  line (`<p style="margin:0"><br></p>`). Headings are 24, 20 and 16px, bold.
- Lists are nested `ul` and `ol` in the `li` above them, 24px in. A quote is
  `<blockquote style="margin:0 0 0 0.8ex;border-left:1px solid #cccccc;padding-left:1ex">`,
  the form mail clients quote replies in.
- Links are blue and underlined, `code` is monospace on a light grey.
- A picture is `<img src="cid:…" alt="…" style="max-width:100%;height:auto;border:0">`,
  with `width` and `height` when the block has them.

The plain text:

- A list item is `- ` or `1. ` (numbered in its list), two spaces in for each
  level; the lines of an item that breaks are lined up under its first.
- A quoted line begins `> `, once for each level (an empty one is `>`).
- A link is its words, then the address in angle brackets: `the plans
  <https://example.com/plans>` (once, where the words are the address, or the
  address after `mailto:` or `tel:`).
- Bold, italic and code are the words alone. A picture is `[alt]`.
- Lines are not wrapped (the mail's encoding carries long lines).

**Pictures in a mail.** The editor's document names each picture by a
`ref` that the page chose nothing of: `picture=` hands the page the file
under it. Keep the bytes beside the document; when the mail is sent,
`richtext.pictures(doc)` says which are still in it, each is an inline part
with its ref as the `Content-ID`, and `richtext.html(doc)` already points at
`cid:<ref>`. To show a draft read back from elsewhere, give the editor
`pictures={ref: address}`.

## Operations

Every edit is a list of operations, each a plain record that names its block
by id. `edit=` hands them over; `richtext.apply` does them to a document.

```text
{op: "insert", block, at, text, marks}          marks: of the text inserted, from 0
{op: "delete", block, at, text, marks}          what was taken out (its length is what counts)
{op: "mark",   block, from, to, kind, href?, on}
{op: "split",  block, at, id}                   the text from `at` on becomes block `id`, after it
{op: "join",   block, into, at, value}          `block`'s text at the end of `into`; `block` goes
{op: "set",    block, kind?, level?, list?, quote?, alt?, width?, height?, ref?, was}
{op: "add",    after, value}                    `after`: a block's id, or none for the first place
{op: "remove", block, after, value}
```

- **A place** is a block's id and an offset in its text, as the document was
  just before the operation. Ids are made at random by whichever editor made
  the block, so two editors never make the same one.
- **Each carries what it took away** (the text and marks deleted, the block
  removed, the kind before a `set`), so each can be taken back without the
  document it was made on. The editor's undo is exactly that: the operations
  that take a step back, which `edit=` hands over like any other.
- An operation that does not apply (its block is not there, a picture's
  text) is passed over by `richtext.apply`, which never fails.

Edits made at the same time by several people are merged with these same
operations: [below](#several-people-at-once).

## Several people at once

Several people can edit one document at once, each in their own editor, and
every copy ends with the same document. The edits travel **sealed**: the
server stores them, numbers them in the order they arrive and hands them to
everyone; it never reads one. The merge is done in each page, the same in a
worker and in a `server fn` (`runtime/shared/richmerge.mote`, part `RXO`).

```text
unit  = {by, n, parent, ops}     what an editor sends: its operations, `by` its own id (new for each
                                 editor), `n` counting its units, `parent` the last entry it had taken
entry = {seq, ...unit}           a unit as the list holds it: the server's number added
      | {seq: 0, doc}            the list's first entry: the document everyone begins with
```

### How: operations moved past each other, in the server's order

The two ways known to work are operational transformation (each operation
is moved past those it had not seen, by a fn of the two) and a sequence
CRDT (each character has an id, and operations name ids). **This is
operational transformation, with the order the server gives**, and the
moving done by every copy, not by the server:

- An entry is moved past the entries the list holds between its `parent`
  and itself, by the same fn, in the same order, on every copy. So every copy
  that has taken the same list holds the same document, byte for byte:
  that is so whatever the fn does, which is why it converges. What the fn
  is for is that each person's screen (their document with their own edits
  not yet in the list) comes out as the list then does, and that the result
  is what both meant ([below](#what-it-does-when-two-edit-one-thing)).
- A dumb server is enough: it keeps opaque blobs in arrival order and
  numbers them (a `sql` table, a `model`, a Postgres row with a sequence).
  A page that was offline for a day takes the entries it missed, moves its
  own edits past them, and sends them: nothing else is needed.
- The document and its operations stay what they were (blocks by id,
  places as offsets in UTF-16 units). A unit of typed text is one insert
  (letters typed one after another are made one), so what travels is small.
  A CRDT would have put an id on every character and kept a tombstone for
  every one deleted, in the document every page holds and in every copy of
  it, for ever; its gain, merging with no server's order at all (peer to
  peer), is not needed where a server orders.
- One unit of an editor's is on its way at a time: the next is sent when
  the last is in the list (its own entry comes back). A unit sent twice
  (a call that got no answer, sent again) is applied once. One that got
  old on its way (its `parent` 500 entries behind the list's end) is handed
  out again, moved on; an entry 1000 behind its `parent` is passed over by
  every copy alike.

### The editor

```text
richtext value=doc log=entries send=post
```

- `log=` is the list, or the newest part of it: the editor takes the
  entries after the last it took and passes over the rest, so the page may
  give it each batch as it comes. Before the first entry (`{seq: 0, doc}`)
  it shows `value` and takes no edits; after it, the list is the document:
  a value the page assigns is not taken, and `value` is written back on
  every change, the others' too (which raise no event: `input`, `change`
  and `edit` are this editor's own edits).
- `send=` is handed each unit to store. Seal it, store it; if that fails,
  store the same one later. A `richtext` with `log=` and no `send=`, or
  the other way round, is refused (`H500`).
- **Others' edits** are drawn where they fall. The caret and the selection
  move with the text they are in (text put exactly at the caret goes after
  it, so a person typing there is not interrupted), a link being edited
  keeps its range, and the undo history is moved past them: **Mod+Z takes
  back only this person's own edits**, from the document as it is now.
- **A word being composed** (an input method, a phone's keyboard) is not
  touched: others' edits wait until the composition ends, then are taken.
  (Drawing them during it would end the input method's word; moving them
  past a word the browser has not yet said is a guess.)

```mote
// docs: whole
shape Stored
  seq: number
  blob: string

derive room = "plans"
state words = ""
state key: bytes = bytes.new(0)
state seen = 0
// Everyone begins with the same document: the list's entry 0, kept with the room when it was made.
state entries: json[] = [{seq: 0, doc: {blocks: [{id: "start", kind: "heading", level: 1, text: "Plans"}]}}]
state outbox: json[] = []
state flushing = false
state doc: json = none

// The server keeps blobs in the order they come, numbered: it cannot read one.
server fn post(room: string, blob: string): number
  let d = sql.open("rooms")?
  sql.run(d, "create table if not exists unit(room text, seq integer, blob text, primary key (room, seq))")?
  sql.run(d, "insert into unit select ?, coalesce(max(seq), 0) + 1, ? from unit where room = ?", [room, blob, room])?
  changed("since", room)
  return 1

server live fn since(room: string, after: number): Stored[]
  let d = sql.open("rooms")?
  sql.run(d, "create table if not exists unit(room text, seq integer, blob text, primary key (room, seq))")?
  let rows: Stored[] = sql.run(d, "select seq, blob from unit where room = ? and seq > ? order by seq limit 500", [room, after])?
  return rows

live fresh: Stored[] = since(room, seen) else []

action join
  key = crypto.argon2id(words, `the room ${room}`, {bytes: true})?

// What came, opened, to the editor; and the next part of the list asked for.
watch fresh
  let got: json[] = []
  for e in fresh
    if e.seq > seen
      let all = bytes.base64(e.blob)?
      let unit: json = JSON.parse(crypto.aes.open(key, all.slice(0, 12), all.slice(12), {aad: room})?.text())
      got.push({...unit, seq: e.seq})
  if got.length
    entries = got
    seen = got[got.length - 1].seq

action send(unit: json)
  outbox.push(unit)
  flush()

// Sealed and stored, one at a time; what could not be stored goes when the line is back.
action flush
  if flushing
    return
  flushing = true
  while outbox.length
    let nonce = crypto.randomBytes(12)
    let sealed = crypto.aes.seal(key, nonce, JSON.stringify(outbox[0]), {aad: room}) else none
    let ok = sealed ? post(room, bytes.join([nonce, sealed]).base64()) else 0 : 0
    if ok == 0
      flushing = false
      return
    outbox.shift()
  flushing = false

watch net.online
  if net.online
    flush()

column gap=8 padding=16
  if key.length == 0
    input value=words placeholder="The room's passphrase" submit=join
  else
    richtext value=doc log=entries send=send aria-label="Plans" height=300
```

The server sees how many units there are, how big, when, and from which
connection; not a word of the document. A unit's own fields (`by`, `n`,
`parent`) are inside the seal, so the server cannot forge or reorder what
an author said. It can withhold entries or show two people different
lists: what it can do with the order is the one thing it is trusted with.

### Without the editor: the calls

The same merge, as values, for a worker, a `server fn` that holds the key,
a program that edits without an editor, or a test:

| call | gives |
|---|---|
| `richtext.copy(doc)`, `richtext.copy(snapshot)`, `richtext.copy()` | A copy: beginning at that document (as entry 0), at a snapshot, or (no argument) at the first `{seq, doc}` entry it takes. |
| `richtext.edit(copy, ops)` | The copy with this person's operations done (on `copy.doc`). |
| `richtext.take(copy, entries)` | The copy with the list's entries taken, in order (those it has taken already are passed over). |
| `richtext.snapshot(copy)` | `{seq, doc, log, seen}`: where another copy can begin, as one that took the whole list would be at `seq`. Store it beside the list (sealed), and let a new copy begin there and take what follows. |
| `richtext.transform(mine, theirs)` | `[mine', theirs']`, two lists of operations made on one document at once, each moved past the other (`theirs` first in the order): `richtext.apply(richtext.apply(d, theirs), mine')` is `richtext.apply(richtext.apply(d, mine), theirs')`. |

A copy is a plain value: `copy.doc` is what this person sees, `copy.seq`
the last entry taken, `copy.out` a unit to send now (or none: after each
call), `copy.came` the others' operations the last `take` did to `doc` (to
move one's own places and history past), none when it began again from a
document. None of them waits; each is the same in a page, a worker and a
`server fn`.

```mote
// docs: whole
state a: json = none
state b: json = none
state list: json[] = []
state said = ""

action go
  // Two copies of one document (the same ids: one document, begun twice).
  let start = richtext.from("one two")
  a = richtext.copy(start)
  b = richtext.copy(start)
  let first = start.blocks[0].id
  a = richtext.edit(a, [{op: "insert", block: first, at: 3, text: " and"}])
  b = richtext.edit(b, [{op: "mark", block: first, from: 0, to: 7, kind: "bold", on: true}])
  list.push({...a.out, seq: 1})
  list.push({...b.out, seq: 2})
  a = richtext.take(a, list)
  b = richtext.take(b, list)
  said = `${richtext.text(a.doc)} · the same: ${JSON.stringify(a.doc) == JSON.stringify(b.doc)}`

button "Merge" press=go
text said
```

### What it does when two edit one thing

| at once | gives |
|---|---|
| Two insert at one place | Both, the one later in the list first; one who goes on typing after their own words, before seeing the other's, stays together (the other's are not put between them). |
| One types inside a range another deletes | What was typed stays; the rest of the range goes. |
| Two deletes that overlap | What either deleted is gone, once. |
| One marks a range (bold, a link), another types inside it | The typed text is marked too (or unmarked, for a mark taken off). |
| Two mark one range with one kind (two links, bold and not) | The one later in the list has its way where they overlap. |
| A split of a block, and a join of it into the one before | The join, then the split where its text went: the new block keeps the kind the joined one had. |
| Two splits at one place | Both: an empty block between. |
| A block taken out while another types in it | It comes back, holding only what was typed. A split or a mark of it goes with it. |
| A block made a heading by one and a list item by another | The one later in the list has its way, key by key (kind, level, list, quote, a picture's alt). |
| Two blocks put after one | Both, the one earlier in the list first. |

### What it does not do yet

- **Cursors of the others** (presence) are not shown, and nothing says where
  anyone is: a page can send its own, sealed, beside the units, if it wants
  them (a place is `{b, o}`, moved past what others did with the copy's
  `came`).
- **No server, no merge**: two copies with no server between them cannot
  merge (there is no order). Peer to peer would need the CRDT this is not.
- Two people each taking out one of the last two blocks at once: the
  document is never empty, so the list keeps one of them; each person's
  screen first showed the other (it then shows the list's).
- A block joined to one that is not just before it (it was, on the author's
  screen): what was put after the joined block lands after the one it
  joined. This and the case above are where a person's own screen and the
  list can differ for a moment (in the test, about one landing in a
  hundred); every copy still ends the same.
- A snapshot carries the last 1000 entries: a copy begun from it takes a
  late entry as one that took the whole list does, unless that entry is
  1000 behind its `parent`, which every copy passes over.

### What was tested of it

`tests/all.sh real_richtext_merge` runs the merge alone in Node, seeded:
100,000 pairs of random edits made on one document, each done after the
other both ways (they differ only where a block is taken out of a document
of two, above); 10,000 runs of 3 to 5 copies editing, undoing, going
offline and back, with units lost and stored twice, each ending with every
copy the same as the server's list replayed from nothing; a copy away while
1200 entries were stored; a copy begun from a snapshot; and each case of the
table above, both ways round. `tests/all.sh real_together` has three pages
in three browsers type at once through a real back end that stores only
sealed units (`crypto.aes`), one cut off from the network while all type and
then back, undo after others' edits, a word composed while another's edit
comes, the same merge in a worker and a `server fn`, and no word of the
document anywhere in the back end's data.

## How it takes the keyboard

The text is a `contenteditable` element. The editor hears the browser's
`beforeinput`, which says what the person means (insert this, a new
paragraph, delete a word backward) with the range the browser worked out
for it; it refuses the browser's own change, does it to the document as
operations, and draws the blocks that changed again. So the caret, the
selection, spellcheck, autocorrect, dictation, the on-screen keyboard and a
screen reader's view are the platform's, and the document is the editor's.

What cannot be refused, an input method composing a word (Japanese,
Chinese, Korean, accents typed in two keys, and many phone keyboards for
every word), is let happen, and read back when the composition ends: each
block's text is compared with the document's, and the difference becomes
operations with the marks of the place it was typed. Nothing is drawn again
while a composition is open.

Why not a hidden `<textarea>`, as the code editor does: a textarea has no
bold, no links and no pictures to point a screen reader at, and drawing the
text over it would fight the input method's own drawing of what it is
composing. Why not a canvas: the platform's selection, input methods and
accessibility would all have to be written again.

## Paste, drop, copy

**Copying out** puts three things on the clipboard: the plain text, the
mail-safe HTML and the blocks themselves, so a paste into another `richtext`
is exact.

**Pasted HTML** is cleaned twice on the way in. First by
[`html.clean`](../text.md#html), in the worker pool, with its styles
(`{styles: true, images: "block"}`): the standard's parser and its
allow-list, so what reaches the editor is HTML that battery wrote out. A
word processor says bold and not-bold in `style` (a whole copy wrapped in
`<b style="font-weight:normal">`), which is why the styles are kept for this
step; pictures from elsewhere are left out. Then by an allow-list of the
editor's own, which decides the document's shape: paragraphs and what holds
them, headings (h4 to h6 become h3), lists and their items, quotes, line
breaks, bold and italic (also a `font-weight` or `font-style` said in a
`style`, the only things read from one), code, and links to `http`,
`https`, `mailto` and `tel`. Everything else is its text alone (a button's
label, say), or nothing at all: scripts, styles, frames, objects, form
fields, and pictures from elsewhere. It is parsed as an inert document, so
nothing in it runs or is fetched, and its `style` attributes are renamed
before it is parsed, so a page under `policy=strict` refuses nothing. The
paste lands when the battery answers (the first one fetches it); if it
cannot be had, the clipboard's plain text is pasted instead. A program with
a `richtext` carries the `html` battery as a file of its build, fetched on
the first paste of HTML. Plain text pasted is a paragraph a line (one line
goes into the line where the caret is).

**Pictures** pasted or dropped as files are put in as pictures. A picture of
the document can be dragged to another place in it.

## What a screen reader is told

- The text is a `textbox` with `aria-multiline`, named by the element's
  `aria-label` ("Rich text" without one), and described: "Tab leaves the
  editor. Alt+F10 goes to the formatting toolbar." Headings, lists (as `ul`
  with `li`, each with its `aria-level`), quotes and links inside it are the
  HTML elements of their kind.
- The toolbar is a `toolbar` named "Formatting" that controls the text; its
  buttons are named ("Bold", "Bulleted list"), say their keys
  (`aria-keyshortcuts`), and those that are on or off say which
  (`aria-pressed`, following the caret or the selection).
- A change of format is said in a polite live region: "Bold on", "Heading
  2", "Bulleted list", "Quote off", "Link added", "Picture added",
  "Undone".

## What was tested, and what was not

`tests/all.sh real_richtext` drives the gallery in headless Chrome with
keys sent as a keyboard sends them, an input method's composition and the
text a keyboard commits (`Input.imeSetComposition`, `Input.insertText`), a
word deleted backward by the browser's own range, an emoji of two code
points, pastes of plain text and of hostile HTML (nothing runs, nothing is
fetched), a picture chosen, dragged and deleted, and the accessibility tree;
then the document made into mail in the page, a worker and a `server fn`
(alike to the byte), and each edit's operations applied to a copy (which
follows the editor); then all of it again under `policy=strict`, with
nothing refused. Edits made at once: [above](#what-was-tested-of-it).

Not tested here, as there is no phone to test on: the keyboards of iOS and
Android. On a phone, check: typing with autocorrect and predictions on (a
word replaced by a suggestion), a word composed by Gboard and Samsung's
keyboard, dictation, Backspace at the start of a list item, selecting with
the system's handles, the system's Bold/Italic in the selection menu (heard
as `formatBold`, `formatItalic`), and pasting from another app. Safari and
Firefox are not driven by the suite either.

## What it is not

No tables, colours, fonts, sizes, alignment, underline or strike-through;
pictures are not resized in the editor (a block's `width` and `height` are
the page's to set); no find and replace; no spellcheck of its own (the
browser's is on); no cursors of the others shown (nothing of where they are
is sent: [above](#what-it-does-not-do-yet)). One document per element; a
value assigned from outside replaces it and lets go of the undo history
(except under `log=`, where the list is the document).
