never use J.stringify in templates

This commit is contained in:
Niklas Fiekas 2017-10-28 14:37:05 +02:00
parent 793bd6b664
commit 4afa1a3f91
21 changed files with 40 additions and 38 deletions

View file

@ -14,8 +14,6 @@ trait JsonHelper {
def toJson[A: Writes](a: A): Html = lila.common.String.html.safeJson(Json toJson a)
def J = Json
def htmlOrNull[A, B](a: Option[A])(f: A => Html) = a.fold(Html("null"))(f)
def jsOrNull[A: Writes](a: Option[A]) = a.fold(Html("null"))(x => toJson(x))

View file

@ -43,7 +43,7 @@
@embedJs {
lichess.startEmbeddedAnalyse({
element: document.querySelector('.embedded_analyse'),
data: @Html(J.stringify(data)),
data: @safeJson(data),
embed: true,
i18n: @board.userAnalysisI18n(withCeval = false, withExplorer = false)
});

View file

@ -1,5 +1,5 @@
@()(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@safeJson(i18nJsObject(
trans.flipBoard,
trans.gameAborted,
trans.checkmate,
@ -90,4 +90,4 @@ trans.maybeIncludeMoreGamesFromThePreferencesMenu,
trans.winPreventedBy50MoveRule,
trans.lossSavedBy50MoveRule,
trans.allSet
)))
))

View file

@ -14,7 +14,7 @@
@embedJs {
lichess = lichess || {};
lichess.analyse = {
data: @Html(J.stringify(data)),
data: @safeJson(data),
i18n: @jsI18n(),
userId: @jsUserId,
chat: @jsOrNull(chatOption map { c =>

View file

@ -6,7 +6,7 @@
@moreJs = {
@jsAt(s"compiled/lichess.editor${isProd??(".min")}.js")
@embedJs {
var data = @Html(J.stringify(JsData(sit, fen, animationDuration)));
var data = @safeJson(JsData(sit, fen, animationDuration));
data.positions = @positionsJson;
LichessEditor(document.getElementById('board_editor'), data);
}

View file

@ -1,5 +1,6 @@
@(withCeval: Boolean = true, withExplorer: Boolean = true)(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@import play.api.libs.json.Json
@safeJson(i18nJsObject(
trans.analysis,
trans.flipBoard,
trans.backToGame,
@ -55,7 +56,7 @@ trans.removesTheDepthLimit,
trans.multipleLines,
trans.cpus,
trans.memory
), J.obj()) ++ withExplorer.fold(i18nJsObject(
), Json.obj()) ++ withExplorer.fold(i18nJsObject(
// explorer (also uses gameOver, checkmate, stalemate, draw, variantEnding)
trans.openingExplorerAndTablebase,
trans.openingExplorer,
@ -87,4 +88,4 @@ trans.maybeIncludeMoreGamesFromThePreferencesMenu,
trans.winPreventedBy50MoveRule,
trans.lossSavedBy50MoveRule,
trans.allSet
), J.obj())))
), Json.obj()))

View file

@ -7,7 +7,7 @@ $(function() {
LichessLearn(document.getElementById('learn_app'), {
data: @jsOrNull(data),
sideElement: document.getElementById('learn_side'),
i18n: @Html(J.stringify(i18nFullDbJsObject(lila.i18n.I18nDb.Learn)))
i18n: @safeJson(i18nFullDbJsObject(lila.i18n.I18nDb.Learn))
});
});
}

View file

