Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sectionParser obliterating text content in certain situations #685

Merged
merged 1 commit into from
Jun 3, 2019
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
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');
});