diff --git a/packages/roosterjs-editor-api/test/utils/toggleListTypeTest.ts b/packages/roosterjs-editor-api/test/utils/toggleListTypeTest.ts index c89bfefc22a..58bfa7c0661 100644 --- a/packages/roosterjs-editor-api/test/utils/toggleListTypeTest.ts +++ b/packages/roosterjs-editor-api/test/utils/toggleListTypeTest.ts @@ -36,7 +36,7 @@ describe('toggleListTypeTest()', () => { '

' + '
' + '' + - '
' + + '
' + '
' ); }); @@ -62,7 +62,7 @@ describe('toggleListTypeTest()', () => { '

' + '
' + '' + - '
' + + '
' + '' + '
' ); diff --git a/packages/roosterjs-editor-dom/lib/list/VListItem.ts b/packages/roosterjs-editor-dom/lib/list/VListItem.ts index c83475365b9..7a9df3a3cc5 100644 --- a/packages/roosterjs-editor-dom/lib/list/VListItem.ts +++ b/packages/roosterjs-editor-dom/lib/list/VListItem.ts @@ -1,5 +1,6 @@ import contains from '../utils/contains'; import getListTypeFromNode from './getListTypeFromNode'; +import getStyles from '../style/getStyles'; import getTagOfNode from '../utils/getTagOfNode'; import isBlockElement from '../utils/isBlockElement'; import moveChildNodes from '../utils/moveChildNodes'; @@ -7,6 +8,7 @@ import safeInstanceOf from '../utils/safeInstanceOf'; import setBulletListMarkers from './setBulletListMarkers'; import setListItemStyle from './setListItemStyle'; import setNumberingListMarkers from './setNumberingListMarkers'; +import setStyles from '../style/setStyles'; import toArray from '../jsUtils/toArray'; import unwrap from '../utils/unwrap'; import wrap from '../utils/wrap'; @@ -366,11 +368,37 @@ export default class VListItem { // 5. If this is not a list item now, need to unwrap the LI node and do proper handling if (this.listTypes.length <= 1) { - wrapIfNotBlockNode( - getTagOfNode(this.node) == 'LI' ? getChildrenAndUnwrap(this.node) : [this.node], - true /*checkFirst*/, - true /*checkLast*/ - ); + // If original
  • node has styles for font and color, we need to apply it to new parent + const isLi = getTagOfNode(this.node) == 'LI'; + const stylesToApply = isLi + ? { + 'font-family': this.node.style.fontFamily, + 'font-size': this.node.style.fontSize, + color: this.node.style.color, + } + : undefined; + + const childNodes = isLi ? getChildrenAndUnwrap(this.node) : [this.node]; + + if (stylesToApply) { + for (let i = 0; i < childNodes.length; i++) { + if (safeInstanceOf(childNodes[i], 'Text')) { + childNodes[i] = wrap(childNodes[i], 'span'); + } + + const node = childNodes[i]; + + if (safeInstanceOf(node, 'HTMLElement')) { + const styles = { + ...stylesToApply, + ...getStyles(node), + }; + setStyles(node, styles); + } + } + } + + wrapIfNotBlockNode(childNodes, true /*checkFirst*/, true /*checkLast*/); } } diff --git a/packages/roosterjs-editor-dom/lib/list/setListItemStyle.ts b/packages/roosterjs-editor-dom/lib/list/setListItemStyle.ts index 4f23570e2c8..12d1e125ffc 100644 --- a/packages/roosterjs-editor-dom/lib/list/setListItemStyle.ts +++ b/packages/roosterjs-editor-dom/lib/list/setListItemStyle.ts @@ -39,19 +39,25 @@ function getInlineChildElementsStyle(element: HTMLElement, styles: string[]) { while (contentTraverser.currentInlineElement != currentInlineElement) { currentInlineElement = contentTraverser.currentInlineElement; let currentNode = currentInlineElement?.getContainerNode() || null; - const currentStyle: Record = {}; + let currentStyle: Record | null = null; currentNode = currentNode ? findClosestElementAncestor(currentNode) : null; - // we should consider of when it is the single childnode of element, the parentNode's style should add + // we should consider of when it is the single child node of element, the parentNode's style should add // such as the "i", "b", "span" node in
  • aa
  • while ( currentNode && currentNode !== element && - safeInstanceOf(currentNode, 'HTMLElement') + safeInstanceOf(currentNode, 'HTMLElement') && + (result.length == 0 || (currentNode.textContent?.trim().length || 0) > 0) ) { styles.forEach(styleName => { const styleValue = (currentNode as HTMLElement).style.getPropertyValue(styleName); + + if (!currentStyle) { + currentStyle = {}; + } + if (styleValue && !currentStyle[styleName]) { currentStyle[styleName] = styleValue; } diff --git a/packages/roosterjs-editor-dom/test/list/VListTest.ts b/packages/roosterjs-editor-dom/test/list/VListTest.ts index 9615c369746..dd3af36b065 100644 --- a/packages/roosterjs-editor-dom/test/list/VListTest.ts +++ b/packages/roosterjs-editor-dom/test/list/VListTest.ts @@ -426,7 +426,7 @@ describe('VList.writeBack', () => { listTypes: [ListType.Ordered], }, ], - '
    item2
    1. item3
    ' + '
    item2
    1. item3
    ' ); }); @@ -498,7 +498,7 @@ describe('VList.writeBack', () => { listTypes: [ListType.Ordered], }, ], - '
    text
    1. item3
    2. item4
    text
    1. item5
    ', + '
    text
    1. item3
    2. item4
    text
    1. item5
    ', ol ); });