@@ -33,7 +33,7 @@ private static ISet<string> InitPhrasingSets()
3333 var sets = new HashSet < string > ( StringComparer . InvariantCultureIgnoreCase ) {
3434 TagNames . A , TagNames . Abbr , TagNames . B , TagNames . Big , TagNames . Cite , TagNames . Code ,
3535 TagNames . Del , TagNames . Dfn , TagNames . Em , TagNames . Font , TagNames . Hr , TagNames . I ,
36- TagNames . Img , TagNames . Ins , TagNames . Kbd , TagNames . Mark , TagNames . NoBr , TagNames . Q ,
36+ TagNames . Ins , TagNames . Kbd , TagNames . Mark , TagNames . NoBr , TagNames . Q ,
3737 TagNames . Rp , TagNames . Rt , TagNames . S , TagNames . Samp , TagNames . Small , TagNames . Span ,
3838 TagNames . Strike , TagNames . Strong , TagNames . Sub , TagNames . Sup , TagNames . Time ,
3939 TagNames . Tt , TagNames . U , TagNames . Var
@@ -65,50 +65,56 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
6565 // If there is a space between two phrasing elements, the user agent should collapse it to a single space character.
6666 if ( context . CollapseWhitespaces )
6767 {
68+ var previousSibling = node . PreviousSibling ;
69+ var nextSibling = node . NextSibling ;
6870 bool startsWithSpace = text [ 0 ] . IsWhiteSpaceCharacter ( ) ,
6971 endsWithSpace = text [ text . Length - 1 ] . IsWhiteSpaceCharacter ( ) ,
7072 preserveBorderSpaces = AllPhrasings . Contains ( node . Parent ! . NodeName ) ,
71- prevIsPhrasing = node . PreviousSibling is not null &&
72- ( AllPhrasings . Contains ( node . PreviousSibling . NodeName ) || node . PreviousSibling ! . NodeType == NodeType . Text ) ,
73- nextIsPhrasing = node . NextSibling is not null &&
74- ( AllPhrasings . Contains ( node . NextSibling . NodeName ) || node . NextSibling ! . NodeType == NodeType . Text ) ;
73+ prevIsPhrasing = previousSibling is not null &&
74+ ( previousSibling . NodeType == NodeType . Text || AllPhrasings . Contains ( previousSibling . NodeName ) ) ,
75+ nextIsPhrasing = nextSibling is not null &&
76+ ( nextSibling . NodeType == NodeType . Text || AllPhrasings . Contains ( nextSibling . NodeName ) ) ;
7577
7678 text = text . CollapseAndStrip ( ) ;
7779
7880 // keep a collapsed single space if it stands between 2 phrasings that respect.
7981 // doesn't ends/starts with a whitespace
8082 if ( text . Length == 0 && prevIsPhrasing && nextIsPhrasing
8183 && ( endsWithSpace || startsWithSpace )
82- && ! ( node . PreviousSibling ! . TextContent . Length == 0
83- || node . NextSibling ! . TextContent . Length == 0
84- || node . PreviousSibling ! . TextContent [ node . PreviousSibling ! . TextContent . Length - 1 ] . IsWhiteSpaceCharacter ( )
85- || node . NextSibling ! . TextContent [ 0 ] . IsWhiteSpaceCharacter ( )
84+ && ! ( previousSibling ! . TextContent . Length == 0
85+ || nextSibling ! . TextContent . Length == 0
86+ || previousSibling ! . TextContent [ previousSibling ! . TextContent . Length - 1 ] . IsWhiteSpaceCharacter ( )
87+ || nextSibling ! . TextContent [ 0 ] . IsWhiteSpaceCharacter ( )
8688 ) )
8789 {
8890 return [ new Run ( new Text ( " " ) { Space = SpaceProcessingModeValues . Preserve } ) ] ;
8991 }
92+
93+ // is this an inter-element whitespace btw 2 phrasings?
94+ var isWhitespace = text . Length == 0 ;
95+
9096 // we strip out all whitespaces and we stand inside a div. Just skip this text content
91- if ( text . Length == 0 && ! preserveBorderSpaces )
97+ if ( isWhitespace && ! ( prevIsPhrasing && nextIsPhrasing ) && ! preserveBorderSpaces )
9298 {
9399 return [ ] ;
94100 }
95101
96102 // if previous element is an image, append a space separator
97- if ( ( startsWithSpace && node . PreviousSibling is IHtmlImageElement )
103+ if ( ( startsWithSpace && previousSibling is IHtmlImageElement )
98104 // if this is a non-empty phrasing element, append a space separator
99- || ( startsWithSpace && prevIsPhrasing
100- && node . PreviousSibling ! . TextContent . Length > 0
101- && ! node . PreviousSibling ! . TextContent [ node . PreviousSibling . TextContent . Length - 1 ] . IsWhiteSpaceCharacter ( ) ) )
105+ || ( ! isWhitespace && startsWithSpace && prevIsPhrasing
106+ && previousSibling ! . TextContent . Length > 0
107+ && ! previousSibling ! . TextContent [ previousSibling . TextContent . Length - 1 ] . IsWhiteSpaceCharacter ( ) ) )
102108 {
103109 text = " " + text ;
104110 }
105111
106- if ( endsWithSpace && (
112+ if ( endsWithSpace && ! isWhitespace && (
107113 // next run is not starting with a linebreak
108- ( nextIsPhrasing && node . NextSibling ! . TextContent . Length > 0 &&
109- ! node . NextSibling ! . TextContent [ 0 ] . IsLineBreak ( ) ) ||
114+ ( nextIsPhrasing && nextSibling ! . TextContent . Length > 0 &&
115+ ! nextSibling ! . TextContent [ 0 ] . IsLineBreak ( ) ) ||
110116 // if there is no more text element or is empty, eat the trailing space
111- ( preserveBorderSpaces && ( node . NextSibling is not null
117+ ( preserveBorderSpaces && ( nextSibling is not null
112118 || node . Parent . NextSibling is not null ) ) ) )
113119 {
114120 text += " " ;
0 commit comments