Skip to content

Commit

Permalink
Merge pull request #200 from dlsc-software-consulting-gmbh/fix-text-view
Browse files Browse the repository at this point in the history
Fix index out-of-bounds in TextViewSkin
  • Loading branch information
dlemmermann authored Aug 26, 2024
2 parents 327d6d6 + a5f6e5c commit 2e17e5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 1 addition & 8 deletions gemsfx/src/main/java/com/dlsc/gemsfx/TextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.WritableValue;
import javafx.collections.MapChangeListener;
import javafx.css.CssMetaData;
import javafx.css.Styleable;
Expand All @@ -18,16 +17,10 @@
import javafx.scene.control.Control;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Skin;
import javafx.scene.control.SkinBase;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.skin.TextInputControlSkin;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.TextFlow;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -331,7 +324,7 @@ public StyleableProperty<Paint> getStyleableProperty(TextView c) {
private static final List<CssMetaData<? extends Styleable, ?>> STYLEABLES;

static {
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>();
final List<CssMetaData<? extends Styleable, ?>> styleables = new ArrayList<>(Control.getClassCssMetaData());
styleables.add(HIGHLIGHT_FILL);
styleables.add(HIGHLIGHT_STROKE);
styleables.add(HIGHLIGHT_TEXT_FILL);
Expand Down
11 changes: 10 additions & 1 deletion gemsfx/src/main/java/com/dlsc/gemsfx/skins/TextViewSkin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dlsc.gemsfx.skins;

import com.dlsc.gemsfx.TextView;
import javafx.beans.InvalidationListener;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Node;
Expand Down Expand Up @@ -173,6 +172,9 @@ private void initListeners() {
private void selectWord(HitInfo hit) {
int charIndex = hit.getCharIndex();
StringBuilder string = getTextFlowContentAsString();
if (isInvalidIndex(string, charIndex)) {
return;
}

int startIndex = -1;
int endIndex = -1;
Expand Down Expand Up @@ -204,6 +206,9 @@ private void selectWord(HitInfo hit) {
private void selectParagraph(HitInfo hit) {
int charIndex = hit.getCharIndex();
StringBuilder string = getTextFlowContentAsString();
if (isInvalidIndex(string, charIndex)) {
return;
}

int startIndex = -1;
int endIndex = -1;
Expand Down Expand Up @@ -274,5 +279,9 @@ private void removeSelection() {
text.setSelectionEnd(selectionEndPos);
wrappingPath.getElements().clear();
}

private boolean isInvalidIndex(StringBuilder string, int charIndex) {
return string.isEmpty() || charIndex < 0 || charIndex >= string.length();
}
}
}

0 comments on commit 2e17e5b

Please sign in to comment.