svelte-rtf-editor 1.8.2 → 1.9.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 +96 -7
- package/dist/RtfViewer.svelte +63 -7
- package/dist/RtfViewer.svelte.d.ts +2 -1
- package/dist/components/InkEditor.svelte +61 -2
- package/dist/components/InkEditor.svelte.d.ts +2 -0
- package/dist/images.d.ts +9 -2
- package/dist/images.js +27 -7
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/rtf-parser.d.ts +16 -3
- package/dist/rtf-parser.js +1100 -457
- package/dist/rtf-writer.d.ts +27 -4
- package/dist/rtf-writer.js +659 -298
- package/dist/table-model.d.ts +42 -0
- package/dist/table-model.js +139 -0
- package/dist/utils.js +160 -17
- package/package.json +1 -1
package/dist/rtf-writer.js
CHANGED
|
@@ -3,14 +3,17 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Walks a contenteditable DOM tree and produces a valid RTF 1.x string.
|
|
5
5
|
* Supports: a document font and body size (Arial 12 pt by default, set with
|
|
6
|
-
* RtfWriteOptions), bold, italic, underline, strikethrough, font sizes
|
|
7
|
-
* foreground colors (\cf), paragraphs
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* RtfWriteOptions), bold, italic, underline, strikethrough, font sizes, headings
|
|
7
|
+
* (h1–h6), foreground colors (\cf), paragraphs with their alignment and indents,
|
|
8
|
+
* bullet/numbered lists and lists inside them, blockquotes, code blocks,
|
|
9
|
+
* hyperlinks, tables with merged cells, images (embedded as \pict picture groups
|
|
10
|
+
* with their captions), horizontal rules, page breaks, line breaks, and Unicode
|
|
11
|
+
* characters.
|
|
10
12
|
*
|
|
11
13
|
* Background color is intentionally not written — RTF highlight support
|
|
12
14
|
* is inconsistent across viewers (TextEdit uses \AppleHighlight, not \highlight).
|
|
13
15
|
*/
|
|
16
|
+
import { tableModel, columnShares, tableRowsInOrder, rowCells } from './table-model.js';
|
|
14
17
|
/** Font used when no fontFamily is given. */
|
|
15
18
|
export const DEFAULT_FONT_FAMILY = 'Arial';
|
|
16
19
|
/** Body text size, in points, used when no fontSize is given. */
|
|
@@ -22,8 +25,24 @@ const MAX_FONT_SIZE = 1638;
|
|
|
22
25
|
* and h3 14 pt. The parser reads headings back with the same ratios.
|
|
23
26
|
*/
|
|
24
27
|
export const HEADING_SCALE = { h1: 2, h2: 1.5, h3: 7 / 6 };
|
|
25
|
-
/**
|
|
26
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Every heading level's size as a multiple of the body size. The editor offers h1 to h3; h4, h5
|
|
30
|
+
* and h6 arrive in pasted text and are written at the sizes a browser shows them: the body
|
|
31
|
+
* size, 0.83 and 0.67 of it.
|
|
32
|
+
*/
|
|
33
|
+
const ALL_HEADING_SCALES = { ...HEADING_SCALE, h4: 1, h5: 5 / 6, h6: 2 / 3 };
|
|
34
|
+
const HEADING_TAGS = new Set(Object.keys(ALL_HEADING_SCALES));
|
|
35
|
+
/**
|
|
36
|
+
* Code blocks are set slightly smaller than the body: 10 pt at 12 pt. The parser reads a code
|
|
37
|
+
* block back by the same ratio.
|
|
38
|
+
*/
|
|
39
|
+
export const PRE_SCALE = 5 / 6;
|
|
40
|
+
/**
|
|
41
|
+
* The color of link text. A document with a link carries it as the first entry of its color
|
|
42
|
+
* table, so a link can be told from underlined text; the parser drops it again inside a link,
|
|
43
|
+
* where the <a> already says it.
|
|
44
|
+
*/
|
|
45
|
+
export const LINK_COLOR = { r: 0, g: 0, b: 255 };
|
|
27
46
|
/** 1 px at 96 dpi = 15 twips (1 twip = 1/1440 in). */
|
|
28
47
|
const TWIPS_PER_PX = 15;
|
|
29
48
|
// ── Color helpers ──
|
|
@@ -287,17 +306,23 @@ function imageBody(el) {
|
|
|
287
306
|
const label = el.getAttribute('alt') || el.getAttribute('src') || 'image';
|
|
288
307
|
return `\\i [Image: ${escapeRtf(label)}]\\i0 `;
|
|
289
308
|
}
|
|
290
|
-
/** \ql / \qc / \qr / \qj for a block's text-align. */
|
|
291
|
-
function
|
|
309
|
+
/** \ql / \qc / \qr / \qj for a block's own text-align, or null when it sets none. */
|
|
310
|
+
function ownAlign(el) {
|
|
292
311
|
// Firefox's Center writes <p align="center">, not a style.
|
|
293
312
|
const align = (el.style?.textAlign || el.getAttribute?.('align') || '').toLowerCase();
|
|
294
313
|
if (align === 'center')
|
|
295
314
|
return '\\qc';
|
|
296
|
-
if (align === 'right')
|
|
315
|
+
if (align === 'right' || align === 'end')
|
|
297
316
|
return '\\qr';
|
|
298
317
|
if (align === 'justify')
|
|
299
318
|
return '\\qj';
|
|
300
|
-
|
|
319
|
+
if (align === 'left' || align === 'start')
|
|
320
|
+
return '\\ql';
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
/** \ql / \qc / \qr / \qj for a block's text-align; left when it sets none. */
|
|
324
|
+
function alignControl(el) {
|
|
325
|
+
return ownAlign(el) ?? '\\ql';
|
|
301
326
|
}
|
|
302
327
|
/** Decoded byte length of a base64 payload, without decoding it. */
|
|
303
328
|
function base64ByteLength(base64) {
|
|
@@ -322,6 +347,9 @@ const BLOCK_OVERHEAD_BYTES = {
|
|
|
322
347
|
h1: 29,
|
|
323
348
|
h2: 29,
|
|
324
349
|
h3: 29,
|
|
350
|
+
h4: 29,
|
|
351
|
+
h5: 29,
|
|
352
|
+
h6: 29,
|
|
325
353
|
li: 35,
|
|
326
354
|
blockquote: 22,
|
|
327
355
|
pre: 27,
|
|
@@ -348,7 +376,7 @@ const BLOCK_OVERHEAD_BYTES = {
|
|
|
348
376
|
ol: 0
|
|
349
377
|
};
|
|
350
378
|
/** Elements that write a paragraph of their own, which inside a table cell carries \intbl. */
|
|
351
|
-
const CELL_PARAGRAPH_TAGS = new Set(['p', 'div', 'h1', 'h2', 'h3', 'li', 'blockquote', 'pre', 'hr', 'figure', 'figcaption']);
|
|
379
|
+
const CELL_PARAGRAPH_TAGS = new Set(['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'li', 'blockquote', 'pre', 'hr', 'figure', 'figcaption']);
|
|
352
380
|
/** The field wrapper around a link, apart from its address. */
|
|
353
381
|
const LINK_OVERHEAD_BYTES = '{\\field{\\*\\fldinst HYPERLINK ""}{\\fldrslt\\ul\\cf1 \\cf0\\ulnone }}'.length;
|
|
354
382
|
/**
|
|
@@ -376,6 +404,9 @@ export function estimateRtfBytes(editorEl) {
|
|
|
376
404
|
}
|
|
377
405
|
total += base64ByteLength(src.slice(comma + 1)) * 2 + PICTURE_OVERHEAD_BYTES;
|
|
378
406
|
}
|
|
407
|
+
// A document with a link carries the link color in its color table ("\red0\green0\blue255;").
|
|
408
|
+
if (hasLink(editorEl))
|
|
409
|
+
total += 22;
|
|
379
410
|
// Text at its escaped length: a character outside ASCII costs its \uN\'3f escape,
|
|
380
411
|
// six to nine bytes rather than one.
|
|
381
412
|
total += escapeRtf(editorEl.textContent || '').length;
|
|
@@ -411,9 +442,23 @@ export function estimateRtfBytes(editorEl) {
|
|
|
411
442
|
if (target)
|
|
412
443
|
total += LINK_OVERHEAD_BYTES + escapeRtf(target).length;
|
|
413
444
|
}
|
|
414
|
-
// Each line break in a code block is written as "\line " (6)
|
|
445
|
+
// Each line break in a code block is written as "\line " (6). It is counted as 1 with
|
|
446
|
+
// the text, or as nothing where it follows another: a run of them is counted there as
|
|
447
|
+
// one space.
|
|
415
448
|
if (tag === 'pre')
|
|
416
|
-
total += ((el.textContent || '').match(/\r\n|\r|\n/g)?.length ?? 0) *
|
|
449
|
+
total += ((el.textContent || '').match(/\r\n|\r|\n/g)?.length ?? 0) * 6;
|
|
450
|
+
// A paragraph that follows loose text first ends that text's line ("\par ").
|
|
451
|
+
if ((tag === 'p' || tag === 'div' || BREAKS_LOOSE_TEXT.has(tag)) && followsInlineContent(el, editorEl))
|
|
452
|
+
total += 5;
|
|
453
|
+
// A heading, code block or quotation inside a list item, quotation or paragraph restates the
|
|
454
|
+
// paragraph around it (\pard, the indent, \par) in ways the counts above do not follow
|
|
455
|
+
// exactly: up to 10 more each, so the estimate stays above the real size.
|
|
456
|
+
if (/^(h[1-6]|pre|blockquote)$/.test(tag) && el.parentElement !== editorEl && el.parentElement?.closest('li, blockquote, p'))
|
|
457
|
+
total += 10;
|
|
458
|
+
// In a list item, text after a wrapper that ends with a block starts a paragraph again
|
|
459
|
+
// ("\pard\intbl\liN\fi-N ", at most 24).
|
|
460
|
+
if (!isBlock(el) && el.closest('li') && endsWithBlock(el))
|
|
461
|
+
total += 24;
|
|
417
462
|
// A paragraph a block writes inside a table cell also carries \intbl (6). A list
|
|
418
463
|
// writes one paragraph per item, so the item is counted, not the list.
|
|
419
464
|
if (CELL_PARAGRAPH_TAGS.has(tag) && el.closest('td, th'))
|
|
@@ -427,12 +472,49 @@ export function estimateRtfBytes(editorEl) {
|
|
|
427
472
|
if (para || (prev && formatCache.get(prev)))
|
|
428
473
|
total += para.length + 6;
|
|
429
474
|
}
|
|
430
|
-
//
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
475
|
+
// A cell's own alignment is written on its paragraph (\qc, 3). A cell down several rows
|
|
476
|
+
// is written once in each of them, marked \clvmgf or \clvmrg (7). A table inside a cell
|
|
477
|
+
// is written as text, which costs less than the cells counted for it.
|
|
478
|
+
if (tag === 'td' || tag === 'th') {
|
|
479
|
+
if (el.style?.textAlign || el.hasAttribute('align'))
|
|
480
|
+
total += 3;
|
|
481
|
+
const rows = parseInt(el.getAttribute('rowspan') ?? '', 10);
|
|
482
|
+
if (rows !== 1 && Number.isFinite(rows) && !el.parentElement?.closest('td, th')) {
|
|
483
|
+
// rowspan="0" reaches the last row; no span reaches past the table.
|
|
484
|
+
const rowCount = el.closest('table')?.querySelectorAll('tr').length ?? 1;
|
|
485
|
+
const spanned = Math.min(rowCount, rows > 0 ? rows : rowCount);
|
|
486
|
+
total += 7 + (spanned - 1) * ((BLOCK_OVERHEAD_BYTES[tag] ?? 0) + 7);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
// A row with fewer cells than the table has columns is filled with empty ones.
|
|
490
|
+
if (tag === 'table' && !el.parentElement?.closest('td, th')) {
|
|
491
|
+
const model = tableModel(el);
|
|
492
|
+
for (const row of model.rows)
|
|
493
|
+
total += row.filter((slot) => slot === null).length * BLOCK_OVERHEAD_BYTES.td;
|
|
494
|
+
}
|
|
495
|
+
// Text or an inline element after a block starts a paragraph of its own where that
|
|
496
|
+
// block set an indent or alignment ("\pard\intbl\qc\li1440 ", and its "\par ").
|
|
497
|
+
if (isBlock(el) && followedByInline(el))
|
|
498
|
+
total += 27;
|
|
499
|
+
// So does text written straight into a list, between its items.
|
|
500
|
+
if (tag === 'ul' || tag === 'ol') {
|
|
501
|
+
for (const child of Array.from(el.childNodes)) {
|
|
502
|
+
if (child.nodeType === Node.TEXT_NODE ? !COLLAPSIBLE_WHITESPACE.test(child.textContent || '') : !isBlock(child))
|
|
503
|
+
total += 27;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// A paragraph inside a list item is written at the item's indent (\liN, up to 7).
|
|
507
|
+
if ((tag === 'p' || tag === 'div') && el.parentElement?.tagName === 'LI')
|
|
508
|
+
total += 8;
|
|
509
|
+
// A numbered item's number is counted as one digit; a list that starts high has more.
|
|
510
|
+
if (tag === 'ol') {
|
|
511
|
+
const last = (parseInt(el.getAttribute('start') ?? '', 10) || 1) + el.children.length;
|
|
512
|
+
total += el.children.length * Math.max(0, String(last).length - 1);
|
|
513
|
+
}
|
|
514
|
+
// A figure with no picture is a paragraph of what it holds: "\pard\intbl\qc\liN " and
|
|
515
|
+
// "\par " (at most 30).
|
|
434
516
|
if (tag === 'figure' && !el.querySelector('img'))
|
|
435
|
-
total +=
|
|
517
|
+
total += 30;
|
|
436
518
|
// A color costs "\cfN " on and off and, the first time, its color-table entry (at most
|
|
437
519
|
// 22 bytes, "\red255\green255\blue255;"); a size costs "\fsNN " on and off.
|
|
438
520
|
if (el.style?.color || (tag === 'font' && el.hasAttribute('color')))
|
|
@@ -467,6 +549,10 @@ export function linkTarget(url) {
|
|
|
467
549
|
return null;
|
|
468
550
|
}
|
|
469
551
|
}
|
|
552
|
+
/** Whether any link in the element is one the writer writes as a link. */
|
|
553
|
+
function hasLink(el) {
|
|
554
|
+
return Array.from(el.querySelectorAll('a[href]')).some((a) => linkTarget(a.getAttribute('href') || '') !== null);
|
|
555
|
+
}
|
|
470
556
|
/** Whether a link address is an absolute web, mail or phone address (see linkTarget). */
|
|
471
557
|
export function isSafeLinkUrl(url) {
|
|
472
558
|
return linkTarget(url) !== null;
|
|
@@ -483,10 +569,18 @@ function cleanFontName(name) {
|
|
|
483
569
|
const cleaned = first.replace(/[\\{};]/g, '').trim();
|
|
484
570
|
return cleaned || DEFAULT_FONT_FAMILY;
|
|
485
571
|
}
|
|
572
|
+
/**
|
|
573
|
+
* Whether a font name is a monospace font's: Courier New, Consolas, Menlo, a "… Mono" family
|
|
574
|
+
* (Roboto Mono, DejaVu Sans Mono), and their PostScript names (CourierNewPSMT, SFMono-Regular).
|
|
575
|
+
* Monotype Corsiva is not one, and neither is American Typewriter, which is proportional.
|
|
576
|
+
*/
|
|
577
|
+
export function isMonospaceFontName(name) {
|
|
578
|
+
return /courier|mono(?!type|ton|fett)|consolas|menlo|monaco|lucida ?console|lucida ?sans ?typewriter|ocr[ -]?[ab]\b|source ?code|fira ?code|cascadia|jetbrains/.test(name.toLowerCase());
|
|
579
|
+
}
|
|
486
580
|
/** RTF font family class for a font name: \froman, \fmodern or \fswiss. */
|
|
487
581
|
function fontFamilyClass(name) {
|
|
488
582
|
const n = name.toLowerCase();
|
|
489
|
-
if (
|
|
583
|
+
if (isMonospaceFontName(n))
|
|
490
584
|
return 'modern';
|
|
491
585
|
if (/times|georgia|garamond|cambria|palatino|antiqua|schoolbook|baskerville|didot|bodoni|serif/.test(n) &&
|
|
492
586
|
!/sans/.test(n)) {
|
|
@@ -524,6 +618,10 @@ export function htmlToRtf(editorEl, options = {}) {
|
|
|
524
618
|
const fontName = cleanFontName(options.fontFamily);
|
|
525
619
|
const bodySize = bodyHalfPoints(options.fontSize);
|
|
526
620
|
const colorMap = new Map();
|
|
621
|
+
// A link's text is \cf1. In a document with a link that entry is the link color, so a
|
|
622
|
+
// link can be told from underlined text; in any other it is black, as it always was.
|
|
623
|
+
if (hasLink(editorEl))
|
|
624
|
+
colorMap.set(colorKey(LINK_COLOR), { ...LINK_COLOR });
|
|
527
625
|
colorMap.set('0,0,0', { r: 0, g: 0, b: 0 });
|
|
528
626
|
collectColors(editorEl, colorMap);
|
|
529
627
|
const colors = Array.from(colorMap.values());
|
|
@@ -559,7 +657,8 @@ export function htmlToRtf(editorEl, options = {}) {
|
|
|
559
657
|
color: 0,
|
|
560
658
|
script: null,
|
|
561
659
|
mono: false,
|
|
562
|
-
indent: 0
|
|
660
|
+
indent: 0,
|
|
661
|
+
cellAlign: ''
|
|
563
662
|
});
|
|
564
663
|
const rtf = '{\\rtf1\\ansi\\ansicpg1252\\deff0' +
|
|
565
664
|
fontTable +
|
|
@@ -592,296 +691,482 @@ function collectColors(el, colorMap) {
|
|
|
592
691
|
function walkChildren(parent, ctx) {
|
|
593
692
|
let rtf = '';
|
|
594
693
|
const children = parent.childNodes;
|
|
595
|
-
for (let i = 0; i < children.length; i++)
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
694
|
+
for (let i = 0; i < children.length; i++)
|
|
695
|
+
rtf += walkNode(children[i], ctx);
|
|
696
|
+
return rtf;
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* One node as RTF. `restate` is off where the caller has already started the paragraph the node
|
|
700
|
+
* goes in (a list item's lines). `endLooseText` is off where the caller has already ended the
|
|
701
|
+
* line before a block (flowRtf).
|
|
702
|
+
*/
|
|
703
|
+
function walkNode(node, ctx, restate = true, endLooseText = true) {
|
|
704
|
+
let rtf = '';
|
|
705
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
706
|
+
const text = node.textContent || '';
|
|
707
|
+
// Whitespace between two blocks (the newline in "</p>\n<p>") shows as
|
|
708
|
+
// nothing in a browser. Written out, it became a space at the start of
|
|
709
|
+
// the next paragraph.
|
|
710
|
+
const tabsShow = ctx.inPre || showsTabs(node);
|
|
711
|
+
if (!tabsShow && COLLAPSIBLE_WHITESPACE.test(text) && besideBlock(node))
|
|
712
|
+
return '';
|
|
713
|
+
if (!ctx.inPre && restate)
|
|
714
|
+
rtf += restateAfterBlock(node, ctx);
|
|
715
|
+
if (ctx.inPre) {
|
|
716
|
+
// Split before escaping — escapeRtf collapses newlines, so the
|
|
717
|
+
// line structure a <pre> block depends on has to become \line
|
|
718
|
+
// first. Handles CR, LF and CRLF alike.
|
|
719
|
+
rtf += text.split(/\r\n|\r|\n/).map((line) => escapeTabs(line, true)).join('\\line ');
|
|
617
720
|
}
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
721
|
+
else {
|
|
722
|
+
rtf += escapeTabs(text, tabsShow);
|
|
723
|
+
}
|
|
724
|
+
return rtf;
|
|
725
|
+
}
|
|
726
|
+
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
727
|
+
return '';
|
|
728
|
+
const el = node;
|
|
729
|
+
const tag = el.tagName.toLowerCase();
|
|
730
|
+
// Text typed straight into the parent before a block ("Diagnosis:<ul>…", the shape Chrome
|
|
731
|
+
// makes when the line after it becomes a list) has no paragraph of its own: it is ended here,
|
|
732
|
+
// or \pard would carry it into the block's first line. A <p> or <div>, and a table inside a
|
|
733
|
+
// cell, end it themselves.
|
|
734
|
+
if (endLooseText && BREAKS_LOOSE_TEXT.has(tag) && !(tag === 'table' && ctx.inTableCell) && followsInlineContent(el, ctx.root)) {
|
|
735
|
+
rtf += '\\par ';
|
|
736
|
+
}
|
|
737
|
+
// A picture directly under the root writes its own paragraph.
|
|
738
|
+
if (restate && !isBlock(el) && !(tag === 'img' && el.parentNode === ctx.root))
|
|
739
|
+
rtf += restateAfterBlock(el, ctx);
|
|
740
|
+
switch (tag) {
|
|
741
|
+
// Heading sizes keep their ratio to the body (HEADING_SCALE). <h4> to <h6> come in with
|
|
742
|
+
// pasted text; each is a paragraph of its own, as large as a browser shows it.
|
|
743
|
+
case 'h1':
|
|
744
|
+
case 'h2':
|
|
745
|
+
case 'h3':
|
|
746
|
+
case 'h4':
|
|
747
|
+
case 'h5':
|
|
748
|
+
case 'h6': {
|
|
749
|
+
const size = scaled(ctx, ALL_HEADING_SCALES[tag]);
|
|
750
|
+
// A heading that holds a block (a list made in it with the browser's list command)
|
|
751
|
+
// is not a paragraph itself; what is inside is written at the heading's size, bold.
|
|
752
|
+
if (Array.from(el.children).some(isBlock)) {
|
|
753
|
+
rtf += `\\fs${size}${ctx.bold ? '' : '\\b'} ${walkChildren(el, { ...ctx, bold: true, size })}${ctx.bold ? '' : '\\b0'}\\fs${ctx.size} `;
|
|
640
754
|
break;
|
|
641
755
|
}
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
const inner = walkChildren(el, colorIdx != null ? { ...ctx, color: colorIdx } : ctx);
|
|
664
|
-
rtf += colorIdx != null
|
|
665
|
-
? `${restate}\\cf${colorIdx} ${inner}\\cf${ctx.color} \\par `
|
|
666
|
-
: `${restate}${inner}\\par `;
|
|
667
|
-
break;
|
|
668
|
-
}
|
|
669
|
-
// A <div> that holds blocks (pasted content wraps paragraphs in one) is not a
|
|
670
|
-
// paragraph itself: each block inside writes its own \\pard, which would drop the
|
|
671
|
-
// div's indent, and the div's own \\par would add a blank line. Its left margin is
|
|
672
|
-
// carried into the blocks instead, and loose text in it gets a paragraph.
|
|
673
|
-
if (tag === 'div' && Array.from(el.children).some(isBlock)) {
|
|
674
|
-
const indent = ctx.indent + leftMarginTwips(el);
|
|
675
|
-
const colorIdx = colorIndexOf(el, ctx);
|
|
676
|
-
const inner = { ...ctx, indent, color: colorIdx ?? ctx.color };
|
|
677
|
-
const lead = startsWithInline(el) ? `\\pard${liControl(indent)} ` : '';
|
|
678
|
-
const tail = endsWithInline(el) ? '\\par ' : '';
|
|
679
|
-
const colorPrefix = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
680
|
-
const colorSuffix = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
681
|
-
rtf += `${afterLooseText ? '\\par ' : ''}${lead}${colorPrefix}${walkChildren(el, inner)}${colorSuffix}${tail}`;
|
|
682
|
-
break;
|
|
683
|
-
}
|
|
684
|
-
// The paragraph's alignment, indents, spacing, keep-with-next and tab stops, if it
|
|
685
|
-
// sets any.
|
|
686
|
-
const para = paragraphFormat(el, ctx);
|
|
687
|
-
// A <p> with no children or only a single <br> is a blank line —
|
|
688
|
-
// emit a bare \par rather than \pard \line\par, which adds an extra line.
|
|
689
|
-
// A bare \par keeps the formatting of the paragraph before it, so a blank line after a
|
|
690
|
-
// formatted paragraph, or with formatting of its own, restates \pard.
|
|
691
|
-
if (isBlankLine(el)) {
|
|
692
|
-
const reset = para || followsFormattedParagraph(el, ctx) ? `\\pard${para} ` : '';
|
|
693
|
-
rtf += `${afterLooseText ? '\\par ' : ''}${reset}\\par `;
|
|
694
|
-
break;
|
|
695
|
-
}
|
|
756
|
+
rtf += `\\pard${inCell(ctx)}${headingLayout(el, ctx)}\\f0\\fs${size}\\b ${walkChildren(el, { ...ctx, bold: true, size })}${ctx.bold ? '' : '\\b0'}\\fs${ctx.size}\\par `;
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
case 'p':
|
|
760
|
+
case 'div': {
|
|
761
|
+
// Inside a table cell the cell has already declared \pard\intbl. Each paragraph
|
|
762
|
+
// ends with \par, so two paragraphs in one cell stay two (the cell drops the last
|
|
763
|
+
// one's); one that follows a list or heading in the cell restates \pard\intbl,
|
|
764
|
+
// so it does not inherit that block's indent.
|
|
765
|
+
// Text typed straight into the parent before this block ("abc<div>def</div>",
|
|
766
|
+
// the shape Chrome makes on Enter) has no paragraph of its own: end it here, or
|
|
767
|
+
// the two lines run together.
|
|
768
|
+
const afterLooseText = followsInlineContent(el, ctx.root);
|
|
769
|
+
// A <div> that holds blocks (pasted content wraps paragraphs in one) is not a
|
|
770
|
+
// paragraph itself: each block inside writes its own \\pard, which would drop the
|
|
771
|
+
// div's indent, and the div's own \\par would add a blank line. Its left margin is
|
|
772
|
+
// carried into the blocks instead, and loose text in it gets a paragraph. A <p> that
|
|
773
|
+
// holds a block is the same: no parser makes one, but a browser's own list command
|
|
774
|
+
// does, leaving the list inside the paragraph it was made from (<p><ul>…</ul></p>).
|
|
775
|
+
if (Array.from(el.children).some(isBlock)) {
|
|
776
|
+
const indent = ctx.indent + leftMarginTwips(el);
|
|
696
777
|
const colorIdx = colorIndexOf(el, ctx);
|
|
697
|
-
const
|
|
778
|
+
const inner = { ...ctx, indent, color: colorIdx ?? ctx.color };
|
|
779
|
+
const lead = startsWithInline(el) ? `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(indent)} ` : '';
|
|
780
|
+
const tail = endsWithInline(el) ? '\\par ' : '';
|
|
698
781
|
const colorPrefix = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
699
782
|
const colorSuffix = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
700
|
-
rtf += `${afterLooseText ? '\\par ' : ''}
|
|
783
|
+
rtf += `${afterLooseText ? '\\par ' : ''}${lead}${colorPrefix}${walkChildren(el, inner)}${colorSuffix}${tail}`;
|
|
701
784
|
break;
|
|
702
785
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
const
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
// Fall back to equal column widths if the style is absent.
|
|
713
|
-
const cellRights = [];
|
|
714
|
-
let cumTwips = 0;
|
|
715
|
-
for (const cell of cells) {
|
|
716
|
-
const pct = parseFloat(cell.style.width) || (100 / cells.length);
|
|
717
|
-
cumTwips += Math.round((pct / 100) * PAGE_WIDTH);
|
|
718
|
-
cellRights.push(cumTwips);
|
|
719
|
-
}
|
|
720
|
-
cellRights[cellRights.length - 1] = PAGE_WIDTH; // snap last edge to avoid rounding drift
|
|
721
|
-
// Row definition
|
|
722
|
-
let rowRtf = '{\\trowd\\trgaph120';
|
|
723
|
-
for (let ci = 0; ci < cells.length; ci++) {
|
|
724
|
-
if (isHeader)
|
|
725
|
-
rowRtf += `\\clbrdrb\\brdrs`;
|
|
726
|
-
rowRtf += `\\cellx${cellRights[ci]}`;
|
|
727
|
-
}
|
|
728
|
-
rowRtf += '\\trkeep\\intbl{';
|
|
729
|
-
// Cell bodies
|
|
730
|
-
const cellCtx = { ...ctx, inTableCell: true };
|
|
731
|
-
for (const cell of cells) {
|
|
732
|
-
const content = withoutTrailingPar(walkChildren(cell, cellCtx));
|
|
733
|
-
rowRtf += `{\\pard\\intbl \\f0 \\sa0 \\li0 \\fi0 ${content}\\par}\\cell`;
|
|
734
|
-
}
|
|
735
|
-
rowRtf += '}\\intbl\\row}';
|
|
736
|
-
rtf += rowRtf;
|
|
786
|
+
if (ctx.inTableCell) {
|
|
787
|
+
// Alignment or an indent needs a paragraph of its own, even as the cell's first.
|
|
788
|
+
const layout = paragraphLayout(el, ctx);
|
|
789
|
+
const restate = afterLooseText
|
|
790
|
+
? `\\par \\pard\\intbl${layout} `
|
|
791
|
+
: el.previousElementSibling || layout ? `\\pard\\intbl${layout} ` : '';
|
|
792
|
+
if (isBlankLine(el)) {
|
|
793
|
+
rtf += `${restate}\\par `;
|
|
794
|
+
break;
|
|
737
795
|
}
|
|
796
|
+
const colorIdx = colorIndexOf(el, ctx);
|
|
797
|
+
const inner = walkChildren(el, colorIdx != null ? { ...ctx, color: colorIdx } : ctx);
|
|
798
|
+
rtf += colorIdx != null
|
|
799
|
+
? `${restate}\\cf${colorIdx} ${inner}\\cf${ctx.color} \\par `
|
|
800
|
+
: `${restate}${inner}\\par `;
|
|
738
801
|
break;
|
|
739
802
|
}
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
const content = walkChildren(el, inner);
|
|
751
|
-
const italicOn = quote && !ctx.italic;
|
|
752
|
-
const italicOff = italicOn ? (tail ? '\\i0' : '\\i0 ') : '';
|
|
753
|
-
rtf += `${lead}${italicOn ? '\\i ' : lead ? ' ' : ''}${content}${italicOff}${tail}`;
|
|
803
|
+
// The paragraph's alignment, indents, spacing, keep-with-next and tab stops, if it
|
|
804
|
+
// sets any.
|
|
805
|
+
const para = paragraphFormat(el, ctx);
|
|
806
|
+
// A <p> with no children or only a single <br> is a blank line —
|
|
807
|
+
// emit a bare \par rather than \pard \line\par, which adds an extra line.
|
|
808
|
+
// A bare \par keeps the formatting of the paragraph before it, so a blank line after a
|
|
809
|
+
// formatted paragraph, or with formatting of its own, restates \pard.
|
|
810
|
+
if (isBlankLine(el)) {
|
|
811
|
+
const reset = para || followsFormattedParagraph(el, ctx) ? `\\pard${para} ` : '';
|
|
812
|
+
rtf += `${afterLooseText ? '\\par ' : ''}${reset}\\par `;
|
|
754
813
|
break;
|
|
755
814
|
}
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
815
|
+
const colorIdx = colorIndexOf(el, ctx);
|
|
816
|
+
const inlineRtf = walkChildren(el, colorIdx != null ? { ...ctx, color: colorIdx } : ctx);
|
|
817
|
+
const colorPrefix = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
818
|
+
const colorSuffix = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
819
|
+
rtf += `${afterLooseText ? '\\par ' : ''}\\pard${para} ${colorPrefix}${inlineRtf}${colorSuffix}\\par `;
|
|
820
|
+
break;
|
|
821
|
+
}
|
|
822
|
+
case 'table':
|
|
823
|
+
rtf += ctx.inTableCell ? nestedTableRtf(el, ctx) : tableRtf(el, ctx);
|
|
824
|
+
break;
|
|
825
|
+
case 'blockquote': {
|
|
826
|
+
// The browser's Indent wraps the lines in a blockquote with a left margin and
|
|
827
|
+
// border: none (Chromium, WebKit, Gmail): that is an indent, not a quotation, so it
|
|
828
|
+
// is not italic. A quotation is indented half an inch, plus any margin of its own.
|
|
829
|
+
// Either way the blocks inside carry the indent, and loose text gets a paragraph.
|
|
830
|
+
const quote = !isIndentBlockquote(el);
|
|
831
|
+
const indent = ctx.indent + leftMarginTwips(el) + (quote ? 720 : 0);
|
|
832
|
+
const inner = { ...ctx, indent, italic: ctx.italic || quote };
|
|
833
|
+
const lead = startsWithInline(el) ? `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(indent)}` : '';
|
|
834
|
+
const tail = endsWithInline(el) ? '\\par ' : '';
|
|
835
|
+
const content = walkChildren(el, inner);
|
|
836
|
+
const italicOn = quote && !ctx.italic;
|
|
837
|
+
const italicOff = italicOn ? (tail ? '\\i0' : '\\i0 ') : '';
|
|
838
|
+
rtf += `${lead}${italicOn ? '\\i ' : lead ? ' ' : ''}${content}${italicOff}${tail}`;
|
|
839
|
+
break;
|
|
840
|
+
}
|
|
841
|
+
case 'pre': {
|
|
842
|
+
const size = scaled(ctx, PRE_SCALE);
|
|
843
|
+
rtf += `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(ctx.indent + leftMarginTwips(el))}\\f1\\fs${size} ${walkChildren(el, { ...ctx, inPre: true, size })}\\f0\\fs${ctx.size}\\par `;
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
case 'hr':
|
|
847
|
+
// A page break read from RTF is shown as a rule that says so, and goes back as \page.
|
|
848
|
+
if (!ctx.inTableCell && isPageBreak(el)) {
|
|
849
|
+
rtf += '\\page ';
|
|
759
850
|
break;
|
|
760
851
|
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
852
|
+
rtf += `\\pard${inCell(ctx)}\\qc \\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\emdash\\par `;
|
|
853
|
+
break;
|
|
854
|
+
case 'ul':
|
|
855
|
+
case 'ol':
|
|
856
|
+
rtf += listRtf(el, ctx);
|
|
857
|
+
break;
|
|
858
|
+
case 'li': {
|
|
859
|
+
// An item with no list around it (a pasted fragment) shows with a bullet.
|
|
860
|
+
const left = ctx.indent + LIST_STEP;
|
|
861
|
+
const start = `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(left)}`;
|
|
862
|
+
rtf += flowRtf(Array.from(el.childNodes), { ...ctx, indent: left }, `${start}\\fi-${LIST_HANG}\\bullet\\tab `, `${start} `);
|
|
863
|
+
break;
|
|
864
|
+
}
|
|
865
|
+
case 'strong':
|
|
866
|
+
case 'b':
|
|
867
|
+
rtf += ctx.bold ? walkChildren(el, ctx) : `\\b ${walkChildren(el, { ...ctx, bold: true })}\\b0 `;
|
|
868
|
+
break;
|
|
869
|
+
case 'em':
|
|
870
|
+
case 'i':
|
|
871
|
+
rtf += ctx.italic ? walkChildren(el, ctx) : `\\i ${walkChildren(el, { ...ctx, italic: true })}\\i0 `;
|
|
872
|
+
break;
|
|
873
|
+
case 'u':
|
|
874
|
+
rtf += ctx.underline ? walkChildren(el, ctx) : `\\ul ${walkChildren(el, { ...ctx, underline: true })}\\ulnone `;
|
|
875
|
+
break;
|
|
876
|
+
case 's':
|
|
877
|
+
case 'strike':
|
|
878
|
+
case 'del':
|
|
879
|
+
rtf += ctx.strike ? walkChildren(el, ctx) : `\\strike ${walkChildren(el, { ...ctx, strike: true })}\\strike0 `;
|
|
880
|
+
break;
|
|
881
|
+
case 'sup':
|
|
882
|
+
case 'sub': {
|
|
883
|
+
const script = tag === 'sup' ? 'super' : 'sub';
|
|
884
|
+
if (ctx.script === script) {
|
|
885
|
+
rtf += walkChildren(el, ctx);
|
|
770
886
|
break;
|
|
771
887
|
}
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
});
|
|
780
|
-
break;
|
|
888
|
+
const restore = ctx.script ? `\\${ctx.script} ` : '\\nosupersub ';
|
|
889
|
+
rtf += `\\${script} ${walkChildren(el, { ...ctx, script })}${restore}`;
|
|
890
|
+
break;
|
|
891
|
+
}
|
|
892
|
+
case 'code':
|
|
893
|
+
if (!ctx.inPre && !ctx.mono) {
|
|
894
|
+
rtf += `\\f1 ${walkChildren(el, { ...ctx, mono: true })}\\f0 `;
|
|
781
895
|
}
|
|
782
|
-
|
|
896
|
+
else {
|
|
783
897
|
rtf += walkChildren(el, ctx);
|
|
784
|
-
break;
|
|
785
|
-
case 'strong':
|
|
786
|
-
case 'b':
|
|
787
|
-
rtf += ctx.bold ? walkChildren(el, ctx) : `\\b ${walkChildren(el, { ...ctx, bold: true })}\\b0 `;
|
|
788
|
-
break;
|
|
789
|
-
case 'em':
|
|
790
|
-
case 'i':
|
|
791
|
-
rtf += ctx.italic ? walkChildren(el, ctx) : `\\i ${walkChildren(el, { ...ctx, italic: true })}\\i0 `;
|
|
792
|
-
break;
|
|
793
|
-
case 'u':
|
|
794
|
-
rtf += ctx.underline ? walkChildren(el, ctx) : `\\ul ${walkChildren(el, { ...ctx, underline: true })}\\ulnone `;
|
|
795
|
-
break;
|
|
796
|
-
case 's':
|
|
797
|
-
case 'strike':
|
|
798
|
-
case 'del':
|
|
799
|
-
rtf += ctx.strike ? walkChildren(el, ctx) : `\\strike ${walkChildren(el, { ...ctx, strike: true })}\\strike0 `;
|
|
800
|
-
break;
|
|
801
|
-
case 'sup':
|
|
802
|
-
case 'sub': {
|
|
803
|
-
const script = tag === 'sup' ? 'super' : 'sub';
|
|
804
|
-
if (ctx.script === script) {
|
|
805
|
-
rtf += walkChildren(el, ctx);
|
|
806
|
-
break;
|
|
807
|
-
}
|
|
808
|
-
const restore = ctx.script ? `\\${ctx.script} ` : '\\nosupersub ';
|
|
809
|
-
rtf += `\\${script} ${walkChildren(el, { ...ctx, script })}${restore}`;
|
|
810
|
-
break;
|
|
811
898
|
}
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
// A link is written only for an absolute web, mail or phone address; anything
|
|
822
|
-
// else (a relative or UNC path, javascript:, data:, file:) as its text alone.
|
|
823
|
-
const target = linkTarget(el.getAttribute('href') || '');
|
|
824
|
-
if (target) {
|
|
825
|
-
const inner = walkChildren(el, { ...ctx, underline: true, color: 1 });
|
|
826
|
-
const underlineOff = ctx.underline ? '' : '\\ulnone';
|
|
827
|
-
rtf += `{\\field{\\*\\fldinst HYPERLINK "${escapeRtf(target)}"}{\\fldrslt\\ul\\cf1 ${inner}\\cf${ctx.color}${underlineOff} }}`;
|
|
828
|
-
}
|
|
829
|
-
else {
|
|
830
|
-
rtf += walkChildren(el, ctx);
|
|
831
|
-
}
|
|
832
|
-
break;
|
|
899
|
+
break;
|
|
900
|
+
case 'a': {
|
|
901
|
+
// A link is written only for an absolute web, mail or phone address; anything
|
|
902
|
+
// else (a relative or UNC path, javascript:, data:, file:) as its text alone.
|
|
903
|
+
const target = linkTarget(el.getAttribute('href') || '');
|
|
904
|
+
if (target) {
|
|
905
|
+
const inner = walkChildren(el, { ...ctx, underline: true, color: 1 });
|
|
906
|
+
const underlineOff = ctx.underline ? '' : '\\ulnone';
|
|
907
|
+
rtf += `{\\field{\\*\\fldinst HYPERLINK "${escapeRtf(target)}"}{\\fldrslt\\ul\\cf1 ${inner}\\cf${ctx.color}${underlineOff} }}`;
|
|
833
908
|
}
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
// picture, tagged with an ignorable {\*\inkcap} destination so it can be
|
|
837
|
-
// re-attached to the figure on import. Other RTF readers skip the tag and
|
|
838
|
-
// simply show an italic line under the image.
|
|
839
|
-
case 'figure': {
|
|
840
|
-
const img = el.querySelector('img');
|
|
841
|
-
const caption = (el.querySelector('figcaption')?.textContent || '').trim();
|
|
842
|
-
const align = `${alignControl(el)}${liControl(ctx.indent + leftMarginTwips(el))}`;
|
|
843
|
-
if (img)
|
|
844
|
-
rtf += `\\pard${inCell(ctx)}${align} ${imageBody(img)}\\par `;
|
|
845
|
-
else
|
|
846
|
-
rtf += walkChildren(el, ctx);
|
|
847
|
-
if (caption) {
|
|
848
|
-
rtf += `{\\*\\inkcap}\\pard${inCell(ctx)}${align}\\i ${escapeRtf(caption)}${ctx.italic ? '' : '\\i0'}\\par `;
|
|
849
|
-
}
|
|
850
|
-
break;
|
|
909
|
+
else {
|
|
910
|
+
rtf += walkChildren(el, ctx);
|
|
851
911
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
912
|
+
break;
|
|
913
|
+
}
|
|
914
|
+
// <figure><img><figcaption>caption</figcaption></figure> — the shape the
|
|
915
|
+
// editor inserts. The caption is written as its own paragraph below the
|
|
916
|
+
// picture, tagged with an ignorable {\*\inkcap} destination so it can be
|
|
917
|
+
// re-attached to the figure on import. Other RTF readers skip the tag and
|
|
918
|
+
// simply show an italic line under the image.
|
|
919
|
+
case 'figure': {
|
|
920
|
+
const img = el.querySelector('img');
|
|
921
|
+
// A figure with a picture takes the caption the editor gives it, wherever it sits. One
|
|
922
|
+
// with no picture takes only its own (a child): a caption further in belongs to what
|
|
923
|
+
// holds it (a figure inside this one), and is written there.
|
|
924
|
+
const captionEl = img ? el.querySelector('figcaption') : Array.from(el.children).find((c) => c.tagName === 'FIGCAPTION');
|
|
925
|
+
const caption = (captionEl?.textContent || '').trim();
|
|
926
|
+
const align = `${ownAlign(el) ?? (cellAlign(ctx) || '\\ql')}${liControl(ctx.indent + leftMarginTwips(el))}`;
|
|
927
|
+
if (img) {
|
|
928
|
+
rtf += `\\pard${inCell(ctx)}${align} ${imageBody(img)}\\par `;
|
|
858
929
|
}
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
const size = sizeOf(el);
|
|
865
|
-
const colorPfx = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
866
|
-
const sizePfx = size != null ? `\\fs${size} ` : '';
|
|
867
|
-
const sizeSfx = size != null ? `\\fs${ctx.size} ` : '';
|
|
868
|
-
const colorSfx = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
869
|
-
const inner = walkChildren(el, { ...ctx, color: colorIdx ?? ctx.color, size: size ?? ctx.size });
|
|
870
|
-
rtf += `${colorPfx}${sizePfx}${inner}${sizeSfx}${colorSfx}`;
|
|
871
|
-
break;
|
|
930
|
+
else {
|
|
931
|
+
// A figure with no picture is a block of what it holds: its lines end with \par, as a
|
|
932
|
+
// <div>'s do, or its text ran into the next paragraph. Its caption is written below.
|
|
933
|
+
const held = Array.from(el.childNodes).filter((n) => n !== captionEl);
|
|
934
|
+
rtf += flowRtf(held, ctx, null, `\\pard${inCell(ctx)}${align} `);
|
|
872
935
|
}
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
const colorIdx = colorIndexOf(el, ctx);
|
|
876
|
-
const colorPfx = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
877
|
-
const colorSfx = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
878
|
-
rtf += `${colorPfx}${walkChildren(el, { ...ctx, color: colorIdx ?? ctx.color })}${colorSfx}`;
|
|
879
|
-
break;
|
|
936
|
+
if (caption) {
|
|
937
|
+
rtf += `{\\*\\inkcap}\\pard${inCell(ctx)}${align}\\i ${escapeRtf(caption)}${ctx.italic ? '' : '\\i0'}\\par `;
|
|
880
938
|
}
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case 'img': {
|
|
942
|
+
// A picture directly under the editor root has no paragraph of its
|
|
943
|
+
// own; anywhere else it is inline content of the enclosing block.
|
|
944
|
+
const standalone = el.parentNode === ctx.root;
|
|
945
|
+
rtf += standalone ? `\\pard ${imageBody(el)}\\par ` : imageBody(el);
|
|
946
|
+
break;
|
|
947
|
+
}
|
|
948
|
+
case 'br':
|
|
949
|
+
// The <br> a browser leaves at the end of a paragraph shows as nothing; written
|
|
950
|
+
// as \line it added an empty line.
|
|
951
|
+
if (ctx.inPre || !isTrailingBreak(el, ctx))
|
|
952
|
+
rtf += '\\line ';
|
|
953
|
+
break;
|
|
954
|
+
case 'span': {
|
|
955
|
+
const colorIdx = colorIndexOf(el, ctx);
|
|
956
|
+
const size = sizeOf(el);
|
|
957
|
+
const colorPfx = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
958
|
+
const sizePfx = size != null ? `\\fs${size} ` : '';
|
|
959
|
+
const sizeSfx = size != null ? `\\fs${ctx.size} ` : '';
|
|
960
|
+
const colorSfx = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
961
|
+
const inner = walkChildren(el, { ...ctx, color: colorIdx ?? ctx.color, size: size ?? ctx.size });
|
|
962
|
+
rtf += `${colorPfx}${sizePfx}${inner}${sizeSfx}${colorSfx}`;
|
|
963
|
+
break;
|
|
884
964
|
}
|
|
965
|
+
// <font color="..."> is produced by execCommand('foreColor') in contenteditable
|
|
966
|
+
case 'font': {
|
|
967
|
+
const colorIdx = colorIndexOf(el, ctx);
|
|
968
|
+
const colorPfx = colorIdx != null ? `\\cf${colorIdx} ` : '';
|
|
969
|
+
const colorSfx = colorIdx != null ? `\\cf${ctx.color} ` : '';
|
|
970
|
+
rtf += `${colorPfx}${walkChildren(el, { ...ctx, color: colorIdx ?? ctx.color })}${colorSfx}`;
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
default:
|
|
974
|
+
rtf += walkChildren(el, ctx);
|
|
975
|
+
break;
|
|
976
|
+
}
|
|
977
|
+
return rtf;
|
|
978
|
+
}
|
|
979
|
+
// ── Lists ──
|
|
980
|
+
/** Each level of a list is indented half an inch further (\li720). */
|
|
981
|
+
const LIST_STEP = 720;
|
|
982
|
+
/** A list item's first line hangs a quarter inch left of its text, where the marker sits (\fi-360). */
|
|
983
|
+
const LIST_HANG = 360;
|
|
984
|
+
/**
|
|
985
|
+
* A <ul> or <ol>: one hanging paragraph per item, opened by a bullet or the item's number and a
|
|
986
|
+
* tab. A list inside an item, or straight inside the list (<ul><ul>, as some editors nest), is
|
|
987
|
+
* indented one level further. Numbers start at the list's `start`.
|
|
988
|
+
*/
|
|
989
|
+
function listRtf(list, ctx) {
|
|
990
|
+
const ordered = list.tagName.toLowerCase() === 'ol';
|
|
991
|
+
// A list read back from RTF carries the indent beyond its level's own as a margin, which
|
|
992
|
+
// is negative for a list nested by less than half an inch.
|
|
993
|
+
const margin = lengthTwips(list.style?.marginLeft, true) ?? 0;
|
|
994
|
+
const left = Math.max(LIST_HANG, ctx.indent + margin + LIST_STEP);
|
|
995
|
+
const start = parseInt(list.getAttribute('start') ?? '', 10);
|
|
996
|
+
let counter = ordered && Number.isFinite(start) ? start : 1;
|
|
997
|
+
const itemCtx = { ...ctx, indent: left };
|
|
998
|
+
const paragraph = `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(left)}`;
|
|
999
|
+
let rtf = '';
|
|
1000
|
+
/** Text or inline elements written straight into the list, between its items. */
|
|
1001
|
+
let loose = [];
|
|
1002
|
+
const flushLoose = () => {
|
|
1003
|
+
if (loose.length > 0)
|
|
1004
|
+
rtf += flowRtf(loose, itemCtx, null, `${paragraph} `);
|
|
1005
|
+
loose = [];
|
|
1006
|
+
};
|
|
1007
|
+
for (const child of Array.from(list.childNodes)) {
|
|
1008
|
+
const tag = child.nodeType === Node.ELEMENT_NODE ? child.tagName.toLowerCase() : '';
|
|
1009
|
+
if (tag === 'li') {
|
|
1010
|
+
flushLoose();
|
|
1011
|
+
const marker = ordered ? ` ${counter}.\\tab ` : '\\bullet\\tab ';
|
|
1012
|
+
rtf += flowRtf(Array.from(child.childNodes), itemCtx, `${paragraph}\\fi-${LIST_HANG}${marker}`, `${paragraph} `);
|
|
1013
|
+
counter++;
|
|
1014
|
+
}
|
|
1015
|
+
else if (tag === 'ul' || tag === 'ol') {
|
|
1016
|
+
flushLoose();
|
|
1017
|
+
rtf += listRtf(child, itemCtx);
|
|
1018
|
+
}
|
|
1019
|
+
else if (child.nodeType === Node.TEXT_NODE && COLLAPSIBLE_WHITESPACE.test(child.textContent || '')) {
|
|
1020
|
+
// The line breaks of the source between items.
|
|
1021
|
+
}
|
|
1022
|
+
else {
|
|
1023
|
+
loose.push(child);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
flushLoose();
|
|
1027
|
+
return rtf;
|
|
1028
|
+
}
|
|
1029
|
+
/**
|
|
1030
|
+
* The content of a list item, or of any place where text and blocks mix, as paragraphs. Text
|
|
1031
|
+
* and inline elements in a row are one paragraph; the first opens with `first` (the item's
|
|
1032
|
+
* marker) and any later one with `plain` (the same indent, no marker). A <p> inside the item,
|
|
1033
|
+
* which is how Word and Google Docs paste a list, is the item's text, not a block after it. A
|
|
1034
|
+
* list or other block inside the item is written as itself, at the item's indent.
|
|
1035
|
+
*/
|
|
1036
|
+
function flowRtf(nodes, ctx, first, plain) {
|
|
1037
|
+
let rtf = '';
|
|
1038
|
+
let marker = first;
|
|
1039
|
+
let open = false;
|
|
1040
|
+
const start = () => {
|
|
1041
|
+
if (open)
|
|
1042
|
+
return;
|
|
1043
|
+
rtf += marker ?? plain;
|
|
1044
|
+
marker = null;
|
|
1045
|
+
open = true;
|
|
1046
|
+
};
|
|
1047
|
+
const end = () => {
|
|
1048
|
+
if (open)
|
|
1049
|
+
rtf += '\\par ';
|
|
1050
|
+
open = false;
|
|
1051
|
+
};
|
|
1052
|
+
for (const node of nodes) {
|
|
1053
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
1054
|
+
if (COLLAPSIBLE_WHITESPACE.test(node.textContent || '') && !showsTabs(node) && besideBlock(node))
|
|
1055
|
+
continue;
|
|
1056
|
+
start();
|
|
1057
|
+
rtf += walkNode(node, ctx, false);
|
|
1058
|
+
continue;
|
|
1059
|
+
}
|
|
1060
|
+
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
1061
|
+
continue;
|
|
1062
|
+
const el = node;
|
|
1063
|
+
const tag = el.tagName.toLowerCase();
|
|
1064
|
+
// A caption outside a figure is text where it stands, as everywhere else.
|
|
1065
|
+
if (!endsItsLine(el)) {
|
|
1066
|
+
start();
|
|
1067
|
+
rtf += walkNode(el, ctx, false);
|
|
1068
|
+
// A wrapper that ends with a block (<b><p>a</p></b>) has ended the line itself.
|
|
1069
|
+
if (endsWithBlock(el))
|
|
1070
|
+
open = false;
|
|
1071
|
+
continue;
|
|
1072
|
+
}
|
|
1073
|
+
end();
|
|
1074
|
+
if ((tag === 'p' || tag === 'div') && !Array.from(el.children).some(isBlock)) {
|
|
1075
|
+
start();
|
|
1076
|
+
const colorIdx = colorIndexOf(el, ctx);
|
|
1077
|
+
rtf += colorIdx != null
|
|
1078
|
+
? `\\cf${colorIdx} ${walkChildren(el, { ...ctx, color: colorIdx })}\\cf${ctx.color} `
|
|
1079
|
+
: walkChildren(el, ctx);
|
|
1080
|
+
end();
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
// An item that begins with a block keeps its marker on a line of its own, except
|
|
1084
|
+
// before a list of its own, where the first marker shown is that list's.
|
|
1085
|
+
if (marker && tag !== 'ul' && tag !== 'ol')
|
|
1086
|
+
rtf += `${marker}\\par `;
|
|
1087
|
+
marker = null;
|
|
1088
|
+
rtf += walkNode(el, ctx, true, false);
|
|
1089
|
+
}
|
|
1090
|
+
end();
|
|
1091
|
+
// An empty item still shows its marker.
|
|
1092
|
+
if (marker)
|
|
1093
|
+
rtf += `${marker}\\par `;
|
|
1094
|
+
return rtf;
|
|
1095
|
+
}
|
|
1096
|
+
// ── Tables ──
|
|
1097
|
+
/** The width a table is written at, in twips: 6 in, the text width of a Letter page with 1.25 in margins. */
|
|
1098
|
+
const PAGE_WIDTH = 8640;
|
|
1099
|
+
/**
|
|
1100
|
+
* A <table> as RTF rows. RTF has no table element: each row declares where its cells end
|
|
1101
|
+
* (\cellx), so the columns only line up if every row is written against the same column edges.
|
|
1102
|
+
* They are worked out once for the table (tableModel, columnShares), and then:
|
|
1103
|
+
*
|
|
1104
|
+
* - a cell across several columns (colspan) is one cell that ends at the right edge of the last
|
|
1105
|
+
* column it covers;
|
|
1106
|
+
* - a cell down several rows (rowspan) is \clvmgf where it starts and an empty \clvmrg cell in
|
|
1107
|
+
* the same columns of each row below, which is how RTF says "merged with the cell above";
|
|
1108
|
+
* - a row with fewer cells than the table has columns is filled with empty cells.
|
|
1109
|
+
*
|
|
1110
|
+
* A <th> gets a bottom border, which is how the parser knows a header cell, and a cell's own
|
|
1111
|
+
* text-align is its paragraphs' alignment.
|
|
1112
|
+
*/
|
|
1113
|
+
function tableRtf(table, ctx) {
|
|
1114
|
+
const model = tableModel(table);
|
|
1115
|
+
if (model.columns === 0)
|
|
1116
|
+
return '';
|
|
1117
|
+
let cumulative = 0;
|
|
1118
|
+
const edges = columnShares(model).map((share) => {
|
|
1119
|
+
cumulative += share;
|
|
1120
|
+
return Math.round(cumulative * PAGE_WIDTH);
|
|
1121
|
+
});
|
|
1122
|
+
edges[edges.length - 1] = PAGE_WIDTH; // snap last edge to avoid rounding drift
|
|
1123
|
+
let rtf = '';
|
|
1124
|
+
const started = new Set();
|
|
1125
|
+
for (let r = 0; r < model.rows.length; r++) {
|
|
1126
|
+
const row = model.rows[r];
|
|
1127
|
+
let definition = '';
|
|
1128
|
+
let bodies = '';
|
|
1129
|
+
for (let c = 0; c < model.columns;) {
|
|
1130
|
+
const cell = row[c];
|
|
1131
|
+
// The run of columns this cell covers in this row. A slot no cell covers is an
|
|
1132
|
+
// empty cell of one column.
|
|
1133
|
+
let last = c;
|
|
1134
|
+
while (cell !== null && last + 1 < model.columns && row[last + 1] === cell)
|
|
1135
|
+
last++;
|
|
1136
|
+
const origin = cell !== null && cell.row === r && !started.has(cell);
|
|
1137
|
+
if (cell && origin)
|
|
1138
|
+
started.add(cell);
|
|
1139
|
+
const merged = cell !== null && cell.rowspan > 1;
|
|
1140
|
+
if (merged)
|
|
1141
|
+
definition += origin ? '\\clvmgf' : '\\clvmrg';
|
|
1142
|
+
if (cell?.el.tagName.toLowerCase() === 'th')
|
|
1143
|
+
definition += '\\clbrdrb\\brdrs';
|
|
1144
|
+
definition += `\\cellx${edges[last]}`;
|
|
1145
|
+
const align = cell && origin ? alignOf(cell.el) : '';
|
|
1146
|
+
const content = cell && origin
|
|
1147
|
+
? withoutTrailingPar(walkChildren(cell.el, { ...ctx, inTableCell: true, cellAlign: align }))
|
|
1148
|
+
: '';
|
|
1149
|
+
bodies += `{\\pard\\intbl${align} \\f0 \\sa0 \\li0 \\fi0 ${content}\\par}\\cell`;
|
|
1150
|
+
c = last + 1;
|
|
1151
|
+
}
|
|
1152
|
+
rtf += `{\\trowd\\trgaph120${definition}\\trkeep\\intbl{${bodies}}\\intbl\\row}`;
|
|
1153
|
+
}
|
|
1154
|
+
return rtf;
|
|
1155
|
+
}
|
|
1156
|
+
/**
|
|
1157
|
+
* A table inside a table cell. RTF can nest tables (\itap, \nestcell), but many readers show a
|
|
1158
|
+
* nested table wrongly or not at all, and opening a row inside a cell, as the writer did, is not
|
|
1159
|
+
* valid RTF. So the inner table is written as lines of text in the cell it is in: each of its
|
|
1160
|
+
* rows a paragraph, its cells a tab apart. Every word is kept; the inner grid is not.
|
|
1161
|
+
*/
|
|
1162
|
+
function nestedTableRtf(table, ctx) {
|
|
1163
|
+
let rtf = followsInlineContent(table, ctx.root) ? '\\par ' : '';
|
|
1164
|
+
for (const row of tableRowsInOrder(table)) {
|
|
1165
|
+
const cells = rowCells(row);
|
|
1166
|
+
if (cells.length === 0)
|
|
1167
|
+
continue;
|
|
1168
|
+
const line = cells.map((cell) => withoutTrailingPar(walkChildren(cell, ctx))).join('\\tab ');
|
|
1169
|
+
rtf += `\\pard\\intbl${cellAlign(ctx)} ${line}\\par `;
|
|
885
1170
|
}
|
|
886
1171
|
return rtf;
|
|
887
1172
|
}
|
|
@@ -907,27 +1192,86 @@ function besideBlock(node) {
|
|
|
907
1192
|
const next = node.nextSibling;
|
|
908
1193
|
return isBlock(prev) || isBlock(next) || (!prev && !next && isBlock(node.parentNode));
|
|
909
1194
|
}
|
|
1195
|
+
/**
|
|
1196
|
+
* Whether a <br> is one a browser shows nothing for: the last thing in its block, where an editor
|
|
1197
|
+
* leaves one so the line has height, or right before a block, which starts a new line anyway.
|
|
1198
|
+
*/
|
|
1199
|
+
function isTrailingBreak(br, ctx) {
|
|
1200
|
+
let node = br;
|
|
1201
|
+
for (;;) {
|
|
1202
|
+
let next = node.nextSibling;
|
|
1203
|
+
while (next &&
|
|
1204
|
+
(next.nodeType === Node.COMMENT_NODE ||
|
|
1205
|
+
(next.nodeType === Node.TEXT_NODE && COLLAPSIBLE_WHITESPACE.test(next.textContent || '') && !showsTabs(next)))) {
|
|
1206
|
+
next = next.nextSibling;
|
|
1207
|
+
}
|
|
1208
|
+
if (next)
|
|
1209
|
+
return isBlock(next);
|
|
1210
|
+
const parent = node.parentNode;
|
|
1211
|
+
if (!parent || parent === ctx.root || isBlock(parent))
|
|
1212
|
+
return true;
|
|
1213
|
+
node = parent;
|
|
1214
|
+
}
|
|
1215
|
+
}
|
|
1216
|
+
/** Blocks other than <p> and <div> that end the loose text before them (walkNode). */
|
|
1217
|
+
const BREAKS_LOOSE_TEXT = new Set(['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'table', 'blockquote', 'pre', 'hr', 'ul', 'ol', 'li', 'figure']);
|
|
910
1218
|
/** A <p> or <div> with no content, or only a <br>: a blank line. */
|
|
911
1219
|
function isBlankLine(el) {
|
|
912
1220
|
return el.childNodes.length === 0 || (el.childNodes.length === 1 && el.childNodes[0].nodeName === 'BR');
|
|
913
1221
|
}
|
|
1222
|
+
/** Collapsible whitespace or a comment: nothing that shows. */
|
|
1223
|
+
function showsNothing(node) {
|
|
1224
|
+
return node.nodeType === Node.COMMENT_NODE || (node.nodeType === Node.TEXT_NODE && COLLAPSIBLE_WHITESPACE.test(node.textContent || ''));
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* Whether a block ends its own line in the RTF. Every block does, except a <figcaption> outside a
|
|
1228
|
+
* figure, whose text is written where it stands.
|
|
1229
|
+
*/
|
|
1230
|
+
function endsItsLine(node) {
|
|
1231
|
+
return isBlock(node) && node.nodeName !== 'FIGCAPTION';
|
|
1232
|
+
}
|
|
1233
|
+
/** Whether an inline element's content ends with a block, which has ended its own line. */
|
|
1234
|
+
function endsWithBlock(el) {
|
|
1235
|
+
let last = el.lastChild;
|
|
1236
|
+
while (last && showsNothing(last))
|
|
1237
|
+
last = last.previousSibling;
|
|
1238
|
+
if (!last || last.nodeType !== Node.ELEMENT_NODE)
|
|
1239
|
+
return false;
|
|
1240
|
+
return endsItsLine(last) || endsWithBlock(last);
|
|
1241
|
+
}
|
|
914
1242
|
/**
|
|
915
|
-
* Whether the nearest sibling before this element, skipping collapsible whitespace,
|
|
916
|
-
* text or an inline element: content with no paragraph of its own.
|
|
1243
|
+
* Whether the nearest sibling before this element, skipping collapsible whitespace and comments,
|
|
1244
|
+
* is text or an inline element: content with no paragraph of its own. Not when that content has
|
|
1245
|
+
* already ended its line: a picture straight under `root` writes its own paragraph, and a wrapper
|
|
1246
|
+
* whose last child is a block (Google Docs wraps a paste in <b id="docs-internal-guid-…">) ends
|
|
1247
|
+
* with that block's.
|
|
917
1248
|
*/
|
|
918
|
-
function followsInlineContent(el) {
|
|
1249
|
+
function followsInlineContent(el, root = null) {
|
|
919
1250
|
let prev = el.previousSibling;
|
|
920
|
-
while (prev &&
|
|
1251
|
+
while (prev && showsNothing(prev))
|
|
921
1252
|
prev = prev.previousSibling;
|
|
922
|
-
}
|
|
923
1253
|
if (!prev)
|
|
924
1254
|
return false;
|
|
925
1255
|
if (prev.nodeType === Node.TEXT_NODE)
|
|
926
1256
|
return true;
|
|
927
|
-
|
|
1257
|
+
if (prev.nodeType !== Node.ELEMENT_NODE || endsItsLine(prev))
|
|
1258
|
+
return false;
|
|
1259
|
+
if (prev.nodeName === 'IMG' && root !== null && prev.parentNode === root)
|
|
1260
|
+
return false;
|
|
1261
|
+
return !endsWithBlock(prev);
|
|
1262
|
+
}
|
|
1263
|
+
/** Whether the nearest sibling after this element, skipping collapsible whitespace, is text or an inline element. */
|
|
1264
|
+
function followedByInline(el) {
|
|
1265
|
+
let next = el.nextSibling;
|
|
1266
|
+
while (next && next.nodeType === Node.TEXT_NODE && COLLAPSIBLE_WHITESPACE.test(next.textContent || '')) {
|
|
1267
|
+
next = next.nextSibling;
|
|
1268
|
+
}
|
|
1269
|
+
if (!next)
|
|
1270
|
+
return false;
|
|
1271
|
+
return next.nodeType === Node.TEXT_NODE || (next.nodeType === Node.ELEMENT_NODE && !isBlock(next));
|
|
928
1272
|
}
|
|
929
1273
|
/** Blocks whose last paragraph always carries an indent or alignment of its own. */
|
|
930
|
-
const FORMATTED_BLOCK_TAGS = new Set(['ul', 'ol', 'blockquote', 'figure', 'hr']);
|
|
1274
|
+
const FORMATTED_BLOCK_TAGS = new Set(['ul', 'ol', 'li', 'blockquote', 'figure', 'hr']);
|
|
931
1275
|
/**
|
|
932
1276
|
* Loose text or an inline element right after a block has no paragraph of its own, and would
|
|
933
1277
|
* keep that block's alignment and indent (a centered line's \qc, a list's \li). Start a plain
|
|
@@ -945,9 +1289,9 @@ function restateAfterBlock(node, ctx) {
|
|
|
945
1289
|
const tag = block.tagName.toLowerCase();
|
|
946
1290
|
const formatted = FORMATTED_BLOCK_TAGS.has(tag) ||
|
|
947
1291
|
(['p', 'div'].includes(tag) && (ctx.inTableCell ? paragraphLayout(block, ctx) : paragraphFormat(block, ctx)) !== '') ||
|
|
948
|
-
(
|
|
1292
|
+
(HEADING_TAGS.has(tag) && headingLayout(block, ctx) !== '') ||
|
|
949
1293
|
(tag === 'pre' && ctx.indent + leftMarginTwips(block) > 0);
|
|
950
|
-
return formatted ? `\\pard${inCell(ctx)}${liControl(ctx.indent)} ` : '';
|
|
1294
|
+
return formatted ? `\\pard${inCell(ctx)}${cellAlign(ctx)}${liControl(ctx.indent)} ` : '';
|
|
951
1295
|
}
|
|
952
1296
|
/**
|
|
953
1297
|
* The cell's own \par ends its last paragraph, so that paragraph's mark is dropped. Only a
|
|
@@ -995,17 +1339,25 @@ function isIndentBlockquote(el) {
|
|
|
995
1339
|
function liControl(twips) {
|
|
996
1340
|
return twips > 0 ? `\\li${twips}` : '';
|
|
997
1341
|
}
|
|
998
|
-
/**
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1342
|
+
/**
|
|
1343
|
+
* \qc, \qr or \qj; left is the default and is not written. Inside a table cell that is aligned
|
|
1344
|
+
* itself, a block with no alignment of its own takes the cell's, and one set left says so.
|
|
1345
|
+
*/
|
|
1346
|
+
function alignOf(el, ctx) {
|
|
1347
|
+
const own = ownAlign(el);
|
|
1348
|
+
const cell = ctx ? cellAlign(ctx) : '';
|
|
1349
|
+
if (own == null)
|
|
1350
|
+
return cell;
|
|
1351
|
+
if (own === '\\ql')
|
|
1352
|
+
return cell ? own : '';
|
|
1353
|
+
return own;
|
|
1002
1354
|
}
|
|
1003
1355
|
/**
|
|
1004
1356
|
* A heading's, or a paragraph's inside a table cell: its alignment, and \liN for the indent of
|
|
1005
1357
|
* the blocks around it plus its own margin.
|
|
1006
1358
|
*/
|
|
1007
1359
|
function paragraphLayout(el, ctx) {
|
|
1008
|
-
return `${alignOf(el)}${liControl(ctx.indent + leftMarginTwips(el))}`;
|
|
1360
|
+
return `${alignOf(el, ctx)}${liControl(ctx.indent + leftMarginTwips(el))}`;
|
|
1009
1361
|
}
|
|
1010
1362
|
/**
|
|
1011
1363
|
* A heading's layout: its alignment and left indent, and outside a table cell its right and
|
|
@@ -1097,6 +1449,15 @@ function edgeIsInline(el, edge) {
|
|
|
1097
1449
|
function inCell(ctx) {
|
|
1098
1450
|
return ctx.inTableCell ? '\\intbl' : '';
|
|
1099
1451
|
}
|
|
1452
|
+
/** The alignment of the table cell the walker is in (\qc, \qr or \qj), which its paragraphs share. */
|
|
1453
|
+
function cellAlign(ctx) {
|
|
1454
|
+
return ctx.inTableCell ? ctx.cellAlign : '';
|
|
1455
|
+
}
|
|
1456
|
+
/** Whether a rule stands for a page break: <hr style="break-after:page">, as the parser writes \page. */
|
|
1457
|
+
function isPageBreak(el) {
|
|
1458
|
+
const value = (prop) => el.style?.getPropertyValue(prop).trim();
|
|
1459
|
+
return value('break-after') === 'page' || value('page-break-after') === 'always';
|
|
1460
|
+
}
|
|
1100
1461
|
/** Color-table index of the element's own color, or null when it sets none. */
|
|
1101
1462
|
function colorIndexOf(el, ctx) {
|
|
1102
1463
|
// Support both inline style.color (spans) and the color attribute (<font>)
|