From 6da9afb4166d50e3cdd55446634c1d45d5cd02ad Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 28 Mar 2017 14:05:24 -0700 Subject: [PATCH 1/4] Add test: typing a character should move caret after inserted character --- .../model/StyledTextAreaBehaviorTest.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java b/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java index c98a45716..5c5434348 100644 --- a/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java +++ b/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java @@ -357,4 +357,23 @@ public void pressingMouseOnSelectionAndDraggingAndReleasingMovesSelectedTextToTh } + public class KeyboardBehaviorTests extends InlineCssTextAreaAppTest { + + @Test + public void typingALetterMovesTheCaretAfterThatInsertedLetter() { + interact(() -> { + area.moveTo(0); + area.clear(); + }); + + String userInputtedText = "some user-inputted text"; + clickOn(area).write(userInputtedText); + + assertEquals(userInputtedText, area.getText()); + assertEquals(userInputtedText.length(), area.getCaretPosition()); + assertTrue(area.getSelectedText().isEmpty()); + } + + } + } \ No newline at end of file From a478f70e53112edf7e33dad2695e8908f73c92eb Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 28 Mar 2017 14:08:08 -0700 Subject: [PATCH 2/4] Fix TextChange initialization bugs: ignore replacements that do nothing. --- .../richtext/model/GenericEditableStyledDocumentBase.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/model/GenericEditableStyledDocumentBase.java b/richtextfx/src/main/java/org/fxmisc/richtext/model/GenericEditableStyledDocumentBase.java index 7763ea2eb..921b38f64 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/model/GenericEditableStyledDocumentBase.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/model/GenericEditableStyledDocumentBase.java @@ -122,6 +122,10 @@ public Position offsetToPosition(int offset, Bias bias) { @Override public void replace(int start, int end, StyledDocument replacement) { ensureValidRange(start, end); + if (replacement.length() == 0 && start == end) { + // ignore a replacement that doesn't do anything + return; + } doc.replace(start, end, ReadOnlyStyledDocument.from(replacement)).exec(this::update); } From 42738fdca8e0c38d87cfd568c5049d913a8ebb5d Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 28 Mar 2017 14:08:53 -0700 Subject: [PATCH 3/4] Fix regression: Update caret position after replace call --- .../src/main/java/org/fxmisc/richtext/GenericStyledArea.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java index 811632ee7..760884f6f 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java @@ -1300,6 +1300,9 @@ public void replaceText(int start, int end, String text) { @Override public void replace(int start, int end, StyledDocument replacement) { content.replace(start, end, replacement); + + int newCaretPos = start + replacement.length(); + selectRange(newCaretPos, newCaretPos); } @Override From e625f02e474c3fc3ae3c4a716c94b31b97209923 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 28 Mar 2017 14:10:11 -0700 Subject: [PATCH 4/4] Fix test bugs: insure caret is at 0 before test begins --- .../fxmisc/richtext/model/StyledTextAreaBehaviorTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java b/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java index 5c5434348..e6e7b11f0 100644 --- a/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java +++ b/richtextfx/src/test/java/org/fxmisc/richtext/model/StyledTextAreaBehaviorTest.java @@ -84,6 +84,7 @@ public void setup() { interact(() -> { area.setDisable(true); area.replaceText("When Area Is Disabled Test: Some text goes here"); + area.moveTo(0); }); } @@ -136,6 +137,8 @@ public void draggingTheMouseDoesNotSelectText() { @Test public void releasingTheMouseAfterDragDoesNothing() { + assertEquals(0, area.getCaretPosition()); + moveTo(firstLineOfArea()) .press(MouseButton.PRIMARY) .dropBy(20, 0); @@ -155,7 +158,10 @@ public class AndTextIsNotSelected extends InlineCssTextAreaAppTest { @Before public void setup() { - interact(() -> area.replaceText(firstParagraph)); + interact(() -> { + area.replaceText(firstParagraph); + area.moveTo(0); + }); } @Test