From 8dee8be3e7f79a973351fcbc92b3973be1c65f82 Mon Sep 17 00:00:00 2001 From: Yves Rijckaert Date: Tue, 10 Dec 2024 14:14:13 +0100 Subject: [PATCH] fix: undefined mappings in csm --- packages/content-source-maps/src/richText.ts | 28 ++++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/packages/content-source-maps/src/richText.ts b/packages/content-source-maps/src/richText.ts index e1885005..55daa0ee 100644 --- a/packages/content-source-maps/src/richText.ts +++ b/packages/content-source-maps/src/richText.ts @@ -15,15 +15,27 @@ export const encodeRichTextValue = ({ hiddenStrings: SourceMapMetadata; }) => { const source = mappings[pointer]; - // remove old pointer to rich text field as we will just be mapping the text nodes - delete mappings[pointer]; - const textNodes = findRichTextNodes(data, pointer); - for (const textNode of textNodes) { - mappings[textNode] = source; - const currentTextNodeValue = get(data, textNode); - const encodedValue = combine(currentTextNodeValue, hiddenStrings); - set(data, textNode, encodedValue); + // Only proceed with mapping if we have a valid source + if (source) { + // We can now safely delete the original pointer as we've preserved the source + delete mappings[pointer]; + + const textNodes = findRichTextNodes(data, pointer); + for (const textNode of textNodes) { + mappings[textNode] = source; + const currentTextNodeValue = get(data, textNode); + const encodedValue = combine(currentTextNodeValue, hiddenStrings); + set(data, textNode, encodedValue); + } + } else { + // If there's no source mapping, just encode the text nodes without creating mappings + const textNodes = findRichTextNodes(data, pointer); + for (const textNode of textNodes) { + const currentTextNodeValue = get(data, textNode); + const encodedValue = combine(currentTextNodeValue, hiddenStrings); + set(data, textNode, encodedValue); + } } };