function clearInlineFormat()

in packages/roosterjs-editor-api/lib/format/clearFormat.ts [165:227]


function clearInlineFormat(editor: IEditor) {
    editor.focus();
    editor.addUndoSnapshot(() => {
        execCommand(editor, DocumentCommand.RemoveFormat);
        editor.queryElements('[class]', QueryScope.OnSelection, node =>
            node.removeAttribute('class')
        );

        const defaultFormat = editor.getDefaultFormat();
        const isDefaultFormatEmpty = Object.keys(defaultFormat).length === 0;
        editor.queryElements('[style]', QueryScope.InSelection, node => {
            STYLES_TO_REMOVE.forEach(style => node.style.removeProperty(style));

            // when default format is empty, keep the HTML minimum by removing style attribute if there's no style
            // (note: because default format is empty, we're not adding style back in)
            if (isDefaultFormatEmpty && node.getAttribute('style') === '') {
                node.removeAttribute('style');
            }
        });

        if (!isDefaultFormatEmpty) {
            if (defaultFormat.fontFamily) {
                setFontName(editor, defaultFormat.fontFamily);
            }
            if (defaultFormat.fontSize) {
                setFontSize(editor, defaultFormat.fontSize);
            }
            if (defaultFormat.textColor) {
                const setColorIgnoredElements = editor.queryElements<HTMLElement>(
                    'a *, a',
                    QueryScope.OnSelection
                );

                let shouldApplyInlineStyle =
                    setColorIgnoredElements.length > 0
                        ? (element: HTMLElement) => setColorIgnoredElements.indexOf(element) == -1
                        : null;

                if (defaultFormat.textColors) {
                    setTextColor(editor, defaultFormat.textColors, shouldApplyInlineStyle);
                } else {
                    setTextColor(editor, defaultFormat.textColor, shouldApplyInlineStyle);
                }
            }
            if (defaultFormat.backgroundColor) {
                if (defaultFormat.backgroundColors) {
                    setBackgroundColor(editor, defaultFormat.backgroundColors);
                } else {
                    setBackgroundColor(editor, defaultFormat.backgroundColor);
                }
            }
            if (defaultFormat.bold) {
                toggleBold(editor);
            }
            if (defaultFormat.italic) {
                toggleItalic(editor);
            }
            if (defaultFormat.underline) {
                toggleUnderline(editor);
            }
        }
    }, ChangeSource.Format);
}