changeset 11187:dbc803ab09d8

8212102: [TextField] IOOBE on paste/replace text with control characters Reviewed-by: kcr, aghaisas
author arapte
date Wed, 30 Jan 2019 14:24:22 +0530
parents 1636fa640ddd
children bcd3723735f6 c8bb215eb1a5
files modules/javafx.controls/src/main/java/javafx/scene/control/TextArea.java modules/javafx.controls/src/main/java/javafx/scene/control/TextField.java modules/javafx.controls/src/main/java/javafx/scene/control/TextInputControl.java modules/javafx.controls/src/test/java/test/javafx/scene/control/TextInputControlTest.java
diffstat 4 files changed, 48 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/TextArea.java	Tue Jan 29 14:19:17 2019 +0530
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/TextArea.java	Wed Jan 30 14:24:22 2019 +0530
@@ -620,6 +620,10 @@
         return new TextAreaSkin(this);
     }
 
+    @Override
+    String filterInput(String text) {
+        return TextInputControl.filterInput(text, false, false);
+    }
 
     /***************************************************************************
      *                                                                         *
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/TextField.java	Tue Jan 29 14:19:17 2019 +0530
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/TextField.java	Wed Jan 30 14:24:22 2019 +0530
@@ -276,6 +276,11 @@
         return new TextFieldSkin(this);
     }
 
+    @Override
+    String filterInput(String text) {
+        return TextInputControl.filterInput(text, true, true);
+    }
+
     /***************************************************************************
      *                                                                         *
      * Stylesheet Handling                                                     *
--- a/modules/javafx.controls/src/main/java/javafx/scene/control/TextInputControl.java	Tue Jan 29 14:19:17 2019 +0530
+++ b/modules/javafx.controls/src/main/java/javafx/scene/control/TextInputControl.java	Wed Jan 30 14:24:22 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -546,12 +546,15 @@
         if (!this.text.isBound()) {
             final int oldLength = getLength();
             TextFormatter<?> formatter = getTextFormatter();
-            TextFormatter.Change change = new TextFormatter.Change(this, getFormatterAccessor(), start, end, text);
+            TextFormatter.Change change;
             if (formatter != null && formatter.getFilter() != null) {
+                change = new TextFormatter.Change(this, getFormatterAccessor(), start, end, text);
                 change = formatter.getFilter().apply(change);
                 if (change == null) {
                     return;
                 }
+            } else {
+                change = new TextFormatter.Change(this, getFormatterAccessor(), start, end, filterInput(text));
             }
 
             // Update the content
@@ -1289,6 +1292,11 @@
         return accessor;
     }
 
+    String filterInput(String text) {
+        // This method should be overridden by child classes.
+        // It is overridden in TextField and TextArea as needed.
+        return text;
+    }
 
     /**
      * A little utility method for stripping out unwanted characters.
--- a/modules/javafx.controls/src/test/java/test/javafx/scene/control/TextInputControlTest.java	Tue Jan 29 14:19:17 2019 +0530
+++ b/modules/javafx.controls/src/test/java/test/javafx/scene/control/TextInputControlTest.java	Wed Jan 30 14:24:22 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -63,7 +63,7 @@
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-
+import static org.junit.Assert.fail;
 /**
  */
 @RunWith(Parameterized.class)
@@ -235,7 +235,33 @@
         assertTrue(passed[0]);
     }
 
-    // TODO test that setting text which includes a \n works
+    // Test for JDK-8212102
+    @Test public void testControlCharacters() {
+        try {
+            String cc = "\r\n\n";
+            String str = "123456";
+
+            textInput.setText(cc);
+
+            textInput.setText(str);
+            textInput.replaceText(0, 6, cc);
+
+            textInput.setText(str);
+            textInput.replaceText(new IndexRange(0, 6), cc);
+
+            textInput.setText(str);
+            textInput.selectAll();
+            textInput.replaceSelection(cc);
+
+            textInput.setText(str);
+            textInput.selectRange(0, 6);
+            textInput.replaceSelection(cc);
+
+        } catch (Exception e) {
+            fail("Control characters(\\r\\n) caused Exception: " + e);
+        }
+    }
+
     @Test public void controlCharactersAreOmitted_setText_getText() {
         String s = "This is " + '\0' + "a test";
         textInput.setText(s);