Skip to content

Commit

Permalink
fix(createRichTextObjectResolver): text node value
Browse files Browse the repository at this point in the history
A text node serialized with parse5 has no child so no value is returned.
With these changes, we check the type and avoid the serialization.

FIXES kontent-ai#4
  • Loading branch information
noreiller committed May 2, 2023
1 parent b13c578 commit 14d39df
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/parser/implementation/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IParserElementAttribute
} from '@kontent-ai/delivery-sdk';
import { parseFragment, serialize } from 'parse5';
import { Element, DocumentFragment, ChildNode, ParentNode } from 'parse5/dist/cjs/tree-adapters/default';
import { Element, DocumentFragment, ChildNode, ParentNode, TextNode } from 'parse5/dist/cjs/tree-adapters/default';
import * as striptags from 'striptags';

export function getChildNodes(documentFragment: DocumentFragment): ChildNode[] {
Expand Down Expand Up @@ -157,7 +157,7 @@ export function convertToParserElement(node: ParentNode): IParserElement {
}
},
html: serialize(node),
text: striptags(serialize(node)),
text: tagName === '#text' ? (node as unknown as TextNode).value : striptags(serialize(node)),
attributes: attributes,
parentElement: (node as Element).parentNode
? convertToParserElement((node as Element).parentNode as ParentNode)
Expand Down
7 changes: 7 additions & 0 deletions test/node/sync/node-rich-text-object-resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ describe('Node rich text object resolver', () => {
it(`Expect correct number of root children nodes`, () => {
assert.ok(resolvedData.data.children.length === expectedRootChildrenNodes);
});

it(`Expect text nodes should be extracted`, () => {
const textChildren = Object.values(resolvedData.data.children).filter(({ tag }) => tag === '#text');
const filledTextChildren = textChildren.filter(({ data: { text } }) => !!text);

assert.ok(textChildren.length === filledTextChildren.length);
});
});

0 comments on commit 14d39df

Please sign in to comment.