Skip to content

Commit 649dfeb

Browse files
authored
refactor: add check for delta values (#4602)
1 parent 66ad975 commit 649dfeb

File tree

2 files changed

+72
-7
lines changed
  • vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src

2 files changed

+72
-7
lines changed

vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/main/java/com/vaadin/flow/component/richtexteditor/RichTextEditor.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ public void setValueChangeMode(ValueChangeMode valueChangeMode) {
169169
* <p>
170170
* Note: {@link Binder} will take care of the {@code null} conversion when
171171
* integrates with the editor, as long as no new converter is defined.
172+
* <p>
173+
* Since v24, this method only accepts values in the HTML format, whereas in
174+
* v23 and earlier this method would accept values in the Delta format. In
175+
* order to prevent data corruption, passing a value that starts with either
176+
* <code>[</code> or <code>{</code> will now throw an
177+
* {@link IllegalArgumentException}, as it might indicate that the value is
178+
* in the Delta format. In order to keep using the Delta format, use
179+
* {@link #asDelta()}, which allows setting, retrieving, and binding the
180+
* value using Binder, in the Delta format. In order to pass an HTML value
181+
* starting with either characters, either wrap the value in a valid HTML
182+
* tag, such as <code>&lt;p&gt;</code>, or use {@link #asHtml()} which does
183+
* not include this check.
172184
*
173185
* @see #asDelta()
174186
* @see AsDelta#setValue(String)
@@ -177,10 +189,27 @@ public void setValueChangeMode(ValueChangeMode valueChangeMode) {
177189
*/
178190
@Override
179191
public void setValue(String value) {
192+
doSetValue(value, true);
193+
}
194+
195+
private void doSetValue(String value, boolean withDeltaCheck) {
180196
Objects.requireNonNull(value, "Null value is not supported");
197+
if (withDeltaCheck) {
198+
checkForDeltaValue(value);
199+
}
181200
super.setValue(value);
182201
}
183202

203+
private void checkForDeltaValue(String value) {
204+
value = value.trim();
205+
if (value.startsWith("[") || value.startsWith("{")) {
206+
throw new IllegalArgumentException(
207+
"The value starts with either '[' or '{' which indicates that this might be a value in the Delta format. "
208+
+ "Since v24, RichTextEditor.setValue only accepts values in the HTML format. "
209+
+ "Please check the JavaDoc for RichTextEditor.setValue for more information.");
210+
}
211+
}
212+
184213
@Override
185214
protected void setPresentationValue(String newPresentationValue) {
186215
String presentationValue = modelToPresentation(newPresentationValue);
@@ -737,15 +766,18 @@ public String toString() {
737766
/**
738767
* Gets an instance of {@code HasValue} for the editor in the HTML format.
739768
* Can be used for binding the value with {@link Binder}.
769+
* <p>
770+
* Note that since v24, the RichTextEditor uses the HTML value by default.
771+
* Instead of using this wrapper, {@link #getValue()} and
772+
* {@link #setValue(String)} can be used directly, and
773+
* {@link RichTextEditor} can be used for binding the HTML value using
774+
* Binder. This method is not intended to be deprecated as it keeps the
775+
* legacy behavior that allows passing values starting with either
776+
* <code>[</code> or <code>{</code>, which is not allowed when using
777+
* {@link #setValue(String)}.
740778
*
741779
* @return an instance of {@code HasValue}
742-
* @deprecated since v24 the RichTextEditor uses the HTML value by default.
743-
* Use {@link RichTextEditor#getValue()} and
744-
* {@link RichTextEditor#setValue(String)} instead. For binding
745-
* the HTML value with Binder, bind the {@link RichTextEditor}
746-
* directly.
747780
*/
748-
@Deprecated
749781
public HasValue<ValueChangeEvent<String>, String> asHtml() {
750782
if (asHtml == null) {
751783
asHtml = new AsHtml();
@@ -780,7 +812,7 @@ private class AsHtml implements HasValue<ValueChangeEvent<String>, String> {
780812
*/
781813
@Override
782814
public void setValue(String value) {
783-
RichTextEditor.this.setValue(value);
815+
RichTextEditor.this.doSetValue(value, false);
784816
}
785817

786818
/**

vaadin-rich-text-editor-flow-parent/vaadin-rich-text-editor-flow/src/test/java/com/vaadin/flow/component/richtexteditor/RichTextEditorTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ public void initialAsDeltaValue() {
5959
rte.getElement().getProperty("value"));
6060
}
6161

62+
@Test
63+
public void setValueStartingWithJsonArray_throws() {
64+
RichTextEditor rte = new RichTextEditor();
65+
66+
thrown.expect(IllegalArgumentException.class);
67+
thrown.expectMessage("The value starts with either '[' or '{'");
68+
69+
rte.setValue("[{\"insert\":\"Foo\"}]");
70+
}
71+
72+
@Test
73+
public void setValueStartingWithJsonObject_throws() {
74+
RichTextEditor rte = new RichTextEditor();
75+
76+
thrown.expect(IllegalArgumentException.class);
77+
thrown.expectMessage("The value starts with either '[' or '{'");
78+
79+
rte.setValue("{\"insert\":\"Foo\"}");
80+
}
81+
6282
// asHtml
6383

6484
@Test
@@ -90,6 +110,19 @@ public void asHtml_setRequiredIndicatorVisible_rteRequiredIndicatorVisible() {
90110
rte.isRequiredIndicatorVisible());
91111
}
92112

113+
@Test
114+
public void asHtml_setValueStartingWithJson_noException() {
115+
RichTextEditor rte = new RichTextEditor();
116+
117+
String value = "[{\"insert\":\"Foo\"}]";
118+
rte.asHtml().setValue(value);
119+
Assert.assertEquals(value, rte.getValue());
120+
121+
value = "{\"insert\":\"Foo\"}";
122+
rte.asHtml().setValue(value);
123+
Assert.assertEquals(value, rte.getValue());
124+
}
125+
93126
// asDelta
94127

95128
@Test

0 commit comments

Comments
 (0)