@ -1,5 +1,7 @@
@(data: play.api.libs.json.JsObject, userTimeline: Vector[lila.timeline.Entry], forumRecent: List[lila.forum.MiniForumPost], tours: List[Tournament], events: List[lila.event.Event], simuls: List[lila.simul.Simul], featured: Option[Game], leaderboard: List[User.LightPerf], tournamentWinners: List[lila.tournament.Winner], puzzle: Option[lila.puzzle.DailyPuzzle], streams: List[lila.tv.StreamOnAir], lastPost: List[lila.blog.MiniPost], playban: Option[lila.playban.TempBan], currentGame: Option[lila.app.mashup.Preload.CurrentGame], nbRounds: Int)(implicit ctx: Context)
@import play.api.libs.json.Json
@underchat = {
<div id="featured_game">
@featured.map { g =>
@ -57,12 +59,12 @@
@embedJs {
window.customWS = true;
lichess_lobby = {
data: @Html(J.stringify(data)),
data: @safeJson(data),
playban: @playban.fold(Html("null")){ pb =>
@Html(J.stringify(J.obj("minutes" -> pb.mins, "remainingSeconds" -> (pb.remainingSeconds + 3))))
@safeJson(Json.obj("minutes" -> pb.mins, "remainingSeconds" -> (pb.remainingSeconds + 3)))
},
currentGame: @currentGame.fold(Html("null")) { cg =>
@Html(J.stringify(cg.json))
@safeJson(cg.json)
},
i18n: @jsI18n()
};

View file

@ -1,5 +1,5 @@
@()(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@safeJson(i18nJsObject(
trans.realTime,
trans.correspondence,
trans.nbGamesInPlay,
@ -25,4 +25,4 @@ trans.quickPairing,
trans.lobby,
trans.custom,
trans.anonymous
)))
))

View file

@ -11,9 +11,9 @@
@embedJs {
lichess = lichess || {};
lichess.practice = {
practice: @Html(J.stringify(data.practice)),
study: @Html(J.stringify(data.study)),
data: @Html(J.stringify(data.analysis)),
practice: @safeJson(data.practice),
study: @safeJson(data.study),
data: @safeJson(data.analysis),
i18n: @board.userAnalysisI18n(),
explorer: {
endpoint: "@explorerEndpoint",

View file

@ -12,9 +12,9 @@
@embedJs {
lichess = lichess || {};
lichess.relay = {
relay: @Html(J.stringify(data.relay)),
study: @Html(J.stringify(data.study)),
data: @Html(J.stringify(data.analysis)),
relay: @safeJson(data.relay),
study: @safeJson(data.study),
data: @safeJson(data.analysis),
i18n: @board.userAnalysisI18n(),
tagTypes: '@lila.study.PgnTags.typesToString',
userId: @jsUserId,

View file

@ -1,5 +1,6 @@
@(g: Game)(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@import play.api.libs.json.Json
@safeJson(i18nJsObject(
trans.flipBoard,
trans.aiNameLevelAiLevel,
trans.yourTurn,
@ -57,8 +58,8 @@ trans.xSecondsToPlayTheFirstMove
trans.kingInTheCenter,
trans.threeChecks,
trans.variantEnding
), J.obj()) ++ g.isTournament.fold(i18nJsObject(
), Json.obj()) ++ g.isTournament.fold(i18nJsObject(
trans.backToTournament,
trans.viewTournament,
trans.standing
), J.obj())))
), Json.obj()))

View file

@ -10,7 +10,7 @@
window.customWS = true;
window.onload = function() {
LichessRound.boot({
data: @Html(J.stringify(data)),
data: @safeJson(data),
i18n: @jsI18n(pov.game),
userId: @jsUserId,
@tour.map { t =>

View file

@ -8,7 +8,7 @@
window.customWS = true;
window.onload = function() {
LichessRound.boot({
data: @Html(J.stringify(data)),
data: @safeJson(data),
i18n: @jsI18n(pov.game),
chat: @jsOrNull(chatOption map { c =>
chat.ChatJsData.json(c.chat, name = trans.spectatorRoom.txt(), timeout = c.timeout, withNote = ctx.isAuth, public = true)

View file

@ -1,5 +1,5 @@
@()(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@safeJson(i18nJsObject(
trans.finished,
trans.withdraw,
trans.join,
@ -11,4 +11,4 @@ trans.nbDraws,
trans.nbLosses,
trans.by,
trans.signIn
)))
))

View file

@ -10,7 +10,7 @@
@jsAt(s"compiled/lichess.simul${isProd??(".min")}.js")
@embedJs {
lichess.simul = {
data: @Html(J.stringify(data)),
data: @safeJson(data),
i18n: @jsI18n(),
socketVersion: @socketVersion,
userId: @jsUserId,

View file

@ -41,8 +41,8 @@
@embedJs {
lichess.startEmbeddedAnalyse({
element: document.querySelector('.embedded_study'),
study: @Html(J.stringify(data.study)),
data: @Html(J.stringify(data.analysis)),
study: @safeJson(data.study),
data: @safeJson(data.analysis),
embed: true,
i18n: @board.userAnalysisI18n(),
userId: "null"

View file

@ -5,8 +5,8 @@
@embedJs {
lichess = lichess || {};
lichess.study = {
study: @Html(J.stringify(data.study)),
data: @Html(J.stringify(data.analysis)),
study: @safeJson(data.study),
data: @safeJson(data.analysis),
i18n: @board.userAnalysisI18n(),
tagTypes: '@lila.study.PgnTags.typesToString',
userId: @jsUserId,

View file

@ -6,7 +6,7 @@
@jsAt("javascripts/vendor/dragscroll.js")
@embedJs {
var app = LichessTournamentSchedule.app(document.getElementById('tournament_schedule'), {
data: @Html(J.stringify(json)),
data: @safeJson(json),
i18n: @jsI18n()
});
lichess.StrongSocket.defaults.params.flag = "tournament";

View file

@ -1,5 +1,5 @@
@()(implicit ctx: Context)
@Html(J.stringify(i18nJsObject(
@safeJson(i18nJsObject(
trans.standing,
trans.starting,
trans.tournamentIsStarting,
@ -26,4 +26,4 @@ trans.viewMoreTournaments,
trans.averageOpponent,
trans.rated,
trans.casual
)))
))

View file

@ -8,7 +8,7 @@
window.customWS = true;
window.onload = function() {
LichessRound.boot({
data: @Html(J.stringify(data)),
data: @safeJson(data),
i18n: @round.jsI18n(pov.game)
}, document.getElementById('lichess'));
};