Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,18 @@ export function decodeRPrFromMarks(marks) {
}

marks.forEach((mark) => {
const type = mark.type.name ?? mark.type;
if (!mark) return;
const type = mark.type?.name ?? mark.type;
if (!type) return;
const attrs = mark.attrs || {};
switch (type) {
case 'strike':
case 'italic':
case 'bold':
runProperties[type] = mark.attrs.value !== '0' && mark.attrs.value !== false;
runProperties[type] = attrs.value !== '0' && attrs.value !== false;
break;
case 'underline': {
const { underlineType, underlineColor, underlineThemeColor, underlineThemeTint, underlineThemeShade } =
mark.attrs;
const { underlineType, underlineColor, underlineThemeColor, underlineThemeTint, underlineThemeShade } = attrs;
const underlineAttrs = {};
if (underlineType) {
underlineAttrs['w:val'] = underlineType;
Expand All @@ -549,20 +551,20 @@ export function decodeRPrFromMarks(marks) {
break;
}
case 'highlight':
if (mark.attrs.color) {
if (mark.attrs.color.toLowerCase() === 'transparent') {
if (attrs.color) {
if (attrs.color.toLowerCase() === 'transparent') {
runProperties.highlight = { 'w:val': 'none' };
} else {
runProperties.highlight = { 'w:val': mark.attrs.color };
runProperties.highlight = { 'w:val': attrs.color };
}
}
break;
case 'link':
runProperties.styleId = 'Hyperlink';
break;
case 'textStyle':
Object.keys(mark.attrs).forEach((attr) => {
const value = mark.attrs[attr];
Object.keys(attrs).forEach((attr) => {
const value = attrs[attr];
switch (attr) {
case 'textTransform':
if (value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,24 @@ describe('decodeRPrFromMarks', () => {
expect(rPr).toEqual({ color: { val: 'FF0000' }, fontSize: 24 });
});

it('should handle sparse mark snapshots with missing attrs', () => {
const marks = [{ type: 'bold' }, { type: 'italic' }, { type: 'textStyle' }];
const rPr = decodeRPrFromMarks(marks);
expect(rPr).toEqual({ bold: true, italic: true });
});

it('should skip null or undefined marks without throwing', () => {
const marks = [null, { type: 'bold', attrs: { value: true } }, undefined];
const rPr = decodeRPrFromMarks(marks);
expect(rPr).toEqual({ bold: true });
});

it('should skip marks with missing or falsy type', () => {
const marks = [{ type: null }, { attrs: { value: true } }, { type: 'bold', attrs: { value: true } }];
const rPr = decodeRPrFromMarks(marks);
expect(rPr).toEqual({ bold: true });
});

it('should decode underline marks', () => {
const marks = [{ type: 'underline', attrs: { underlineType: 'single', underlineColor: '#FF0000' } }];
const rPr = decodeRPrFromMarks(marks);
Expand Down
Loading