Skip to content

Commit

Permalink
Fix sectionParser obliterating plain text content in certain circumst…
Browse files Browse the repository at this point in the history
…ances (#685)

closes #683

- `addSection` which is called by parser plugins was removing the section rather than closing it when it contains text due to a missing conditional
  • Loading branch information
kevinansfield committed Jun 3, 2019
1 parent c73613a commit e5f877f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class SectionParser {
addSection: (section) => {
// avoid creating empty paragraphs due to wrapper elements around
// parser-plugin-handled elements
if (this.state.section.isMarkerable && !this.state.text) {
if (this.state.section.isMarkerable && !this.state.text && !this.state.section.text) {
this.state.section = null;
} else {
this._closeCurrentSection();
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/parsers/section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,28 @@ test('#parse skips nested Comment nodes', (assert) => {
assert.equal(section.text, 'some text', 'parses text surrounded by comments');
assert.equal(section.markers.length, 1, 'only 1 marker');
});

// https://github.com/bustle/mobiledoc-kit/issues/683
test('#parse handles card-creating element after plain text', (assert) => {
let container = buildDOM(`
<div><p>Before<a href="https:/example.com/image.png"><img src="https://example.com/image.png"></a></p><p>After</p></div>
`);

let element = container.firstChild;
let plugins = [function(element, builder, {addSection, nodeFinished}) {
if (element.tagName !== 'IMG') {
return;
}
let payload = {url: element.src};
let cardSection = builder.createCardSection('test-image', payload);
addSection(cardSection);
nodeFinished();
}];
parser = new SectionParser(builder, {plugins});
let sections = parser.parse(element);

assert.equal(sections.length, 3, '3 sections');
assert.equal(sections[0].text.trim(), 'Before');
assert.equal(sections[1].type, 'card-section');
assert.equal(sections[2].text.trim(), 'After');
});

0 comments on commit e5f877f

Please sign in to comment.