Skip to content

Commit

Permalink
Merge pull request #671 from kevinansfield/fix-section-parser-issues
Browse files Browse the repository at this point in the history
Fix section parser issues
  • Loading branch information
kevinansfield committed Feb 13, 2019
2 parents 5578616 + c6ad8cd commit c8c3af8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,19 @@ class SectionParser {
) {
// don't break out of the list for list items that contain a single <p>.
// deals with typical case of <li><p>Text</p></li><li><p>Text</p></li>
if (this.state.section.isListItem && tagName === 'p' && !node.nextSibling) {
if (
this.state.section.isListItem &&
tagName === 'p' &&
!node.nextSibling &&
contains(VALID_LIST_ITEM_TAGNAMES, normalizeTagName(node.parentElement.tagName))
) {
this.parseElementNode(node);
return;
}

// avoid creating empty paragraphs due to wrapper elements around
// section-creating elements
if (this.state.section.isMarkerable && !this.state.text) {
if (this.state.section.isMarkerable && !this.state.text && this.state.section.markers.length === 0) {
this.state.section = null;
} else {
this._closeCurrentSection();
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/parsers/section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,46 @@ test('#parse doesn\'t group consecutive lists of different types', (assert) => {
assert.equal(ol.items.objectAt(0).text, 'Two');
});

test('#parse handles p following list', (assert) => {
let container = buildDOM(`
<div><ol><li>li1</li><li>li2</li><p>para</p></div>
`);

let element = container.firstChild;
parser = new SectionParser(builder);
let sections = parser.parse(element);

assert.equal(sections.length, 2, 'two sections');

let ol = sections[0];
assert.equal(ol.items.length, 2, 'two list items');

let p = sections[1];
assert.equal(p.text, 'para');
});

test('#parse handles link in a heading followed by paragraph', (assert) => {
let container = buildDOM(`
<div><h4><a href="https://example.com">Linked header</a></h4><p>test</p></div>
`);

let element = container.firstChild;
parser = new SectionParser(builder);
let sections = parser.parse(element);

assert.equal(sections.length, 2, '2 sections');
assert.equal(sections[0].text, 'Linked header');

let markers = sections[0].markers.toArray();
assert.equal(markers.length, 1, '1 marker');
let [marker] = markers;
assert.equal(marker.value, 'Linked header');
assert.ok(marker.hasMarkup('a'), 'has A markup');

let markup = marker.markups[0];
assert.equal(markup.getAttribute('href'), 'https://example.com');
});

test('#parse skips STYLE nodes', (assert) => {
let element = buildDOM(`
<style>.rule { font-color: red; }</style>
Expand Down

0 comments on commit c8c3af8

Please sign in to comment.