Skip to content

Commit

Permalink
trim leading/trailing whitespace from sections that can occur when pa…
Browse files Browse the repository at this point in the history
…rsing indented HTML
  • Loading branch information
kevinansfield committed Mar 13, 2019
1 parent 002f0d6 commit 333c293
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/js/parsers/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export function transformHTMLText(textContent) {
return text;
}

export function trimSectionText(section) {
if (section.isMarkerable && section.markers.length) {
let { head, tail } = section.markers;
head.value = head.value.replace(/^\s+/, '');
tail.value = tail.value.replace(/\s+$/, '');
}
}

function isGoogleDocsContainer(element) {
return !isTextNode(element) &&
!isCommentNode(element) &&
Expand Down Expand Up @@ -108,6 +116,10 @@ class DOMParser {
this.appendSections(post, sections);
});

// trim leading/trailing whitespace of markerable sections to avoid
// unnessary whitespace from indented HTML input
forEach(post.sections, section => trimSectionText(section));

return post;
}

Expand Down
4 changes: 3 additions & 1 deletion src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import {
} from 'mobiledoc-kit/utils/array-utils';

import {
transformHTMLText
transformHTMLText,
trimSectionText
} from '../parsers/dom';

import assert from '../utils/assert';
Expand Down Expand Up @@ -283,6 +284,7 @@ class SectionParser {

// push listItems onto the listSection or add a new section
if (state.section.isListItem && lastSection && lastSection.isListSection) {
trimSectionText(state.section);
lastSection.items.append(state.section);
} else {
// remove empty list sections before creating a new section
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/parsers/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,24 @@ test('singly-nested ol lis are parsed correctly', (assert) => {
assert.equal(section.items.objectAt(1).text, 'second element');
});

test('nested html doesn\'t create unneccessary whitespace', (assert) => {
let element = buildDOM(`
<div>
<p>
One
<p>
<p>
Two
</p>
</div>
`);
const post = parser.parse(element);

assert.equal(post.sections.length, 2, '2 sections');
assert.equal(post.sections.objectAt(0).text, 'One');
assert.equal(post.sections.objectAt(1).text, 'Two');
});

/*
* FIXME: Google docs nests uls like this
test('lis in nested uls are flattened (when ul is child of ul)', (assert) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/parsers/section-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ test('#parse handles insignificant whitespace', (assert) => {
let [list] = sections;
assert.equal(list.type, 'list-section');
assert.equal(list.items.length, 1, '1 list item');
assert.equal(list.items.objectAt(0).text, ' One ');
assert.equal(list.items.objectAt(0).text, 'One');
});

test('#parse avoids empty paragraph around wrapped list', (assert) => {
Expand Down

0 comments on commit 333c293

Please sign in to comment.