svelte-rtf-editor 1.10.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 +38 -5
- package/dist/components/InkEditor.svelte +797 -85
- package/dist/components/InkEditor.svelte.d.ts +5 -0
- package/dist/components/Toolbar.svelte +4 -4
- package/dist/index.d.ts +1 -0
- package/dist/rtf-writer.js +36 -21
- package/dist/sanitize.d.ts +43 -6
- package/dist/sanitize.js +192 -59
- package/package.json +1 -1
package/dist/sanitize.js
CHANGED
|
@@ -135,8 +135,49 @@ function shownText(el, tag) {
|
|
|
135
135
|
}
|
|
136
136
|
/** Attributes any element may keep. */
|
|
137
137
|
const GLOBAL_ATTRIBUTES = new Set(['style', 'dir', 'lang', 'title', 'hidden', 'align', 'data-rtf-tab-stops']);
|
|
138
|
+
/**
|
|
139
|
+
* What an application asked to have kept (the editor's `keepAttributes` and `keepClasses`), as
|
|
140
|
+
* the sets the sanitizer reads. Only `data-` attributes can be asked for: they mark an element
|
|
141
|
+
* and do nothing else, where `onclick`, `src` or `style` asked for here would undo what this
|
|
142
|
+
* file is for. A class is kept by its name alone; what it looks like is the page's stylesheet,
|
|
143
|
+
* which is why none is kept unless the application names it. A name that is neither is left out.
|
|
144
|
+
*/
|
|
145
|
+
export function keptMarks(options) {
|
|
146
|
+
const attributes = new Set();
|
|
147
|
+
for (const name of namesIn(options?.keepAttributes)) {
|
|
148
|
+
const lower = name.toLowerCase();
|
|
149
|
+
if (/^data-[a-z0-9_.-]+$/.test(lower))
|
|
150
|
+
attributes.add(lower);
|
|
151
|
+
}
|
|
152
|
+
// Every class name given is taken as it is. A rule for what a name may look like refused
|
|
153
|
+
// `md:flex`, `w-1/2` and `résumé` without saying so, and kept nothing out: a name is only
|
|
154
|
+
// ever compared with the classes an element has, and one with a space in it is none of them.
|
|
155
|
+
const classes = new Set(namesIn(options?.keepClasses));
|
|
156
|
+
return { attributes, classes };
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* The names in one of the two settings. A caller without types can pass one name where a list
|
|
160
|
+
* is meant (`keepClasses="summary"`): read as a list it is its letters, which kept classes named
|
|
161
|
+
* "s" and "u" and not "summary". It is taken as a list of that one name. What is not a list is
|
|
162
|
+
* no names, and what is not text is not a name, so a `null` in a list keeps no class "null".
|
|
163
|
+
*/
|
|
164
|
+
function namesIn(given) {
|
|
165
|
+
// Array.from gives no items for what is neither a list nor like one (a number, an object).
|
|
166
|
+
const list = typeof given === 'string' ? [given] : given == null ? [] : Array.from(given);
|
|
167
|
+
return list.filter((name) => typeof name === 'string').map((name) => name.trim());
|
|
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
|
+
}
|
|
138
179
|
/** Attributes only some elements may keep. */
|
|
139
|
-
const TAG_ATTRIBUTES = {
|
|
180
|
+
const TAG_ATTRIBUTES = table({
|
|
140
181
|
a: ['href', 'target', 'rel'],
|
|
141
182
|
img: ['src', 'alt', 'width', 'height'],
|
|
142
183
|
td: ['colspan', 'rowspan', 'width'],
|
|
@@ -148,7 +189,7 @@ const TAG_ATTRIBUTES = {
|
|
|
148
189
|
li: ['value'],
|
|
149
190
|
font: ['color', 'size', 'face'],
|
|
150
191
|
details: ['open']
|
|
151
|
-
};
|
|
192
|
+
});
|
|
152
193
|
/**
|
|
153
194
|
* The one class that means something here: Safari's wrapper around a typed tab. Any other class
|
|
154
195
|
* would take on whatever the page's own stylesheet gives that name.
|
|
@@ -169,9 +210,9 @@ const UNSAFE_STYLE_VALUE = /url\s*\(|expression\s*\(|javascript:|@import|behavio
|
|
|
169
210
|
// the editing area cuts it off, or text of no size. They would be in the RTF and not on the
|
|
170
211
|
// screen, so those values go too.
|
|
171
212
|
/** 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 };
|
|
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 });
|
|
173
214
|
/** Lengths counted in the element's font size: how many of them make one. */
|
|
174
|
-
const FONT_UNITS = { em: 1, ex: 2, ch: 2 };
|
|
215
|
+
const FONT_UNITS = table({ em: 1, ex: 2, ch: 2 });
|
|
175
216
|
/** The font size where nothing above sets one. */
|
|
176
217
|
const BASE_FONT_PX = 16;
|
|
177
218
|
/**
|
|
@@ -180,10 +221,10 @@ const BASE_FONT_PX = 16;
|
|
|
180
221
|
*/
|
|
181
222
|
const LEAST_FONT_PX = 0.5;
|
|
182
223
|
/** The font sizes that have names, in px. */
|
|
183
|
-
const NAMED_FONT_PX = {
|
|
224
|
+
const NAMED_FONT_PX = table({
|
|
184
225
|
'xx-small': 9, 'x-small': 10, small: 13, medium: 16, large: 18, 'x-large': 24, 'xx-large': 32, 'xxx-large': 48,
|
|
185
226
|
'-webkit-xxx-large': 48
|
|
186
|
-
};
|
|
227
|
+
});
|
|
187
228
|
/** A step up or down in size (`larger`, `smaller`): the factor a browser uses. */
|
|
188
229
|
const FONT_STEP = 1.2;
|
|
189
230
|
/**
|
|
@@ -201,13 +242,13 @@ const INLINE_TAGS = new Set([
|
|
|
201
242
|
'small', 'big', 'abbr', 'cite', 'q', 'label', 'kbd', 'samp', 'var', 'tt', 'bdi', 'bdo', 'time', 'dfn'
|
|
202
243
|
]);
|
|
203
244
|
/** What each part of a border picture is when there is none: what `border: …` sets them to. */
|
|
204
|
-
const NO_BORDER_IMAGE = {
|
|
245
|
+
const NO_BORDER_IMAGE = table({
|
|
205
246
|
'border-image-source': ['none', 'initial'],
|
|
206
247
|
'border-image-slice': ['100%', 'initial'],
|
|
207
248
|
'border-image-width': ['1', 'initial'],
|
|
208
249
|
'border-image-outset': ['0', '0px', 'initial'],
|
|
209
250
|
'border-image-repeat': ['stretch', 'initial']
|
|
210
|
-
};
|
|
251
|
+
});
|
|
211
252
|
/** A little over the widest page there is, 22 inches (2112 px). */
|
|
212
253
|
const MOST_PX = 2200;
|
|
213
254
|
/** The elements that have a width or a height of their own in a document. */
|
|
@@ -219,10 +260,12 @@ const DISPLAY_VALUES = new Set([
|
|
|
219
260
|
]);
|
|
220
261
|
/** One number of a style value with its unit, as px; null for a percentage, a bare number or a unit not known. */
|
|
221
262
|
function lengthPx(digits, unit, fontPx) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
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;
|
|
226
269
|
return null;
|
|
227
270
|
}
|
|
228
271
|
const NUMBER_AND_UNIT = /(-?\d*\.?\d+(?:e[+-]?\d+)?)([a-z%]*)/g;
|
|
@@ -272,8 +315,9 @@ function oneLengthPx(value, fontPx) {
|
|
|
272
315
|
*/
|
|
273
316
|
function fontSizePx(value, parentPx) {
|
|
274
317
|
const text = value.trim().toLowerCase();
|
|
275
|
-
|
|
276
|
-
|
|
318
|
+
const named = NAMED_FONT_PX.get(text);
|
|
319
|
+
if (named !== undefined)
|
|
320
|
+
return named;
|
|
277
321
|
if (text === 'larger')
|
|
278
322
|
return parentPx * FONT_STEP;
|
|
279
323
|
if (text === 'smaller')
|
|
@@ -393,12 +437,13 @@ function sanitizeStyle(el, tag, fonts) {
|
|
|
393
437
|
const value = style.getPropertyValue(name);
|
|
394
438
|
if (name === 'font-size')
|
|
395
439
|
continue;
|
|
396
|
-
|
|
440
|
+
const noPicture = NO_BORDER_IMAGE.get(name);
|
|
441
|
+
if (noPicture !== undefined) {
|
|
397
442
|
// `border: 1px solid` also says "no border picture", and a browser lists that with
|
|
398
443
|
// the properties it read. That much is no reason to write the attribute again; a
|
|
399
444
|
// border picture itself is drawn outside its element, and a document has none.
|
|
400
|
-
if (!
|
|
401
|
-
drop.push(...
|
|
445
|
+
if (!noPicture.includes(value.trim().toLowerCase()))
|
|
446
|
+
drop.push(...NO_BORDER_IMAGE.keys());
|
|
402
447
|
continue;
|
|
403
448
|
}
|
|
404
449
|
if (!STYLE_PROPERTY.test(name) || UNSAFE_STYLE_VALUE.test(value) || !sizeAllowed(name, value, tag, fontPx))
|
|
@@ -468,13 +513,14 @@ function namesAGlobal(id) {
|
|
|
468
513
|
}
|
|
469
514
|
return true;
|
|
470
515
|
}
|
|
471
|
-
function sanitizeAttributes(el, tag, fonts, idTaken) {
|
|
472
|
-
const own = TAG_ATTRIBUTES
|
|
516
|
+
function sanitizeAttributes(el, tag, fonts, idTaken, keep) {
|
|
517
|
+
const own = TAG_ATTRIBUTES.get(tag);
|
|
473
518
|
for (const attr of Array.from(el.attributes)) {
|
|
474
519
|
const name = attr.name.toLowerCase();
|
|
475
520
|
if (name === 'class') {
|
|
476
|
-
|
|
477
|
-
|
|
521
|
+
const stay = Array.from(el.classList).filter((token) => token === KEPT_CLASS || keep?.classes.has(token));
|
|
522
|
+
if (stay.length > 0)
|
|
523
|
+
el.setAttribute('class', stay.join(' '));
|
|
478
524
|
else
|
|
479
525
|
el.removeAttribute(attr.name);
|
|
480
526
|
continue;
|
|
@@ -491,7 +537,7 @@ function sanitizeAttributes(el, tag, fonts, idTaken) {
|
|
|
491
537
|
}
|
|
492
538
|
continue;
|
|
493
539
|
}
|
|
494
|
-
if (!GLOBAL_ATTRIBUTES.has(name) && !own?.includes(name))
|
|
540
|
+
if (!GLOBAL_ATTRIBUTES.has(name) && !own?.includes(name) && !keep?.attributes.has(name))
|
|
495
541
|
el.removeAttribute(attr.name);
|
|
496
542
|
// width="100000" is a table a hundred thousand pixels wide.
|
|
497
543
|
else if ((name === 'width' || name === 'height') && !sizeAttributeAllowed(attr.value))
|
|
@@ -527,25 +573,25 @@ function isReadAloudCopy(el) {
|
|
|
527
573
|
/**
|
|
528
574
|
* Make the elements under `root` safe, in place. With `only`, just the elements it accepts (the
|
|
529
575
|
* 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.
|
|
576
|
+
* `idTaken`, an id it says the page already uses is taken off. With `keep` (see keptMarks),
|
|
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.
|
|
531
582
|
* Returns whether anything was taken out or changed.
|
|
532
583
|
*/
|
|
533
|
-
export function sanitizeTree(root, only, idTaken) {
|
|
584
|
+
export function sanitizeTree(root, only, idTaken, keep, wordsLeft, leftOut) {
|
|
534
585
|
let changed = false;
|
|
535
586
|
const fonts = new Map();
|
|
536
587
|
/** The words of the last element that left its words, where they are still in the document. */
|
|
537
588
|
let lastKept = null;
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
if (!root.contains(el))
|
|
541
|
-
continue;
|
|
542
|
-
if (only && !only(el))
|
|
543
|
-
continue;
|
|
589
|
+
/** One element made safe; returns whether it was taken out or changed. */
|
|
590
|
+
const check = (el) => {
|
|
544
591
|
const tag = el.tagName.toLowerCase();
|
|
545
592
|
if (REMOVED_TAGS.has(tag) || isReadAloudCopy(el)) {
|
|
546
593
|
el.remove();
|
|
547
|
-
|
|
548
|
-
continue;
|
|
594
|
+
return true;
|
|
549
595
|
}
|
|
550
596
|
if (TEXT_KEPT_TAGS.has(tag)) {
|
|
551
597
|
const text = shownText(el, tag);
|
|
@@ -554,12 +600,12 @@ export function sanitizeTree(root, only, idTaken) {
|
|
|
554
600
|
const kept = el.ownerDocument.createTextNode(lastKept !== null && el.previousSibling === lastKept ? ` ${text}` : text);
|
|
555
601
|
el.replaceWith(kept);
|
|
556
602
|
lastKept = kept;
|
|
603
|
+
wordsLeft?.(el, kept);
|
|
557
604
|
}
|
|
558
605
|
else {
|
|
559
606
|
el.remove();
|
|
560
607
|
}
|
|
561
|
-
|
|
562
|
-
continue;
|
|
608
|
+
return true;
|
|
563
609
|
}
|
|
564
610
|
// A custom element would be given its behaviour by the page: only what it holds is kept,
|
|
565
611
|
// and nothing of one that was hidden, which would show once the element is gone.
|
|
@@ -568,13 +614,31 @@ export function sanitizeTree(root, only, idTaken) {
|
|
|
568
614
|
el.remove();
|
|
569
615
|
else
|
|
570
616
|
el.replaceWith(...Array.from(el.childNodes));
|
|
571
|
-
|
|
572
|
-
continue;
|
|
617
|
+
return true;
|
|
573
618
|
}
|
|
574
619
|
const before = el.attributes.length + (el.getAttribute('style') ?? '') + (el.getAttribute('href') ?? '') + (el.getAttribute('width') ?? '') + (el.getAttribute('height') ?? '');
|
|
575
|
-
sanitizeAttributes(el, tag, fonts, idTaken);
|
|
576
|
-
|
|
620
|
+
sanitizeAttributes(el, tag, fonts, idTaken, keep);
|
|
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();
|
|
577
639
|
changed = true;
|
|
640
|
+
leftOut?.(el);
|
|
641
|
+
}
|
|
578
642
|
}
|
|
579
643
|
return changed;
|
|
580
644
|
}
|
|
@@ -582,17 +646,18 @@ export function sanitizeTree(root, only, idTaken) {
|
|
|
582
646
|
* HTML made safe to show and edit: see the top of this file for what is kept. It is parsed in a
|
|
583
647
|
* document of its own, where nothing in it runs or loads, before any of it reaches the page.
|
|
584
648
|
* 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.
|
|
649
|
+
* not be checked is not passed on. `keepAttributes` and `keepClasses` name marks of the
|
|
650
|
+
* application's own to leave on (see keptMarks).
|
|
586
651
|
*/
|
|
587
|
-
export function sanitizeHtml(html) {
|
|
588
|
-
return safeBody(html)?.innerHTML ?? '';
|
|
652
|
+
export function sanitizeHtml(html, options) {
|
|
653
|
+
return safeBody(html, undefined, keptMarks(options))?.innerHTML ?? '';
|
|
589
654
|
}
|
|
590
655
|
/** The HTML, read in a document of its own and made safe there; null where it cannot be. */
|
|
591
|
-
function safeBody(html, idTaken) {
|
|
656
|
+
function safeBody(html, idTaken, keep, leftOut) {
|
|
592
657
|
if (!html || typeof DOMParser === 'undefined')
|
|
593
658
|
return null;
|
|
594
659
|
const doc = new DOMParser().parseFromString(`<!DOCTYPE html><html><body>${html}</body></html>`, 'text/html');
|
|
595
|
-
sanitizeTree(doc.body, undefined, idTaken);
|
|
660
|
+
sanitizeTree(doc.body, undefined, idTaken, keep, undefined, leftOut);
|
|
596
661
|
return doc.body;
|
|
597
662
|
}
|
|
598
663
|
/**
|
|
@@ -600,16 +665,79 @@ function safeBody(html, idTaken) {
|
|
|
600
665
|
* checked is what is shown. Written out as text and read in again, a second reading has to
|
|
601
666
|
* agree with the first for the check to mean anything, and that is where a sanitizer is got
|
|
602
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).
|
|
603
669
|
*/
|
|
604
|
-
export function sanitizedNodes(html, into, idTaken) {
|
|
605
|
-
const body = safeBody(html, idTaken);
|
|
670
|
+
export function sanitizedNodes(html, into, idTaken, keep, leftOut) {
|
|
671
|
+
const body = safeBody(html, idTaken, keep, leftOut);
|
|
606
672
|
if (!body)
|
|
607
673
|
return [];
|
|
608
674
|
return Array.from(body.childNodes).map((node) => into.importNode(node, true));
|
|
609
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
|
+
}
|
|
610
725
|
/** Elements whose text is not text of the page. */
|
|
611
726
|
const NOT_PAGE_TEXT = new Set(['script', 'style', 'title', 'template', 'noscript', 'textarea']);
|
|
612
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
|
+
}
|
|
613
741
|
/**
|
|
614
742
|
* Read from a paste's HTML what the browser's own paste may lose: null where it holds neither
|
|
615
743
|
* Word's hidden text nor a text box, which is nearly every paste.
|
|
@@ -663,6 +791,10 @@ export function pasteNotes(html) {
|
|
|
663
791
|
* characters were the hidden ones. An element left with nothing in it goes too, where
|
|
664
792
|
* `mayRemove` allows (a paragraph that was hidden whole would stay as an empty line in the
|
|
665
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.
|
|
666
798
|
*/
|
|
667
799
|
export function removeWordHidden(root, from, to, notes, mayRemove) {
|
|
668
800
|
const texts = [];
|
|
@@ -675,25 +807,26 @@ export function removeWordHidden(root, from, to, notes, mayRemove) {
|
|
|
675
807
|
for (let i = children.length - 1; i >= 0; i--)
|
|
676
808
|
todo.push(children[i]);
|
|
677
809
|
}
|
|
678
|
-
const
|
|
810
|
+
const shown = withoutSpace(texts.map((text) => text.data).join(''));
|
|
679
811
|
// A browser may leave what is not displayed out of a paste, or bring it along.
|
|
680
812
|
for (const pieces of [notes.pieces, notes.pieces.filter((piece) => !piece.undisplayed)]) {
|
|
681
|
-
|
|
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)
|
|
682
818
|
continue;
|
|
683
819
|
/** The hidden runs, as [first, after last] among the characters that are not white space. */
|
|
684
820
|
const runs = [];
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
}
|
|
695
|
-
at += length;
|
|
696
|
-
}
|
|
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
|
+
});
|
|
697
830
|
if (runs.length === 0)
|
|
698
831
|
return false;
|
|
699
832
|
let index = 0;
|
package/package.json
CHANGED