Skip to content

Commit

Permalink
fix(server): allow unicode characters when updating statement from so…
Browse files Browse the repository at this point in the history
…urce
  • Loading branch information
fushar committed Oct 16, 2023
1 parent 7f99c38 commit d140dba
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
type="horizontal"
compact=false
action=""
acceptCharset=""
multipart=false
labelWidth=3
fieldWidth=9
Expand All @@ -13,6 +14,7 @@
method="${method}"
class="form-${type}"
<#if action?has_content>action="${action}"</#if>
<#if acceptCharset?has_content>accept-charset="${acceptCharset}"</#if>
<#if multipart>enctype="multipart/form-data"</#if>
>
<#global globalFormType = type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/>
</#if>
Expand Down

0 comments on commit d140dba

Please sign in to comment.