@@ -64,14 +64,14 @@ public static String GetInnerText(this IElement element)
64
64
65
65
if ( ! hidden . Value )
66
66
{
67
+ var offset = 0 ;
67
68
var sb = StringBuilderPool . Obtain ( ) ;
68
69
var requiredLineBreakCounts = new Dictionary < Int32 , Int32 > ( ) ;
69
70
InnerTextCollection ( element , sb , requiredLineBreakCounts , element . ParentElement ? . ComputeCurrentStyle ( ) ) ;
70
71
71
72
// Remove any runs of consecutive required line break count items at the start or end of results.
72
73
requiredLineBreakCounts . Remove ( 0 ) ;
73
74
requiredLineBreakCounts . Remove ( sb . Length ) ;
74
- var offset = 0 ;
75
75
76
76
// SortedDictionary would be nicer
77
77
foreach ( var keyval in requiredLineBreakCounts . OrderBy ( kv => kv . Key ) )
@@ -193,7 +193,10 @@ private static void ItcInCssBox(ICssStyleDeclaration elementStyle, ICssStyleDecl
193
193
194
194
if ( node is IText textElement )
195
195
{
196
- ProcessText ( textElement . Data , sb , parentStyle ) ;
196
+ var lastLine = node . NextSibling is null ||
197
+ String . IsNullOrEmpty ( node . NextSibling . TextContent ) ||
198
+ node . NextSibling is IHtmlBreakRowElement ;
199
+ ProcessText ( textElement . Data , sb , parentStyle , lastLine ) ;
197
200
}
198
201
else if ( node is IHtmlBreakRowElement )
199
202
{
@@ -225,14 +228,14 @@ private static void ItcInCssBox(ICssStyleDeclaration elementStyle, ICssStyleDecl
225
228
}
226
229
else if ( node is IHtmlParagraphElement )
227
230
{
228
- requiredLineBreakCounts . TryGetValue ( startIndex , out int startIndexCount ) ;
231
+ requiredLineBreakCounts . TryGetValue ( startIndex , out var startIndexCount ) ;
229
232
230
233
if ( startIndexCount < 2 )
231
234
{
232
235
requiredLineBreakCounts [ startIndex ] = 2 ;
233
236
}
234
237
235
- requiredLineBreakCounts . TryGetValue ( sb . Length , out int endIndexCount ) ;
238
+ requiredLineBreakCounts . TryGetValue ( sb . Length , out var endIndexCount ) ;
236
239
237
240
if ( endIndexCount < 2 )
238
241
{
@@ -255,14 +258,14 @@ private static void ItcInCssBox(ICssStyleDeclaration elementStyle, ICssStyleDecl
255
258
256
259
if ( isBlockLevel . Value )
257
260
{
258
- requiredLineBreakCounts . TryGetValue ( startIndex , out int startIndexCount ) ;
261
+ requiredLineBreakCounts . TryGetValue ( startIndex , out var startIndexCount ) ;
259
262
260
263
if ( startIndexCount < 1 )
261
264
{
262
265
requiredLineBreakCounts [ startIndex ] = 1 ;
263
266
}
264
267
265
- requiredLineBreakCounts . TryGetValue ( sb . Length , out int endIndexCount ) ;
268
+ requiredLineBreakCounts . TryGetValue ( sb . Length , out var endIndexCount ) ;
266
269
267
270
if ( endIndexCount < 1 )
268
271
{
@@ -387,7 +390,7 @@ private static Boolean IsBlockLevel(INode node)
387
390
}
388
391
}
389
392
390
- private static void ProcessText ( String text , StringBuilder sb , ICssStyleDeclaration style )
393
+ private static void ProcessText ( String text , StringBuilder sb , ICssStyleDeclaration style , Boolean lastLine )
391
394
{
392
395
var startIndex = sb . Length ;
393
396
var whiteSpace = style ? . GetWhiteSpace ( ) ;
@@ -459,11 +462,13 @@ private static void ProcessText(String text, StringBuilder sb, ICssStyleDeclarat
459
462
sb . Append ( c ) ;
460
463
}
461
464
462
- if ( isWhiteSpace ) // ended with whitespace
465
+ // ended with whitespace
466
+ if ( isWhiteSpace && lastLine )
463
467
{
464
468
for ( var offset = sb . Length - 1 ; offset >= startIndex ; offset -- )
465
469
{
466
470
var c = sb [ offset ] ;
471
+
467
472
if ( ! Char . IsWhiteSpace ( c ) || c == Symbols . NoBreakSpace )
468
473
{
469
474
sb . Remove ( offset + 1 , sb . Length - 1 - offset ) ;
0 commit comments