@formepdf/shared 0.24.0 → 0.26.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/dist/parser.d.ts +7 -0
- package/dist/parser.js +57 -7
- package/package.json +1 -1
package/dist/parser.d.ts
CHANGED
|
@@ -53,5 +53,12 @@ interface DocumentProps {
|
|
|
53
53
|
type Exact<A, B> = (<T>() => T extends A ? 1 : 2) extends (<T>() => T extends B ? 1 : 2) ? true : false;
|
|
54
54
|
type AssertTrue<T extends true> = T;
|
|
55
55
|
export type _DocumentClaimParity = AssertTrue<Exact<Pick<DocumentProps, keyof FormeDocumentClaimProps>, FormeDocumentClaimProps>>;
|
|
56
|
+
/**
|
|
57
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
58
|
+
* call and attached to the document under this symbol, where
|
|
59
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
60
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
61
|
+
*/
|
|
62
|
+
export declare const SERIALIZER_WARNINGS: unique symbol;
|
|
56
63
|
export declare function parseMarkup(markup: string): FormeDocument;
|
|
57
64
|
export {};
|
package/dist/parser.js
CHANGED
|
@@ -35,7 +35,30 @@ function isElement(node) {
|
|
|
35
35
|
function isText(node) {
|
|
36
36
|
return node.nodeName === '#text';
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Warnings the serializer raises (a dropped list child) collected during a
|
|
40
|
+
* call and attached to the document under this symbol, where
|
|
41
|
+
* `@formepdf/core` picks them up into the render's `warnings`. A symbol
|
|
42
|
+
* key never reaches `JSON.stringify`, so the engine input is unchanged.
|
|
43
|
+
*/
|
|
44
|
+
export const SERIALIZER_WARNINGS = Symbol.for('formepdf.serializerWarnings');
|
|
45
|
+
let collectedWarnings = null;
|
|
38
46
|
export function parseMarkup(markup) {
|
|
47
|
+
const outer = collectedWarnings;
|
|
48
|
+
const warnings = [];
|
|
49
|
+
collectedWarnings = warnings;
|
|
50
|
+
try {
|
|
51
|
+
const doc = parseMarkupDocument(markup);
|
|
52
|
+
if (warnings.length > 0) {
|
|
53
|
+
doc[SERIALIZER_WARNINGS] = warnings;
|
|
54
|
+
}
|
|
55
|
+
return doc;
|
|
56
|
+
}
|
|
57
|
+
finally {
|
|
58
|
+
collectedWarnings = outer;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function parseMarkupDocument(markup) {
|
|
39
62
|
const fragment = parseFragment(markup);
|
|
40
63
|
const root = findDocumentRoot(fragment.childNodes);
|
|
41
64
|
const props = decodeProps(root, 'Document');
|
|
@@ -443,13 +466,23 @@ function parseList(element, ordered) {
|
|
|
443
466
|
? mapListMarker(props.type, 'decimal')
|
|
444
467
|
: mapListMarker(props.marker, 'disc');
|
|
445
468
|
const start = typeof props.start === 'number' && props.start >= 1 ? props.start : 1;
|
|
446
|
-
// Children must be ListItems
|
|
447
|
-
//
|
|
448
|
-
//
|
|
449
|
-
//
|
|
450
|
-
const
|
|
451
|
-
|
|
452
|
-
|
|
469
|
+
// Children must be ListItems. Loose whitespace and `{#if}` anchors
|
|
470
|
+
// (comments) between items are dropped quietly as markup noise; any other
|
|
471
|
+
// element or text is dropped with a warning, matching the react adapter
|
|
472
|
+
// (issue #161), since the engine only lays out ListItems here.
|
|
473
|
+
const listName = ordered ? 'OrderedList' : 'UnorderedList';
|
|
474
|
+
const children = [];
|
|
475
|
+
for (const c of element.childNodes) {
|
|
476
|
+
if (isElement(c) && c.tagName === 'forme-list-item') {
|
|
477
|
+
children.push(parseListItem(c));
|
|
478
|
+
}
|
|
479
|
+
else if (isElement(c)) {
|
|
480
|
+
warnDroppedListChild(listName, `<${componentNameOf(c.tagName)}>`);
|
|
481
|
+
}
|
|
482
|
+
else if (isText(c) && c.value.trim() !== '') {
|
|
483
|
+
warnDroppedListChild(listName, `text ${JSON.stringify(c.value.trim())}`);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
453
486
|
const node = {
|
|
454
487
|
kind: { type: 'List', ordered, marker_type: markerType, start },
|
|
455
488
|
style: mapStyle(props.style),
|
|
@@ -459,6 +492,23 @@ function parseList(element, ordered) {
|
|
|
459
492
|
node.bookmark = props.bookmark;
|
|
460
493
|
return node;
|
|
461
494
|
}
|
|
495
|
+
/** `forme-view` -> `View`; any other tag is shown as written. */
|
|
496
|
+
function componentNameOf(tagName) {
|
|
497
|
+
if (!tagName.startsWith('forme-'))
|
|
498
|
+
return tagName;
|
|
499
|
+
return tagName
|
|
500
|
+
.slice('forme-'.length)
|
|
501
|
+
.split('-')
|
|
502
|
+
.map(part => part.charAt(0).toUpperCase() + part.slice(1))
|
|
503
|
+
.join('');
|
|
504
|
+
}
|
|
505
|
+
function warnDroppedListChild(listName, what) {
|
|
506
|
+
const message = `[Forme] <${listName}> dropped ${what}: only <ListItem> children are rendered in a list. `
|
|
507
|
+
+ `Wrap the content in <ListItem>.`;
|
|
508
|
+
// The console still gets it: renderDocument returns bytes only.
|
|
509
|
+
console.warn(message);
|
|
510
|
+
collectedWarnings?.push(message);
|
|
511
|
+
}
|
|
462
512
|
function parseListItem(element) {
|
|
463
513
|
const props = decodeProps(element, 'ListItem');
|
|
464
514
|
// ListItem content is laid out by the engine using layout_children,
|