Skip to content

Commit 70509b5

Browse files
Merge pull request #697 from Jugen/Fix-styles-not-applying-to-paragraph-breaks
Fix for #696
2 parents 89c2e6a + 5a710cb commit 70509b5

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

richtextfx/src/main/java/org/fxmisc/richtext/model/Paragraph.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public Paragraph<PS, SEG, S> restyle(int from, int to, S style) {
318318

319319
public Paragraph<PS, SEG, S> restyle(int from, StyleSpans<? extends S> styleSpans) {
320320
int len = styleSpans.length();
321-
if(styleSpans.equals(getStyleSpans(from, from + len))) {
321+
if(styleSpans.equals(getStyleSpans(from, from + len)) || styleSpans.getSpanCount() == 0) {
322322
return this;
323323
}
324324
if(length() == 0) {

richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import static org.junit.Assert.*;
44

5+
import java.util.Collection;
6+
import java.util.Collections;
7+
58
import org.junit.Test;
69

710
public class ParagraphTest {
@@ -37,4 +40,26 @@ public void restylingEmptyParagraphViaStyleSpansWorks() {
3740
assertTrue(restyledP.getStyleSpans().styleStream().allMatch( b -> b ));
3841
}
3942

43+
// Relates to #696 (caused by #685, coming from #449) where an empty
44+
// StyleSpans being applied to an empty paragraph results in an Exception
45+
@Test
46+
public void restylingEmptyParagraphViaEmptyStyleSpansWorks() {
47+
48+
Collection<String> test = Collections.singleton("test");
49+
TextOps<String, Collection<String>> segOps = SegmentOps.styledTextOps();
50+
Paragraph<Void, String, Collection<String>> p = new Paragraph<>(null, segOps, "", test);
51+
assertEquals( 0, p.length() );
52+
53+
StyleSpans<Collection<String>> spans = new StyleSpans<Collection<String>>()
54+
{
55+
@Override public Position position( int major, int minor ) { return null; }
56+
@Override public Position offsetToPosition( int offset, Bias bias ) { return null; }
57+
@Override public StyleSpan<Collection<String>> getStyleSpan( int index ) { return null; }
58+
@Override public int getSpanCount() { return 0; }
59+
@Override public int length() { return 0; }
60+
};
61+
62+
Paragraph<Void, String, Collection<String>> restyledP = p.restyle(0, spans);
63+
assertEquals( test, restyledP.getStyleSpans().getStyleSpan( 0 ).getStyle() );
64+
}
4065
}

0 commit comments

Comments
 (0)