Skip to content

Commit

Permalink
Don't add linefeed to Text nodes (#332)
Browse files Browse the repository at this point in the history
* Don't add linefeed to Text nodes

* add test

* fix demo issue

* fix issue getting document with caret position not defined

* add test resources

* fix issue with block pattern

* add javadoc
  • Loading branch information
jperedadnr authored Jun 21, 2024
1 parent 5e96749 commit 9a8d486
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 4 deletions.
6 changes: 6 additions & 0 deletions rta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@
<configuration>
<trimStackTrace>false</trimStackTrace>
<argLine>--add-opens javafx.graphics/com.sun.javafx.application=ALL-UNNAMED</argLine>
<systemProperties>
<property>
<name>java.util.logging.config.file</name>
<value>src/test/resources/logging.properties</value>
</property>
</systemProperties>
</configuration>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private Text buildText(String content, TextDecoration decoration) {
return lfText;
}
Objects.requireNonNull(decoration);
Text text = new Text(Objects.requireNonNull(content));
Text text = new Text(Objects.requireNonNull(content).replace("\n", ""));
String foreground = decoration.getForeground();
text.setFill(COLOR_MAP.computeIfAbsent(foreground, s -> parseColorOrDefault(foreground, Color.BLACK)));
text.setStrikethrough(decoration.isStrikethrough());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@
*/
public class UnitBuffer {

/**
* The block pattern is defined by
* \ufeff, a @ or a # symbol, any text (any combination of letters, numbers, punctuation and spaces), and a final \ufeff
* For example:
* {@code \ufeff@name foo33!\ufeff} has group 0: {@code @}, and group 1: {@code name foo33!}
*/
private static final Pattern BLOCK_PATTERN = Pattern.compile(
TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT + "(@|#)([\\p{L}|\\p{N}|\\s]*)" + TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT,
TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT + "([@#])([\\p{L}\\p{N}\\p{P}\\s]*)" + TextBuffer.ZERO_WIDTH_NO_BREAK_SPACE_TEXT,
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

private final List<Unit> unitList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,8 @@ private void updateProperties() {

private Document getCurrentDocument(Selection selection) {
// text and indices should be based on the exportable text
int caret = getTextBuffer().getText(0, getCaretPosition()).length();
int caretPosition = getCaretPosition() < 0 ? getTextLength() : getCaretPosition();
int caret = getTextBuffer().getText(0, caretPosition).length();
int start = selection.isDefined() ? selection.getStart() : 0;
int end = selection.isDefined() ? selection.getEnd() : getTextLength();
return new Document(getTextBuffer().getText(start, end), getTextBuffer().getDecorationModelList(start, end), caret);
Expand Down
36 changes: 36 additions & 0 deletions rta/src/test/java/com/gluonhq/richtextarea/ui/RTATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -88,6 +89,7 @@
import static javafx.scene.text.FontWeight.BOLD;
import static javafx.scene.text.FontWeight.NORMAL;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -827,6 +829,40 @@ public void copyPaste3Test(FxRobot robot) {
assertEquals(NORMAL, ((TextDecoration) dm4.getDecoration()).getFontWeight());
assertEquals("black", ((TextDecoration) dm4.getDecoration()).getForeground());
}
@Test
public void multiLineDocumentTest(FxRobot robot) {
run(() -> {
String text = "Hello\nRTA";
TextDecoration textDecoration = TextDecoration.builder().presets().fontFamily("Arial").build();
ParagraphDecoration paragraphDecoration = ParagraphDecoration.builder().presets().build();
Document document = new Document(text,
List.of(new DecorationModel(0, 6, textDecoration, paragraphDecoration),
new DecorationModel(6, 3, textDecoration, paragraphDecoration)), text.length());
richTextArea.getActionFactory().open(document).execute(new ActionEvent());
});
waitForFxEvents();

verifyThat(".rich-text-area", node -> node instanceof RichTextArea);
verifyThat(".rich-text-area", NodeMatchers.isFocused());
RichTextArea rta = robot.lookup(".rich-text-area").query();
assertEquals(9, rta.getTextLength());
assertEquals(9, rta.getCaretPosition());
assertNotNull(rta.getDocument());
assertEquals(9, rta.getDocument().getCaretPosition());
assertEquals("Hello\nRTA", rta.getDocument().getText());
String internalText = "Hello\nRTA";
assertEquals(9, internalText.length());
assertEquals(internalText, getInternalText(rta.getDocument(), 9));
assertEquals(2, robot.lookup(".text-flow").queryAll().size());
for (int i = 0; i < 2; i++) {
assertInstanceOf(TextFlow.class, robot.lookup(".text-flow").nth(i).query());
TextFlow tf = robot.lookup(".text-flow").nth(i).query();
assertEquals(1, tf.getChildren().size());
assertInstanceOf(Text.class, tf.getChildren().get(0));
assertFalse(((Text) tf.getChildren().get(0)).getText().contains("\n"));
}
sleep(4, TimeUnit.SECONDS);
}

@Test
public void findASCIIEmojiTest(FxRobot robot) {
Expand Down
9 changes: 9 additions & 0 deletions rta/src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
handlers = java.util.logging.ConsoleHandler
.level = INFO

java.util.logging.ConsoleHandler.level = FINEST
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

com.gluonhq.richtextarea.model.level = INFO
com.gluonhq.richtextarea.undo.level = INFO
com.gluonhq.richtextarea.viewmodel.level = INFO
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public class FullFeaturedDemo extends Application {
"It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).\n" +
"Where does\u200bit\u200bcome\u200bfrom?\u200b\u200b\n" +
"Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of \"de Finibus Bonorum et Malorum\" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, \"Lorem ipsum dolor sit amet..\", comes from a line in section 1.10.32.\n" +
"The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.\n",
"The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from \"de Finibus Bonorum et Malorum\" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.",
decorations, 2314);

private final Label textLengthLabel = new Label();
Expand Down

0 comments on commit 9a8d486

Please sign in to comment.