胸腺只允许红宝石标签,逃脱其他标签

时间:2018-10-09 14:22:27

标签: thymeleaf

我想在这样的百里香模板中显示ruby html标签:

<h1 th:text="(${author.displayNameReading} != null) ? '<ruby><rb>' + ${author.displayName} + '</rb><rt>' + ${author.displayNameReading} + '</rt></ruby>' : ${author.displayName}" th:lang="${author.locale}">Some author name</h1>

如果我使用th:text,它将被转义。如果我使用utext,它会起作用,但随后我将失去其他html标签的所有安全性。

是否只能在th:text内使用ruby,rt和rb标签?

1 个答案:

答案 0 :(得分:0)

为什么要尝试将所有内容填充到th:text属性中?您可以轻松地将所有信息拆分为新标签-更具可读性(格式类似于常规html,字符串连接少)且更安全(不需要th:utext)。例如:

<h1 th:lang="${author.locale}">
  <ruby th:if="${author.displayNameReading != null}">
    <rb th:text="${author.displayName}" />
    <rt th:text="${author.displayNameReading}" />
  </ruby>

  <span th:unless="${author.displayNameReading != null}" th:text="${author.displayName}" />
</h1>