Skip to content

Commit b13bebb

Browse files
committed
Fixed section parser handling of markup sections inside lists
closes #714 - if the section parser hits a list item and it's current state is not a previous list item or a list section then open a new list section using using the LI's parent list type - prevents `ListItem` sections being created outside of a `ListSection`
1 parent 596de01 commit b13bebb

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/js/parsers/section.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ class SectionParser {
152152
let isNestedListSection = isListSection && this.state.section.isListItem;
153153
let lastSection = this.sections[this.sections.length - 1];
154154

155+
// lists can continue after breaking out for a markup section,
156+
// in that situation, start a new list using the same list type
157+
if (isListItem && this.state.section.isMarkupSection) {
158+
this._closeCurrentSection();
159+
this._updateStateFromElement(node.parentElement);
160+
}
161+
155162
// we can hit a list item after parsing a nested list, when that happens
156163
// and the lists are of different types we need to make sure we switch
157164
// the list type back

tests/unit/parsers/section-test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,58 @@ test("#parse handles multiple headers in list item", assert => {
274274
assert.equal(h2.tagName, 'h2');
275275
});
276276

277+
// https://github.com/bustle/mobiledoc-kit/issues/714
278+
test('#parse handles list items following a markup-section breakout', (assert) => {
279+
let container = buildDOM(`
280+
<ul><li>One</li><li><h2>Two</h2></li><li>Three</li><li>Four</li></ul>
281+
`);
282+
283+
let element = container.firstChild;
284+
parser = new SectionParser(builder);
285+
let sections = parser.parse(element);
286+
287+
assert.equal(sections.length, 3, '3 sections');
288+
289+
assert.equal(sections[0].type, 'list-section');
290+
assert.equal(sections[0].items.length, 1);
291+
assert.equal(sections[0].items.objectAt(0).text, 'One');
292+
293+
assert.equal(sections[1].type, 'markup-section');
294+
assert.equal(sections[1].tagName, 'h2');
295+
assert.equal(sections[1].text, 'Two');
296+
297+
assert.equal(sections[2].type, 'list-section');
298+
assert.equal(sections[2].items.length, 2);
299+
assert.equal(sections[2].items.objectAt(0).text, 'Three');
300+
assert.equal(sections[2].items.objectAt(1).text, 'Four');
301+
});
302+
303+
// https://github.com/bustle/mobiledoc-kit/issues/714
304+
test('#parse handles "invalid" non-li elems inside lists', (assert) => {
305+
let container = buildDOM(`
306+
<ul><li>One</li><h2>Two</h2><li>Three</li><li>Four</li></ul>
307+
`);
308+
309+
let element = container.firstChild;
310+
parser = new SectionParser(builder);
311+
let sections = parser.parse(element);
312+
313+
assert.equal(sections.length, 3, '3 sections');
314+
315+
assert.equal(sections[0].type, 'list-section');
316+
assert.equal(sections[0].items.length, 1);
317+
assert.equal(sections[0].items.objectAt(0).text, 'One');
318+
319+
assert.equal(sections[1].type, 'markup-section');
320+
assert.equal(sections[1].tagName, 'h2');
321+
assert.equal(sections[1].text, 'Two');
322+
323+
assert.equal(sections[2].type, 'list-section');
324+
assert.equal(sections[2].items.length, 2);
325+
assert.equal(sections[2].items.objectAt(0).text, 'Three');
326+
assert.equal(sections[2].items.objectAt(1).text, 'Four');
327+
});
328+
277329
// see https://github.com/bustle/mobiledoc-kit/issues/656
278330
test('#parse handles list following node handled by parserPlugin', (assert) => {
279331
let container = buildDOM(`
@@ -536,3 +588,4 @@ test('#parse handles card-creating element after plain text', (assert) => {
536588
assert.equal(sections[1].type, 'card-section');
537589
assert.equal(sections[2].text.trim(), 'After');
538590
});
591+

0 commit comments

Comments
 (0)