All files / roosterjs-content-model-dom/lib/formatHandlers/block htmlAlignFormatHandler.ts

100% Statements 13/13
100% Branches 8/8
100% Functions 2/2
100% Lines 13/13

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 381x 1x                     1x         736x 606x   606x   606x 21x 21x 21x         993x   993x 8x        
import { calcAlign, ResultMap } from '../utils/dir';
import { directionFormatHandler } from './directionFormatHandler';
import type {
    DirectionFormat,
    HtmlAlignFormat,
    TextAlignFormat,
} from 'roosterjs-content-model-types';
import type { FormatHandler } from '../FormatHandler';
 
/**
 * @internal
 */
export const htmlAlignFormatHandler: FormatHandler<
    DirectionFormat & HtmlAlignFormat & TextAlignFormat
> = {
    parse: (format, element, context, defaultStyle) => {
        // When there is text-align in CSS style on the same element, we should ignore HTML align
        if (!element.style.textAlign) {
            directionFormatHandler.parse(format, element, context, defaultStyle);
 
            const htmlAlign = element.getAttribute('align');
 
            if (htmlAlign) {
                format.htmlAlign = calcAlign(htmlAlign, format.direction);
                delete format.textAlign;
                delete context.blockFormat.textAlign;
            }
        }
    },
    apply: (format, element) => {
        const dir: 'ltr' | 'rtl' = format.direction == 'rtl' ? 'rtl' : 'ltr';
 
        if (format.htmlAlign) {
            element.setAttribute('align', ResultMap[format.htmlAlign][dir]);
        }
    },
};