@@ -231,21 +231,26 @@ export const generateFragmentFromHtml = (
231231 }
232232 }
233233 }
234- for ( const childNode of node . childNodes ) {
234+ for ( let index = 0 ; index < node . childNodes . length ; index += 1 ) {
235+ const childNode = node . childNodes [ index ] ;
235236 if ( defaultTreeAdapter . isElementNode ( childNode ) ) {
236237 const child = convertElementToInstance ( childNode ) ;
237238 if ( child ) {
238239 instance . children . push ( child ) ;
239240 }
240241 }
241242 if ( defaultTreeAdapter . isTextNode ( childNode ) ) {
243+ // trim spaces around rich text
242244 if ( spaceRegex . test ( childNode . value ) ) {
243- continue ;
245+ if ( index === 0 || index === node . childNodes . length - 1 ) {
246+ continue ;
247+ }
244248 }
249+ const childValue = childNode . value . replaceAll ( / \s + / g, " " ) ;
245250 let child : Instance [ "children" ] [ number ] = {
246251 type : "text" ,
247252 // collapse spacing characters inside of text to avoid preserved newlines
248- value : childNode . value . replaceAll ( / \s + / g , " " ) ,
253+ value : childValue === " " ? childValue : childValue . trim ( ) ,
249254 } ;
250255 // textarea content is initial value
251256 // and represented with fake value attribute
@@ -271,6 +276,10 @@ export const generateFragmentFromHtml = (
271276 // <article></article>
272277 // </div>
273278 if ( hasNonRichTextContent ) {
279+ // remove spaces between elements outside of rich text
280+ if ( spaceRegex . test ( childNode . value ) ) {
281+ continue ;
282+ }
274283 const span : Instance = {
275284 type : "instance" ,
276285 id : getNewId ( ) ,
0 commit comments