svelte-rtf-editor 1.11.0 → 1.12.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 +27 -4
- package/dist/components/InkEditor.svelte +779 -85
- package/dist/components/InkEditor.svelte.d.ts +1 -0
- package/dist/components/Toolbar.svelte +4 -4
- package/dist/rtf-writer.js +36 -21
- package/dist/sanitize.d.ts +19 -4
- package/dist/sanitize.js +150 -51
- package/package.json +1 -1
|
@@ -57,6 +57,7 @@ declare const InkEditor: import("svelte").Component<Props, {
|
|
|
57
57
|
exportFile: (format?: string) => void;
|
|
58
58
|
importRtf: () => void;
|
|
59
59
|
setFontSize: (points: number) => void;
|
|
60
|
+
clearFormatting: () => void;
|
|
60
61
|
}, "">;
|
|
61
62
|
type InkEditor = ReturnType<typeof InkEditor>;
|
|
62
63
|
export default InkEditor;
|
|
@@ -104,9 +104,12 @@
|
|
|
104
104
|
|
|
105
105
|
<div class="toolbar-divider"></div>
|
|
106
106
|
|
|
107
|
-
<!--
|
|
107
|
+
<!-- Color, and all of the above taken off again -->
|
|
108
108
|
<div class="toolbar-group">
|
|
109
109
|
<ColorPicker value="#2C2520" title="Text color" onchange={(c) => exec('foreColor', c)} />
|
|
110
|
+
<ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
|
|
111
|
+
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/><line x1="18" y1="4" x2="4" y2="20" stroke="var(--accent)" stroke-width="2"/></svg>
|
|
112
|
+
</ToolbarButton>
|
|
110
113
|
</div>
|
|
111
114
|
|
|
112
115
|
<div class="toolbar-divider"></div>
|
|
@@ -197,9 +200,6 @@
|
|
|
197
200
|
<ToolbarButton title="Redo" onclick={() => exec('redo')}>
|
|
198
201
|
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M21 7v6h-6"/><path d="M21 13c0 0-2.03-7-9-7a9 9 0 0 0-9 9 9 9 0 0 0 9 9c3.5 0 6-1.5 7.5-4"/></svg>
|
|
199
202
|
</ToolbarButton>
|
|
200
|
-
<ToolbarButton title="Clear formatting" onclick={() => exec('removeFormat')}>
|
|
201
|
-
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M4 7V4h16v3"/><path d="M9 20h6"/><path d="M12 4v16"/><line x1="18" y1="4" x2="4" y2="20" stroke="var(--accent)" stroke-width="2"/></svg>
|
|
202
|
-
</ToolbarButton>
|
|
203
203
|
</div>
|
|
204
204
|
</div>
|
|
205
205
|
|
package/dist/rtf-writer.js
CHANGED
|
@@ -88,20 +88,26 @@ function parseColor(colorStr) {
|
|
|
88
88
|
};
|
|
89
89
|
}
|
|
90
90
|
// Basic named colors
|
|
91
|
-
|
|
92
|
-
black: { r: 0, g: 0, b: 0 },
|
|
93
|
-
white: { r: 255, g: 255, b: 255 },
|
|
94
|
-
red: { r: 255, g: 0, b: 0 },
|
|
95
|
-
green: { r: 0, g: 128, b: 0 },
|
|
96
|
-
blue: { r: 0, g: 0, b: 255 },
|
|
97
|
-
yellow: { r: 255, g: 255, b: 0 },
|
|
98
|
-
orange: { r: 255, g: 165, b: 0 },
|
|
99
|
-
purple: { r: 128, g: 0, b: 128 },
|
|
100
|
-
gray: { r: 128, g: 128, b: 128 },
|
|
101
|
-
grey: { r: 128, g: 128, b: 128 }
|
|
102
|
-
};
|
|
103
|
-
return named[s] || null;
|
|
91
|
+
return NAMED_COLORS.get(s) ?? null;
|
|
104
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* The colors known by name. A Map, like every table here that is asked for a name out of the
|
|
95
|
+
* document (a color, a tag): an object also answers to the names every object has, so
|
|
96
|
+
* <font color="constructor"> was a color, and went into the color table as
|
|
97
|
+
* "\redundefined\greenundefined\blueundefined".
|
|
98
|
+
*/
|
|
99
|
+
const NAMED_COLORS = new Map(Object.entries({
|
|
100
|
+
black: { r: 0, g: 0, b: 0 },
|
|
101
|
+
white: { r: 255, g: 255, b: 255 },
|
|
102
|
+
red: { r: 255, g: 0, b: 0 },
|
|
103
|
+
green: { r: 0, g: 128, b: 0 },
|
|
104
|
+
blue: { r: 0, g: 0, b: 255 },
|
|
105
|
+
yellow: { r: 255, g: 255, b: 0 },
|
|
106
|
+
orange: { r: 255, g: 165, b: 0 },
|
|
107
|
+
purple: { r: 128, g: 0, b: 128 },
|
|
108
|
+
gray: { r: 128, g: 128, b: 128 },
|
|
109
|
+
grey: { r: 128, g: 128, b: 128 }
|
|
110
|
+
}));
|
|
105
111
|
function colorKey(c) {
|
|
106
112
|
return `${c.r},${c.g},${c.b}`;
|
|
107
113
|
}
|
|
@@ -451,7 +457,7 @@ const PICTURE_OVERHEAD_BYTES = 80;
|
|
|
451
457
|
* and closing runs measured from walkChildren. Anything not listed costs
|
|
452
458
|
* nothing of its own beyond the text it contains.
|
|
453
459
|
*/
|
|
454
|
-
const BLOCK_OVERHEAD_BYTES = {
|
|
460
|
+
const BLOCK_OVERHEAD_BYTES = new Map(Object.entries({
|
|
455
461
|
p: 14,
|
|
456
462
|
div: 14,
|
|
457
463
|
h1: 29,
|
|
@@ -490,7 +496,16 @@ const BLOCK_OVERHEAD_BYTES = {
|
|
|
490
496
|
hr: 154, // "\pard\qc " + 20 x "\emdash" + "\par "
|
|
491
497
|
ul: 0,
|
|
492
498
|
ol: 0
|
|
493
|
-
};
|
|
499
|
+
}));
|
|
500
|
+
/**
|
|
501
|
+
* What an element of this tag costs, or nothing for one not listed. (Looked up in a Map: asked
|
|
502
|
+
* of an object, an element named <constructor> cost a function, and the estimate, added to
|
|
503
|
+
* that, was a piece of text. An application comparing it with a size limit then let every
|
|
504
|
+
* document through.)
|
|
505
|
+
*/
|
|
506
|
+
function overheadBytes(tag) {
|
|
507
|
+
return BLOCK_OVERHEAD_BYTES.get(tag) ?? 0;
|
|
508
|
+
}
|
|
494
509
|
/**
|
|
495
510
|
* Blocks that are written as a <div> is: a paragraph when they hold text, a container when they
|
|
496
511
|
* hold blocks. Pasted pages are made of them, and each one not known as a block had its lines
|
|
@@ -599,7 +614,7 @@ export function estimateRtfBytes(editorEl, options = {}) {
|
|
|
599
614
|
// A <div> that only holds blocks writes nothing of its own: its blocks are counted.
|
|
600
615
|
const container = tag === 'div' && Array.from(el.children).some(isBlock) && !startsWithInline(el) && !endsWithInline(el);
|
|
601
616
|
if (!container)
|
|
602
|
-
total +=
|
|
617
|
+
total += overheadBytes(tag);
|
|
603
618
|
// A heading and a code block each write their size and the size around them.
|
|
604
619
|
if (HEADING_TAGS.has(tag) || tag === 'pre')
|
|
605
620
|
total += 2 * sizeDigits;
|
|
@@ -667,14 +682,14 @@ export function estimateRtfBytes(editorEl, options = {}) {
|
|
|
667
682
|
// rowspan="0" reaches the last row; no span reaches past the table.
|
|
668
683
|
const rowCount = el.closest('table')?.querySelectorAll('tr').length ?? 1;
|
|
669
684
|
const spanned = Math.min(rowCount, rows > 0 ? rows : rowCount);
|
|
670
|
-
total += 7 + (spanned - 1) * ((
|
|
685
|
+
total += 7 + (spanned - 1) * (overheadBytes(tag) + 7);
|
|
671
686
|
}
|
|
672
687
|
}
|
|
673
688
|
// A row with fewer cells than the table has columns is filled with empty ones.
|
|
674
689
|
if (tag === 'table' && !el.parentElement?.closest('td, th')) {
|
|
675
690
|
const model = tableModel(el);
|
|
676
691
|
for (const row of model.rows)
|
|
677
|
-
total += row.filter((slot) => slot === null).length *
|
|
692
|
+
total += row.filter((slot) => slot === null).length * overheadBytes('td');
|
|
678
693
|
}
|
|
679
694
|
// Anything in a table that is in no cell is written before it, each run a paragraph
|
|
680
695
|
// ("\pard\intbl\qc\liN " and "\par ", at most 27).
|
|
@@ -1185,7 +1200,7 @@ function walkNodeAt(node, ctx, restate, endLooseText) {
|
|
|
1185
1200
|
}
|
|
1186
1201
|
// ── Character formatting ──
|
|
1187
1202
|
/** What a tag by itself does to its text. */
|
|
1188
|
-
const TAG_FORMAT = {
|
|
1203
|
+
const TAG_FORMAT = new Map(Object.entries({
|
|
1189
1204
|
b: { bold: true },
|
|
1190
1205
|
strong: { bold: true },
|
|
1191
1206
|
i: { italic: true },
|
|
@@ -1200,7 +1215,7 @@ const TAG_FORMAT = {
|
|
|
1200
1215
|
del: { strike: true },
|
|
1201
1216
|
sup: { script: 'super' },
|
|
1202
1217
|
sub: { script: 'sub' }
|
|
1203
|
-
};
|
|
1218
|
+
}));
|
|
1204
1219
|
/** Whether a font-weight is bold; null when it does not say. */
|
|
1205
1220
|
function cssBold(weight) {
|
|
1206
1221
|
if (!weight)
|
|
@@ -1225,7 +1240,7 @@ function cssBold(weight) {
|
|
|
1225
1240
|
*/
|
|
1226
1241
|
function charFormat(el, rawTag, ctx, block = false) {
|
|
1227
1242
|
const style = el.style;
|
|
1228
|
-
const tagFormat = TAG_FORMAT
|
|
1243
|
+
const tagFormat = TAG_FORMAT.get(rawTag) ?? {};
|
|
1229
1244
|
const weight = cssBold(style?.fontWeight);
|
|
1230
1245
|
const bold = weight ?? (tagFormat.bold || ctx.bold);
|
|
1231
1246
|
const fontStyle = style?.fontStyle ?? '';
|
package/dist/sanitize.d.ts
CHANGED
|
@@ -48,10 +48,14 @@ export declare function keptMarks(options: KeepOptions | undefined): Kept;
|
|
|
48
48
|
* Make the elements under `root` safe, in place. With `only`, just the elements it accepts (the
|
|
49
49
|
* ones a paste brought in) are looked at; the rest of the document is left as it is. With
|
|
50
50
|
* `idTaken`, an id it says the page already uses is taken off. With `keep` (see keptMarks),
|
|
51
|
-
* the application's own `data-` attributes and classes stay.
|
|
51
|
+
* the application's own `data-` attributes and classes stay. `wordsLeft` is told of each element
|
|
52
|
+
* taken out with its words left in its place (a button, a text box), and of the text that holds
|
|
53
|
+
* them: an editor whose Redo puts the element back needs to know those words are there already.
|
|
54
|
+
* `leftOut` is told of each element that could not be checked, and so was taken out with what
|
|
55
|
+
* was in it: whoever shows the rest can say that a part is missing.
|
|
52
56
|
* Returns whether anything was taken out or changed.
|
|
53
57
|
*/
|
|
54
|
-
export declare function sanitizeTree(root: Element, only?: (el: Element) => boolean, idTaken?: (id: string) => boolean, keep?: Kept): boolean;
|
|
58
|
+
export declare function sanitizeTree(root: Element, only?: (el: Element) => boolean, idTaken?: (id: string) => boolean, keep?: Kept, wordsLeft?: (el: Element, words: Text) => void, leftOut?: (el: Element) => void): boolean;
|
|
55
59
|
/**
|
|
56
60
|
* HTML made safe to show and edit: see the top of this file for what is kept. It is parsed in a
|
|
57
61
|
* document of its own, where nothing in it runs or loads, before any of it reaches the page.
|
|
@@ -65,8 +69,9 @@ export declare function sanitizeHtml(html: string, options?: KeepOptions): strin
|
|
|
65
69
|
* checked is what is shown. Written out as text and read in again, a second reading has to
|
|
66
70
|
* agree with the first for the check to mean anything, and that is where a sanitizer is got
|
|
67
71
|
* round; it is also a third pass over a document that may hold megabytes of pictures.
|
|
72
|
+
* `leftOut` is told of each element that could not be checked (see sanitizeTree).
|
|
68
73
|
*/
|
|
69
|
-
export declare function sanitizedNodes(html: string, into: Document, idTaken?: (id: string) => boolean, keep?: Kept): Node[];
|
|
74
|
+
export declare function sanitizedNodes(html: string, into: Document, idTaken?: (id: string) => boolean, keep?: Kept, leftOut?: (el: Element) => void): Node[];
|
|
70
75
|
/** A run of a paste's text. */
|
|
71
76
|
interface PastePiece {
|
|
72
77
|
text: string;
|
|
@@ -82,6 +87,12 @@ export interface PasteNotes {
|
|
|
82
87
|
/** What each of its text boxes held. */
|
|
83
88
|
textareas: string[];
|
|
84
89
|
}
|
|
90
|
+
/**
|
|
91
|
+
* Whether a paste's HTML holds words. Word and Excel copy their text as HTML and put a picture
|
|
92
|
+
* of the same text beside it; a browser copies a picture as the picture and an `<img>` with no
|
|
93
|
+
* words. So a paste that has both is words where its HTML has any, and a picture where not.
|
|
94
|
+
*/
|
|
95
|
+
export declare function pasteHasWords(html: string): boolean;
|
|
85
96
|
/**
|
|
86
97
|
* Read from a paste's HTML what the browser's own paste may lose: null where it holds neither
|
|
87
98
|
* Word's hidden text nor a text box, which is nearly every paste.
|
|
@@ -94,6 +105,10 @@ export declare function pasteNotes(html: string): PasteNotes | null;
|
|
|
94
105
|
* characters were the hidden ones. An element left with nothing in it goes too, where
|
|
95
106
|
* `mayRemove` allows (a paragraph that was hidden whole would stay as an empty line in the
|
|
96
107
|
* RTF). Returns whether anything was taken out.
|
|
108
|
+
*
|
|
109
|
+
* With null for `to`, the paste is one that Redo has put back, after its hidden text was taken
|
|
110
|
+
* out once. A browser then brings back some of that text and not the rest, so each hidden
|
|
111
|
+
* piece may be there or not; all the other text of the paste has to be, from `from` on.
|
|
97
112
|
*/
|
|
98
|
-
export declare function removeWordHidden(root: Element, from: number, to: number, notes: PasteNotes, mayRemove?: (el: Element) => boolean): boolean;
|
|
113
|
+
export declare function removeWordHidden(root: Element, from: number, to: number | null, notes: PasteNotes, mayRemove?: (el: Element) => boolean): boolean;
|
|
99
114
|
export {};
|
package/dist/sanitize.js
CHANGED
|
@@ -166,8 +166,18 @@ function namesIn(given) {
|
|
|
166
166
|
const list = typeof given === 'string' ? [given] : given == null ? [] : Array.from(given);
|
|
167
167
|
return list.filter((name) => typeof name === 'string').map((name) => name.trim());
|
|
168
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* A table to look names up in that come from the document: a tag, a unit, the name of a size.
|
|
171
|
+
* Written as an object, such a table also answers to the names every object has (`constructor`,
|
|
172
|
+
* `toString`, `__proto__`), with a function where a list or a number was meant: an element
|
|
173
|
+
* named <constructor> with an attribute of its own stopped the sanitizer where it stood. A Map
|
|
174
|
+
* holds what was put in it and nothing else.
|
|
175
|
+
*/
|
|
176
|
+
function table(entries) {
|
|
177
|
+
return new Map(Object.entries(entries));
|
|
178
|
+
}
|
|
169
179
|
/** Attributes only some elements may keep. */
|
|
170
|
-
const TAG_ATTRIBUTES = {
|
|
180
|
+
const TAG_ATTRIBUTES = table({
|
|
171
181
|
a: ['href', 'target', 'rel'],
|
|
172
182
|
img: ['src', 'alt', 'width', 'height'],
|
|
173
183
|
td: ['colspan', 'rowspan', 'width'],
|
|
@@ -179,7 +189,7 @@ const TAG_ATTRIBUTES = {
|
|
|
179
189
|
li: ['value'],
|
|
180
190
|
font: ['color', 'size', 'face'],
|
|
181
191
|
details: ['open']
|
|
182
|
-
};
|
|
192
|
+
});
|
|
183
193
|
/**
|
|
184
194
|
* The one class that means something here: Safari's wrapper around a typed tab. Any other class
|
|
185
195
|
* would take on whatever the page's own stylesheet gives that name.
|
|
@@ -200,9 +210,9 @@ const UNSAFE_STYLE_VALUE = /url\s*\(|expression\s*\(|javascript:|@import|behavio
|
|
|
200
210
|
// the editing area cuts it off, or text of no size. They would be in the RTF and not on the
|
|
201
211
|
// screen, so those values go too.
|
|
202
212
|
/** Lengths in px, for the limits below. The units that follow the font are in FONT_UNITS. */
|
|
203
|
-
const PX_PER_UNIT = { px: 1, pt: 4 / 3, pc: 16, in: 96, cm: 96 / 2.54, mm: 96 / 25.4, q: 96 / 101.6, rem: 16 };
|
|
213
|
+
const PX_PER_UNIT = table({ px: 1, pt: 4 / 3, pc: 16, in: 96, cm: 96 / 2.54, mm: 96 / 25.4, q: 96 / 101.6, rem: 16 });
|
|
204
214
|
/** Lengths counted in the element's font size: how many of them make one. */
|
|
205
|
-
const FONT_UNITS = { em: 1, ex: 2, ch: 2 };
|
|
215
|
+
const FONT_UNITS = table({ em: 1, ex: 2, ch: 2 });
|
|
206
216
|
/** The font size where nothing above sets one. */
|
|
207
217
|
const BASE_FONT_PX = 16;
|
|
208
218
|
/**
|
|
@@ -211,10 +221,10 @@ const BASE_FONT_PX = 16;
|
|
|
211
221
|
*/
|
|
212
222
|
const LEAST_FONT_PX = 0.5;
|
|
213
223
|
/** The font sizes that have names, in px. */
|
|
214
|
-
const NAMED_FONT_PX = {
|
|
224
|
+
const NAMED_FONT_PX = table({
|
|
215
225
|
'xx-small': 9, 'x-small': 10, small: 13, medium: 16, large: 18, 'x-large': 24, 'xx-large': 32, 'xxx-large': 48,
|
|
216
226
|
'-webkit-xxx-large': 48
|
|
217
|
-
};
|
|
227
|
+
});
|
|
218
228
|
/** A step up or down in size (`larger`, `smaller`): the factor a browser uses. */
|
|
219
229
|
const FONT_STEP = 1.2;
|
|
220
230
|
/**
|
|
@@ -232,13 +242,13 @@ const INLINE_TAGS = new Set([
|
|
|
232
242
|
'small', 'big', 'abbr', 'cite', 'q', 'label', 'kbd', 'samp', 'var', 'tt', 'bdi', 'bdo', 'time', 'dfn'
|
|
233
243
|
]);
|
|
234
244
|
/** What each part of a border picture is when there is none: what `border: …` sets them to. */
|
|
235
|
-
const NO_BORDER_IMAGE = {
|
|
245
|
+
const NO_BORDER_IMAGE = table({
|
|
236
246
|
'border-image-source': ['none', 'initial'],
|
|
237
247
|
'border-image-slice': ['100%', 'initial'],
|
|
238
248
|
'border-image-width': ['1', 'initial'],
|
|
239
249
|
'border-image-outset': ['0', '0px', 'initial'],
|
|
240
250
|
'border-image-repeat': ['stretch', 'initial']
|
|
241
|
-
};
|
|
251
|
+
});
|
|
242
252
|
/** A little over the widest page there is, 22 inches (2112 px). */
|
|
243
253
|
const MOST_PX = 2200;
|
|
244
254
|
/** The elements that have a width or a height of their own in a document. */
|
|
@@ -250,10 +260,12 @@ const DISPLAY_VALUES = new Set([
|
|
|
250
260
|
]);
|
|
251
261
|
/** One number of a style value with its unit, as px; null for a percentage, a bare number or a unit not known. */
|
|
252
262
|
function lengthPx(digits, unit, fontPx) {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
263
|
+
const px = PX_PER_UNIT.get(unit);
|
|
264
|
+
if (px !== undefined)
|
|
265
|
+
return Number(digits) * px;
|
|
266
|
+
const perFont = FONT_UNITS.get(unit);
|
|
267
|
+
if (perFont !== undefined)
|
|
268
|
+
return (Number(digits) * fontPx) / perFont;
|
|
257
269
|
return null;
|
|
258
270
|
}
|
|
259
271
|
const NUMBER_AND_UNIT = /(-?\d*\.?\d+(?:e[+-]?\d+)?)([a-z%]*)/g;
|
|
@@ -303,8 +315,9 @@ function oneLengthPx(value, fontPx) {
|
|
|
303
315
|
*/
|
|
304
316
|
function fontSizePx(value, parentPx) {
|
|
305
317
|
const text = value.trim().toLowerCase();
|
|
306
|
-
|
|
307
|
-
|
|
318
|
+
const named = NAMED_FONT_PX.get(text);
|
|
319
|
+
if (named !== undefined)
|
|
320
|
+
return named;
|
|
308
321
|
if (text === 'larger')
|
|
309
322
|
return parentPx * FONT_STEP;
|
|
310
323
|
if (text === 'smaller')
|
|
@@ -424,12 +437,13 @@ function sanitizeStyle(el, tag, fonts) {
|
|
|
424
437
|
const value = style.getPropertyValue(name);
|
|
425
438
|
if (name === 'font-size')
|
|
426
439
|
continue;
|
|
427
|
-
|
|
440
|
+
const noPicture = NO_BORDER_IMAGE.get(name);
|
|
441
|
+
if (noPicture !== undefined) {
|
|
428
442
|
// `border: 1px solid` also says "no border picture", and a browser lists that with
|
|
429
443
|
// the properties it read. That much is no reason to write the attribute again; a
|
|
430
444
|
// border picture itself is drawn outside its element, and a document has none.
|
|
431
|
-
if (!
|
|
432
|
-
drop.push(...
|
|
445
|
+
if (!noPicture.includes(value.trim().toLowerCase()))
|
|
446
|
+
drop.push(...NO_BORDER_IMAGE.keys());
|
|
433
447
|
continue;
|
|
434
448
|
}
|
|
435
449
|
if (!STYLE_PROPERTY.test(name) || UNSAFE_STYLE_VALUE.test(value) || !sizeAllowed(name, value, tag, fontPx))
|
|
@@ -500,7 +514,7 @@ function namesAGlobal(id) {
|
|
|
500
514
|
return true;
|
|
501
515
|
}
|
|
502
516
|
function sanitizeAttributes(el, tag, fonts, idTaken, keep) {
|
|
503
|
-
const own = TAG_ATTRIBUTES
|
|
517
|
+
const own = TAG_ATTRIBUTES.get(tag);
|
|
504
518
|
for (const attr of Array.from(el.attributes)) {
|
|
505
519
|
const name = attr.name.toLowerCase();
|
|
506
520
|
if (name === 'class') {
|
|
@@ -560,25 +574,24 @@ function isReadAloudCopy(el) {
|
|
|
560
574
|
* Make the elements under `root` safe, in place. With `only`, just the elements it accepts (the
|
|
561
575
|
* ones a paste brought in) are looked at; the rest of the document is left as it is. With
|
|
562
576
|
* `idTaken`, an id it says the page already uses is taken off. With `keep` (see keptMarks),
|
|
563
|
-
* the application's own `data-` attributes and classes stay.
|
|
577
|
+
* the application's own `data-` attributes and classes stay. `wordsLeft` is told of each element
|
|
578
|
+
* taken out with its words left in its place (a button, a text box), and of the text that holds
|
|
579
|
+
* them: an editor whose Redo puts the element back needs to know those words are there already.
|
|
580
|
+
* `leftOut` is told of each element that could not be checked, and so was taken out with what
|
|
581
|
+
* was in it: whoever shows the rest can say that a part is missing.
|
|
564
582
|
* Returns whether anything was taken out or changed.
|
|
565
583
|
*/
|
|
566
|
-
export function sanitizeTree(root, only, idTaken, keep) {
|
|
584
|
+
export function sanitizeTree(root, only, idTaken, keep, wordsLeft, leftOut) {
|
|
567
585
|
let changed = false;
|
|
568
586
|
const fonts = new Map();
|
|
569
587
|
/** The words of the last element that left its words, where they are still in the document. */
|
|
570
588
|
let lastKept = null;
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
if (!root.contains(el))
|
|
574
|
-
continue;
|
|
575
|
-
if (only && !only(el))
|
|
576
|
-
continue;
|
|
589
|
+
/** One element made safe; returns whether it was taken out or changed. */
|
|
590
|
+
const check = (el) => {
|
|
577
591
|
const tag = el.tagName.toLowerCase();
|
|
578
592
|
if (REMOVED_TAGS.has(tag) || isReadAloudCopy(el)) {
|
|
579
593
|
el.remove();
|
|
580
|
-
|
|
581
|
-
continue;
|
|
594
|
+
return true;
|
|
582
595
|
}
|
|
583
596
|
if (TEXT_KEPT_TAGS.has(tag)) {
|
|
584
597
|
const text = shownText(el, tag);
|
|
@@ -587,12 +600,12 @@ export function sanitizeTree(root, only, idTaken, keep) {
|
|
|
587
600
|
const kept = el.ownerDocument.createTextNode(lastKept !== null && el.previousSibling === lastKept ? ` ${text}` : text);
|
|
588
601
|
el.replaceWith(kept);
|
|
589
602
|
lastKept = kept;
|
|
603
|
+
wordsLeft?.(el, kept);
|
|
590
604
|
}
|
|
591
605
|
else {
|
|
592
606
|
el.remove();
|
|
593
607
|
}
|
|
594
|
-
|
|
595
|
-
continue;
|
|
608
|
+
return true;
|
|
596
609
|
}
|
|
597
610
|
// A custom element would be given its behaviour by the page: only what it holds is kept,
|
|
598
611
|
// and nothing of one that was hidden, which would show once the element is gone.
|
|
@@ -601,13 +614,31 @@ export function sanitizeTree(root, only, idTaken, keep) {
|
|
|
601
614
|
el.remove();
|
|
602
615
|
else
|
|
603
616
|
el.replaceWith(...Array.from(el.childNodes));
|
|
604
|
-
|
|
605
|
-
continue;
|
|
617
|
+
return true;
|
|
606
618
|
}
|
|
607
619
|
const before = el.attributes.length + (el.getAttribute('style') ?? '') + (el.getAttribute('href') ?? '') + (el.getAttribute('width') ?? '') + (el.getAttribute('height') ?? '');
|
|
608
620
|
sanitizeAttributes(el, tag, fonts, idTaken, keep);
|
|
609
|
-
|
|
621
|
+
return before !== el.attributes.length + (el.getAttribute('style') ?? '') + (el.getAttribute('href') ?? '') + (el.getAttribute('width') ?? '') + (el.getAttribute('height') ?? '');
|
|
622
|
+
};
|
|
623
|
+
for (const el of Array.from(root.querySelectorAll('*'))) {
|
|
624
|
+
// Gone already, with something it was in.
|
|
625
|
+
if (!root.contains(el))
|
|
626
|
+
continue;
|
|
627
|
+
if (only && !only(el))
|
|
628
|
+
continue;
|
|
629
|
+
try {
|
|
630
|
+
if (check(el))
|
|
631
|
+
changed = true;
|
|
632
|
+
}
|
|
633
|
+
catch {
|
|
634
|
+
// An element that cannot be checked, for a reason nobody thought of, is not kept: it
|
|
635
|
+
// goes, with what is in it, and the elements after it are still looked at. Stopping
|
|
636
|
+
// here left the rest of a paste in the document as it came in, and whoever called
|
|
637
|
+
// with nothing to say which half had been made safe.
|
|
638
|
+
el.remove();
|
|
610
639
|
changed = true;
|
|
640
|
+
leftOut?.(el);
|
|
641
|
+
}
|
|
611
642
|
}
|
|
612
643
|
return changed;
|
|
613
644
|
}
|
|
@@ -622,11 +653,11 @@ export function sanitizeHtml(html, options) {
|
|
|
622
653
|
return safeBody(html, undefined, keptMarks(options))?.innerHTML ?? '';
|
|
623
654
|
}
|
|
624
655
|
/** The HTML, read in a document of its own and made safe there; null where it cannot be. */
|
|
625
|
-
function safeBody(html, idTaken, keep) {
|
|
656
|
+
function safeBody(html, idTaken, keep, leftOut) {
|
|
626
657
|
if (!html || typeof DOMParser === 'undefined')
|
|
627
658
|
return null;
|
|
628
659
|
const doc = new DOMParser().parseFromString(`<!DOCTYPE html><html><body>${html}</body></html>`, 'text/html');
|
|
629
|
-
sanitizeTree(doc.body, undefined, idTaken, keep);
|
|
660
|
+
sanitizeTree(doc.body, undefined, idTaken, keep, undefined, leftOut);
|
|
630
661
|
return doc.body;
|
|
631
662
|
}
|
|
632
663
|
/**
|
|
@@ -634,16 +665,79 @@ function safeBody(html, idTaken, keep) {
|
|
|
634
665
|
* checked is what is shown. Written out as text and read in again, a second reading has to
|
|
635
666
|
* agree with the first for the check to mean anything, and that is where a sanitizer is got
|
|
636
667
|
* round; it is also a third pass over a document that may hold megabytes of pictures.
|
|
668
|
+
* `leftOut` is told of each element that could not be checked (see sanitizeTree).
|
|
637
669
|
*/
|
|
638
|
-
export function sanitizedNodes(html, into, idTaken, keep) {
|
|
639
|
-
const body = safeBody(html, idTaken, keep);
|
|
670
|
+
export function sanitizedNodes(html, into, idTaken, keep, leftOut) {
|
|
671
|
+
const body = safeBody(html, idTaken, keep, leftOut);
|
|
640
672
|
if (!body)
|
|
641
673
|
return [];
|
|
642
674
|
return Array.from(body.childNodes).map((node) => into.importNode(node, true));
|
|
643
675
|
}
|
|
676
|
+
/** Where each part of a paste begins, when they are all there one after the other from `from`. */
|
|
677
|
+
function partsInARow(from, parts) {
|
|
678
|
+
let at = from;
|
|
679
|
+
return parts.map((part) => {
|
|
680
|
+
const start = at;
|
|
681
|
+
at += part.text.length;
|
|
682
|
+
return start;
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Where each part of a paste begins in `shown`, from `from` on, when a hidden part may be
|
|
687
|
+
* missing (null for one that is): the first reading that fits, taking each hidden part as there
|
|
688
|
+
* where it can be. Null where there is no reading: this text is not that paste.
|
|
689
|
+
*/
|
|
690
|
+
function partsPutBack(shown, from, parts) {
|
|
691
|
+
const starts = [];
|
|
692
|
+
/** Where the reading stood before each part, to go back to. */
|
|
693
|
+
const stood = [];
|
|
694
|
+
let at = from;
|
|
695
|
+
let i = 0;
|
|
696
|
+
// (A reading is given up and taken again from an earlier part; a text made to be read in
|
|
697
|
+
// very many ways is not read at all.)
|
|
698
|
+
for (let steps = 0; i < parts.length; steps++) {
|
|
699
|
+
if (steps > 200_000)
|
|
700
|
+
return null;
|
|
701
|
+
stood[i] = at;
|
|
702
|
+
if (shown.startsWith(parts[i].text, at)) {
|
|
703
|
+
starts[i] = at;
|
|
704
|
+
at += parts[i].text.length;
|
|
705
|
+
i++;
|
|
706
|
+
}
|
|
707
|
+
else if (parts[i].hidden) {
|
|
708
|
+
starts[i] = null;
|
|
709
|
+
i++;
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
// The last hidden part taken as there is taken as missing, and read on from it.
|
|
713
|
+
let back = i - 1;
|
|
714
|
+
while (back >= 0 && !(parts[back].hidden && starts[back] !== null))
|
|
715
|
+
back--;
|
|
716
|
+
if (back < 0)
|
|
717
|
+
return null;
|
|
718
|
+
starts[back] = null;
|
|
719
|
+
at = stood[back];
|
|
720
|
+
i = back + 1;
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
return starts;
|
|
724
|
+
}
|
|
644
725
|
/** Elements whose text is not text of the page. */
|
|
645
726
|
const NOT_PAGE_TEXT = new Set(['script', 'style', 'title', 'template', 'noscript', 'textarea']);
|
|
646
727
|
const withoutSpace = (text) => text.replace(/\s/g, '');
|
|
728
|
+
/**
|
|
729
|
+
* Whether a paste's HTML holds words. Word and Excel copy their text as HTML and put a picture
|
|
730
|
+
* of the same text beside it; a browser copies a picture as the picture and an `<img>` with no
|
|
731
|
+
* words. So a paste that has both is words where its HTML has any, and a picture where not.
|
|
732
|
+
*/
|
|
733
|
+
export function pasteHasWords(html) {
|
|
734
|
+
if (!html || typeof DOMParser === 'undefined')
|
|
735
|
+
return false;
|
|
736
|
+
const body = new DOMParser().parseFromString(html, 'text/html').body;
|
|
737
|
+
for (const el of Array.from(body.querySelectorAll(Array.from(NOT_PAGE_TEXT).join(','))))
|
|
738
|
+
el.remove();
|
|
739
|
+
return (body.textContent ?? '').trim() !== '';
|
|
740
|
+
}
|
|
647
741
|
/**
|
|
648
742
|
* Read from a paste's HTML what the browser's own paste may lose: null where it holds neither
|
|
649
743
|
* Word's hidden text nor a text box, which is nearly every paste.
|
|
@@ -697,6 +791,10 @@ export function pasteNotes(html) {
|
|
|
697
791
|
* characters were the hidden ones. An element left with nothing in it goes too, where
|
|
698
792
|
* `mayRemove` allows (a paragraph that was hidden whole would stay as an empty line in the
|
|
699
793
|
* RTF). Returns whether anything was taken out.
|
|
794
|
+
*
|
|
795
|
+
* With null for `to`, the paste is one that Redo has put back, after its hidden text was taken
|
|
796
|
+
* out once. A browser then brings back some of that text and not the rest, so each hidden
|
|
797
|
+
* piece may be there or not; all the other text of the paste has to be, from `from` on.
|
|
700
798
|
*/
|
|
701
799
|
export function removeWordHidden(root, from, to, notes, mayRemove) {
|
|
702
800
|
const texts = [];
|
|
@@ -709,25 +807,26 @@ export function removeWordHidden(root, from, to, notes, mayRemove) {
|
|
|
709
807
|
for (let i = children.length - 1; i >= 0; i--)
|
|
710
808
|
todo.push(children[i]);
|
|
711
809
|
}
|
|
712
|
-
const
|
|
810
|
+
const shown = withoutSpace(texts.map((text) => text.data).join(''));
|
|
713
811
|
// A browser may leave what is not displayed out of a paste, or bring it along.
|
|
714
812
|
for (const pieces of [notes.pieces, notes.pieces.filter((piece) => !piece.undisplayed)]) {
|
|
715
|
-
|
|
813
|
+
const parts = pieces.map((piece) => ({ text: withoutSpace(piece.text), hidden: piece.hidden })).filter((part) => part.text !== '');
|
|
814
|
+
if (to !== null && parts.map((part) => part.text).join('') !== shown.slice(from, to))
|
|
815
|
+
continue;
|
|
816
|
+
const starts = to === null ? partsPutBack(shown, from, parts) : partsInARow(from, parts);
|
|
817
|
+
if (starts === null)
|
|
716
818
|
continue;
|
|
717
819
|
/** The hidden runs, as [first, after last] among the characters that are not white space. */
|
|
718
820
|
const runs = [];
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
at += length;
|
|
730
|
-
}
|
|
821
|
+
parts.forEach((part, i) => {
|
|
822
|
+
if (!part.hidden || starts[i] === null)
|
|
823
|
+
return;
|
|
824
|
+
const last = runs[runs.length - 1];
|
|
825
|
+
if (last && last[1] === starts[i])
|
|
826
|
+
last[1] = starts[i] + part.text.length;
|
|
827
|
+
else
|
|
828
|
+
runs.push([starts[i], starts[i] + part.text.length]);
|
|
829
|
+
});
|
|
731
830
|
if (runs.length === 0)
|
|
732
831
|
return false;
|
|
733
832
|
let index = 0;
|
package/package.json
CHANGED