39 lines
1.4 KiB
Java
39 lines
1.4 KiB
Java
package com.yunhai.cms.theme;
|
|
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* themeVarsCss 构建器(方案 §5.3,对应评审 R4):
|
|
* 服务端预构建 :root{...} 覆盖串,只拼通过 color 正则校验的声明值——
|
|
* 模板仅做 th:utext 输出,安全性由"服务端只拼校验过的值"保证(可单测)。
|
|
*/
|
|
public final class ThemeVarsCss {
|
|
|
|
private ThemeVarsCss() {
|
|
}
|
|
|
|
/**
|
|
* 按主题 schema 将参数值聚合为 CSS 变量声明串,形如
|
|
* {@code :root{--primary:#b01a2e;--bg:#f7f5f2;}};无有效声明返回空串。
|
|
* 输入应为已按 schema 过滤的合法值({@link ThemeInfo#filterValidValues})。
|
|
*/
|
|
public static String build(ThemeInfo theme, Map<String, String> params) {
|
|
if (theme == null || params == null || params.isEmpty()) {
|
|
return "";
|
|
}
|
|
StringBuilder sb = new StringBuilder(":root{");
|
|
for (ThemeParam p : theme.getParams()) {
|
|
if (!p.isCssVarParam()) {
|
|
continue;
|
|
}
|
|
String value = params.get(p.getKey());
|
|
// 双重校验(方案 §9 注入面):入口已过滤,此处仍只拼通过 color 正则的值
|
|
if (value == null || !p.isValidValue(value)) {
|
|
continue;
|
|
}
|
|
sb.append(p.getCssVar()).append(':').append(value).append(';');
|
|
}
|
|
return sb.length() == 6 ? "" : sb.append('}').toString();
|
|
}
|
|
}
|