All files / roosterjs-content-model-dom/lib/domToModel/utils stackFormat.ts

97.62% Statements 41/42
95.83% Branches 23/24
100% Functions 6/6
97.62% Lines 41/42

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 1751x                                                         1x     1x                   1x                     12285x 12285x   2457x 2457x 2457x 2457x 2457x 2457x   2457x   2457x 2457x 2457x 2457x 2457x               2457x   37x               23x             2397x               2457x   5x                   2452x               2457x   137x         2320x               4914x   76x     496x     4342x   4342x 2179x               45x       4342x 3196x       3196x 43x 43x       4342x      
import { getObjectKeys } from '../../domUtils/getObjectKeys';
import type {
    ContentModelBlockFormat,
    ContentModelCode,
    ContentModelFormatBase,
    ContentModelLink,
    ContentModelParagraphDecorator,
    ContentModelSegmentFormat,
    DomToModelContext,
} from 'roosterjs-content-model-types';
 
/**
 * @internal
 */
export interface StackFormatOptions {
    segment?: 'shallowClone' | 'shallowCloneForBlock' | 'empty';
    paragraph?: 'shallowClone' | 'shallowCloneForGroup' | 'empty';
    blockDecorator?: 'empty';
    link?: 'linkDefault' | 'cloneFormat' | 'empty';
    code?: 'codeDefault' | 'empty';
}
 
// Some styles, such as background color, won't be inherited by block element if it was originally
// declared from an inline element. So we need to skip them.
// e.g.
// <span style="background-color: red">
//   line 1       <---------------------------- in red here
//   <div>line 2</div>  <---------------------- not in red here
// </span>
const SkippedStylesForBlockOnSegmentOnSegment: (keyof ContentModelSegmentFormat)[] = [
    'backgroundColor',
];
const SkippedStylesForTable: (keyof ContentModelBlockFormat)[] = [
    'marginLeft',
    'marginRight',
    'paddingLeft',
    'paddingRight',
];
 
/**
 * @internal
 */
export function stackFormat(
    context: DomToModelContext,
    options: StackFormatOptions,
    callback: () => void
) {
    const {
        segmentFormat,
        blockFormat,
        link: linkFormat,
        code: codeFormat,
        blockDecorator: decoratorFormat,
    } = context;
    const { segment, paragraph, link, code, blockDecorator } = options;
 
    try {
        context.segmentFormat = stackFormatInternal(segmentFormat, segment);
        context.blockFormat = stackFormatInternal(blockFormat, paragraph);
        context.link = stackLinkInternal(linkFormat, link);
        context.code = stackCodeInternal(codeFormat, code);
        context.blockDecorator = stackDecoratorInternal(decoratorFormat, blockDecorator);
 
        callback();
    } finally {
        context.segmentFormat = segmentFormat;
        context.blockFormat = blockFormat;
        context.link = linkFormat;
        context.code = codeFormat;
        context.blockDecorator = decoratorFormat;
    }
}
 
function stackLinkInternal(
    linkFormat: ContentModelLink,
    link?: 'linkDefault' | 'cloneFormat' | 'empty'
) {
    switch (link) {
        case 'linkDefault':
            return {
                format: {
                    underline: true,
                },
                dataset: {},
            };
 
        case 'empty':
            return {
                format: {},
                dataset: {},
            };
 
        case 'cloneFormat':
        default:
            return {
                dataset: linkFormat.dataset,
                format: { ...linkFormat.format },
            };
    }
}
 
function stackCodeInternal(codeFormat: ContentModelCode, code?: 'codeDefault' | 'empty') {
    switch (code) {
        case 'codeDefault':
            return {
                format: {
                    fontFamily: 'monospace',
                },
            };
        case 'empty':
            return {
                format: {},
            };
        default:
            return codeFormat;
    }
}
 
function stackDecoratorInternal(
    format: ContentModelParagraphDecorator,
    decorator?: 'decoratorDefault' | 'empty'
) {
    switch (decorator) {
        case 'empty':
            return {
                format: {},
                tagName: '',
            };
        default:
            return format;
    }
}
 
function stackFormatInternal<T extends ContentModelFormatBase>(
    format: T,
    processType?: 'shallowClone' | 'shallowCloneForBlock' | 'shallowCloneForGroup' | 'empty'
): T | {} {
    switch (processType) {
        case 'empty':
            return {};
 
        case undefined:
            return format;
 
        default:
            const result = { ...format };
 
            getObjectKeys(format).forEach(key => {
                if (
                    (processType == 'shallowCloneForBlock' &&
                        SkippedStylesForBlockOnSegmentOnSegment.indexOf(
                            key as keyof ContentModelSegmentFormat
                        ) >= 0) ||
                    (processType == 'shallowCloneForGroup' &&
                        SkippedStylesForTable.indexOf(key as keyof ContentModelBlockFormat) >= 0)
                ) {
                    delete result[key];
                }
            });
 
            if (processType == 'shallowClone' || processType == 'shallowCloneForGroup') {
                const blockFormat = format as ContentModelBlockFormat;
 
                // For a new paragraph, if current text indent is already applied to previous block in the same level,
                // we need to ignore it according to browser rendering behavior
                if (blockFormat.textIndent) {
                    delete (result as ContentModelBlockFormat).isTextIndentApplied;
                    blockFormat.isTextIndentApplied = true;
                }
            }
 
            return result;
    }
}