svelte-rtf-editor 1.8.3 → 1.10.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 +212 -26
- package/dist/RtfViewer.svelte +69 -7
- package/dist/RtfViewer.svelte.d.ts +2 -1
- package/dist/components/ImageOverlay.svelte +11 -3
- package/dist/components/ImageOverlay.svelte.d.ts +2 -0
- package/dist/components/InkEditor.svelte +705 -35
- package/dist/components/InkEditor.svelte.d.ts +14 -3
- package/dist/components/Modal.svelte +57 -3
- package/dist/components/Toolbar.svelte +44 -1
- package/dist/components/Toolbar.svelte.d.ts +5 -0
- package/dist/components/ToolbarButton.svelte +8 -2
- package/dist/components/ToolbarButton.svelte.d.ts +4 -0
- package/dist/images.d.ts +60 -7
- package/dist/images.js +268 -59
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/rtf-parser.d.ts +16 -3
- package/dist/rtf-parser.js +1682 -534
- package/dist/rtf-writer.d.ts +40 -9
- package/dist/rtf-writer.js +1175 -359
- package/dist/sanitize.d.ts +77 -0
- package/dist/sanitize.js +755 -0
- package/dist/table-model.d.ts +49 -0
- package/dist/table-model.js +203 -0
- package/dist/utils.js +689 -87
- package/package.json +16 -12
package/dist/sanitize.js
ADDED
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML that is safe to put in the editor.
|
|
3
|
+
*
|
|
4
|
+
* The editor shows HTML it is given: the `content` prop, setHTML(), a draft restored from
|
|
5
|
+
* localStorage, and whatever a paste brings. Written to the page as it is, an
|
|
6
|
+
* <img onerror="…"> in any of those runs as script, and a pasted block with position: fixed
|
|
7
|
+
* covers the page the editor sits in. So all of it goes through here first.
|
|
8
|
+
*
|
|
9
|
+
* What stays is what a document is made of: text, the elements that format it, links to web,
|
|
10
|
+
* mail and phone addresses, pictures, tables. What goes:
|
|
11
|
+
*
|
|
12
|
+
* - elements that run code or load something of their own (<script>, <iframe>, <object>,
|
|
13
|
+
* <svg>, …), with everything in them;
|
|
14
|
+
* - elements that take input (<button>, <textarea>, <select>, <input>) and formulas (<math>):
|
|
15
|
+
* the element goes and the words it showed stay, as text. Words it did not show (the element
|
|
16
|
+
* or a part of it was hidden) go with it;
|
|
17
|
+
* - every attribute not listed below, which is how event handlers (onclick, onerror) go;
|
|
18
|
+
* - `class`, which would take on what the page's own stylesheet gives that name, an `id` that
|
|
19
|
+
* the page already uses for something of its own, and the `id` of a link;
|
|
20
|
+
* - a link address that is not a web, mail or phone address (javascript:, data:, file:), and a
|
|
21
|
+
* picture address that is not a web address or picture data;
|
|
22
|
+
* - every style property not listed below (position, z-index, …), any value with a url(), and
|
|
23
|
+
* any size that would put the element, or its words, outside the page it is on or out of
|
|
24
|
+
* sight (a margin below zero, a padding of thousands of pixels, a first line that starts left
|
|
25
|
+
* of its paragraph, text of no size).
|
|
26
|
+
*/
|
|
27
|
+
import { linkTarget } from './rtf-writer.js';
|
|
28
|
+
import { isSafeImageUrl } from './images.js';
|
|
29
|
+
/** Elements taken out with everything in them. */
|
|
30
|
+
const REMOVED_TAGS = new Set([
|
|
31
|
+
'script', 'style', 'template', 'noscript', 'iframe', 'frame', 'frameset', 'object', 'embed', 'applet',
|
|
32
|
+
'svg', 'link', 'meta', 'base', 'title', 'head', 'audio', 'video', 'source', 'track', 'canvas',
|
|
33
|
+
'option', 'optgroup', 'datalist', 'dialog', 'map', 'area', 'portal', 'slot'
|
|
34
|
+
]);
|
|
35
|
+
/**
|
|
36
|
+
* Elements taken out that leave the words they showed: a button's label, what a text box or a
|
|
37
|
+
* list held, a formula. Taken out whole, "Ki-67 30%" with its figure in a formula became
|
|
38
|
+
* "Ki-67", and nothing said so.
|
|
39
|
+
*/
|
|
40
|
+
const TEXT_KEPT_TAGS = new Set(['button', 'textarea', 'select', 'input', 'math']);
|
|
41
|
+
/**
|
|
42
|
+
* The kinds of <input> that do not show what they hold as words: a box to tick, a file, a color,
|
|
43
|
+
* a slider, a password (dots), and the kind that is not shown at all. Every other kind does,
|
|
44
|
+
* a kind no browser knows among them, which is shown as a text box.
|
|
45
|
+
*/
|
|
46
|
+
const WORDLESS_INPUT_TYPES = new Set(['checkbox', 'radio', 'file', 'image', 'color', 'range', 'hidden', 'password']);
|
|
47
|
+
/** The parts of a formula that are read left to right, as they are written. */
|
|
48
|
+
const LINEAR_MATH = new Set(['mrow', 'mn', 'mo', 'mi', 'mtext', 'ms', 'mspace', 'mstyle', 'mpadded', 'semantics', 'annotation', 'annotation-xml']);
|
|
49
|
+
/** What stands in for a formula that cannot be written as a line of text (a fraction, a root). */
|
|
50
|
+
const FORMULA_MARK = '[formula]';
|
|
51
|
+
/** Word's hidden text, as a paste from Word carries it: a property no browser keeps. */
|
|
52
|
+
const WORD_HIDDEN = /mso-hide\s*:\s*all/i;
|
|
53
|
+
/** A style that says of its own element that it is not shown. */
|
|
54
|
+
const STYLED_HIDDEN = /(^|;)\s*(display\s*:\s*none|visibility\s*:\s*(hidden|collapse))\b/i;
|
|
55
|
+
/**
|
|
56
|
+
* Whether an element says of itself that it is not shown. (One hidden by something it is in
|
|
57
|
+
* stays inside that, and stays hidden with it.)
|
|
58
|
+
*/
|
|
59
|
+
function hiddenByItself(el) {
|
|
60
|
+
if (el.hasAttribute('hidden'))
|
|
61
|
+
return true;
|
|
62
|
+
const style = el.getAttribute('style');
|
|
63
|
+
return style !== null && (STYLED_HIDDEN.test(style) || WORD_HIDDEN.test(style));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The text of an element as a browser shows it: without the parts that are hidden, and without
|
|
67
|
+
* what is in a <script> or <style>, which is text to the DOM and never words on the screen.
|
|
68
|
+
*/
|
|
69
|
+
function visibleText(el) {
|
|
70
|
+
let text = '';
|
|
71
|
+
const todo = [el];
|
|
72
|
+
while (todo.length > 0) {
|
|
73
|
+
const node = todo.pop();
|
|
74
|
+
if (node.nodeType === 3) {
|
|
75
|
+
text += node.data;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (node.nodeType !== 1)
|
|
79
|
+
continue;
|
|
80
|
+
if (node !== el) {
|
|
81
|
+
const tag = node.tagName.toLowerCase();
|
|
82
|
+
if (REMOVED_TAGS.has(tag) || hiddenByItself(node))
|
|
83
|
+
continue;
|
|
84
|
+
if (tag === 'br')
|
|
85
|
+
text += ' ';
|
|
86
|
+
}
|
|
87
|
+
const children = Array.from(node.childNodes);
|
|
88
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
89
|
+
todo.push(children[i]);
|
|
90
|
+
}
|
|
91
|
+
return text;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The words one of the TEXT_KEPT_TAGS elements showed. A formula of numbers and signs in a row
|
|
95
|
+
* is those characters. One with a fraction, a power or a root is not: its parts read in a row
|
|
96
|
+
* say something else ("1/2" would read "12"), so it is marked as a formula left out.
|
|
97
|
+
*/
|
|
98
|
+
function shownText(el, tag) {
|
|
99
|
+
const tidy = (text) => (text ?? '').replace(/\s+/g, ' ').trim();
|
|
100
|
+
// Hidden, it showed no words: kept, they would be on the screen and in the RTF for the
|
|
101
|
+
// first time.
|
|
102
|
+
if (hiddenByItself(el))
|
|
103
|
+
return '';
|
|
104
|
+
if (tag === 'button')
|
|
105
|
+
return tidy(visibleText(el));
|
|
106
|
+
if (tag === 'textarea')
|
|
107
|
+
return (el.value ?? el.textContent ?? '').trim();
|
|
108
|
+
if (tag === 'input') {
|
|
109
|
+
const type = (el.getAttribute('type') ?? '').trim().toLowerCase();
|
|
110
|
+
// (A button of this kind with no value shows a word of the browser's own, in the
|
|
111
|
+
// reader's language: not a word of the document.)
|
|
112
|
+
return WORDLESS_INPUT_TYPES.has(type) ? '' : tidy(el.value ?? el.getAttribute('value'));
|
|
113
|
+
}
|
|
114
|
+
if (tag === 'select') {
|
|
115
|
+
const options = Array.from(el.querySelectorAll('option'));
|
|
116
|
+
const chosen = options.filter((option) => option.hasAttribute('selected'));
|
|
117
|
+
// A list shown open (several lines, or several choices) has what was chosen in it, which
|
|
118
|
+
// may be nothing or more than one. A list shown closed shows one: the first, if none is.
|
|
119
|
+
const open = el.hasAttribute('multiple') || Number(el.getAttribute('size')) > 1;
|
|
120
|
+
const shown = open ? chosen : [chosen[chosen.length - 1] ?? options[0]];
|
|
121
|
+
return shown.map((option) => tidy(option?.textContent)).filter((words) => words !== '').join(', ');
|
|
122
|
+
}
|
|
123
|
+
// A formula. Text written straight into <math>, outside any of its parts, is not drawn.
|
|
124
|
+
let text = '';
|
|
125
|
+
for (const part of Array.from(el.querySelectorAll('*'))) {
|
|
126
|
+
const name = part.tagName.toLowerCase();
|
|
127
|
+
if (!LINEAR_MATH.has(name))
|
|
128
|
+
return FORMULA_MARK;
|
|
129
|
+
if (name === 'mspace')
|
|
130
|
+
text += ' ';
|
|
131
|
+
else if (part.children.length === 0 && !name.startsWith('annotation') && !part.closest('annotation, annotation-xml'))
|
|
132
|
+
text += part.textContent ?? '';
|
|
133
|
+
}
|
|
134
|
+
return tidy(text);
|
|
135
|
+
}
|
|
136
|
+
/** Attributes any element may keep. */
|
|
137
|
+
const GLOBAL_ATTRIBUTES = new Set(['style', 'dir', 'lang', 'title', 'hidden', 'align', 'data-rtf-tab-stops']);
|
|
138
|
+
/** Attributes only some elements may keep. */
|
|
139
|
+
const TAG_ATTRIBUTES = {
|
|
140
|
+
a: ['href', 'target', 'rel'],
|
|
141
|
+
img: ['src', 'alt', 'width', 'height'],
|
|
142
|
+
td: ['colspan', 'rowspan', 'width'],
|
|
143
|
+
th: ['colspan', 'rowspan', 'width'],
|
|
144
|
+
table: ['width'],
|
|
145
|
+
col: ['span', 'width'],
|
|
146
|
+
colgroup: ['span', 'width'],
|
|
147
|
+
ol: ['start', 'type'],
|
|
148
|
+
li: ['value'],
|
|
149
|
+
font: ['color', 'size', 'face'],
|
|
150
|
+
details: ['open']
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* The one class that means something here: Safari's wrapper around a typed tab. Any other class
|
|
154
|
+
* would take on whatever the page's own stylesheet gives that name.
|
|
155
|
+
*/
|
|
156
|
+
const KEPT_CLASS = 'Apple-tab-span';
|
|
157
|
+
/** Style properties that format text, a paragraph, a picture or a table. */
|
|
158
|
+
const STYLE_PROPERTY = /^(color|background-color|font-(size|weight|style|family)|text-(align|indent)|text-decoration(-.*)?|vertical-align|white-space(-.*)?|text-wrap(-.*)?|(margin|padding)(-(top|right|bottom|left))?|border(-.*)?|width|height|display|visibility|break-after|page-break-after|(-moz-)?tab-size|line-height|list-style-type)$/;
|
|
159
|
+
/** A style value that loads something or runs something. */
|
|
160
|
+
const UNSAFE_STYLE_VALUE = /url\s*\(|expression\s*\(|javascript:|@import|behavior\s*:/i;
|
|
161
|
+
// ── Sizes ──
|
|
162
|
+
// `position` is not the only way to draw over the page an editor sits in. A margin below zero
|
|
163
|
+
// pulls a block up over the toolbar, a padding of thousands of pixels makes a link the size of
|
|
164
|
+
// the page, a width of thousands a block that runs out over what is beside the editor. So the
|
|
165
|
+
// properties that give an element its place and size are kept only within what a document
|
|
166
|
+
// uses: nothing below zero where it would move the element, nothing larger than a page.
|
|
167
|
+
//
|
|
168
|
+
// The other half is words put out of sight: a first line pulled left of its paragraph, where
|
|
169
|
+
// the editing area cuts it off, or text of no size. They would be in the RTF and not on the
|
|
170
|
+
// screen, so those values go too.
|
|
171
|
+
/** Lengths in px, for the limits below. The units that follow the font are in FONT_UNITS. */
|
|
172
|
+
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 };
|
|
173
|
+
/** Lengths counted in the element's font size: how many of them make one. */
|
|
174
|
+
const FONT_UNITS = { em: 1, ex: 2, ch: 2 };
|
|
175
|
+
/** The font size where nothing above sets one. */
|
|
176
|
+
const BASE_FONT_PX = 16;
|
|
177
|
+
/**
|
|
178
|
+
* Text below this is not there to be read: a size of 0 shows nothing at all. (The smallest size
|
|
179
|
+
* RTF can give, half a point, is above it, so a size read from RTF is never taken off.)
|
|
180
|
+
*/
|
|
181
|
+
const LEAST_FONT_PX = 0.5;
|
|
182
|
+
/** The font sizes that have names, in px. */
|
|
183
|
+
const NAMED_FONT_PX = {
|
|
184
|
+
'xx-small': 9, 'x-small': 10, small: 13, medium: 16, large: 18, 'x-large': 24, 'xx-large': 32, 'xxx-large': 48,
|
|
185
|
+
'-webkit-xxx-large': 48
|
|
186
|
+
};
|
|
187
|
+
/** A step up or down in size (`larger`, `smaller`): the factor a browser uses. */
|
|
188
|
+
const FONT_STEP = 1.2;
|
|
189
|
+
/**
|
|
190
|
+
* How far a list may be pulled left: the half inch a list level is indented by itself. The
|
|
191
|
+
* reader writes a list indented by less than that with the difference as a margin below zero
|
|
192
|
+
* ("margin-left:-18pt"), and the writer reads it back; taken off, the list was written half an
|
|
193
|
+
* inch further in. It is the one margin below zero that is kept.
|
|
194
|
+
*/
|
|
195
|
+
const LIST_OUTDENT_PX = 48;
|
|
196
|
+
/** The most padding for a piece of text inside a line, where padding moves nothing and only covers. */
|
|
197
|
+
const INLINE_PADDING_PX = 32;
|
|
198
|
+
/** Elements that are a piece of text inside a line. */
|
|
199
|
+
const INLINE_TAGS = new Set([
|
|
200
|
+
'a', 'span', 'b', 'strong', 'i', 'em', 'u', 's', 'strike', 'del', 'ins', 'sub', 'sup', 'font', 'code', 'mark',
|
|
201
|
+
'small', 'big', 'abbr', 'cite', 'q', 'label', 'kbd', 'samp', 'var', 'tt', 'bdi', 'bdo', 'time', 'dfn'
|
|
202
|
+
]);
|
|
203
|
+
/** What each part of a border picture is when there is none: what `border: …` sets them to. */
|
|
204
|
+
const NO_BORDER_IMAGE = {
|
|
205
|
+
'border-image-source': ['none', 'initial'],
|
|
206
|
+
'border-image-slice': ['100%', 'initial'],
|
|
207
|
+
'border-image-width': ['1', 'initial'],
|
|
208
|
+
'border-image-outset': ['0', '0px', 'initial'],
|
|
209
|
+
'border-image-repeat': ['stretch', 'initial']
|
|
210
|
+
};
|
|
211
|
+
/** A little over the widest page there is, 22 inches (2112 px). */
|
|
212
|
+
const MOST_PX = 2200;
|
|
213
|
+
/** The elements that have a width or a height of their own in a document. */
|
|
214
|
+
const SIZED_TAGS = new Set(['img', 'table', 'td', 'th', 'col', 'colgroup', 'hr']);
|
|
215
|
+
/** The ways of laying an element out that a document uses. */
|
|
216
|
+
const DISPLAY_VALUES = new Set([
|
|
217
|
+
'none', 'inline', 'block', 'inline-block', 'list-item', 'table', 'table-row', 'table-cell', 'table-row-group',
|
|
218
|
+
'table-header-group', 'table-footer-group', 'table-column', 'table-column-group', 'table-caption'
|
|
219
|
+
]);
|
|
220
|
+
/** One number of a style value with its unit, as px; null for a percentage, a bare number or a unit not known. */
|
|
221
|
+
function lengthPx(digits, unit, fontPx) {
|
|
222
|
+
if (unit in PX_PER_UNIT)
|
|
223
|
+
return Number(digits) * PX_PER_UNIT[unit];
|
|
224
|
+
if (unit in FONT_UNITS)
|
|
225
|
+
return (Number(digits) * fontPx) / FONT_UNITS[unit];
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
const NUMBER_AND_UNIT = /(-?\d*\.?\d+(?:e[+-]?\d+)?)([a-z%]*)/g;
|
|
229
|
+
/**
|
|
230
|
+
* Whether every number in a value is a length within `most` px (a percentage within
|
|
231
|
+
* `mostPercent`, a bare number within `mostNumber`), and none below zero, or none further
|
|
232
|
+
* below it than `mostBelow` px. An em is `fontPx`, the size of the element's own text.
|
|
233
|
+
* A value worked out by the browser (calc(), var(), min()) cannot be checked, and is not within.
|
|
234
|
+
*/
|
|
235
|
+
function within(value, limits, fontPx) {
|
|
236
|
+
if (/[()]/.test(value))
|
|
237
|
+
return false;
|
|
238
|
+
for (const [, digits, unit] of value.toLowerCase().matchAll(NUMBER_AND_UNIT)) {
|
|
239
|
+
const number = Number(digits);
|
|
240
|
+
if (!Number.isFinite(number))
|
|
241
|
+
return false;
|
|
242
|
+
const size = Math.abs(number);
|
|
243
|
+
if (unit === '%') {
|
|
244
|
+
if (number < 0 || size > (limits.mostPercent ?? 100))
|
|
245
|
+
return false;
|
|
246
|
+
}
|
|
247
|
+
else if (unit === '') {
|
|
248
|
+
if (number < 0 || size > (limits.mostNumber ?? 0))
|
|
249
|
+
return false;
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
// (A unit not known is also a share of the window, vw or vh: not a size a document has.)
|
|
253
|
+
const px = lengthPx(digits, unit, fontPx);
|
|
254
|
+
if (px === null || px > limits.most || px < -(limits.mostBelow ?? 0))
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return true;
|
|
259
|
+
}
|
|
260
|
+
/** A style value that is one length, in px; null for anything else (auto, a percentage, two values). */
|
|
261
|
+
function oneLengthPx(value, fontPx) {
|
|
262
|
+
const match = /^\s*(-?\d*\.?\d+(?:e[+-]?\d+)?)([a-z]*)\s*$/i.exec(value);
|
|
263
|
+
if (!match)
|
|
264
|
+
return null;
|
|
265
|
+
if (Number(match[1]) === 0)
|
|
266
|
+
return 0;
|
|
267
|
+
return lengthPx(match[1], match[2].toLowerCase(), fontPx);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The size in px that a font-size gives, under text of `parentPx`; null where it cannot be told
|
|
271
|
+
* (a value worked out by the browser, a unit that is a share of the window).
|
|
272
|
+
*/
|
|
273
|
+
function fontSizePx(value, parentPx) {
|
|
274
|
+
const text = value.trim().toLowerCase();
|
|
275
|
+
if (text in NAMED_FONT_PX)
|
|
276
|
+
return NAMED_FONT_PX[text];
|
|
277
|
+
if (text === 'larger')
|
|
278
|
+
return parentPx * FONT_STEP;
|
|
279
|
+
if (text === 'smaller')
|
|
280
|
+
return parentPx / FONT_STEP;
|
|
281
|
+
if (text === 'inherit' || text === 'initial' || text === 'unset' || text === 'revert')
|
|
282
|
+
return text === 'initial' ? BASE_FONT_PX : parentPx;
|
|
283
|
+
const match = /^(\d*\.?\d+(?:e[+-]?\d+)?)([a-z%]*)$/.exec(text);
|
|
284
|
+
if (!match)
|
|
285
|
+
return null;
|
|
286
|
+
if (match[2] === '%')
|
|
287
|
+
return (Number(match[1]) * parentPx) / 100;
|
|
288
|
+
if (match[2] === '')
|
|
289
|
+
return Number(match[1]) === 0 ? 0 : null;
|
|
290
|
+
return lengthPx(match[1], match[2], parentPx);
|
|
291
|
+
}
|
|
292
|
+
/** Whether a font size is one text can have: large enough to be there, no larger than a page. */
|
|
293
|
+
function fontSizeAllowed(px) {
|
|
294
|
+
return px !== null && px >= LEAST_FONT_PX && px <= MOST_PX;
|
|
295
|
+
}
|
|
296
|
+
/** The size of the text around an element: what its ancestors' styles make it. */
|
|
297
|
+
function fontPxAround(el, known) {
|
|
298
|
+
const above = [];
|
|
299
|
+
let px = BASE_FONT_PX;
|
|
300
|
+
for (let up = el.parentElement; up; up = up.parentElement) {
|
|
301
|
+
const found = known.get(up);
|
|
302
|
+
if (found !== undefined) {
|
|
303
|
+
px = found;
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
above.push(up);
|
|
307
|
+
}
|
|
308
|
+
for (let i = above.length - 1; i >= 0; i--) {
|
|
309
|
+
const own = above[i].style?.getPropertyValue('font-size');
|
|
310
|
+
const size = own ? fontSizePx(own, px) : null;
|
|
311
|
+
if (fontSizeAllowed(size))
|
|
312
|
+
px = size;
|
|
313
|
+
known.set(above[i], px);
|
|
314
|
+
}
|
|
315
|
+
return px;
|
|
316
|
+
}
|
|
317
|
+
/** Whether a property that places or sizes an element has a value a document would give it. */
|
|
318
|
+
function sizeAllowed(name, value, tag, fontPx) {
|
|
319
|
+
if (name === 'margin-left' && (tag === 'ul' || tag === 'ol'))
|
|
320
|
+
return within(value, { most: MOST_PX, mostBelow: LIST_OUTDENT_PX }, fontPx);
|
|
321
|
+
if (name.startsWith('padding') && INLINE_TAGS.has(tag))
|
|
322
|
+
return within(value, { most: INLINE_PADDING_PX, mostPercent: 0 }, fontPx);
|
|
323
|
+
if (name.startsWith('margin') || name.startsWith('padding'))
|
|
324
|
+
return within(value, { most: MOST_PX }, fontPx);
|
|
325
|
+
// (A first line that starts left of its paragraph is looked at with the paragraph's own
|
|
326
|
+
// indent, in sanitizeStyle.)
|
|
327
|
+
if (name === 'text-indent')
|
|
328
|
+
return within(value, { most: MOST_PX, mostBelow: MOST_PX }, fontPx);
|
|
329
|
+
if (name === 'width' || name === 'height')
|
|
330
|
+
return SIZED_TAGS.has(tag) && within(value, { most: MOST_PX }, fontPx);
|
|
331
|
+
if (name === 'display')
|
|
332
|
+
return DISPLAY_VALUES.has(value.trim().toLowerCase());
|
|
333
|
+
if (name === 'line-height')
|
|
334
|
+
return within(value, { most: 400, mostPercent: 1000, mostNumber: 10 }, fontPx);
|
|
335
|
+
if (/^border.*width$/.test(name) || name === 'border-spacing' || name === 'text-decoration-thickness')
|
|
336
|
+
return within(value, { most: 100 }, fontPx);
|
|
337
|
+
if (name.endsWith('tab-size'))
|
|
338
|
+
return within(value, { most: MOST_PX, mostNumber: 100 }, fontPx);
|
|
339
|
+
return true;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Whether a width or height attribute is within a page. (A browser reads the number at its
|
|
343
|
+
* start and takes it as px, or as a share of the width with a % after it.)
|
|
344
|
+
*/
|
|
345
|
+
function sizeAttributeAllowed(value) {
|
|
346
|
+
const match = /^\s*(\d*\.?\d+)\s*(%?)/.exec(value);
|
|
347
|
+
if (!match)
|
|
348
|
+
return true;
|
|
349
|
+
return Number(match[1]) <= (match[2] === '%' ? 100 : MOST_PX);
|
|
350
|
+
}
|
|
351
|
+
/** The style text split into what it declares, each as it was written. */
|
|
352
|
+
function declarations(raw) {
|
|
353
|
+
return raw
|
|
354
|
+
.split(';')
|
|
355
|
+
.map((text) => ({ name: (text.includes(':') ? text.slice(0, text.indexOf(':')) : text).trim().toLowerCase(), text }))
|
|
356
|
+
.filter((declaration) => declaration.name !== '');
|
|
357
|
+
}
|
|
358
|
+
/**
|
|
359
|
+
* Whether a declaration, taken alone, is one this browser does not read. Asked only where two
|
|
360
|
+
* of an attribute's names are a short form and one of its parts ("margin" and "margin-left"):
|
|
361
|
+
* there the properties the browser read do not say which of the two it read them from, and
|
|
362
|
+
* "margin: 0; margin-left: expression(…)" passed as read in full.
|
|
363
|
+
*/
|
|
364
|
+
function unreadAlone(text, el) {
|
|
365
|
+
const probe = el.ownerDocument.createElement('span');
|
|
366
|
+
probe.setAttribute('style', text);
|
|
367
|
+
return !probe.style || probe.style.length === 0;
|
|
368
|
+
}
|
|
369
|
+
function sanitizeStyle(el, tag, fonts) {
|
|
370
|
+
const raw = el.getAttribute('style');
|
|
371
|
+
if (raw === null)
|
|
372
|
+
return;
|
|
373
|
+
const style = el.style;
|
|
374
|
+
if (!style) {
|
|
375
|
+
el.removeAttribute('style');
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const wordHidden = WORD_HIDDEN.test(raw);
|
|
379
|
+
const drop = [];
|
|
380
|
+
// The size of this element's text first: its other lengths may be counted in it.
|
|
381
|
+
let fontPx = fontPxAround(el, fonts);
|
|
382
|
+
const fontSize = style.getPropertyValue('font-size');
|
|
383
|
+
if (fontSize) {
|
|
384
|
+
const px = fontSizePx(fontSize, fontPx);
|
|
385
|
+
if (fontSizeAllowed(px))
|
|
386
|
+
fontPx = px;
|
|
387
|
+
else
|
|
388
|
+
drop.push('font-size');
|
|
389
|
+
}
|
|
390
|
+
fonts.set(el, fontPx);
|
|
391
|
+
for (let i = 0; i < style.length; i++) {
|
|
392
|
+
const name = style.item(i);
|
|
393
|
+
const value = style.getPropertyValue(name);
|
|
394
|
+
if (name === 'font-size')
|
|
395
|
+
continue;
|
|
396
|
+
if (name in NO_BORDER_IMAGE) {
|
|
397
|
+
// `border: 1px solid` also says "no border picture", and a browser lists that with
|
|
398
|
+
// the properties it read. That much is no reason to write the attribute again; a
|
|
399
|
+
// border picture itself is drawn outside its element, and a document has none.
|
|
400
|
+
if (!NO_BORDER_IMAGE[name].includes(value.trim().toLowerCase()))
|
|
401
|
+
drop.push(...Object.keys(NO_BORDER_IMAGE));
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (!STYLE_PROPERTY.test(name) || UNSAFE_STYLE_VALUE.test(value) || !sizeAllowed(name, value, tag, fontPx))
|
|
405
|
+
drop.push(name);
|
|
406
|
+
}
|
|
407
|
+
// A first line may start left of its paragraph as far as the paragraph is itself moved in:
|
|
408
|
+
// a hanging indent, "margin-left:36pt;text-indent:-36pt". Further, and its first words are
|
|
409
|
+
// left of the editing area, where nothing shows them.
|
|
410
|
+
const first = style.getPropertyValue('text-indent');
|
|
411
|
+
if (first.includes('-') && !drop.includes('text-indent')) {
|
|
412
|
+
const side = el.closest('[dir]')?.getAttribute('dir')?.toLowerCase() === 'rtl' ? 'right' : 'left';
|
|
413
|
+
let room = 0;
|
|
414
|
+
for (const name of [`margin-${side}`, `padding-${side}`]) {
|
|
415
|
+
if (!drop.includes(name))
|
|
416
|
+
room += oneLengthPx(style.getPropertyValue(name), fontPx) ?? 0;
|
|
417
|
+
}
|
|
418
|
+
const indent = oneLengthPx(first, fontPx);
|
|
419
|
+
if (indent === null || -indent > room + 0.5)
|
|
420
|
+
drop.push('text-indent');
|
|
421
|
+
}
|
|
422
|
+
// The attribute stays as it was written only if all of it is something read and kept. What
|
|
423
|
+
// this browser did not read as a property does nothing on this page, but it is still in the
|
|
424
|
+
// attribute for whatever shows the HTML next: "width: expression(…)" went through that way.
|
|
425
|
+
// So where the attribute names a property that is not kept, or a value that loads or runs
|
|
426
|
+
// something, it is written again from the properties that were read and kept.
|
|
427
|
+
const declared = declarations(raw);
|
|
428
|
+
const names = declared.map((declaration) => declaration.name);
|
|
429
|
+
const read = Array.from({ length: style.length }, (_, i) => style.item(i));
|
|
430
|
+
const shortAndPart = names.some((name) => names.some((other) => other.startsWith(`${name}-`)));
|
|
431
|
+
// (A property named twice is read once, and which one cannot be told from here.)
|
|
432
|
+
const unread = UNSAFE_STYLE_VALUE.test(raw) ||
|
|
433
|
+
new Set(names).size !== names.length ||
|
|
434
|
+
names.some((name) => !STYLE_PROPERTY.test(name) || !read.some((property) => property === name || property.startsWith(`${name}-`))) ||
|
|
435
|
+
(shortAndPart && declared.some((declaration) => unreadAlone(declaration.text, el)));
|
|
436
|
+
if (drop.length === 0 && !unread)
|
|
437
|
+
return;
|
|
438
|
+
for (const name of drop)
|
|
439
|
+
style.removeProperty(name);
|
|
440
|
+
// What Word hid stays hidden, in a way the browser keeps: mso-hide is not a property it
|
|
441
|
+
// reads, so the text showed on screen while the RTF left it out.
|
|
442
|
+
if (wordHidden)
|
|
443
|
+
style.setProperty('display', 'none');
|
|
444
|
+
const kept = style.cssText;
|
|
445
|
+
if (kept)
|
|
446
|
+
el.setAttribute('style', kept);
|
|
447
|
+
else
|
|
448
|
+
el.removeAttribute('style');
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Whether an id is a name the page's scripts look up on `window` (id="location", or a variable
|
|
452
|
+
* of the page's own). In a browser every element's id is also a name on `window`, which gives
|
|
453
|
+
* the element; that is no such name, or a document shown a second time, with its first showing
|
|
454
|
+
* still in the editor, would lose every id it has.
|
|
455
|
+
*/
|
|
456
|
+
function namesAGlobal(id) {
|
|
457
|
+
if (!(id in globalThis))
|
|
458
|
+
return false;
|
|
459
|
+
try {
|
|
460
|
+
const value = globalThis[id];
|
|
461
|
+
if (typeof Element !== 'undefined' && value instanceof Element)
|
|
462
|
+
return value.id !== id;
|
|
463
|
+
if (typeof HTMLCollection !== 'undefined' && value instanceof HTMLCollection)
|
|
464
|
+
return value[0]?.id !== id;
|
|
465
|
+
}
|
|
466
|
+
catch {
|
|
467
|
+
// Not to be read from here: something of the browser's own.
|
|
468
|
+
}
|
|
469
|
+
return true;
|
|
470
|
+
}
|
|
471
|
+
function sanitizeAttributes(el, tag, fonts, idTaken) {
|
|
472
|
+
const own = TAG_ATTRIBUTES[tag];
|
|
473
|
+
for (const attr of Array.from(el.attributes)) {
|
|
474
|
+
const name = attr.name.toLowerCase();
|
|
475
|
+
if (name === 'class') {
|
|
476
|
+
if (el.classList.contains(KEPT_CLASS))
|
|
477
|
+
el.setAttribute('class', KEPT_CLASS);
|
|
478
|
+
else
|
|
479
|
+
el.removeAttribute(attr.name);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (name === 'id') {
|
|
483
|
+
// A document's own ids are kept: an application marks its paragraphs with them. Not
|
|
484
|
+
// one that names something the page's scripts look up on `document` or `window`
|
|
485
|
+
// (id="cookie", id="location"), and not one the page already has an element for
|
|
486
|
+
// (`idTaken`): the newcomer would answer to getElementById in its place, and take on
|
|
487
|
+
// what the page's stylesheet gives "#sidebar". And not one on a link: a script that
|
|
488
|
+
// reads `window.settings` and finds a link there reads the link's address as text.
|
|
489
|
+
if (attr.value in document || namesAGlobal(attr.value) || idTaken?.(attr.value) || (tag === 'a' && el.hasAttribute('href'))) {
|
|
490
|
+
el.removeAttribute(attr.name);
|
|
491
|
+
}
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (!GLOBAL_ATTRIBUTES.has(name) && !own?.includes(name))
|
|
495
|
+
el.removeAttribute(attr.name);
|
|
496
|
+
// width="100000" is a table a hundred thousand pixels wide.
|
|
497
|
+
else if ((name === 'width' || name === 'height') && !sizeAttributeAllowed(attr.value))
|
|
498
|
+
el.removeAttribute(attr.name);
|
|
499
|
+
}
|
|
500
|
+
// What is in a <details> is in the RTF whether it is open or not, so it is shown open.
|
|
501
|
+
if (tag === 'details')
|
|
502
|
+
el.setAttribute('open', '');
|
|
503
|
+
if (tag === 'a' && el.hasAttribute('href')) {
|
|
504
|
+
const target = linkTarget(el.getAttribute('href') || '');
|
|
505
|
+
if (target === null) {
|
|
506
|
+
el.removeAttribute('href');
|
|
507
|
+
el.removeAttribute('target');
|
|
508
|
+
}
|
|
509
|
+
else if (el.getAttribute('target')) {
|
|
510
|
+
// A link that opens a new tab does not hand that tab this page.
|
|
511
|
+
el.setAttribute('rel', 'noopener noreferrer');
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
if (tag === 'img' && el.hasAttribute('src') && !isSafeImageUrl(el.getAttribute('src') || '')) {
|
|
515
|
+
el.removeAttribute('src');
|
|
516
|
+
}
|
|
517
|
+
sanitizeStyle(el, tag, fonts);
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Whether an element is a formula's copy for a screen reader, beside the same formula written
|
|
521
|
+
* as text: KaTeX writes both, and its stylesheet hides this one. Kept, "30%" read "30%30%".
|
|
522
|
+
*/
|
|
523
|
+
function isReadAloudCopy(el) {
|
|
524
|
+
return (el.classList.contains('katex-mathml') &&
|
|
525
|
+
Array.from(el.parentElement?.children ?? []).some((beside) => beside.classList.contains('katex-html')));
|
|
526
|
+
}
|
|
527
|
+
/**
|
|
528
|
+
* Make the elements under `root` safe, in place. With `only`, just the elements it accepts (the
|
|
529
|
+
* ones a paste brought in) are looked at; the rest of the document is left as it is. With
|
|
530
|
+
* `idTaken`, an id it says the page already uses is taken off.
|
|
531
|
+
* Returns whether anything was taken out or changed.
|
|
532
|
+
*/
|
|
533
|
+
export function sanitizeTree(root, only, idTaken) {
|
|
534
|
+
let changed = false;
|
|
535
|
+
const fonts = new Map();
|
|
536
|
+
/** The words of the last element that left its words, where they are still in the document. */
|
|
537
|
+
let lastKept = null;
|
|
538
|
+
for (const el of Array.from(root.querySelectorAll('*'))) {
|
|
539
|
+
// Gone already, with something it was in.
|
|
540
|
+
if (!root.contains(el))
|
|
541
|
+
continue;
|
|
542
|
+
if (only && !only(el))
|
|
543
|
+
continue;
|
|
544
|
+
const tag = el.tagName.toLowerCase();
|
|
545
|
+
if (REMOVED_TAGS.has(tag) || isReadAloudCopy(el)) {
|
|
546
|
+
el.remove();
|
|
547
|
+
changed = true;
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
if (TEXT_KEPT_TAGS.has(tag)) {
|
|
551
|
+
const text = shownText(el, tag);
|
|
552
|
+
if (text) {
|
|
553
|
+
// Two boxes side by side were two things on the screen: "1" and "5", not "15".
|
|
554
|
+
const kept = el.ownerDocument.createTextNode(lastKept !== null && el.previousSibling === lastKept ? ` ${text}` : text);
|
|
555
|
+
el.replaceWith(kept);
|
|
556
|
+
lastKept = kept;
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
el.remove();
|
|
560
|
+
}
|
|
561
|
+
changed = true;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
// A custom element would be given its behaviour by the page: only what it holds is kept,
|
|
565
|
+
// and nothing of one that was hidden, which would show once the element is gone.
|
|
566
|
+
if (tag.includes('-')) {
|
|
567
|
+
if (hiddenByItself(el))
|
|
568
|
+
el.remove();
|
|
569
|
+
else
|
|
570
|
+
el.replaceWith(...Array.from(el.childNodes));
|
|
571
|
+
changed = true;
|
|
572
|
+
continue;
|
|
573
|
+
}
|
|
574
|
+
const before = el.attributes.length + (el.getAttribute('style') ?? '') + (el.getAttribute('href') ?? '') + (el.getAttribute('width') ?? '') + (el.getAttribute('height') ?? '');
|
|
575
|
+
sanitizeAttributes(el, tag, fonts, idTaken);
|
|
576
|
+
if (before !== el.attributes.length + (el.getAttribute('style') ?? '') + (el.getAttribute('href') ?? '') + (el.getAttribute('width') ?? '') + (el.getAttribute('height') ?? ''))
|
|
577
|
+
changed = true;
|
|
578
|
+
}
|
|
579
|
+
return changed;
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* HTML made safe to show and edit: see the top of this file for what is kept. It is parsed in a
|
|
583
|
+
* document of its own, where nothing in it runs or loads, before any of it reaches the page.
|
|
584
|
+
* Where there is no DOM to parse it with (on a server), nothing is returned: HTML that could
|
|
585
|
+
* not be checked is not passed on.
|
|
586
|
+
*/
|
|
587
|
+
export function sanitizeHtml(html) {
|
|
588
|
+
return safeBody(html)?.innerHTML ?? '';
|
|
589
|
+
}
|
|
590
|
+
/** The HTML, read in a document of its own and made safe there; null where it cannot be. */
|
|
591
|
+
function safeBody(html, idTaken) {
|
|
592
|
+
if (!html || typeof DOMParser === 'undefined')
|
|
593
|
+
return null;
|
|
594
|
+
const doc = new DOMParser().parseFromString(`<!DOCTYPE html><html><body>${html}</body></html>`, 'text/html');
|
|
595
|
+
sanitizeTree(doc.body, undefined, idTaken);
|
|
596
|
+
return doc.body;
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* The same, as nodes for `into` to hold. The editor puts these in the page as they are: what was
|
|
600
|
+
* checked is what is shown. Written out as text and read in again, a second reading has to
|
|
601
|
+
* agree with the first for the check to mean anything, and that is where a sanitizer is got
|
|
602
|
+
* round; it is also a third pass over a document that may hold megabytes of pictures.
|
|
603
|
+
*/
|
|
604
|
+
export function sanitizedNodes(html, into, idTaken) {
|
|
605
|
+
const body = safeBody(html, idTaken);
|
|
606
|
+
if (!body)
|
|
607
|
+
return [];
|
|
608
|
+
return Array.from(body.childNodes).map((node) => into.importNode(node, true));
|
|
609
|
+
}
|
|
610
|
+
/** Elements whose text is not text of the page. */
|
|
611
|
+
const NOT_PAGE_TEXT = new Set(['script', 'style', 'title', 'template', 'noscript', 'textarea']);
|
|
612
|
+
const withoutSpace = (text) => text.replace(/\s/g, '');
|
|
613
|
+
/**
|
|
614
|
+
* Read from a paste's HTML what the browser's own paste may lose: null where it holds neither
|
|
615
|
+
* Word's hidden text nor a text box, which is nearly every paste.
|
|
616
|
+
*/
|
|
617
|
+
export function pasteNotes(html) {
|
|
618
|
+
if (typeof DOMParser === 'undefined' || !(WORD_HIDDEN.test(html) || /<textarea/i.test(html)))
|
|
619
|
+
return null;
|
|
620
|
+
const doc = new DOMParser().parseFromString(html, 'text/html');
|
|
621
|
+
const notes = { pieces: [], textareas: [] };
|
|
622
|
+
// What is pasted is what lies between these two comments, where a program wrote them.
|
|
623
|
+
let inside = !html.includes('<!--StartFragment');
|
|
624
|
+
const todo = [{ node: doc.documentElement, hidden: false, undisplayed: false }];
|
|
625
|
+
while (todo.length > 0) {
|
|
626
|
+
const { node, hidden, undisplayed } = todo.pop();
|
|
627
|
+
if (node.nodeType === 8) {
|
|
628
|
+
const comment = node.data.trim();
|
|
629
|
+
if (comment.startsWith('StartFragment'))
|
|
630
|
+
inside = true;
|
|
631
|
+
else if (comment.startsWith('EndFragment'))
|
|
632
|
+
inside = false;
|
|
633
|
+
continue;
|
|
634
|
+
}
|
|
635
|
+
if (node.nodeType === 3) {
|
|
636
|
+
if (inside)
|
|
637
|
+
notes.pieces.push({ text: node.data, hidden, undisplayed });
|
|
638
|
+
continue;
|
|
639
|
+
}
|
|
640
|
+
if (node.nodeType !== 1)
|
|
641
|
+
continue;
|
|
642
|
+
const el = node;
|
|
643
|
+
const tag = el.tagName.toLowerCase();
|
|
644
|
+
if (tag === 'textarea' && inside)
|
|
645
|
+
notes.textareas.push(el.textContent ?? '');
|
|
646
|
+
if (NOT_PAGE_TEXT.has(tag))
|
|
647
|
+
continue;
|
|
648
|
+
const style = el.getAttribute('style') ?? '';
|
|
649
|
+
const state = {
|
|
650
|
+
hidden: hidden || WORD_HIDDEN.test(style),
|
|
651
|
+
undisplayed: undisplayed || el.hasAttribute('hidden') || STYLED_HIDDEN.test(style)
|
|
652
|
+
};
|
|
653
|
+
const children = Array.from(el.childNodes);
|
|
654
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
655
|
+
todo.push({ node: children[i], ...state });
|
|
656
|
+
}
|
|
657
|
+
return notes;
|
|
658
|
+
}
|
|
659
|
+
/**
|
|
660
|
+
* Take Word's hidden text out of what a paste put under `root`. The paste is the text from the
|
|
661
|
+
* `from`th to the `to`th character of `root` that is not white space. Nothing is done unless
|
|
662
|
+
* that text is the paste's own, character for character: only then is it known which of the
|
|
663
|
+
* characters were the hidden ones. An element left with nothing in it goes too, where
|
|
664
|
+
* `mayRemove` allows (a paragraph that was hidden whole would stay as an empty line in the
|
|
665
|
+
* RTF). Returns whether anything was taken out.
|
|
666
|
+
*/
|
|
667
|
+
export function removeWordHidden(root, from, to, notes, mayRemove) {
|
|
668
|
+
const texts = [];
|
|
669
|
+
const todo = [root];
|
|
670
|
+
while (todo.length > 0) {
|
|
671
|
+
const node = todo.pop();
|
|
672
|
+
if (node.nodeType === 3)
|
|
673
|
+
texts.push(node);
|
|
674
|
+
const children = Array.from(node.childNodes);
|
|
675
|
+
for (let i = children.length - 1; i >= 0; i--)
|
|
676
|
+
todo.push(children[i]);
|
|
677
|
+
}
|
|
678
|
+
const pasted = withoutSpace(texts.map((text) => text.data).join('')).slice(from, to);
|
|
679
|
+
// A browser may leave what is not displayed out of a paste, or bring it along.
|
|
680
|
+
for (const pieces of [notes.pieces, notes.pieces.filter((piece) => !piece.undisplayed)]) {
|
|
681
|
+
if (withoutSpace(pieces.map((piece) => piece.text).join('')) !== pasted)
|
|
682
|
+
continue;
|
|
683
|
+
/** The hidden runs, as [first, after last] among the characters that are not white space. */
|
|
684
|
+
const runs = [];
|
|
685
|
+
let at = from;
|
|
686
|
+
for (const piece of pieces) {
|
|
687
|
+
const length = withoutSpace(piece.text).length;
|
|
688
|
+
if (piece.hidden && length > 0) {
|
|
689
|
+
const last = runs[runs.length - 1];
|
|
690
|
+
if (last && last[1] === at)
|
|
691
|
+
last[1] = at + length;
|
|
692
|
+
else
|
|
693
|
+
runs.push([at, at + length]);
|
|
694
|
+
}
|
|
695
|
+
at += length;
|
|
696
|
+
}
|
|
697
|
+
if (runs.length === 0)
|
|
698
|
+
return false;
|
|
699
|
+
let index = 0;
|
|
700
|
+
let run = 0;
|
|
701
|
+
/** Whether the last character kept was white space, and whether a hidden one came after it. */
|
|
702
|
+
let afterSpace = false;
|
|
703
|
+
let afterHidden = false;
|
|
704
|
+
for (const text of texts) {
|
|
705
|
+
/** The stretches of this text to take out, as [first, after last]. */
|
|
706
|
+
const out = [];
|
|
707
|
+
const data = text.data;
|
|
708
|
+
for (let i = 0; i < data.length; i++) {
|
|
709
|
+
while (run < runs.length && runs[run][1] <= index)
|
|
710
|
+
run++;
|
|
711
|
+
const [first, after] = run < runs.length ? runs[run] : [Infinity, Infinity];
|
|
712
|
+
let hide;
|
|
713
|
+
if (/\s/.test(data[i])) {
|
|
714
|
+
// White space is hidden with the run it is inside of. At the run's edges it
|
|
715
|
+
// stays, but as one space: "one [hidden] two" is "one two", not "one two".
|
|
716
|
+
hide = (index > first && index < after) || (afterHidden && afterSpace);
|
|
717
|
+
if (!hide)
|
|
718
|
+
afterSpace = true;
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
hide = index >= first && index < after;
|
|
722
|
+
if (hide)
|
|
723
|
+
afterHidden = true;
|
|
724
|
+
else
|
|
725
|
+
afterSpace = afterHidden = false;
|
|
726
|
+
index++;
|
|
727
|
+
}
|
|
728
|
+
if (!hide)
|
|
729
|
+
continue;
|
|
730
|
+
const last = out[out.length - 1];
|
|
731
|
+
if (last && last[1] === i)
|
|
732
|
+
last[1] = i + 1;
|
|
733
|
+
else
|
|
734
|
+
out.push([i, i + 1]);
|
|
735
|
+
}
|
|
736
|
+
if (out.length === 0)
|
|
737
|
+
continue;
|
|
738
|
+
// Taken out stretch by stretch, last first: a caret in this text then stays where it
|
|
739
|
+
// was among the characters that are left. Given new text whole, it would jump to
|
|
740
|
+
// the start.
|
|
741
|
+
for (let i = out.length - 1; i >= 0; i--)
|
|
742
|
+
text.deleteData(out[i][0], out[i][1] - out[i][0]);
|
|
743
|
+
let emptied = text.parentElement;
|
|
744
|
+
if (text.data === '')
|
|
745
|
+
text.remove();
|
|
746
|
+
while (emptied && emptied !== root && emptied.childNodes.length === 0 && mayRemove?.(emptied)) {
|
|
747
|
+
const up = emptied.parentElement;
|
|
748
|
+
emptied.remove();
|
|
749
|
+
emptied = up;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
return true;
|
|
753
|
+
}
|
|
754
|
+
return false;
|
|
755
|
+
}
|