Skip to content
Merged
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
@@ -1,4 +1,5 @@
import { createListItem } from '../../modelApi/creators/createListItem';
import { createListLevel } from '../../modelApi/creators/createListLevel';
import { parseFormat } from '../utils/parseFormat';
import { stackFormat } from '../utils/stackFormat';
import type { ElementProcessor } from 'roosterjs-content-model-types';
Expand All @@ -8,8 +9,21 @@ import type { ElementProcessor } from 'roosterjs-content-model-types';
*/
export const listItemProcessor: ElementProcessor<HTMLLIElement> = (group, element, context) => {
const { listFormat } = context;
const originalListParent = listFormat.listParent;
let shouldPopListLevel = false;

try {
listFormat.listParent = listFormat.listParent ?? group;

const listParent = listFormat.listParent;

if (listFormat.levels.length == 0) {
listFormat.levels.push(
createListLevel(listFormat.potentialListType || 'UL', context.blockFormat)
);
shouldPopListLevel = true;
}

if (listFormat.listParent && listFormat.levels.length > 0) {
stackFormat(
context,
{
Expand All @@ -31,7 +45,7 @@ export const listItemProcessor: ElementProcessor<HTMLLIElement> = (group, elemen
context
);

listFormat.listParent!.blocks.push(listItem);
listParent.blocks.push(listItem);

parseFormat(
element,
Expand All @@ -54,14 +68,11 @@ export const listItemProcessor: ElementProcessor<HTMLLIElement> = (group, elemen
}
}
);
} else {
const currentBlocks = listFormat.listParent?.blocks;
const lastItem = currentBlocks?.[currentBlocks?.length - 1];
} finally {
if (shouldPopListLevel) {
listFormat.levels.pop();
}

context.elementProcessors['*'](
lastItem?.blockType == 'BlockGroup' ? lastItem : group,
element,
context
);
listFormat.listParent = originalListParent;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ export const listProcessor: ElementProcessor<HTMLOListElement | HTMLUListElement
paragraph: 'shallowCloneForGroup',
},
() => {
const level: ContentModelListLevel = createListLevel(
element.tagName as 'OL' | 'UL',
context.blockFormat
);
const tagName = element.tagName as 'OL' | 'UL';
const level: ContentModelListLevel = createListLevel(tagName, context.blockFormat);
const { listFormat } = context;

parseFormat(element, context.formatParsers.dataset, level.dataset, context);
Expand All @@ -31,6 +29,7 @@ export const listProcessor: ElementProcessor<HTMLOListElement | HTMLUListElement

const originalListParent = listFormat.listParent;

listFormat.potentialListType = tagName;
listFormat.listParent = listFormat.listParent || group;
listFormat.levels.push(level);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ describe('childProcessor', () => {
levels: [],
listParent: undefined,
threadItemCounts: [1],
potentialListType: 'OL',
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@ describe('listItemProcessor', () => {
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: li,
blockGroupType: 'ListItem',
blocks: [],
format: {},
levels: [
{
listType: 'UL',
format: {},
dataset: {},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
},
],
});
Expand Down Expand Up @@ -381,10 +392,21 @@ describe('listItemProcessor without format handlers', () => {
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: li,
blockGroupType: 'ListItem',
blocks: [],
format: {},
levels: [
{
listType: 'UL',
format: {},
dataset: {},
},
],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
},
],
});
Expand Down
37 changes: 37 additions & 0 deletions packages/roosterjs-content-model-dom/test/endToEndTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3027,4 +3027,41 @@ describe('End to end test for DOM => Model => DOM/TEXT', () => {
'<sup><a href="http://www.bing.com">www.bing.com</a></sup>'
);
});

it('LI without UL followed by other blocks', () => {
runTest(
'<li>test</li><div>other</div>',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
segments: [{ segmentType: 'Text', text: 'test', format: {} }],
format: {},
isImplicit: true,
},
],
levels: [{ listType: 'UL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
{
blockType: 'Paragraph',
segments: [{ segmentType: 'Text', text: 'other', format: {} }],
format: {},
},
],
},
'test\r\nother',
'<ul><li>test</li></ul><div>other</div>'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2043,17 +2043,16 @@ describe('wordOnlineHandler', () => {
// .test
// .test
// .test
it('shuold process html properly, when list items are not in side ul tag', () => {
it('should process html properly, when list items are not inside ul tag', () => {
runTest(
'<div class="ListContainerWrapper"><ul class="BulletListStyle1" role="list"></ul><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li></div>',
'<li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li>',
'<ul><li><p role="presentation">test</p></li><li><p role="presentation">test</p></li><li><p role="presentation">test</p></li></ul>',
{
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2064,12 +2063,17 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'UL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2080,12 +2084,17 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'UL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2096,6 +2105,12 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'UL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
],
Expand Down Expand Up @@ -2345,17 +2360,16 @@ describe('wordOnlineHandler', () => {
// result:
// 1. text
// 2. text
it('shuold process html properly, if list item in a ListContainerWrapper are not inside ol ', () => {
it('should process html properly, if list item in a ListContainerWrapper are not inside ol ', () => {
runTest(
'<div class="ListContainerWrapper"><ol class="NumberListStyle1" role="list"></ol><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1" class="OutlineElement Ltr"><p>test</p></li></div>',
'<li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li><li class="OutlineElement" role="listitem" aria-level="1"><p>test</p></li>',
'<ol start="1"><li><p role="presentation">test</p></li><li><p role="presentation">test</p></li><li><p role="presentation">test</p></li></ol>',
{
Comment thread
JiuqingSong marked this conversation as resolved.
blockGroupType: 'Document',
blocks: [
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2366,12 +2380,17 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'OL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2382,12 +2401,17 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'OL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
{
blockType: 'BlockGroup',
blockGroupType: 'General',
element: jasmine.anything() as any,
blockGroupType: 'ListItem',
blocks: [
{
blockType: 'Paragraph',
Expand All @@ -2398,6 +2422,12 @@ describe('wordOnlineHandler', () => {
decorator: { tagName: 'p', format: {} },
},
],
levels: [{ listType: 'OL', format: {}, dataset: {} }],
formatHolder: {
segmentType: 'SelectionMarker',
isSelected: false,
format: {},
},
format: {},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface DomToModelListFormat {
* Current list type stack
*/
levels: ContentModelListLevel[];

/**
* This is used for handling an abnormal case where list items are not inside a ul or ol tag
* It is not common and against the HTML specification, but we need to handle it for robustness
*/
potentialListType?: 'OL' | 'UL';
}

/**
Expand Down
Loading