Skip to content

Commit 3e13d21

Browse files
Merge pull request #718 from JordanMartinez/fixUnconsecutiveStyleIssue
Fix unconsecutive style issue
2 parents c23d566 + 25e6442 commit 3e13d21

3 files changed

Lines changed: 28 additions & 19 deletions

File tree

richtextfx/src/integrationTest/java/org/fxmisc/richtext/SceneGraphTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ protected List<Path> getBorderPaths(int index) {
8282
return getParagraphTextChildren(index, n -> n instanceof BorderPath, n -> (BorderPath) n);
8383
}
8484

85+
protected List<Path> getBackgroundPaths(int index) {
86+
return getParagraphTextChildren(index, n -> n instanceof BackgroundPath, n -> (BackgroundPath) n);
87+
}
88+
8589
private <T> List<T> getParagraphTextChildren(int index, Predicate<Node> instanceOfCheck, Function<Node, T> cast) {
8690
TextFlow tf = getParagraphText(index);
8791

richtextfx/src/integrationTest/java/org/fxmisc/richtext/style/StylingTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,21 @@ public void consecutive_underline_styles_that_are_the_same_are_rendered_with_one
173173

174174
assertEquals(1, getUnderlinePaths(0).size());
175175
}
176+
177+
@Test
178+
public void unconsecutive_background_styles_should_each_be_rendered_with_their_own_shape() {
179+
String style = "-rtfx-background-color: #ccc;";
180+
interact(() -> {
181+
// Text: |aba abba|
182+
// Style: |x x x x|
183+
184+
area.replaceText("aba abba");
185+
area.setStyle(0, 1, style);
186+
area.setStyle(2, 3, style);
187+
area.setStyle(4, 5, style);
188+
area.setStyle(7, 8, style);
189+
});
190+
List<Path> paths = getBackgroundPaths(0);
191+
assertEquals(4, paths.size());
192+
}
176193
}

richtextfx/src/main/java/org/fxmisc/richtext/ParagraphText.java

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.function.Consumer;
1414
import java.util.function.Function;
1515
import java.util.function.Supplier;
16+
import java.util.function.UnaryOperator;
1617

1718
import javafx.beans.property.ObjectProperty;
1819
import javafx.beans.property.SimpleObjectProperty;
@@ -27,7 +28,6 @@
2728
import javafx.geometry.Insets;
2829
import javafx.scene.Node;
2930
import javafx.scene.control.IndexRange;
30-
import javafx.scene.layout.Background;
3131
import javafx.scene.paint.Color;
3232
import javafx.scene.paint.Paint;
3333
import javafx.scene.shape.LineTo;
@@ -158,27 +158,15 @@ public ObjectProperty<Paint> highlightTextFillProperty() {
158158
});
159159

160160
// set up custom css shape helpers
161-
Supplier<Path> createBackgroundShape = () -> {
162-
Path shape = new BackgroundPath();
163-
shape.setManaged(false);
164-
shape.layoutXProperty().bind(leftInset);
165-
shape.layoutYProperty().bind(topInset);
166-
return shape;
167-
};
168-
Supplier<Path> createBorderShape = () -> {
169-
Path shape = new BorderPath();
170-
shape.setManaged(false);
171-
shape.layoutXProperty().bind(leftInset);
172-
shape.layoutYProperty().bind(topInset);
173-
return shape;
174-
};
175-
Supplier<Path> createUnderlineShape = () -> {
176-
Path shape = new UnderlinePath();
161+
UnaryOperator<Path> configurePath = shape -> {
177162
shape.setManaged(false);
178163
shape.layoutXProperty().bind(leftInset);
179164
shape.layoutYProperty().bind(topInset);
180165
return shape;
181166
};
167+
Supplier<Path> createBackgroundShape = () -> configurePath.apply(new BackgroundPath());
168+
Supplier<Path> createBorderShape = () -> configurePath.apply(new BorderPath());
169+
Supplier<Path> createUnderlineShape = () -> configurePath.apply(new UnderlinePath());
182170

183171
Consumer<Collection<Path>> clearUnusedShapes = paths -> getChildren().removeAll(paths);
184172
Consumer<Path> addToBackground = path -> getChildren().add(0, path);
@@ -482,8 +470,8 @@ private void updateSharedShapeRange(T value, int start, int end, BiFunction<T, T
482470
T lastShapeValue = lastShapeValueRange._1;
483471

484472
// calculate smallest possible position which is consecutive to the given start position
485-
final int prevEndNext = lastShapeValueRange.get2().getEnd() + 1;
486-
if (start <= prevEndNext && // Consecutive?
473+
final int prevEndNext = lastShapeValueRange.get2().getEnd();
474+
if (start == prevEndNext && // Consecutive?
487475
equals.apply(lastShapeValue, value)) { // Same style?
488476

489477
IndexRange lastRange = lastShapeValueRange._2;

0 commit comments

Comments
 (0)