svelte-rtf-editor 1.12.0 → 1.13.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 +104 -11
- package/dist/RtfViewer.svelte +9 -4
- package/dist/RtfViewer.svelte.d.ts +1 -0
- package/dist/components/InkEditor.svelte +552 -44
- package/dist/components/InkEditor.svelte.d.ts +4 -0
- package/dist/components/Modal.svelte +9 -13
- package/dist/components/Toolbar.svelte +71 -16
- package/dist/components/Toolbar.svelte.d.ts +11 -0
- package/dist/list-markers.d.ts +38 -0
- package/dist/list-markers.js +129 -0
- package/dist/paste-pictures.d.ts +22 -0
- package/dist/paste-pictures.js +57 -0
- package/dist/rtf-parser.d.ts +23 -0
- package/dist/rtf-parser.js +186 -17
- package/dist/rtf-writer.d.ts +55 -3
- package/dist/rtf-writer.js +257 -32
- package/dist/sanitize.d.ts +36 -0
- package/dist/sanitize.js +78 -5
- package/dist/utils.js +22 -1
- package/package.json +1 -1
package/dist/rtf-parser.js
CHANGED
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
*
|
|
12
12
|
* \cb (character background) is silently ignored on import.
|
|
13
13
|
*/
|
|
14
|
-
import { HEADING_SCALE, PRE_SCALE, LINK_COLOR, isMonospaceFontName, linkTarget, normalizeFontSize } from './rtf-writer.js';
|
|
14
|
+
import { HEADING_SCALE, PRE_SCALE, LINK_COLOR, DEFAULT_FONT_FAMILY, fontChoices, fontKey, isMonospaceFontName, linkTarget, normalizeFontSize, hardToReadOnWhite } from './rtf-writer.js';
|
|
15
|
+
import { letterReadings, listTypeAttribute } from './list-markers.js';
|
|
15
16
|
// ── Highlight palette (RTF \highlight 1-16) ──
|
|
16
17
|
const HIGHLIGHT_COLORS = {
|
|
17
18
|
1: '#000000', // Black
|
|
@@ -57,6 +58,8 @@ const HEX_NIBBLE = (() => {
|
|
|
57
58
|
const TAB_HTML = '<span style="white-space:pre">\t</span>';
|
|
58
59
|
/** Text so far holding only a list marker: a bullet, or a number and a period. */
|
|
59
60
|
const LIST_MARKER = /^\s*(?:\u2022|(\d+)\.)\s*$/;
|
|
61
|
+
/** The same for an item numbered in letters or Roman numerals: "C.", "iv.". */
|
|
62
|
+
const LETTER_MARKER = /^\s*([A-Z]{1,15}|[a-z]{1,15})\.\s*$/;
|
|
60
63
|
/**
|
|
61
64
|
* The bullet Word writes inside {\listtext …} and {\pntext …}: byte B7 of the Symbol font, which
|
|
62
65
|
* arrives as a middle dot or as the font's private-use character.
|
|
@@ -209,6 +212,42 @@ function renderPicture(group) {
|
|
|
209
212
|
const style = styles.length ? ` style="${styles.join(';')}"` : '';
|
|
210
213
|
return `<img src="data:${mime};base64,${base64}"${style} alt="">`;
|
|
211
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* The pictures of an RTF document, in the order they are in it: each as the data address a
|
|
217
|
+
* browser shows (PNG or JPEG), or null for one it cannot show (a metafile).
|
|
218
|
+
*
|
|
219
|
+
* Word copies a picture to the clipboard twice: in its HTML as the address of a file on the
|
|
220
|
+
* writer's own disk, which no page may open, and in its RTF as the picture itself. So the
|
|
221
|
+
* pictures of a paste are read from here (see pastePictures). The second copy Word writes of
|
|
222
|
+
* each for older readers (\nonshppict, and a shape's \shprslt) is not counted.
|
|
223
|
+
*/
|
|
224
|
+
export function rtfPictures(rtf) {
|
|
225
|
+
let tree;
|
|
226
|
+
try {
|
|
227
|
+
tree = parseTree(rtf);
|
|
228
|
+
}
|
|
229
|
+
catch {
|
|
230
|
+
return [];
|
|
231
|
+
}
|
|
232
|
+
const pictures = [];
|
|
233
|
+
const todo = [tree];
|
|
234
|
+
while (todo.length > 0) {
|
|
235
|
+
const group = todo.pop();
|
|
236
|
+
const first = group.children.find((n) => n.type === 'control' && n.word !== '*');
|
|
237
|
+
if (first?.word === 'nonshppict' || first?.word === 'shprslt')
|
|
238
|
+
continue;
|
|
239
|
+
if (first?.word === 'pict') {
|
|
240
|
+
pictures.push(/ src="([^"]+)"/.exec(renderPicture(group))?.[1] ?? null);
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
for (let i = group.children.length - 1; i >= 0; i--) {
|
|
244
|
+
const child = group.children[i];
|
|
245
|
+
if (child.type === 'group')
|
|
246
|
+
todo.push(child);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return pictures;
|
|
250
|
+
}
|
|
212
251
|
// ── Tokenizer ──
|
|
213
252
|
/**
|
|
214
253
|
* What \'80 to \'9f stand for in Windows-1252, the code page RTF's \ansi text is written in:
|
|
@@ -508,6 +547,7 @@ function symbolText(text) {
|
|
|
508
547
|
function readFontTable(group) {
|
|
509
548
|
const mono = new Set();
|
|
510
549
|
const symbol = new Set();
|
|
550
|
+
const names = new Map();
|
|
511
551
|
let num = null;
|
|
512
552
|
let name = '';
|
|
513
553
|
let fixed = false;
|
|
@@ -518,6 +558,7 @@ function readFontTable(group) {
|
|
|
518
558
|
mono.add(num);
|
|
519
559
|
if (symbolSet && isSymbolFontName(name))
|
|
520
560
|
symbol.add(num);
|
|
561
|
+
names.set(num, name.trim());
|
|
521
562
|
}
|
|
522
563
|
num = null;
|
|
523
564
|
name = '';
|
|
@@ -557,7 +598,20 @@ function readFontTable(group) {
|
|
|
557
598
|
}
|
|
558
599
|
read(group.children.filter((n) => n.type !== 'group'));
|
|
559
600
|
finish();
|
|
560
|
-
return { mono, symbol };
|
|
601
|
+
return { mono, symbol, names };
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* A font name as programs that name each cut of a font write it, brought back to the family:
|
|
605
|
+
* TextEdit's "Helvetica-Bold", "TimesNewRomanPS-ItalicMT" and "ArialMT" are Helvetica, Times New
|
|
606
|
+
* Roman and Arial. Without spaces or capitals, to be compared with a name taken the same way.
|
|
607
|
+
*/
|
|
608
|
+
function looseFontKey(name) {
|
|
609
|
+
return name
|
|
610
|
+
.toLowerCase()
|
|
611
|
+
.replace(/-(regular|roman|book|light|medium|semibold|demibold|bold|heavy|black|italic|oblique|bolditalic|boldoblique)[a-z]*$/, '')
|
|
612
|
+
.replace(/(ps)?mt$/, '')
|
|
613
|
+
.replace(/ps$/, '')
|
|
614
|
+
.replace(/[\s_-]/g, '');
|
|
561
615
|
}
|
|
562
616
|
// ── Links ──
|
|
563
617
|
/** The longest field instruction looked at for a link's address. */
|
|
@@ -741,6 +795,7 @@ function sameFormatButSize(a, b) {
|
|
|
741
795
|
a.color === b.color &&
|
|
742
796
|
a.background === b.background &&
|
|
743
797
|
a.mono === b.mono &&
|
|
798
|
+
a.font === b.font &&
|
|
744
799
|
a.link === b.link);
|
|
745
800
|
}
|
|
746
801
|
export function rtfToHtml(rtfString, options = {}) {
|
|
@@ -752,6 +807,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
752
807
|
let colorTable = [''];
|
|
753
808
|
let monoFonts = new Set();
|
|
754
809
|
let symbolFonts = new Set();
|
|
810
|
+
let fontNames = new Map();
|
|
755
811
|
let defaultFont = 0;
|
|
756
812
|
/** Where finished paragraphs and tables go: the document, or the footnote or text box being read. */
|
|
757
813
|
let blocks = [];
|
|
@@ -811,7 +867,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
811
867
|
/** \nestcell seen: a tab is owed if more text follows on the line. */
|
|
812
868
|
let nestTabPending = false;
|
|
813
869
|
function newCellDef() {
|
|
814
|
-
return { right: 0, header: false, vmgf: false, vmrg: false, hmgf: false, hmrg: false };
|
|
870
|
+
return { right: 0, header: false, vmgf: false, vmrg: false, hmgf: false, hmrg: false, shade: '' };
|
|
815
871
|
}
|
|
816
872
|
// The document is the first group. A byte-order mark or blank lines before it (a file saved
|
|
817
873
|
// by a tool that adds them) do not make it something else: with those the color and font
|
|
@@ -832,7 +888,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
832
888
|
colorTable = parseColorTable(child);
|
|
833
889
|
}
|
|
834
890
|
else if (firstControl?.word === 'fonttbl') {
|
|
835
|
-
({ mono: monoFonts, symbol: symbolFonts } = readFontTable(child));
|
|
891
|
+
({ mono: monoFonts, symbol: symbolFonts, names: fontNames } = readFontTable(child));
|
|
836
892
|
}
|
|
837
893
|
}
|
|
838
894
|
else if (child.type === 'control' && child.word === 'deff' && child.param != null) {
|
|
@@ -841,6 +897,27 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
841
897
|
}
|
|
842
898
|
// In a document whose own font is monospace, code cannot be told from text by its font.
|
|
843
899
|
const documentIsMono = monoFonts.has(defaultFont);
|
|
900
|
+
// The fonts the application offers. Text in any other font, and text in the font the screen
|
|
901
|
+
// shows the document in, carries no font.
|
|
902
|
+
const offered = fontChoices(options.fonts);
|
|
903
|
+
const screenFont = fontKey(options.fontFamily) || fontKey(DEFAULT_FONT_FAMILY);
|
|
904
|
+
const fontCss = new Map();
|
|
905
|
+
/** The font with this number in the font table, as CSS, if it is one text is shown in. */
|
|
906
|
+
function fontOf(index) {
|
|
907
|
+
if (offered.length === 0)
|
|
908
|
+
return '';
|
|
909
|
+
let css = fontCss.get(index);
|
|
910
|
+
if (css === undefined) {
|
|
911
|
+
const name = fontNames.get(index) ?? '';
|
|
912
|
+
const key = fontKey(name);
|
|
913
|
+
const loose = looseFontKey(name);
|
|
914
|
+
const isScreenFont = key === screenFont || loose === looseFontKey(screenFont);
|
|
915
|
+
const choice = offered.find((font) => font.key === key) ?? offered.find((font) => looseFontKey(font.key) === loose);
|
|
916
|
+
css = !key || isScreenFont || !choice ? '' : choice.css;
|
|
917
|
+
fontCss.set(index, css);
|
|
918
|
+
}
|
|
919
|
+
return css;
|
|
920
|
+
}
|
|
844
921
|
const linkColor = `rgb(${LINK_COLOR.r},${LINK_COLOR.g},${LINK_COLOR.b})`;
|
|
845
922
|
// The screen's body size, in half-points. It is given, not guessed from the
|
|
846
923
|
// document: guessing misread short documents and ones set below 12 pt.
|
|
@@ -859,6 +936,8 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
859
936
|
fontSize: 24, // 12pt default
|
|
860
937
|
colorIndex: 0,
|
|
861
938
|
highlightIndex: 0,
|
|
939
|
+
backIndex: 0,
|
|
940
|
+
paragraphBackIndex: 0,
|
|
862
941
|
fontIndex: defaultFont,
|
|
863
942
|
ucSkip: 1,
|
|
864
943
|
inList: false,
|
|
@@ -878,8 +957,26 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
878
957
|
function cloneState() {
|
|
879
958
|
return { ...currentState() };
|
|
880
959
|
}
|
|
960
|
+
/** A color of the table as the background it is: none for the automatic color, and none for white, which the page is. */
|
|
961
|
+
function shadeAt(index) {
|
|
962
|
+
const color = index > 0 && index < colorTable.length ? colorTable[index] : '';
|
|
963
|
+
return color === 'rgb(255,255,255)' ? '' : color;
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Shading is not shown: every word is on the white of the page. A color given to words so
|
|
967
|
+
* that they could be read on their shading (the white header of a table whose header row is
|
|
968
|
+
* dark, a white title on a shaded bar) cannot be read without it, and the cell or the line
|
|
969
|
+
* looked empty. Such words are shown in the page's own color. A color that can be read on
|
|
970
|
+
* white stays, shaded or not.
|
|
971
|
+
*/
|
|
972
|
+
function readsOnWhite(color) {
|
|
973
|
+
const parts = color.match(/^rgb\((\d+),(\d+),(\d+)\)$/);
|
|
974
|
+
return !parts || !hardToReadOnWhite({ r: +parts[1], g: +parts[2], b: +parts[3] });
|
|
975
|
+
}
|
|
881
976
|
function formatOf(state) {
|
|
882
977
|
let color = state.colorIndex > 0 && state.colorIndex < colorTable.length ? colorTable[state.colorIndex] : '';
|
|
978
|
+
if (color && (shadeAt(state.backIndex) || shadeAt(state.paragraphBackIndex)) && !readsOnWhite(color))
|
|
979
|
+
color = '';
|
|
883
980
|
// Black is the color text has anyway, and a link's own color is the <a>'s to give.
|
|
884
981
|
if (color === 'rgb(0,0,0)' || (state.link && color === linkColor))
|
|
885
982
|
color = '';
|
|
@@ -897,6 +994,8 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
897
994
|
// A list marker is set in whatever font the list uses (Word's second-level "o" is in
|
|
898
995
|
// Courier New): it is not code.
|
|
899
996
|
mono: !documentIsMono && !state.inList && monoFonts.has(state.fontIndex),
|
|
997
|
+
// (Not a list marker's, as above: Word sets its bullets in Symbol and Wingdings.)
|
|
998
|
+
font: state.inList ? '' : fontOf(state.fontIndex),
|
|
900
999
|
link: state.link
|
|
901
1000
|
};
|
|
902
1001
|
}
|
|
@@ -910,13 +1009,17 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
910
1009
|
const onlySpaces = ONLY_SPACES.test(text);
|
|
911
1010
|
let result = escapeText(text);
|
|
912
1011
|
const styles = [];
|
|
913
|
-
|
|
1012
|
+
// (A highlight is shown, and its words are read on it.)
|
|
1013
|
+
if (fmt.color && (!opts.onWhite || fmt.background || readsOnWhite(fmt.color)))
|
|
914
1014
|
styles.push(`color:${fmt.color}`);
|
|
915
1015
|
if (fmt.background)
|
|
916
1016
|
styles.push(`background-color:${fmt.background}`);
|
|
917
1017
|
if (!onlySpaces && fmt.fontSize !== bodySize && fmt.fontSize !== opts.dropSize) {
|
|
918
1018
|
styles.push(`font-size:${fmt.fontSize / 2}pt`);
|
|
919
1019
|
}
|
|
1020
|
+
// (Code is in the code font: a font named for it would be the one it is shown in.)
|
|
1021
|
+
if (!onlySpaces && fmt.font && !fmt.mono && !opts.pre)
|
|
1022
|
+
styles.push(`font-family:${escapeAttribute(fmt.font)}`);
|
|
920
1023
|
if (styles.length > 0)
|
|
921
1024
|
result = `<span style="${styles.join(';')}">${result}</span>`;
|
|
922
1025
|
if (fmt.mono && !opts.pre)
|
|
@@ -1113,6 +1216,12 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1113
1216
|
appendText(text.slice(from), state);
|
|
1114
1217
|
}
|
|
1115
1218
|
/** The text of the line being read, back to the last line break, if text is all it holds. */
|
|
1219
|
+
/** The cell being read, with the colors left out that cannot be read on white; undefined where it has none. */
|
|
1220
|
+
function cellOnWhite() {
|
|
1221
|
+
if (!runs.some((run) => run.fmt.color && !run.fmt.background && !readsOnWhite(run.fmt.color)))
|
|
1222
|
+
return undefined;
|
|
1223
|
+
return trimSpaces(renderRuns(runs, { onWhite: true }));
|
|
1224
|
+
}
|
|
1116
1225
|
function lineText() {
|
|
1117
1226
|
let text = '';
|
|
1118
1227
|
for (let i = runs.length - 1; i >= 0; i--) {
|
|
@@ -1148,6 +1257,20 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1148
1257
|
}
|
|
1149
1258
|
return;
|
|
1150
1259
|
}
|
|
1260
|
+
// "C." or "iv.", which is a marker only on a paragraph set as the writer sets a list
|
|
1261
|
+
// item: on any other line it is what someone typed ("A.", a tab, the specimen), and
|
|
1262
|
+
// the tab after it stays a tab.
|
|
1263
|
+
const lettered = opensParagraph && lead != null ? lead.match(LETTER_MARKER) : null;
|
|
1264
|
+
if (lettered &&
|
|
1265
|
+
state.firstLineIndent === -LIST_HANG &&
|
|
1266
|
+
state.leftIndent >= LIST_HANG &&
|
|
1267
|
+
state.rightIndent === 0 &&
|
|
1268
|
+
(state.align || 'left') === 'left' &&
|
|
1269
|
+
letterReadings(lettered[1]).length > 0) {
|
|
1270
|
+
pushHtml('gap', ' ', state);
|
|
1271
|
+
marker = { kind: 'ol', number: 0, letters: lettered[1], end: runs.length };
|
|
1272
|
+
return;
|
|
1273
|
+
}
|
|
1151
1274
|
pushHtml('tab', TAB_HTML, state);
|
|
1152
1275
|
// Word's bullet, inside {\listtext …} or {\pntext …}.
|
|
1153
1276
|
if (opensParagraph && lead != null && state.inList && SYMBOL_BULLET.test(lead)) {
|
|
@@ -1169,15 +1292,17 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1169
1292
|
for (let ci = 0; ci < row.cells.length; ci++) {
|
|
1170
1293
|
const def = row.defs[ci] ?? { ...newCellDef(), right: prevRight + (step || 1440) };
|
|
1171
1294
|
const cell = row.cells[ci];
|
|
1295
|
+
// The words of a shaded cell, as they can be read without the shading.
|
|
1296
|
+
const cellHtml = def.shade && cell.onWhite !== undefined ? cell.onWhite : cell.html;
|
|
1172
1297
|
const last = placed[placed.length - 1];
|
|
1173
1298
|
if (def.hmrg && last) {
|
|
1174
1299
|
last.right = Math.max(last.right, def.right);
|
|
1175
|
-
if (
|
|
1176
|
-
last.html += (last.html ? ' ' : '') +
|
|
1300
|
+
if (cellHtml)
|
|
1301
|
+
last.html += (last.html ? ' ' : '') + cellHtml;
|
|
1177
1302
|
}
|
|
1178
1303
|
else {
|
|
1179
1304
|
placed.push({
|
|
1180
|
-
html:
|
|
1305
|
+
html: cellHtml, align: cell.align, def,
|
|
1181
1306
|
left: prevRight, right: Math.max(def.right, prevRight),
|
|
1182
1307
|
start: 0, end: 0, rowspan: 1, merged: false
|
|
1183
1308
|
});
|
|
@@ -1567,7 +1692,15 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1567
1692
|
state.colorIndex = p;
|
|
1568
1693
|
break;
|
|
1569
1694
|
case 'cb':
|
|
1570
|
-
|
|
1695
|
+
case 'chcbpat':
|
|
1696
|
+
state.backIndex = p ?? 0;
|
|
1697
|
+
break;
|
|
1698
|
+
case 'cbpat':
|
|
1699
|
+
state.paragraphBackIndex = p ?? 0;
|
|
1700
|
+
break;
|
|
1701
|
+
case 'clcbpat':
|
|
1702
|
+
pendingDef.shade = shadeAt(p ?? 0);
|
|
1703
|
+
break;
|
|
1571
1704
|
case 'highlight':
|
|
1572
1705
|
if (p != null)
|
|
1573
1706
|
state.highlightIndex = p;
|
|
@@ -1721,6 +1854,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1721
1854
|
}
|
|
1722
1855
|
currentRowCells.push({
|
|
1723
1856
|
html: trimSpaces(renderRuns(runs)),
|
|
1857
|
+
onWhite: cellOnWhite(),
|
|
1724
1858
|
// The alignment of the cell's last paragraph, also where that paragraph
|
|
1725
1859
|
// was ended inside a group ({\pard\intbl\qr 1.0\par}\cell, as this writer
|
|
1726
1860
|
// wrote cells up to 1.9.0).
|
|
@@ -1744,7 +1878,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1744
1878
|
// Text after the row's last \cell is a cell that was not closed, not the
|
|
1745
1879
|
// start of the paragraph after the table.
|
|
1746
1880
|
if (trimSpaces(renderRuns(runs))) {
|
|
1747
|
-
currentRowCells.push({ html: trimSpaces(renderRuns(runs)), align: alignHere(state) });
|
|
1881
|
+
currentRowCells.push({ html: trimSpaces(renderRuns(runs)), onWhite: cellOnWhite(), align: alignHere(state) });
|
|
1748
1882
|
newRuns();
|
|
1749
1883
|
}
|
|
1750
1884
|
// A row with no definition of its own repeats the one before it.
|
|
@@ -1778,6 +1912,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1778
1912
|
// \pard resets the paragraph, not the characters: bold, italic, underline
|
|
1779
1913
|
// and strikethrough carry on until \plain or their own \b0. Word relies on it.
|
|
1780
1914
|
state.align = 'left';
|
|
1915
|
+
state.paragraphBackIndex = 0;
|
|
1781
1916
|
resetIndents(state);
|
|
1782
1917
|
// Being in a table is a paragraph property too. Without this, one \intbl with
|
|
1783
1918
|
// no \row made a single cell of the rest of the document. The marker group
|
|
@@ -1864,6 +1999,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1864
1999
|
state.fontSize = 24;
|
|
1865
2000
|
state.colorIndex = 0;
|
|
1866
2001
|
state.highlightIndex = 0;
|
|
2002
|
+
state.backIndex = 0;
|
|
1867
2003
|
state.fontIndex = defaultFont;
|
|
1868
2004
|
state.hidden = false;
|
|
1869
2005
|
state.deleted = false;
|
|
@@ -1946,6 +2082,7 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1946
2082
|
return {
|
|
1947
2083
|
kind: b.marker.kind,
|
|
1948
2084
|
number: b.marker.number,
|
|
2085
|
+
letters: b.marker.letters,
|
|
1949
2086
|
left: b.left,
|
|
1950
2087
|
html: trimSpaces(renderRuns(b.runs.slice(b.marker.end)))
|
|
1951
2088
|
};
|
|
@@ -1955,6 +2092,37 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1955
2092
|
const out = [];
|
|
1956
2093
|
let html = '';
|
|
1957
2094
|
const open = [];
|
|
2095
|
+
/**
|
|
2096
|
+
* The number an item has and the way its list counts. Letters can be read two ways: "C"
|
|
2097
|
+
* is the third letter and the Roman 100. It is the one that goes on with the list the
|
|
2098
|
+
* item is in; where it opens a list, the one the next item at its level goes on with
|
|
2099
|
+
* ("D." or "CI."); and with neither to go by, a Roman numeral for "I" and for several
|
|
2100
|
+
* letters, a letter for any other.
|
|
2101
|
+
*/
|
|
2102
|
+
const readingOf = (at, top) => {
|
|
2103
|
+
const item = list[at];
|
|
2104
|
+
if (!item.letters)
|
|
2105
|
+
return { style: 'decimal', number: item.number };
|
|
2106
|
+
const readings = letterReadings(item.letters);
|
|
2107
|
+
if (top && top.kind === 'ol' && item.left === top.left) {
|
|
2108
|
+
const goesOn = readings.find((r) => r.style === top.style && r.number === top.next);
|
|
2109
|
+
if (goesOn)
|
|
2110
|
+
return goesOn;
|
|
2111
|
+
}
|
|
2112
|
+
if (readings.length === 1)
|
|
2113
|
+
return readings[0];
|
|
2114
|
+
for (let i = at + 1; i < list.length && list[i].left >= item.left; i++) {
|
|
2115
|
+
if (list[i].left > item.left)
|
|
2116
|
+
continue;
|
|
2117
|
+
const after = list[i].letters ? letterReadings(list[i].letters) : [];
|
|
2118
|
+
const led = readings.find((r) => after.some((a) => a.style === r.style && a.number === r.number + 1));
|
|
2119
|
+
if (led)
|
|
2120
|
+
return led;
|
|
2121
|
+
break;
|
|
2122
|
+
}
|
|
2123
|
+
const roman = item.letters.length > 1 || /^[Ii]$/.test(item.letters);
|
|
2124
|
+
return readings.find((r) => r.style.endsWith(roman ? 'roman' : 'alpha')) ?? readings[0];
|
|
2125
|
+
};
|
|
1958
2126
|
const closeList = () => {
|
|
1959
2127
|
const done = open.pop();
|
|
1960
2128
|
html += `</li></${done.kind}>`;
|
|
@@ -1963,14 +2131,15 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1963
2131
|
html = '';
|
|
1964
2132
|
}
|
|
1965
2133
|
};
|
|
1966
|
-
for (const item of list) {
|
|
2134
|
+
for (const [at, item] of list.entries()) {
|
|
1967
2135
|
// A smaller indent ends the lists nested deeper than it.
|
|
1968
2136
|
while (open.length > 0 && item.left < open[open.length - 1].left)
|
|
1969
2137
|
closeList();
|
|
1970
2138
|
const top = open[open.length - 1];
|
|
1971
|
-
|
|
2139
|
+
const { style, number } = readingOf(at, top);
|
|
2140
|
+
if (top && item.left === top.left && item.kind === top.kind && (item.kind === 'ul' || (number === top.next && style === top.style))) {
|
|
1972
2141
|
html += '</li><li>';
|
|
1973
|
-
top.next =
|
|
2142
|
+
top.next = number + 1;
|
|
1974
2143
|
}
|
|
1975
2144
|
else {
|
|
1976
2145
|
// A change from bullets to numbers, or a number out of sequence, is a new list
|
|
@@ -1980,10 +2149,10 @@ export function rtfToHtml(rtfString, options = {}) {
|
|
|
1980
2149
|
const parent = open[open.length - 1];
|
|
1981
2150
|
// What the indent adds to the half inch a list level has by itself.
|
|
1982
2151
|
const margin = item.left - (parent ? parent.left : 0) - LIST_STEP;
|
|
1983
|
-
const
|
|
1984
|
-
const
|
|
1985
|
-
html += `<${item.kind}${start}${style}><li>`;
|
|
1986
|
-
open.push({ kind: item.kind, left: item.left, next:
|
|
2152
|
+
const start = item.kind === 'ol' && number !== 1 ? ` start="${number}"` : '';
|
|
2153
|
+
const type = item.kind === 'ol' && style !== 'decimal' ? ` type="${listTypeAttribute(style)}"` : '';
|
|
2154
|
+
html += `<${item.kind}${type}${start}${margin !== 0 ? ` style="margin-left:${twipsToPt(margin)}"` : ''}><li>`;
|
|
2155
|
+
open.push({ kind: item.kind, left: item.left, next: number + 1, style });
|
|
1987
2156
|
}
|
|
1988
2157
|
html += item.html || '<br>';
|
|
1989
2158
|
}
|
package/dist/rtf-writer.d.ts
CHANGED
|
@@ -12,18 +12,33 @@
|
|
|
12
12
|
*
|
|
13
13
|
* Background color is intentionally not written — RTF highlight support
|
|
14
14
|
* is inconsistent across viewers (TextEdit uses \AppleHighlight, not \highlight).
|
|
15
|
+
* So a text color that can be read only on its background is not written either: see
|
|
16
|
+
* lostWithoutItsBackground.
|
|
15
17
|
*/
|
|
18
|
+
interface RGB {
|
|
19
|
+
r: number;
|
|
20
|
+
g: number;
|
|
21
|
+
b: number;
|
|
22
|
+
}
|
|
16
23
|
/** Options for htmlToRtf. */
|
|
17
24
|
export interface RtfWriteOptions {
|
|
18
25
|
/**
|
|
19
26
|
* Font for the whole document: body text, headings, lists and tables.
|
|
20
|
-
* Code keeps a monospace font. Default: DEFAULT_FONT_FAMILY.
|
|
27
|
+
* Code keeps a monospace font. Default: DEFAULT_FONT_FAMILY. Given a CSS list, the first
|
|
28
|
+
* family is the font written; the rest is for a screen (see cssFontFamily).
|
|
21
29
|
*/
|
|
22
30
|
fontFamily?: string;
|
|
23
31
|
/**
|
|
24
32
|
* Body text size in points. Headings scale from it. Default: 12.
|
|
25
33
|
*/
|
|
26
34
|
fontSize?: number;
|
|
35
|
+
/**
|
|
36
|
+
* The fonts text may be set in, beside the document's own: the editor's `fonts`. Text in one
|
|
37
|
+
* of them is written in it (a font of its own in the font table); a font that is not named
|
|
38
|
+
* here is not written, and its text is in the document font. Each is a font name, or a CSS
|
|
39
|
+
* list whose first family is the name. Default: none, and every font is the document's.
|
|
40
|
+
*/
|
|
41
|
+
fonts?: readonly string[];
|
|
27
42
|
}
|
|
28
43
|
/**
|
|
29
44
|
* Font used when no fontFamily is given: what the editor and the viewer show, and what the RTF
|
|
@@ -58,6 +73,10 @@ export declare const LINK_COLOR: {
|
|
|
58
73
|
readonly g: 0;
|
|
59
74
|
readonly b: 255;
|
|
60
75
|
};
|
|
76
|
+
/** The contrast of two colors as WCAG counts it: 1 for the same color, 21 for black on white. */
|
|
77
|
+
export declare function colorContrast(a: RGB, b: RGB): number;
|
|
78
|
+
/** Whether words in a color are hard to read on a white page. */
|
|
79
|
+
export declare function hardToReadOnWhite(color: RGB): boolean;
|
|
61
80
|
/**
|
|
62
81
|
* Size htmlToRtf would produce, without doing the conversion.
|
|
63
82
|
*
|
|
@@ -88,6 +107,32 @@ export declare function estimateRtfBytes(editorEl: HTMLElement, options?: RtfWri
|
|
|
88
107
|
export declare function linkTarget(url: string): string | null;
|
|
89
108
|
/** Whether a link address is an absolute web, mail or phone address (see linkTarget). */
|
|
90
109
|
export declare function isSafeLinkUrl(url: string): boolean;
|
|
110
|
+
/** A font an application offers for text (the editor's `fonts`). */
|
|
111
|
+
export interface FontChoice {
|
|
112
|
+
/** Its name, as the toolbar and the RTF's font table give it: "Calibri". */
|
|
113
|
+
name: string;
|
|
114
|
+
/** What text set in it carries in the HTML: the name and what to show where it is missing. */
|
|
115
|
+
css: string;
|
|
116
|
+
/** The name as it is looked up by: without regard to capitals. */
|
|
117
|
+
key: string;
|
|
118
|
+
}
|
|
119
|
+
/** Whether two font values name the same families in the same order, however they are written. */
|
|
120
|
+
export declare function sameFontList(a: unknown, b: unknown): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* The name a font value is looked up by: its first family, without quotes and without regard to
|
|
123
|
+
* capitals. '' where there is none.
|
|
124
|
+
*/
|
|
125
|
+
export declare function fontKey(value: unknown): string;
|
|
126
|
+
/**
|
|
127
|
+
* The fonts an application offers, as the editor, the sanitizer, the writer and the reader use
|
|
128
|
+
* them. Each is given as a name ("Calibri") or as a CSS list whose first family is the name
|
|
129
|
+
* ("Calibri, Carlito, sans-serif"): the rest of the list is what a screen without that font
|
|
130
|
+
* shows the text in. A name alone is given the generic family that suits it, so that such a
|
|
131
|
+
* screen does not fall back to the browser's own font. A name that could not be written in a
|
|
132
|
+
* font table or a style (it has a brace, a semicolon, a bracket) is left out, and so is a
|
|
133
|
+
* generic family by itself and a name given twice.
|
|
134
|
+
*/
|
|
135
|
+
export declare function fontChoices(fonts: unknown): FontChoice[];
|
|
91
136
|
/**
|
|
92
137
|
* Whether a font name is a monospace font's: Courier New, Consolas, Menlo, a "… Mono" family
|
|
93
138
|
* (Roboto Mono, DejaVu Sans Mono), and their PostScript names (CourierNewPSMT, SFMono-Regular).
|
|
@@ -95,8 +140,14 @@ export declare function isSafeLinkUrl(url: string): boolean;
|
|
|
95
140
|
*/
|
|
96
141
|
export declare function isMonospaceFontName(name: string): boolean;
|
|
97
142
|
/**
|
|
98
|
-
* CSS font-family value for
|
|
99
|
-
*
|
|
143
|
+
* CSS font-family value for the document font: the name, with the generic fallback that matches
|
|
144
|
+
* it, e.g. "Arial", sans-serif.
|
|
145
|
+
*
|
|
146
|
+
* Given a CSS list, the first family is the font (the one the RTF names) and the rest is what a
|
|
147
|
+
* screen without that font shows the document in, in that order: "Segoe UI, Selawik, sans-serif"
|
|
148
|
+
* is Segoe UI where it is installed and Selawik where the page has loaded that. A list that does
|
|
149
|
+
* not end in a generic family is given the one that matches the font. A family that is no font
|
|
150
|
+
* name (it has a brace, a semicolon, a bracket) is left out.
|
|
100
151
|
*/
|
|
101
152
|
export declare function cssFontFamily(name: string | undefined): string;
|
|
102
153
|
/**
|
|
@@ -107,3 +158,4 @@ export declare function cssFontFamily(name: string | undefined): string;
|
|
|
107
158
|
*/
|
|
108
159
|
export declare function normalizeFontSize(fontSize: number | undefined): number;
|
|
109
160
|
export declare function htmlToRtf(editorEl: HTMLElement, options?: RtfWriteOptions): string;
|
|
161
|
+
export {};
|