Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
[FIX] Parser: parse <br> consistently with Renderer behaviour
Browse files Browse the repository at this point in the history
Until this commit, the Parser just added a LINE_BREAK in VDocument for
each <br>. But our own Renderer doesn't render a single <br> for each
LINE_BREAK. This commit makes the Parser consistent with that behaviour.
Also includes tests to account for that.
  • Loading branch information
Zynton authored and dmo-odoo committed Nov 26, 2019
1 parent 203d69f commit 499f7ed
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/core/utils/Parser.ts
Expand Up @@ -89,6 +89,19 @@ function parseElementNode(currentContext: ParsingContext): ParsingContext {
const context = { ...currentContext };

const node = context.node;
// A <br/> with no siblings is there only to make its parent visible.
// Consume it since it was just parsed as its parent element node.
// TODO: do this less naively to account for formatting space.
if (node.childNodes.length === 1 && node.childNodes[0].nodeName === 'BR') {
context.node = node.childNodes[0];
}
// A trailing <br/> after another <br/> is there only to make its previous
// sibling visible. Consume it since it was just parsed as a single BR
// within our abstraction.
// TODO: do this less naively to account for formatting space.
if (node.nodeName === 'BR' && node.nextSibling && node.nextSibling.nodeName === 'BR') {
context.node = node.nextSibling;
}
if (Format.tags.includes(context.node.nodeName)) {
// Format nodes (e.g. B, I, U) are parsed differently than regular
// elements since they are not represented by a proper VNode in our
Expand Down
19 changes: 18 additions & 1 deletion test/utils/Parser.test.ts
Expand Up @@ -18,7 +18,24 @@ describe('utils', () => {
expect(p.children[0].type).to.equal(VNodeType.CHAR);
expect(p.children[0].value).to.equal('a');
});

it('should parse a "p" tag with no content', () => {
const element = document.createElement('div');
element.innerHTML = '<p><br></p>';
const vDocument = Parser.parse(element);
const p = vDocument.root.firstChild();
// The placeholder <br> should not be parsed.
expect(p.hasChildren()).to.be.false;
});
it('should parse two trailing consecutive <br> as one LINE_BREAK', () => {
const element = document.createElement('div');
element.innerHTML = '<p>a<br><br>';
const vDocument = Parser.parse(element);
const p = vDocument.root.firstChild();
// Only one <br> should be parsed.
expect(p.children.length).to.equal(2);
expect(p.lastChild().type).to.equal(VNodeType.LINE_BREAK);
expect(p.lastChild().previousSibling().value).to.equal('a');
});
it('handles nested formatted nodes', () => {
const element = document.createElement('div');
element.innerHTML = '<p>a<i>b<b>c</b>d</i></p>';
Expand Down

0 comments on commit 499f7ed

Please sign in to comment.