Skip to content

Commit

Permalink
Run parser plugins for top-level unknown elements
Browse files Browse the repository at this point in the history
closes #494
- run parser plugins for the top-level section elements
    - do not continue parsing children elements if a parser marks the top-level element as finished
- update tests to double-check the output for top-level elements is unchanged
  • Loading branch information
kevinansfield committed Jun 18, 2018
1 parent be446f6 commit daf2fe0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,18 @@ class SectionParser {

this._updateStateFromElement(element);

let childNodes = isTextNode(element) ? [element] : element.childNodes;
let finished = this.runPlugins(element);

if (this.state.section.isListSection) {
this.parseListItems(childNodes);
} else {
forEach(childNodes, el => {
this.parseNode(el);
});
if (!finished) {
let childNodes = isTextNode(element) ? [element] : element.childNodes;

if (this.state.section.isListSection) {
this.parseListItems(childNodes);
} else {
forEach(childNodes, el => {
this.parseNode(el);
});
}
}

this._closeCurrentSection();
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/editor/editor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ test('editor parses HTML post using parser plugins', (assert) => {
editor = new Editor({html, parserPlugins: [parserPlugin]});
assert.ok(!!editor.post, 'editor loads post');

assert.deepEqual(seenTagNames, ['TEXTAREA', 'IMG']);
assert.deepEqual(seenTagNames, ['P', 'TEXTAREA', 'IMG']);
});

test('#activeMarkups returns the markups at cursor when range is collapsed', (assert) => {
Expand Down
20 changes: 16 additions & 4 deletions tests/unit/parsers/html-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,37 @@ test('newlines ("\\n") are replaced with space characters', (assert) => {
// see https://github.com/bustlelabs/mobiledoc-kit/issues/494
test('top-level unknown void elements are parsed', (assert) => {
let html = `<video />`;
parseHTML(html, {plugins: [videoParserPlugin]});
let post = parseHTML(html, {plugins: [videoParserPlugin]});
let {post: expected} = Helpers.postAbstract.buildFromText([]);

assert.ok(didParseVideo);
assert.postIsSimilar(post, expected);
});

// see https://github.com/bustlelabs/mobiledoc-kit/issues/494
test('top-level unknown elements are parsed', (assert) => {
let html = `<video>...inner...</video>`;
parseHTML(html, {plugins: [videoParserPlugin]});
let post = parseHTML(html, {plugins: [videoParserPlugin]});
let {post: expected} = Helpers.postAbstract.buildFromText(['...inner...']);

assert.ok(didParseVideo);
assert.postIsSimilar(post, expected);
});

test('nested void unknown elements are parsed', (assert) => {
let html = `<p>...<video />...</p>`;
parseHTML(html, {plugins: [videoParserPlugin]});
let post = parseHTML(html, {plugins: [videoParserPlugin]});
let {post: expected} = Helpers.postAbstract.buildFromText(['......']);

assert.ok(didParseVideo);
assert.postIsSimilar(post, expected);
});

test('nested unknown elements are parsed', (assert) => {
let html = `<p>...<video>inner</video>...</p>`;
parseHTML(html, {plugins: [videoParserPlugin]});
let post = parseHTML(html, {plugins: [videoParserPlugin]});
let {post: expected} = Helpers.postAbstract.buildFromText(['...inner...']);

assert.ok(didParseVideo);
assert.postIsSimilar(post, expected);
});

0 comments on commit daf2fe0

Please sign in to comment.