Commit 293daa19 authored by Michael Metternich's avatar Michael Metternich

CMS-10942 Reintegrate bugfix/CMS-10942 into release/4.2

parents 9fc63b7b 5b7fd4a8
/*
* Copyright 2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2013-2018 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -44,15 +44,21 @@ public class JsonUtils {
}
}
public static void appendToCommaSeparatedString(JSONObject object, String key, String value) throws JSONException {
public static void appendToCommaSeparatedString(final JSONObject object, final String key,
final String value) throws JSONException {
appendToSeparatedString(object, key, value, ",");
}
private static void appendToSeparatedString(final JSONObject object, final String key, final String value,
final String separator) throws JSONException {
if (object.has(key)) {
final String commaSeparated = object.getString(key);
if (StringUtils.isBlank(commaSeparated)) {
final String separatedValue = object.getString(key);
if (StringUtils.isBlank(separatedValue)) {
object.put(key, value);
} else if (StringUtils.trim(commaSeparated).endsWith(",")) {
object.put(key, commaSeparated + value);
} else if (StringUtils.trim(separatedValue).endsWith(separator)) {
object.put(key, separatedValue + value);
} else {
object.put(key, commaSeparated + ',' + value);
object.put(key, separatedValue + separator + value);
}
} else {
object.put(key, value);
......@@ -140,16 +146,22 @@ public class JsonUtils {
/**
* Appends a JSON object to an existing one. New values will be copied to the existing object.
* Strings will be appended to existing strings using a comma as separator. Arrays will be joined with existing
* arrays, otherwise existing arrays will get the new values appended. Existing objects will be appended recursively.
* Strings will be appended to existing strings using a comma or semicolon as separator. Arrays will be joined with
* existing arrays, otherwise existing arrays will get the new values appended. Existing objects will be appended
* recursively.
*
* For example, if the existing object is:
* <p><strong>Please note:</strong> The default separator used when appending strings is a comma. For the key
* "extraAllowedContent" a semicolon is used. This exception is only valid for Hippo 11. From version 12 the
* configuration of "extraAllowedContent" will be changed.</p>
*
* <pre>For example, if the existing object is:
* {
* a: 'a1',
* b: {
* c: [ 'c1', 'c2' ]
* d: [ 'd1', 'd2' ]
* }
* },
* extraAllowedContent: 'th;'
* }
*
* And the appended object is:
......@@ -159,7 +171,8 @@ public class JsonUtils {
* c: 'c3',
* d: [ 'd3', 'd4' ],
* e: false
* }
* },
* extraAllowedContent: 'q;cite;'
* }
*
* The resulting object will be:
......@@ -169,12 +182,13 @@ public class JsonUtils {
* c: [ 'c1', 'c2', 'c3' ],
* d: [ 'd1', 'd2', 'd3', 'd4' ],
* e: false
* }
* }
* },
* extraAllowedContent: 'th;q;cite;'
* }</pre>
*
* @param object the object to append values to
* @param values the values to append
* @throws JSONException
* @throws JSONException when for example the JSON formatting is invalid
*/
public static void append(JSONObject object, JSONObject values) throws JSONException {
if (values == null) {
......@@ -191,7 +205,11 @@ public class JsonUtils {
if (oldValue instanceof JSONObject && newValue instanceof JSONObject) {
append((JSONObject)oldValue, (JSONObject)newValue);
} else if (oldValue instanceof String && newValue instanceof String) {
appendToCommaSeparatedString(object, newKey, (String)newValue);
if ("extraAllowedContent".equals(newKey)) {
appendToSeparatedString(object, newKey, (String) newValue, ";");
} else {
appendToCommaSeparatedString(object, newKey, (String) newValue);
}
} else if (oldValue instanceof JSONArray && newValue instanceof JSONArray) {
JSONArray concatenated = concatArrays((JSONArray) oldValue, (JSONArray) newValue);
object.put(newKey, concatenated);
......
/*
* Copyright 2013 Hippo B.V. (http://www.onehippo.com)
* Copyright 2013-2018 Hippo B.V. (http://www.onehippo.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -21,12 +21,14 @@ import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
/**
* Tests {@link CKEditorStringUtils}.
* Tests
*/
public class JsonUtilsTest {
......@@ -239,4 +241,45 @@ public class JsonUtilsTest {
assertEquals("first,second", child.getString("childKey"));
}
@Test
public void appendToExtraAllowedContent() throws JSONException {
object.put("extraAllowedContent", "th;");
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q'}"));
assertThat(object.getString("extraAllowedContent"), is("th;q"));
}
@Test
public void appendToExtraAllowedContent2() throws JSONException {
object.put("extraAllowedContent", "th");
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q'}"));
assertThat(object.getString("extraAllowedContent"), is("th;q"));
}
@Test
public void appendToExtraAllowedContent3() throws JSONException {
object.put("extraAllowedContent", "th;");
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q;cite;'}"));
assertThat(object.getString("extraAllowedContent"), is("th;q;cite;"));
}
@Test
public void appendToExtraAllowedContent4() throws JSONException {
object.put("extraAllowedContent", "th");
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q;cite;'}"));
assertThat(object.getString("extraAllowedContent"), is("th;q;cite;"));
}
@Test
public void appendToExtraAllowedContent5() throws JSONException {
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q;cite;'}"));
assertThat(object.getString("extraAllowedContent"), is("q;cite;"));
}
@Test
public void appendToExtraAllowedContent6() throws JSONException {
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'q'}"));
JsonUtils.append(object, new JSONObject("{ extraAllowedContent: 'cite'}"));
assertThat(object.getString("extraAllowedContent"), is("q;cite"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment