From d140dba94cb52a4d278d731794dfea198ebf7cea Mon Sep 17 00:00:00 2001 From: Ashar Fuadi Date: Mon, 16 Oct 2023 18:16:31 +0700 Subject: [PATCH] fix(server): allow unicode characters when updating statement from source --- .../statement/LessonStatementResource.java | 3 ++- .../editorial/ProblemEditorialResource.java | 3 ++- .../statement/ProblemStatementResource.java | 3 ++- .../sandalphon/resource/StatementUtils.java | 21 +++++++++++++++++++ .../main/resources/judgels/michael/forms.ftl | 2 ++ .../michael/resource/editStatementView.ftl | 2 +- 6 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 judgels-backends/judgels-server-app/src/main/java/judgels/sandalphon/resource/StatementUtils.java diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/lesson/statement/LessonStatementResource.java b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/lesson/statement/LessonStatementResource.java index cfadf6599..cb15d8670 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/lesson/statement/LessonStatementResource.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/lesson/statement/LessonStatementResource.java @@ -32,6 +32,7 @@ import judgels.sandalphon.api.lesson.Lesson; import judgels.sandalphon.api.lesson.LessonStatement; import judgels.sandalphon.resource.StatementLanguageStatus; +import judgels.sandalphon.resource.StatementUtils; import judgels.sandalphon.resource.WorldLanguageRegistry; import judgels.service.ServiceUtils; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; @@ -96,7 +97,7 @@ public Response updateStatement( lessonStore.createUserCloneIfNotExists(actor.getUserJid(), lesson.getJid()); statementStore.updateStatement(actor.getUserJid(), lesson.getJid(), language, new LessonStatement.Builder() .title(form.title) - .text(form.text) + .text(StatementUtils.convertUnicodeToHtmlEntities(form.text)) .build()); return redirect("/lessons/" + lessonId + "/statements"); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/editorial/ProblemEditorialResource.java b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/editorial/ProblemEditorialResource.java index c3aee67fe..5502c7eae 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/editorial/ProblemEditorialResource.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/editorial/ProblemEditorialResource.java @@ -32,6 +32,7 @@ import judgels.sandalphon.api.problem.Problem; import judgels.sandalphon.api.problem.ProblemEditorial; import judgels.sandalphon.resource.StatementLanguageStatus; +import judgels.sandalphon.resource.StatementUtils; import judgels.sandalphon.resource.WorldLanguageRegistry; import judgels.service.ServiceUtils; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; @@ -120,7 +121,7 @@ public Response updateEditorial( problemStore.createUserCloneIfNotExists(actor.getUserJid(), problem.getJid()); editorialStore.updateEditorial(actor.getUserJid(), problem.getJid(), language, new ProblemEditorial.Builder() - .text(form.text) + .text(StatementUtils.convertUnicodeToHtmlEntities(form.text)) .build()); return redirect("/problems/" + problemId + "/editorials"); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/statement/ProblemStatementResource.java b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/statement/ProblemStatementResource.java index 976a0c158..4de83c0d4 100644 --- a/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/statement/ProblemStatementResource.java +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/michael/problem/statement/ProblemStatementResource.java @@ -32,6 +32,7 @@ import judgels.sandalphon.api.problem.Problem; import judgels.sandalphon.api.problem.ProblemStatement; import judgels.sandalphon.resource.StatementLanguageStatus; +import judgels.sandalphon.resource.StatementUtils; import judgels.sandalphon.resource.WorldLanguageRegistry; import judgels.service.ServiceUtils; import org.glassfish.jersey.media.multipart.FormDataContentDisposition; @@ -80,7 +81,7 @@ public Response updateStatement( problemStore.createUserCloneIfNotExists(actor.getUserJid(), problem.getJid()); statementStore.updateStatement(actor.getUserJid(), problem.getJid(), language, new ProblemStatement.Builder() .title(form.title) - .text(form.text) + .text(StatementUtils.convertUnicodeToHtmlEntities(form.text)) .build()); return redirect("/problems/" + problem.getType().name().toLowerCase() + "/" + problemId + "/statements"); diff --git a/judgels-backends/judgels-server-app/src/main/java/judgels/sandalphon/resource/StatementUtils.java b/judgels-backends/judgels-server-app/src/main/java/judgels/sandalphon/resource/StatementUtils.java new file mode 100644 index 000000000..b3cf2f911 --- /dev/null +++ b/judgels-backends/judgels-server-app/src/main/java/judgels/sandalphon/resource/StatementUtils.java @@ -0,0 +1,21 @@ +package judgels.sandalphon.resource; + +public class StatementUtils { + private StatementUtils() {} + + public static String convertUnicodeToHtmlEntities(String s) { + StringBuilder result = new StringBuilder(); + + for (int i = 0; i < s.length(); i++) { + char c = s.charAt(i); + + if (c >= 128) { + result.append("&#").append((int) c).append(";"); + } else { + result.append(c); + } + } + + return result.toString(); + } +} diff --git a/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/forms.ftl b/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/forms.ftl index 4de5e9bff..9c5af0b2a 100644 --- a/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/forms.ftl +++ b/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/forms.ftl @@ -5,6 +5,7 @@ type="horizontal" compact=false action="" + acceptCharset="" multipart=false labelWidth=3 fieldWidth=9 @@ -13,6 +14,7 @@ method="${method}" class="form-${type}" <#if action?has_content>action="${action}" + <#if acceptCharset?has_content>accept-charset="${acceptCharset}" <#if multipart>enctype="multipart/form-data" > <#global globalFormType = type> diff --git a/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/resource/editStatementView.ftl b/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/resource/editStatementView.ftl index 78662e7a1..a91613178 100644 --- a/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/resource/editStatementView.ftl +++ b/judgels-backends/judgels-server-app/src/main/resources/judgels/michael/resource/editStatementView.ftl @@ -8,7 +8,7 @@ <#include "ckeditor.ftl"> <@switchLanguage.view languages=enabledLanguages language=language/> - <@forms.form type="vertical"> + <@forms.form type="vertical" acceptCharset="utf-8"> <#if formValues.title?has_content> <@forms.input name="title" label="Title" required=true disabled=!canEdit/>