Skip to content

Commit bb66021

Browse files
author
Andy Goryachev
committed
8366202: RichTextArea: wrong style used for typed text
Reviewed-by: lkostyra, zelmidaoui
1 parent 5684edf commit bb66021

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

modules/jfx.incubator.richtext/src/main/java/jfx/incubator/scene/control/richtext/model/RichTextModel.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,20 @@ public int getTextLength() {
301301
}
302302

303303
/**
304-
* Retrieves the style attributes from the previous character (or next, if at the beginning).
304+
* Retrieves the style attributes at the specified offset.
305305
* @param offset the offset
306306
* @return the style info
307307
*/
308308
public StyleAttributeMap getStyleAttributeMap(int offset) {
309-
int off = 0;
309+
int pos = 0;
310310
int ct = size();
311311
for (int i = 0; i < ct; i++) {
312312
RSegment seg = get(i);
313313
int len = seg.getTextLength();
314-
if (offset < (off + len) || (i == ct - 1)) {
314+
pos += len;
315+
if ((offset <= pos) || (i + 1 == ct)) {
315316
return seg.getStyleAttributeMap();
316317
}
317-
off += len;
318318
}
319319
return StyleAttributeMap.EMPTY;
320320
}

modules/jfx.incubator.richtext/src/test/java/test/jfx/incubator/scene/control/richtext/RichTextAreaTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
public class RichTextAreaTest {
8080
private RichTextArea control;
8181
private static final StyleAttributeMap BOLD = StyleAttributeMap.builder().setBold(true).build();
82+
private static final StyleAttributeMap ITALIC = StyleAttributeMap.builder().setItalic(true).build();
8283

8384
@BeforeEach
8485
public void beforeEach() {
@@ -474,6 +475,31 @@ public void hasNonEmptySelection() {
474475
assertTrue(control.hasNonEmptySelection());
475476
}
476477

478+
@Test
479+
public void insertBetweenSegments() {
480+
TextPos p = control.appendText("a", BOLD);
481+
control.appendText("b", ITALIC);
482+
control.appendText("c", BOLD);
483+
control.select(p);
484+
control.insertTab();
485+
control.insertTab();
486+
assertEquals("a\t\tbc", text());
487+
control.select(TextPos.ofLeading(0, 2));
488+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
489+
control.select(TextPos.ZERO);
490+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
491+
control.select(TextPos.ofLeading(0, 1));
492+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
493+
control.select(TextPos.ofLeading(0, 2));
494+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
495+
control.select(TextPos.ofLeading(0, 3));
496+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
497+
control.select(TextPos.ofLeading(0, 4));
498+
assertEquals(BOLD, control.getActiveStyleAttributeMap());
499+
control.select(TextPos.ofLeading(0, 5));
500+
assertEquals(ITALIC, control.getActiveStyleAttributeMap());
501+
}
502+
477503
@Test
478504
public void insertLineBreak() {
479505
control.appendText("123");

0 commit comments

Comments
 (0)