svelte-rtf-editor 1.9.0 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -15,7 +15,8 @@ RTF viewer and rich-text editor components for **Svelte 5**.
15
15
  - Block-level controls: headings (H1–H3), paragraph, blockquote, code block
16
16
  - Images: upload, paste or drag-and-drop, with a description below each picture, drag-to-resize and left/centre/right placement — embedded in the RTF, not linked
17
17
  - Import an RTF file from disk; export as RTF, HTML, or Markdown
18
- - Optional auto-save to `localStorage`
18
+ - Optional auto-save to `localStorage` (off unless asked for)
19
+ - HTML from outside (the `content` prop, `setHTML`, a restored draft, a paste) is made safe before it is shown. See [HTML from outside](#html-from-outside)
19
20
  - Word count and character count status bar
20
21
  - Keyboard shortcuts (`Ctrl+S` to save, `Ctrl+B/I/U` for formatting)
21
22
  - **Utility functions** — `rtfToHtml`, `htmlToRtf`, `readRtfFile`, `htmlToMarkdown`, `downloadFile`
@@ -108,7 +109,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
108
109
  | Prop | Type | Default | Description |
109
110
  |--------------|----------|-----------|------------------------------------------|
110
111
  | `content` | `string` | — | Raw RTF string to parse and display |
111
- | `fontFamily` | `string` | `'Arial'` | Font the document is shown in |
112
+ | `fontFamily` | `string` | `'Segoe UI'` | Font the document is shown in. See [Fonts](#fonts) |
112
113
  | `fontSize` | `number` | `12` | Body text size in points |
113
114
  | `maxRtfBytes` | `number` | `52428800` | The largest document it reads (50 MB); `0` reads any size. See [Size limit on import](#size-limit-on-import) |
114
115
 
@@ -120,26 +121,30 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
120
121
 
121
122
  | Prop | Type | Default | Description |
122
123
  |-----------------|------------|------------------------------|-----------------------------------------------------------|
123
- | `content` | `string` | `'<p></p>'` | Initial HTML content |
124
+ | `content` | `string` | `'<p><br></p>'` | Initial HTML content. Made safe before it is shown: see [HTML from outside](#html-from-outside) |
124
125
  | `placeholder` | `string` | `'Start writing something beautiful...'` | Placeholder text when editor is empty |
125
- | `autosave` | `boolean` | `true` | Save content to `localStorage` on change |
126
- | `storageKey` | `string` | `'ink-editor-content'` | `localStorage` key used for auto-save |
126
+ | `autosave` | `boolean` | `false` | Keep a draft in `localStorage`, and show it on mount in place of `content`. See [Auto-save](#auto-save) |
127
+ | `storageKey` | `string` | `'ink-editor-content'` | `localStorage` key the draft is kept under. Give each document its own |
128
+ | `ariaLabel` | `string` | `'Document'` | The name of the editing area for a screen reader |
127
129
  | `showToolbar` | `boolean` | `true` | Show the formatting toolbar |
128
130
  | `showStatusBar` | `boolean` | `true` | Show the word/character count bar |
129
131
  | `minHeight` | `string` | `'40vh'` | CSS `min-height` of the editing area |
130
132
  | `readonly` | `boolean` | `false` | Disable editing |
131
133
  | `maxImageEdge` | `number` | `1600` | Longest edge (px) an inserted image is scaled down to; `0` keeps full size |
132
- | `maxImageBytes` | `number` | `524288` | Encoded byte ceiling per image; oversized pictures are re-encoded until they fit. `0` disables |
134
+ | `maxImageBytes` | `number` | `524288` | Encoded byte ceiling per image; an oversized picture is re-encoded and shrunk until it fits, and is not inserted if it cannot. `0` disables |
133
135
  | `maxRtfBytes` | `number` | `52428800` | The largest RTF file Import reads (50 MB); `0` reads any size |
134
136
  | `maxImageDisplayWidth` | `number` | `624` | Width (px) a newly inserted image is displayed at, unless the column is narrower — 6.5in of page. `0` fills the column |
135
- | `fontFamily` | `string` | `'Arial'` | Document font, on screen and in the RTF. See [Fonts](#fonts) |
137
+ | `fontFamily` | `string` | `'Segoe UI'` | Document font, on screen and in the RTF. See [Fonts](#fonts) |
136
138
  | `fontSize` | `number` | `12` | Body text size in points, on screen and in the RTF |
139
+ | `fontSizes` | `number[]` | `[8, 9, 10, 11, 12, 14, 16, 18, 20, 24, 28, 36]` | The sizes, in points, the toolbar's size list offers for selected text. The body size is always among them; `[]` takes the list off the toolbar. See [Size of selected text](#size-of-selected-text) |
140
+ | `keepAttributes` | `string[]` | `[]` | `data-` attributes of your own that stay on HTML you set or the user pastes. See [HTML from outside](#html-from-outside) |
141
+ | `keepClasses` | `string[]` | `[]` | Class names of your own that stay the same way |
137
142
 
138
143
  #### Callback props
139
144
 
140
145
  | Prop | Payload | Description |
141
146
  |------------|----------------------------------------------|---------------------------------------------|
142
- | `onchange` | `{ html, text, wordCount, charCount, estimatedRtfBytes }` | Fired on every content change |
147
+ | `onchange` | `{ html, text, wordCount, charCount, estimatedRtfBytes }` | Fired on every content change. `html`, `text` and `estimatedRtfBytes` are worked out when the handler reads them, so a handler that only wants the counts costs nothing on a long document. They are the document as it is when first read: read them in the handler. A payload kept and read after a later edit gives the document as it is by then |
143
148
  | `onsave` | `{ html }` | Fired on auto-save or `Ctrl+S` |
144
149
  | `onimport` | `{ html }` | Fired after a successful RTF file import |
145
150
 
@@ -148,7 +153,7 @@ Pass an HTML string as `content`. If the content is already RTF, convert it firs
148
153
  ```ts
149
154
  editor.getHTML() // → string — current editor HTML
150
155
  editor.getText() // → string — plain text (no tags)
151
- editor.setHTML(html) // → void — replace content programmatically
156
+ editor.setHTML(html) // → void — replace content programmatically (made safe first)
152
157
  editor.getMarkdown() // → string — Markdown conversion of content
153
158
  editor.getRTF() // → string — RTF conversion of content
154
159
  editor.getRtfSize() // → number — exact byte size of that RTF
@@ -156,15 +161,51 @@ editor.clear() // → void — clear the editor
156
161
  editor.focus() // → void — focus the editor
157
162
  editor.exportFile(format) // → void — download as 'html' | 'md' | 'rtf'
158
163
  editor.importRtf() // → void — open the file picker to import .rtf
164
+ editor.setFontSize(14) // → void — size, in points, of the selected text, or of what is typed next
159
165
  ```
160
166
 
167
+ #### Auto-save
168
+
169
+ `autosave` is off by default. Turned on, the editor writes the document's HTML to `localStorage` under `storageKey` a second after each change, and on mount shows a draft it finds there **in place of `content`**. Two things follow, and both matter where the documents are patient reports:
170
+
171
+ - Give every document its own `storageKey` (`storageKey={`report-${id}`}`). With one key for all of them, the draft of the last document edited is shown in place of the next one opened.
172
+ - The draft is plain text in the browser's storage until `clear()` is called. Call it when the document is saved or abandoned, or leave `autosave` off and keep drafts yourself from `onsave`/`onchange`.
173
+
174
+ #### HTML from outside
175
+
176
+ Whatever HTML reaches the editor is made safe before it is shown: the `content` prop, `setHTML()`, a draft restored from `localStorage`, and what a paste brings. `sanitizeHtml` is exported for HTML you show yourself (stored editor output in a preview, say).
177
+
178
+ | Taken out | Why |
179
+ |---|---|
180
+ | `<script>`, `<style>`, `<iframe>`, `<object>`, `<embed>`, `<svg>`, `<video>`, `<audio>`, with what is in them | They run code or load a page of their own |
181
+ | `<button>`, `<textarea>`, `<select>`, `<input>` and `<math>`: the element goes, the words it showed stay as text | They take input, or are not something RTF can carry. A formula of numbers and signs in a row stays as those characters (`30%`); one with a fraction, a power or a root is written `[formula]`, since its parts read in a row would say something else. Only words that were shown stay: none of an element that was hidden (`hidden`, `display:none`, `visibility:hidden`, `mso-hide:all`), none of a hidden part of a button, and a formula that KaTeX wrote twice, once as text and once to be read aloud, is kept once. Elements side by side leave their words a space apart (`1 5`, not `15`) |
182
+ | Every attribute but the ones a document uses (`style`, `href`, `src`, `alt`, `colspan`, `start`, …) | That is how `onclick`, `onerror` and the rest go |
183
+ | A link that is not `http(s):`, `mailto:` or `tel:`; a picture address that is not a web address or picture data | `javascript:`, `data:text/html`, `file:` |
184
+ | Style properties that do not format text, a paragraph, a picture or a table (`position`, `z-index`, …), and any value with `url()` | A pasted block with `position: fixed` covered the page the editor sat in |
185
+ | A size that would put an element outside the page: a margin below zero, a margin, padding or width over 2200 px (a little more than the widest page), a width or height on anything but a picture, a table, its cells or a rule, a `width` or `height` attribute over 2200 or 100%, a padding over 32 px on a piece of text inside a line, a border picture, a size in `vw`/`vh` or worked out with `calc()`, a `display` other than the ones a document uses | A block with a negative margin was drawn over the toolbar, and a link with a padding of thousands of pixels was the size of the page. Sizes counted in ems or percent are worked out from the text size the document's styles give, so they do not grow past the limit by being nested. One margin below zero is kept: on a list, up to the half inch a list is indented by itself, which is how a list read from RTF carries an indent smaller than that |
186
+ | A size that would put words out of sight: a first line that starts further left than its paragraph is moved in (`text-indent: -100%`), and a text size under half a pixel | The words were in the RTF and not on the screen. A hanging indent (`margin-left:36pt; text-indent:-36pt`) stays as written |
187
+ | Style text the browser did not read as a property (`width: expression(…)`, a property named twice) | It does nothing here, but it would still be in the HTML for whatever shows it next. Where there is any, the attribute is written again from the properties that were read and kept. An attribute that is all read and kept stays exactly as it was written, `border: 1px solid black` included |
188
+ | `class`; an `id` that names something on `document` or `window`; the `id` of a link; in the editor, an `id` the page already uses for an element outside an editor | They would take on the page's own styles, or answer to a name or an `id` the page's scripts look up. A document's own ids are kept, also when it is shown again while its first showing is still in the editor. They are still names on `window`, as every `id` in a page is: a page whose scripts read settings from `window` by a name they do not define should not show documents from strangers |
189
+
190
+ **Marks of your own.** An application that marks elements in its documents and reads the marks back (`<p data-section="2">`), or styles one by a class (`<pre class="summary">`), names them in `keepAttributes` and `keepClasses`, and they stay on HTML it sets and on what the user pastes:
191
+
192
+ ```svelte
193
+ <InkEditor content={html} keepAttributes={['data-section', 'data-row']} keepClasses={['summary']} />
194
+ ```
195
+
196
+ Only attribute names that start with `data-` are kept, and after it only letters, digits, `-`, `_` and `.`; anything else in the list (`onclick`, `style`, `src`) is ignored, so the list cannot let in what the table above takes out. Any class name can be named, `md:flex` and `w-1/2` included. A kept class is kept by name alone: how it looks is your stylesheet's doing, and a paste that happens to use the same name gets it too. A kept attribute comes in with a paste the same way, with whatever value the pasted page gave it, so read a mark back as you would read anything a user typed; and a marked paragraph copied and pasted is two paragraphs with the same mark. Neither changes the RTF. `sanitizeHtml(html, { keepAttributes, keepClasses })` does the same for HTML you show yourself. Without the settings both are taken off, as since 1.10.0: an application that upgraded from an earlier version and relies on one has to name it.
197
+
198
+ The editing area also clips what it holds: nothing in a document is drawn outside it, whatever its styles, and a table wider than the area scrolls inside it. The frame around a selected picture stays inside it too. Text that Word marked hidden (`mso-hide: all`) is hidden on screen as it is left out of the RTF, and in a paste it is not pasted. A closed `<details>` is shown open, since what is in it is written to the RTF either way.
199
+
200
+ `getHTML()` returns what is in the editor, which has been through this. HTML from `rtfToHtml` is safe by construction (see [What `rtfToHtml` may return](#what-rtftohtml-may-return)).
201
+
161
202
  ---
162
203
 
163
204
  ## Utility functions
164
205
 
165
206
  ```ts
166
207
  import {
167
- rtfToHtml, htmlToRtf, estimateRtfBytes, readRtfFile, htmlToMarkdown, downloadFile,
208
+ rtfToHtml, htmlToRtf, estimateRtfBytes, readRtfFile, htmlToMarkdown, downloadFile, sanitizeHtml,
168
209
  DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE, DEFAULT_MAX_RTF_BYTES, isSafeLinkUrl
169
210
  } from 'svelte-rtf-editor';
170
211
  ```
@@ -172,33 +213,57 @@ import {
172
213
  | Function | Description |
173
214
  |---------------------------------------|----------------------------------------------------|
174
215
  | `rtfToHtml(rtf: string, options?: { fontSize?, maxBytes? }): string` | Parse an RTF string and return an HTML string. `fontSize` is the size of the screen it will be shown on (default 12). Throws a `RangeError` for a string longer than `maxBytes` (default 50 MB; see [Size limit on import](#size-limit-on-import)) |
175
- | `htmlToRtf(el: HTMLElement, options?: { fontFamily?, fontSize? }): string` | Convert a DOM element's content to RTF, in Arial 12 pt unless told otherwise |
176
- | `estimateRtfBytes(el: HTMLElement): number` | Approximate RTF size of a DOM element without converting it |
216
+ | `htmlToRtf(el: HTMLElement, options?: { fontFamily?, fontSize? }): string` | Convert a DOM element's content to RTF, in Segoe UI 12 pt unless told otherwise |
217
+ | `estimateRtfBytes(el: HTMLElement, options?: { fontFamily?, fontSize? }): number` | Approximate RTF size of a DOM element without converting it. Give it the options `htmlToRtf` is called with; it is never below the real size |
177
218
  | `isSafeLinkUrl(url: string): boolean` | Whether a link address is a web, mail or phone address; `htmlToRtf` and `htmlToMarkdown` write any other link as its text alone, and `rtfToHtml` reads any other as text |
178
219
  | `readRtfFile(file: File, options?: { fontSize?, maxBytes? }): Promise<string>` | Read a `.rtf` File object and return HTML. Rejects, without reading it, for a file larger than `maxBytes` |
179
220
  | `htmlToMarkdown(el: HTMLElement): string` | Convert a DOM element's content to Markdown. See [Markdown export](#markdown-export) |
221
+ | `sanitizeHtml(html: string, options?: { keepAttributes?: string[]; keepClasses?: string[] }): string` | HTML made safe to show and edit. See [HTML from outside](#html-from-outside). Needs a DOM (`DOMParser`); without one it returns `''` |
180
222
  | `downloadFile(name, content, mime): void` | Trigger a file download in the browser |
181
223
 
182
224
  ---
183
225
 
184
226
  ## Fonts
185
227
 
186
- The editor and the viewer show a document in the same font and size the RTF carries, so what you see on screen is what the receiving system gets. Both default to **Arial 12 pt**.
228
+ The editor and the viewer show a document in the same font and size the RTF carries, so what you see on screen is what the receiving system gets. Both default to **Segoe UI 12 pt** (`DEFAULT_FONT_FAMILY` and `DEFAULT_FONT_SIZE`, which the package exports).
229
+
230
+ To use another font, pass it to each of the three places a document goes through. Nothing else has to change:
187
231
 
188
232
  ```svelte
189
233
  <InkEditor fontFamily="Calibri" fontSize={11} bind:this={editor} />
190
234
  ```
191
235
 
236
+ ```svelte
237
+ <RtfViewer content={rtf} fontFamily="Calibri" fontSize={11} />
238
+ ```
239
+
192
240
  ```ts
193
241
  htmlToRtf(el, { fontFamily: 'Calibri', fontSize: 11 });
194
242
  ```
195
243
 
244
+ An application that shows documents in more than one place does well to keep the two values in one module of its own and pass them from there, so that a change of font is one line.
245
+
246
+ The default was Arial through 1.9.0. A document is written in the font of the editor that saves it, not in the font it was written in before: a report saved in Arial and opened and saved again by an editor left at the default comes out in Segoe UI. To keep Arial, pass `fontFamily="Arial"`.
247
+
248
+ Segoe UI is installed with Windows. On a system without it (macOS, Linux, a phone) the screen shows the system's sans-serif font in its place, and the RTF still names Segoe UI, which is what the receiving system reads.
249
+
196
250
  - `fontFamily` is used for the whole document: body text, headings, lists and tables. Code keeps Courier New.
197
251
  - `fontSize` is the body size in points. Headings scale from it: at 12 pt, `h1` is 24 pt, `h2` 18 pt and `h3` 14 pt.
198
252
  - If you call `htmlToRtf` yourself on the editor's content, pass the same options you gave the editor, or the two produce different RTF.
199
253
  - `fontFamily` names one font. Given a CSS list such as `'Arial, sans-serif'`, only the first family is used.
200
254
  - `fontSize` is rounded to the nearest half-point, which is what RTF measures in, and kept between 1 and 1638 pt.
201
- - If you show RTF in an editor or viewer set to another size, pass the same `fontSize` to `rtfToHtml`. Text at that size then carries no size of its own, text at any other size keeps it, and headings are recognized by the same ratios the writer uses. `InkEditor`'s RTF import and `RtfViewer` do this themselves. At another size than 12 pt, a heading must also be bold, as every heading the writer emits is.
255
+ - If you show RTF in an editor or viewer set to another size, pass the same `fontSize` to `rtfToHtml`. Text at that size then carries no size of its own, text at any other size keeps it, and headings are recognized by the same ratios the writer uses. `InkEditor`'s RTF import and `RtfViewer` do this themselves. A heading must also be bold, at every body size, as every heading the writer emits is.
256
+
257
+ ### Size of selected text
258
+
259
+ The toolbar has a list of sizes, next to the list of paragraph kinds. A size chosen there is given to the selected text; with nothing selected, to what is typed next. The list shows the size of the text at the caret, also when it is not one of the sizes offered, and is empty where the selection starts in one size and ends in another.
260
+
261
+ - The size is written to the RTF as `\fsN` around that text, and read back as that size by `rtfToHtml`.
262
+ - Text set back to the size the text around it has (the body size in a paragraph, the heading's size in a heading) is given that size as `1em`, and follows `fontSize` again like text that was never sized. The writer reads `1em` as no size.
263
+ - The sizes offered are `fontSizes`; pass your own list, or `[]` to leave the size list out. `editor.setFontSize(points)` does the same from your own toolbar.
264
+ - The editor uses the browser's own font-size command, so a change of size is undone and redone with everything else. In the HTML the size is on a `<font>` element (`<font style="font-size: 14pt">`).
265
+ - A whole paragraph that is bold and at the size a heading has is read back from RTF as that heading: it looks the same, and the list of paragraph kinds then names it a heading.
266
+ - `htmlToRtf` also reads `<font size="1">` to `<font size="7">`, which is how older pages and mail carry sizes, as the sizes a browser shows them at (7.5, 10, 12, 13.5, 18, 24 and 36 pt). They were shown at those sizes and written at the body size.
202
267
 
203
268
  The font has to be installed on the machine that displays the document. The RTF names it, and a reader without it substitutes one of its own.
204
269
 
@@ -249,7 +314,7 @@ A `<p>` or `<div>` keeps its own indents, spacing and tab stops in the RTF, so a
249
314
 
250
315
  - **Lists.** Only the shape above is read back as a list: a bullet or `N.`, a tab, and a paragraph that hangs by 360 twips and has no right indent or alignment of its own. Word's lists have that shape and are read as lists, including the bullet it writes in a `{\listtext}` group. Anything else stays the paragraph it is, marker and all, because it could not be written back the same: a line typed as `- Invasive adenocarcinoma.`, a list hung by another distance, one marked `a.` or `1)`, a centered one. A list in a table cell is read as lines of text.
251
316
  - **List items.** A paragraph inside an item (`<li><p>…</p></li>`, as Word and Google Docs paste a list) is the item's text; a second paragraph in the item is a line under it. A list written straight inside a list (`<ul><ul>`) is one level deeper. An item with no list around it is written as a bulleted line. The list a browser's own list command makes, which the toolbar's list buttons run, is left inside the paragraph it was made from (`<p><ul>…</ul></p>`); it is written as the list alone.
252
- - **Headings.** A paragraph is read as a heading when all of its text is at a heading's size, and at a body size other than 12 pt when all of it is bold too. A large word in an ordinary paragraph is a large word. A heading set larger than its level keeps that size.
317
+ - **Headings.** A paragraph is read as a heading when all of its text is at a heading's size and all of it is bold, as every heading `htmlToRtf` writes is. Size alone does not make a heading: a report set in 14 pt throughout is 14 pt paragraphs, with the words that were bold still the only bold ones. (Through 1.9.0, at a 12 pt body size, large text was read as a heading whether bold or not, and was saved bold.) A large word in an ordinary paragraph is a large word. A heading set larger than its level keeps that size.
253
318
  - **Links.** A link is read back only for an address `isSafeLinkUrl` accepts, the same test the writer applies: an absolute web, mail or phone address. A field with a `javascript:`, `data:` or `file:` address, a relative or network path, or a link to a bookmark is read as its text alone. A web link opens in a tab of its own, so a click in `RtfViewer` does not replace your application. A document with a link carries the link color as the first entry of its color table (`\red0\green0\blue255`); a document without one is written as before.
254
319
  - **Code.** Text in a monospace font other than the document's own font is read as code, and a paragraph set wholly in it at the code block size as a code block. In a document whose own font is monospace, nothing is.
255
320
  - **Line breaks.** The `<br>` a browser leaves at the end of a paragraph, a list item or a table cell shows as nothing and is not written. A line break that does end a paragraph in the RTF (`\line\par`) is read as `<br><br>`, which a browser shows as that empty last line.
@@ -261,10 +326,12 @@ RTF has no table element: each row says where its cells end (`\cellx`), so colum
261
326
 
262
327
  - **`colspan`**: one cell that ends at the right edge of the last column it covers.
263
328
  - **`rowspan`**: `\clvmgf` on the cell where it starts and an empty `\clvmrg` cell in the same columns of each row below. `rowspan="0"` reaches the last row, and no span reaches past the table.
264
- - **Column widths** come from the cells' own `width` in percent. A cell across several columns gives what it has left to those of them with no width; columns with none share what remains. The table is written 6 in wide.
329
+ - **Column widths** come from the cells' own `width`, or else from `<col>` and `<colgroup>`. A cell across several columns gives what it has left to those of them with no width; columns with none share what remains. Widths given as lengths (`120px`, `90pt`, the bare `width="120"` of a pasted table) keep the columns in proportion; beside percentages they count only where the table's own width is a length to measure them against. The table is written 6 in wide.
330
+ - **`<caption>`** is written as a centered paragraph before the table, where a browser shows it, and is read back as that paragraph.
265
331
  - **A column no cell starts in is not a column.** `colspan="100"` on a cell meant to cross the whole table adds none.
266
332
  - **A row short of cells** is filled with empty ones, so its edges are the table's.
267
- - **`<th>`** gets a bottom border, on that cell only, which is how `rtfToHtml` knows a header cell.
333
+ - **`<th>`** gets a bottom border, on that cell only, which is how `rtfToHtml` knows a header cell: a bare `\clbrdrb\brdrs` and no border on any other side. A bottom border with a width or a color (`\brdrw`, `\brdrcf`), which is how Word and TextEdit write a line someone drew under a cell, is not a header: a signature line stays an ordinary cell.
334
+ - **Each cell ends with `\cell` inside its group**, with no `\par` before it, and the last row of a table carries `\lastrow`. Through 1.9.0 a cell ended `\par}\cell`, which Apple's reader (TextEdit, Pages, Mail) showed as an empty second paragraph in every cell, and two tables with nothing between them were read back as one. `rtfToHtml` reads both shapes.
268
335
  - **`<thead>` and `<tfoot>`** are written first and last, wherever the source has them.
269
336
  - **A cell's `text-align`** is written on its paragraphs (`\qc`, `\qr`, `\qj`), and read back onto the cell.
270
337
  - **A table inside a table cell** is written as lines of text in that cell: each of its rows a paragraph, its cells a tab apart. RTF can nest tables, but many readers show a nested table wrongly or not at all; every word is kept, the inner grid is not. `rtfToHtml` reads a nested table Word wrote the same way.
@@ -277,8 +344,32 @@ RTF has no table element: each row says where its cells end (`\cellx`), so colum
277
344
  - `\pard` resets the paragraph, not the characters: bold, italic, underline and strikethrough carry on until `\plain` or their own `\b0`.
278
345
  - `\sect` ends a paragraph, as `\par` does.
279
346
  - A font table is read in both shapes: one group per font, and TextEdit's run of fonts with no groups.
347
+ - **Alignment** (`\qc`, `\qr`, `\qj`) holds until `\pard`, as an indent does: Word and TextEdit state it once for a run of centered paragraphs or right-aligned cells. It ends with the group it was set in, and with the row.
348
+ - **Tracked changes.** Text a tracked change deleted (`\deleted`) is not shown; what replaced it is. Shown, "malignant" struck out and "benign" put in read "malignantbenign".
349
+ - **Hidden text** (`\v`) is not shown, and neither are index and contents entries (`\xe`, `\tc`), nor a footnote that sits in hidden or deleted text.
350
+ - **The Symbol font.** Text set in a font named Symbol is read as the characters that font shows: `m` is μ, `\'b0` is °, `\'b3` is ≥. So is the U+F0xx form Word writes, and a `SYMBOL` field (Insert Symbol in Word). Other symbol fonts (Wingdings) are not mapped.
351
+ - **Footnotes and endnotes** go to the end of the document, each after the number the text refers to it by; the sentence the note sits in is not split.
352
+ - **Text boxes** are read as paragraphs after the paragraph, or the table, they are anchored in. A picture with text wrapped around it is shown where it is anchored, and an embedded object as the picture or text it carries to be shown.
353
+ - **Binary picture data** (`\binN`) is stepped over; a brace among its bytes no longer ends the document. The picture itself is not shown.
354
+ - **U+2028**, which TextEdit writes for Shift-Return, is a line break, and U+2029 a paragraph mark. **A tab character** in the file is a tab, as `\tab` is: TextEdit writes every tab that way. Any other control character is dropped, written as itself or as an escape.
355
+ - **`\lastrow`** ends a table, so a table that follows another with no paragraph between is a table of its own.
356
+ - A byte-order mark or blank lines before `{\rtf1` do not hide the color and font tables.
280
357
  - Damaged input does not stop a document being read: a `\'` with no hex digits after it, a `\u` out of range, half of a surrogate pair, a color component outside 0 to 255, groups nested tens of thousands deep, `\intbl` with no `\row`, a table row the file ends in the middle of.
281
358
 
359
+ ### Writing HTML that was pasted
360
+
361
+ The editor's own buttons make a small set of elements. A paste brings more, and `htmlToRtf` writes what the screen shows of it:
362
+
363
+ - **Formatting set by a style.** `font-weight`, `font-style`, `text-decoration` and `vertical-align` on an element are bold, italic, underline, strikethrough, superscript and subscript, which is how Google Docs writes all of them. A style can also take formatting off: Docs wraps a paste in `<b style="font-weight:normal">`, which is not bold. `color` is written on a heading, a list item and a table cell as it is on a paragraph.
364
+ - **What is not shown is not written:** `<style>`, `<script>` and `<template>`, an element with `hidden`, `display:none` or `visibility:hidden`, and Word's hidden text (`mso-hide:all`).
365
+ - **Blocks of a web page** (`<section>`, `<article>`, `<header>`, `<footer>`, `<main>`, `<aside>`, `<nav>`, `<address>`, `<details>`, `<summary>`, `<dl>`, `<dt>`, `<dd>`, `<center>`, `<fieldset>`, `<form>`) are written as a `<div>` is: each ends its line.
366
+ - **Alignment on a block around others** (`<div style="text-align:center">`, `<center>`) is the alignment of the paragraphs, headings and table cells inside it that set none. A list inside it is written left-aligned, so that it is still read back as a list.
367
+ - **`white-space: pre-wrap`** (also `pre`, `pre-line`, `break-spaces`): a new line in the text is a line break.
368
+ - **A figure with more than its picture** (text typed beside the picture, a second picture) is written as everything it holds, then its caption. The editor's own figure, one picture and a caption, is written as before.
369
+ - **A picture that cannot be embedded** is written as `[Image: description]`, or its address, and never as its data.
370
+ - **Control characters** are not written: a vertical tab or form feed becomes a space, the rest are dropped. 0x0B and 0x1C are among them, the bytes that start and end an HL7 message on the wire.
371
+ - **Loose text after a table** starts a plain paragraph of its own.
372
+
282
373
  ### What `rtfToHtml` may return
283
374
 
284
375
  `RtfViewer` puts the HTML `rtfToHtml` returns into the page as it is, so that HTML is limited to a fixed set, whatever the file holds: the elements `p`, `br`, `strong`, `em`, `u`, `s`, `sup`, `sub`, `span`, `code`, `pre`, `h1` to `h3`, `hr`, `ul`, `ol`, `li`, `a`, `table`, `tr`, `td`, `th`, `figure`, `figcaption` and `img`, and the attributes `style` (built from numbers and fixed words only), `href` (a web, mail or phone address, escaped), `target` and `rel` (fixed), `start`, `colspan` and `rowspan` (digits), and on a picture `src` (a PNG or JPEG data URL) and `alt`. Text from the file is always escaped. The test suite checks this against hostile files.
@@ -287,9 +378,15 @@ RTF has no table element: each row says where its cells end (`\cellx`), so colum
287
378
 
288
379
  `rtfToHtml` parses a document in one go on the main thread, and a picture costs two characters per byte, so the only bound on how long a page stops responding is the size of what it is handed. A string longer than `maxBytes` (`DEFAULT_MAX_RTF_BYTES`, 50 MB) is refused with a `RangeError` before any of it is parsed; `readRtfFile` rejects a larger file without reading it, and the editor's import shows that message; `RtfViewer` shows it in place of the document. Pass `maxBytes: 0` to read a document of any size, or a smaller number to refuse sooner; the components take the same number as `maxRtfBytes`. Where reports can carry large pictures, raise it: a 30 MB picture is a document of over 60 MB.
289
380
 
381
+ Size is not the only way to exhaust a page: a file of nothing but braces is small and still costs memory for each one. A document of more than five million parts (groups, control words, runs of text) is refused with a `RangeError` too, whatever `maxBytes` is. A long Word document has some hundreds of thousands; a picture is one for each line of the file its data takes up.
382
+
290
383
  ### Markdown export
291
384
 
292
- `htmlToMarkdown` writes a table as a Markdown table (a merged cell where it starts, with the cells it covers left empty, since Markdown has none), a numbered list from its `start`, and `<h4>` to `<h6>` as headings. Characters that Markdown would read as markup are escaped in text (`*`, `_`, `` ` ``, `#`, `[`, `]`, `<`, `~`, `\`, and `|` in a table cell), and so are `-`, `+`, `>` and `1.` at the start of a line: a report line typed as `- Benign.` stays that line. A link with an address `isSafeLinkUrl` refuses is written as its text alone.
385
+ `htmlToMarkdown` writes a table as a Markdown table (a merged cell where it starts, with the cells it covers left empty, since Markdown has none), a numbered list from its `start`, and `<h4>` to `<h6>` as headings. Characters that Markdown would read as markup are escaped in text (`*`, `_`, `` ` ``, `#`, `[`, `]`, `<`, `~`, `\` and `|`), and so are `-`, `+`, `>` and `1.` at the start of a line: a report line typed as `- Benign.` stays that line. A link with an address `isSafeLinkUrl` refuses is written as its text alone.
386
+
387
+ A line break in a paragraph is a hard break (two spaces before the new line; a `<br>` in a heading or a table cell, which are one line). Text typed before a block is a paragraph of its own, and so is each `<div>`, `<section>`, `<dt>` and `<dd>`. A quotation of several paragraphs, or one holding a code block, carries its `>` on every line. A list written straight inside a list is nested under the item before it. Bold with a space inside the tag (`<b>diagnosis </b>`) is written with the space outside the marks, and `<b style="font-weight:normal">` is not bold. `<sup>`, `<sub>`, `<u>` and `<ins>` are written as those tags, which Markdown allows, and `<del>` as `~~`. A table's `<caption>` is a line before the table; a figure writes everything it holds, then its caption. `&copy;` typed as text stays those characters, and a line of `---` or `===` is escaped so that it is not a rule or a heading. What the screen does not show (`<style>`, `<script>`, `hidden`, `display:none`, `visibility:hidden`, `mso-hide:all`) is not written.
388
+
389
+ Code is the one thing written as it is, so it has to stay inside its code span or its fenced block. A line break in a code span is written as a space, which is how Markdown shows one; a code block starts a line of its own, whatever element is around it (in a heading or a table cell, which are one line, it is a code span on that line); every line of a quotation carries its `>`. Underline is the `<u>` tag around the text itself, and so are `<ins>`, `<sup>` and `<sub>`. No tag is ever alone on a line, or first on one after white space, where it would open a block of HTML: an empty line in a paragraph is a `<br>` at the end of the line before it, and a line that holds white space alone is written as `&nbsp;`. A tab at the start of a line is written as `&#9;`, since a tab there makes the line a code block. Code right after code is one code span, because two spans back to back run their backticks together. A `|` is escaped in all text, not only in a table: two typed lines such as `Stain | Result` and `--- | ---` are a table to a viewer that knows tables. A numbered list starts no lower than 0 and ends no higher than 999999999, the numbers Markdown can write. Bold, a link and underline are not written around a code block; around a quotation, a list or a table they are written on the text inside. Markdown allows HTML, and this export keeps a document's text from being read as any, but a page that shows Markdown from more than one source should still use a viewer that does not pass HTML through, or sanitize what the viewer makes.
293
390
 
294
391
  ## Theming
295
392
 
@@ -362,6 +459,8 @@ A browser copies a paragraph with its looks written out as inline styles, and re
362
459
 
363
460
  A picture copied with its text comes in by address (`<img src="https://…">`). The editor fetches it and holds its data in the document, as it does for an address typed in the image dialog, so it reaches the RTF as a picture. When it cannot be fetched (the site forbids it, there is no network, it takes longer than 15 seconds or is larger than 25 MB) the picture still shows and the editor says that it will export as a placeholder. The pictures in one paste are fetched at the same time, so one slow site does not hold up the others. A picture pasted as data in a format RTF cannot carry (GIF, WebP, SVG), or larger than `maxImageBytes`, is converted and scaled the same way.
364
461
 
462
+ A picture pasted as data that cannot be brought under `maxImageBytes`, or that the browser cannot open, is taken out of the paste, and the editor says so. What was only there for it goes with it: its figure and caption, a link around it, a paragraph or list item that held nothing else. Text that shared its paragraph stays, a table cell stays, and so does anything that was in the document before the paste. Chromium, pasting a figure at the end of a line of text, moves its pictures into that line and leaves the caption in a figure of its own (inside the `<div>` or `<section>` the figure came in, if it had one); that caption goes with the picture too, unless another picture pasted with it is still there. Pasting a block into an empty line, Chromium puts the block in the line's place; if nothing of the paste is left once its pictures are out, the empty line is put back, with the caret in it. Pasting a figure into the middle of a line, it turns the caption into ordinary text of that line, and there the caption stays, since it can no longer be told from the words around it. When one paste has a picture left out and another that will export as a placeholder, the message says both.
463
+
365
464
  Paragraph alignment (left, center, right, justify) is written to RTF as `\qc`, `\qr` or `\qj`.
366
465
 
367
466
  Tab stays in the editor so a line can be indented. To leave the editor from the keyboard, press
@@ -385,15 +484,20 @@ Click an image to select it. A frame appears with corner handles you can drag to
385
484
 
386
485
  Images are held inline as base64 data URLs, so a document is self-contained — it auto-saves, exports and re-imports with the pictures intact, and needs no image server.
387
486
 
388
- - **RTF export** writes each picture as a real `\pict` group (`\pngblip` / `\jpegblip`) sized with `\picwgoal` / `\pichgoal`, so it opens with the images in Word, TextEdit and other RTF readers. The description follows as an italic line, tagged with an ignorable `{\*\inkcap}` destination that this library uses to re-attach it to the figure on import; other readers simply skip the tag.
487
+ - **RTF export** writes each picture as a real `\pict` group (`\pngblip` / `\jpegblip`) sized with `\picwgoal` / `\pichgoal`, so it opens with the images in Word and other RTF readers that show pictures. (TextEdit opens the file and shows its text, but does not show the pictures of a plain `.rtf` file.) The description follows as an italic line, tagged with an ignorable `{\*\inkcap}` destination that this library uses to re-attach it to the figure on import; other readers simply skip the tag.
389
488
  - **RTF import** decodes `\pict` PNG and JPEG data (including pictures wrapped in `{\*\shppict}`) back into images at their stored size. Picture formats a browser cannot display — metafiles and device-dependent bitmaps — are skipped.
390
- - **Formats**: PNG and JPEG are embedded as-is. Anything else (GIF, WebP, SVG, …) is re-encoded to PNG when inserted, since RTF carries no other bitmap types. Animated images keep their first frame.
489
+ - **Formats**: PNG and JPEG are embedded as-is. Anything else (GIF, WebP, SVG, …) is re-encoded to PNG when inserted, since RTF carries no other bitmap types. Animated images keep their first frame. A picture the browser cannot open (a TIFF, a HEIC photograph in most browsers, a damaged file) is not inserted, whatever its size, and the editor says that it could not be opened: it would show as a broken picture, and its data could not be converted for the RTF. That holds for a file, for picture data typed in the dialog and for picture data pasted with text. A picture fetched from an address that cannot be opened keeps its address and exports as the placeholder, as above.
391
490
  - **Images added by address**, typed in the dialog or pasted with text copied from a web page, are fetched and inlined so they survive export. When the fetch is blocked (CORS or offline) the address is kept and the picture still displays, but it exports as an `[Image: …]` placeholder instead of picture data — the editor says so at insert time rather than letting it go unnoticed until the document is read downstream.
392
491
  - **Size**: RTF stores picture data as hexadecimal — two characters per byte — so an embedded image costs twice its file size in the document. Resizing an image in the editor changes its display size only; two caps applied at insert time are what bound the payload:
393
492
  - `maxImageEdge` (1600 px) scales the picture down so neither edge exceeds it.
394
- - `maxImageBytes` (512 KB, so roughly 1 MB of document per image) is the ceiling that actually matters. A picture over it is re-encoded until it fits: a PNG is tried as PNG first so screenshots and diagrams stay sharp, falls back to JPEG only when it is too heavy *and* has no transparency to lose, then steps down through 80%, 64% and 50% of the target size. Whichever candidate fits first is kept.
493
+ - `maxImageBytes` (512 KB, so roughly 1 MB of document per image) is the ceiling that actually matters, and it is a ceiling: no picture goes into the document over it. A picture over it is re-encoded until it fits:
494
+ 1. At full size, then 80%, 64% and 50% of it, a PNG is tried as PNG first so screenshots and diagrams stay sharp, and as JPEG only when it is too heavy *and* has no transparency to lose.
495
+ 2. If none of those fits, the picture is made smaller still, each step sized from how far over the last one was, down to 16 px on its longest edge. A picture with transparency is laid on white and written as a JPEG from full size again, which keeps far more of it than a PNG a fraction of the size.
496
+ 3. A picture that still does not fit is not inserted, and the editor says so. This takes a limit of a few hundred bytes, or a picture the browser cannot redraw.
395
497
 
396
- Set either to `0` to disable it. Both caps are applied by re-encoding through a `<canvas>`, so they only take effect in a browser — in a runtime without one (SSR, jsdom/happy-dom tests) the original bytes are passed through untouched. Do not rely on them to bound a payload assembled server-side.
498
+ Measured in Chromium with a 6.8 MB, 4000×3000 JPEG dropped on the editor: 312 KB at the defaults, 92 KB at a limit of 100 KB, 14 KB at 20 KB, 4.8 KB at 5 KB, refused at 300 bytes. A 6.9 MB PNG with transparency: a 368 KB JPEG on white at the defaults.
499
+
500
+ Set either to `0` to disable it. With `maxImageEdge` at `0` a very large picture can be more than the browser's canvas holds (268 million pixels in Chromium, about 16.7 million in Safari on a phone). A canvas asked for more gives back an empty picture and no error, so such a picture is first brought down to a size the canvas holds, half the area at a time, and the steps above start from there. Both caps are applied by re-encoding through a `<canvas>`, so pictures are only made smaller in a browser. In a runtime without one (SSR, jsdom/happy-dom tests) a picture within `maxImageBytes` is passed through untouched and one over it is refused, since it cannot be made to fit. The caps apply when a picture is inserted: HTML handed to the editor through `content` or `setHTML` is not checked, so do not rely on them to bound a payload assembled elsewhere.
397
501
  - **Page fit**: a newly inserted picture is displayed at `maxImageDisplayWidth` (624 px) rather than filling the editor. RTF records the display size in twips — 624 px × 15 = 9360 twips = 6.5 in, the text width of a Letter page with 1 in margins — so pictures arrive sized to the page they will be printed or filed on, not to the width of the browser window. Resize handles still go up to the full column width when a picture deserves it.
398
502
 
399
503
  ### Sending RTF over a transport
@@ -424,7 +528,7 @@ The transport cares about the size of the finished RTF, and pictures make that u
424
528
  <button onclick={() => console.log(editor.getRtfSize())}>Check size</button>
425
529
  ```
426
530
 
427
- `getRtfSize()` does the full conversion — call it when a decision depends on it, not on every keystroke. `estimatedRtfBytes` counts picture data exactly (two characters per byte, which is what dominates) and approximates the rest, leaving an error of a few hundred bytes at most. The output is ASCII, so bytes and characters are the same number.
531
+ `getRtfSize()` does the full conversion — call it when a decision depends on it, not on every keystroke. `estimatedRtfBytes` counts picture data exactly (two characters per byte, which is what dominates) and approximates the rest, never below the real size and, for what the editor itself makes, a few hundred bytes above it at most. (A pasted page of many small elements is counted further above, a few bytes for each.) The editor passes its own `fontFamily` and `fontSize`; calling `estimateRtfBytes` yourself, pass the options you give `htmlToRtf`. The output is ASCII, so bytes and characters are the same number.
428
532
 
429
533
  The library reports the size and stops there; whether an oversized document may be sent is the host application's decision.
430
534
 
@@ -449,15 +553,18 @@ Commit both your code and the generated `.changeset/*.md` file together.
449
553
 
450
554
  ### Checks
451
555
 
452
- Three commands must pass before a change is merged, and CI (`.github/workflows/ci.yml`) runs them on every push and pull request:
556
+ Four commands must pass before a change is merged, and CI (`.github/workflows/ci.yml`) runs them on every push and pull request:
453
557
 
454
558
  ```bash
455
- npm run check # svelte-check: types
456
- npm test # vitest
457
- npm run prepack # build the package and lint it with publint
559
+ npm run check # svelte-check: types
560
+ npm run test:coverage # vitest, with a floor under the coverage (vite.config.ts)
561
+ npm run prepack # build the package and lint it with publint
562
+ npm run test:e2e # Playwright, in Chromium (once: npx playwright install chromium)
458
563
  ```
459
564
 
460
- The publish workflow runs the same three before it publishes, so a release cannot go out with a failing test.
565
+ `npm test` runs the same unit tests without measuring coverage. They run in a test DOM, which has no caret, no editing commands and no clipboard; `e2e/` holds what only a browser shows: where the first words typed go, what a key does after a dialog closes, what a real paste brings in. It drives the page at `src/routes/e2e`.
566
+
567
+ The publish workflow runs the same four before it publishes, so a release cannot go out with a failing test.
461
568
 
462
569
  `src/lib/__tests__/random-documents.test.ts` builds 400 documents at random on each run and checks what must hold for all of them: no text lost, the RTF settles after one reload, the size estimate is never low, the HTML read back is made of known elements. For a longer run, in batches:
463
570
 
@@ -39,7 +39,13 @@
39
39
  let copied = $state(false);
40
40
 
41
41
  async function copyRtf() {
42
- await navigator.clipboard.writeText(content ?? "");
42
+ // The clipboard can be refused (no permission, a page that is not served securely):
43
+ // then nothing was copied, and the button does not say it was.
44
+ try {
45
+ await navigator.clipboard.writeText(content ?? "");
46
+ } catch {
47
+ return;
48
+ }
43
49
  copied = true;
44
50
  setTimeout(() => (copied = false), 2000);
45
51
  }
@@ -16,6 +16,8 @@
16
16
  target?: HTMLImageElement | null;
17
17
  /** Positioned element the overlay coordinates are relative to. */
18
18
  bounds?: HTMLElement | null;
19
+ /** The editing area. It scrolls sideways, and the frame is not drawn outside it. */
20
+ within?: HTMLElement | null;
19
21
  /** Widest the image may become — the editor's content box width in px. */
20
22
  maxWidth?: () => number;
21
23
  onchange?: () => void;
@@ -23,7 +25,7 @@
23
25
  oncaption?: (img: HTMLImageElement) => void;
24
26
  }
25
27
 
26
- let { target = null, bounds = null, maxWidth, onchange, onremove, oncaption }: Props = $props();
28
+ let { target = null, bounds = null, within = null, maxWidth, onchange, onremove, oncaption }: Props = $props();
27
29
 
28
30
  const MIN_WIDTH = 40;
29
31
  const CORNERS = ['nw', 'ne', 'sw', 'se'] as const;
@@ -41,8 +43,14 @@
41
43
  }
42
44
  const t = target.getBoundingClientRect();
43
45
  const b = bounds.getBoundingClientRect();
44
- frame = { left: t.left - b.left, top: t.top - b.top, width: t.width, height: t.height };
45
- visible = t.width > 0 && t.height > 0;
46
+ // A picture in a wide table can be scrolled part or all of the way out of the editing
47
+ // area. The frame is drawn over the part of it still in view, and not at all when none
48
+ // is: it used to follow the picture out over the page beside the editor, tools and all.
49
+ const area = within?.getBoundingClientRect();
50
+ const left = area ? Math.max(t.left, area.left) : t.left;
51
+ const right = area ? Math.min(t.right, area.right) : t.right;
52
+ frame = { left: left - b.left, top: t.top - b.top, width: Math.max(0, right - left), height: t.height };
53
+ visible = t.width > 0 && t.height > 0 && right > left;
46
54
  }
47
55
 
48
56
  $effect(() => {
@@ -14,6 +14,8 @@ interface Props {
14
14
  target?: HTMLImageElement | null;
15
15
  /** Positioned element the overlay coordinates are relative to. */
16
16
  bounds?: HTMLElement | null;
17
+ /** The editing area. It scrolls sideways, and the frame is not drawn outside it. */
18
+ within?: HTMLElement | null;
17
19
  /** Widest the image may become — the editor's content box width in px. */
18
20
  maxWidth?: () => number;
19
21
  onchange?: () => void;