@@ -104,12 +104,41 @@ function _followsInlineSpace(node: Node): boolean {
104104function _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 *
0 commit comments