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

Commit 0bd9488

Browse files
Zyntondmo-odoo
authored andcommitted
[FIX] formattingSpace: remove space between inline elem and block
1 parent a0feb63 commit 0bd9488

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

packages/utils/src/formattingSpace.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,41 @@ function _followsInlineSpace(node: Node): boolean {
104104
function _isAtSegmentBreak(node: Node, side: 'start' | 'end'): boolean {
105105
const siblingSide = side === 'start' ? 'previousSibling' : 'nextSibling';
106106
const sibling = node && node[siblingSide];
107-
const isAgainstAnotherSegment = sibling && _isSegment(sibling);
107+
const isAgainstAnotherSegment = _isAgainstAnotherSegment(node, side);
108108
const isAtEdgeOfOwnSegment = _isBlockEdge(node, side);
109109
// In the DOM, a space before a BR is rendered but a space after a BR isn't.
110110
const isBeforeBR = side === 'end' && sibling && nodeName(sibling) === 'BR';
111111
return (isAgainstAnotherSegment && !isBeforeBR) || isAtEdgeOfOwnSegment;
112112
}
113+
/**
114+
* Return true if the given node is just before or just after another segment.
115+
* Eg: <div>abc<div>def</div></div> -> abc is before another segment (div).
116+
* Eg: <div><a>abc</a> <div>def</div></div> -> abc is before another segment
117+
* (div).
118+
*
119+
* @param {Node} node
120+
* @param {'start'|'end'} side
121+
* @returns {boolean}
122+
*/
123+
function _isAgainstAnotherSegment(node: Node, side: 'start' | 'end'): boolean {
124+
const siblingSide = side === 'start' ? 'previousSibling' : 'nextSibling';
125+
const sibling = node && node[siblingSide];
126+
if (sibling) {
127+
return sibling && _isSegment(sibling);
128+
} else {
129+
// Look further (eg.: `<div><a>abc</a> <div>def</div></div>`: the
130+
// space should be removed).
131+
let ancestor = node;
132+
while (ancestor && !ancestor[siblingSide]) {
133+
ancestor = ancestor.parentNode;
134+
}
135+
let cousin = ancestor && !_isSegment(ancestor) && ancestor.nextSibling;
136+
while (cousin && isInstanceOf(cousin, Text)) {
137+
cousin = cousin.nextSibling;
138+
}
139+
return cousin && _isSegment(cousin);
140+
}
141+
}
113142
/**
114143
* Return true if the node is a segment according to W3 formatting model.
115144
*

packages/utils/test/formattingSpace.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,9 @@ describe('removeFormattingSpace() util function', () => {
322322
),
323323
).to.equal('ef ', 'test #2');
324324
});
325+
it('should remove space between a <a> and a <div>', async () => {
326+
const nodes = htmlToElements('<div><a>abc</a> <div>def</div></div>');
327+
expect(removeFormattingSpace(nodes[0].childNodes[1])).to.equal('');
328+
});
325329
});
326330
});

0 commit comments

Comments
 (0)