refactor: normalize custom data attributes

This commit is contained in:
Erzbir
2026-08-17 01:36:51 +08:00
parent 52acc4636a
commit b6e84de616
10 changed files with 54 additions and 63 deletions
+6 -6
View File
@@ -99,7 +99,7 @@
| 自定义深色名称 | `style.custom_dark` | 深色配色选择 `custom` 时填写 | | 自定义深色名称 | `style.custom_dark` | 深色配色选择 `custom` 时填写 |
| 自定义浅色名称 | `style.custom_light` | 浅色配色选择 `custom` 时填写 | | 自定义浅色名称 | `style.custom_light` | 浅色配色选择 `custom` 时填写 |
访客配色列表会从页面样式表中自动发现 `html[theme-color-scheme='name']` 选择器, 列表中的 `default` 用于清除访客选择并恢复主题设置, 访客配色列表会从页面样式表中自动发现 `html[data-theme-color-scheme='name']` 选择器, 列表中的 `default` 用于清除访客选择并恢复主题设置,
因此不要将 `default` 用作自定义配色名称 因此不要将 `default` 用作自定义配色名称
### 页面设置 ### 页面设置
@@ -163,7 +163,7 @@
下面的示例增加名为 `forest` 的配色: 下面的示例增加名为 `forest` 的配色:
```css ```css
html[theme-color-scheme='forest'] { html[-data-theme-color-scheme='forest'] {
--color-theme-background: #0f1a14; --color-theme-background: #0f1a14;
--color-theme-foreground: #c7d8cc; --color-theme-foreground: #c7d8cc;
--color-theme-accent: #68d391; --color-theme-accent: #68d391;
@@ -187,7 +187,7 @@ html[theme-color-scheme='forest'] {
直接使用内置配色名称即可覆盖对应变量: 直接使用内置配色名称即可覆盖对应变量:
```css ```css
html[theme-color-scheme='matrix'] { html[-data-theme-color-scheme='matrix'] {
--color-theme-background: #07130b; --color-theme-background: #07130b;
--color-theme-foreground: #7dff9b; --color-theme-foreground: #7dff9b;
--color-theme-accent: #39ff68; --color-theme-accent: #39ff68;
@@ -252,7 +252,7 @@ html[theme-color-scheme='matrix'] {
修改特定配色下的字体: 修改特定配色下的字体:
```css ```css
html[theme-color-scheme='matrix'] { html[-data-theme-color-scheme='matrix'] {
--family-theme-font: system-ui; --family-theme-font: system-ui;
} }
``` ```
@@ -293,11 +293,11 @@ OR:
如果要注入 `<script>` 或 `<link rel="stylesheet">`, 根据插入的位置需要为标签添加一些属性才能保证行为正常 如果要注入 `<script>` 或 `<link rel="stylesheet">`, 根据插入的位置需要为标签添加一些属性才能保证行为正常
- `<head>`: 重新执行所有请求页面新插入的无 `no-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">` - `<head>`: 重新执行所有请求页面新插入的无 `data-no-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">`
- `<footer>`: 重新执行有 `data-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">` - `<footer>`: 重新执行有 `data-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">`
- `.content`: 重新执行有 `data-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">` - `.content`: 重新执行有 `data-pjax` 属性的 `<script>` 和 `<link rel="stylesheet">`
设置 `no-pjax` 属性, 可以防止被 PJAX 处理 设置 `data-no-pjax` 属性, 可以防止被 PJAX 处理
### 已知的问题 ### 已知的问题
+12 -12
View File
@@ -63,7 +63,7 @@ function insertStyle(dom, parentElement, selectors) {
if (old) { if (old) {
parentElement.removeChild(old); parentElement.removeChild(old);
} else { } else {
let styles = parentElement.querySelectorAll('style:not([no-pjax])'); let styles = parentElement.querySelectorAll('style:not([data-no-pjax])');
styles.forEach((s) => { styles.forEach((s) => {
if (s) { if (s) {
if (djb2(s.innerHTML) === hash) { if (djb2(s.innerHTML) === hash) {
@@ -72,7 +72,7 @@ function insertStyle(dom, parentElement, selectors) {
} }
}); });
} }
style.setAttribute("data-hash", hash); style.dataset.hash = hash;
parentElement.appendChild(style); parentElement.appendChild(style);
} }
}); });
@@ -114,7 +114,7 @@ function insertInlineScript(dom, parentElement, selectors) {
if (old) { if (old) {
parentElement.removeChild(old); parentElement.removeChild(old);
} else { } else {
let scripts = parentElement.querySelectorAll('script:not([src]):not([no-pjax])'); let scripts = parentElement.querySelectorAll('script:not([src]):not([data-no-pjax])');
scripts.forEach((s) => { scripts.forEach((s) => {
if (s) { if (s) {
if (djb2(s.innerHTML) === hash) { if (djb2(s.innerHTML) === hash) {
@@ -125,7 +125,7 @@ function insertInlineScript(dom, parentElement, selectors) {
} }
const newScript = renewElement(script); const newScript = renewElement(script);
newScript.innerHTML = script.innerHTML; newScript.innerHTML = script.innerHTML;
newScript.setAttribute("data-hash", hash); newScript.dataset.hash = hash;
parentElement.appendChild(newScript); parentElement.appendChild(newScript);
} }
}); });
@@ -134,17 +134,17 @@ function insertInlineScript(dom, parentElement, selectors) {
// 这个函数将请求的页面 head 中 script 和 link 都插入进来合成新页面 // 这个函数将请求的页面 head 中 script 和 link 都插入进来合成新页面
function insertResourcesToHead(dom, parentElement) { function insertResourcesToHead(dom, parentElement) {
insertStyleLink(dom, parentElement, 'head link[rel="stylesheet"]:not([no-pjax])'); insertStyleLink(dom, parentElement, 'head link[rel="stylesheet"]:not([data-no-pjax])');
insertStyle(dom, parentElement, 'head style:not([no-pjax])'); insertStyle(dom, parentElement, 'head style:not([data-no-pjax])');
insertScript(dom, parentElement, 'head script[src]:not([no-pjax])'); insertScript(dom, parentElement, 'head script[src]:not([data-no-pjax])');
insertInlineScript(dom, parentElement, 'head script:not([src]):not([no-pjax])'); insertInlineScript(dom, parentElement, 'head script:not([src]):not([data-no-pjax])');
} }
// 这个函数将请求的页面 footer 中 script 和 link 都插入进来合成新页面 // 这个函数将请求的页面 footer 中 script 和 link 都插入进来合成新页面
function insertResourcesToFooter(dom, parentElement) { function insertResourcesToFooter(dom, parentElement) {
insertStyleLink(dom, parentElement, 'footer link[rel="stylesheet"][data-pjax]:not([no-pjax])'); insertStyleLink(dom, parentElement, 'footer link[rel="stylesheet"][data-pjax]:not([data-no-pjax])');
insertScript(dom, parentElement, 'footer script[src][data-pjax]:not([no-pjax])'); insertScript(dom, parentElement, 'footer script[src][data-pjax]:not([data-no-pjax])');
insertInlineScript(dom, parentElement, 'footer script:not([src])[data-pjax]:not([no-pjax])'); insertInlineScript(dom, parentElement, 'footer script:not([src])[data-pjax]:not([data-no-pjax])');
} }
// 处理请求的页面新增样式和脚本 // 处理请求的页面新增样式和脚本
@@ -184,7 +184,7 @@ document.addEventListener("pjax:complete", function () {
// 处理头和尾有新增的情况 // 处理头和尾有新增的情况
processNewScripts(_responseText); processNewScripts(_responseText);
// 处理替换的内容中存在脚本的情况 // 处理替换的内容中存在脚本的情况
let pjaxDoms = document.querySelectorAll(".content script[data-pjax]:not([no-pjax])"); let pjaxDoms = document.querySelectorAll(".content script[data-pjax]:not([data-no-pjax])");
pjaxDoms.forEach((element) => { pjaxDoms.forEach((element) => {
let code = element.text || element.textContent || element.innerHTML || ""; let code = element.text || element.textContent || element.innerHTML || "";
let parent = element.parentNode; let parent = element.parentNode;
+1 -1
View File
@@ -4,7 +4,7 @@ import {
} from "../utils/theme-preferences.js"; } from "../utils/theme-preferences.js";
import {getMessage} from "../utils/messages.js"; import {getMessage} from "../utils/messages.js";
const COLOR_SCHEME_SELECTOR_PATTERN = /html\[theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g; const COLOR_SCHEME_SELECTOR_PATTERN = /html\[data-theme-color-scheme\s*=\s*(['"]?)([^'"\]\s]+)\1\]/g;
function collectColorSchemes(cssRules, schemes) { function collectColorSchemes(cssRules, schemes) {
for (const rule of cssRules) { for (const rule of cssRules) {
+4 -9
View File
@@ -1,10 +1,5 @@
import {syncColorSchemePicker} from "./color-scheme-picker.js"; import {syncColorSchemePicker} from "./color-scheme-picker.js";
import { import {getStoredPreference, PREFERENCE_KEYS, setStoredPreference} from "../utils/theme-preferences.js";
PIXEL_STYLE_ATTRIBUTE,
PREFERENCE_KEYS,
getStoredPreference,
setStoredPreference
} from "../utils/theme-preferences.js";
function registerSearchButton() { function registerSearchButton() {
const searchButton = document.querySelector('[data-header-action="open-search"]'); const searchButton = document.querySelector('[data-header-action="open-search"]');
@@ -22,7 +17,7 @@ function registerThemeToggle() {
if (!toggle || typeof window.applyTheme !== "function") return; if (!toggle || typeof window.applyTheme !== "function") return;
toggle.addEventListener("click", function () { toggle.addEventListener("click", function () {
const currentScheme = document.documentElement.getAttribute("data-color-scheme"); const currentScheme = document.documentElement.dataset.colorScheme;
const nextMode = currentScheme === "dark" ? "light" : "dark"; const nextMode = currentScheme === "dark" ? "light" : "dark";
window.applyTheme(nextMode, {clearColorScheme: true}); window.applyTheme(nextMode, {clearColorScheme: true});
@@ -38,10 +33,10 @@ function registerPixelToggle() {
pixelToggle.addEventListener("click", function () { pixelToggle.addEventListener("click", function () {
const storedStatus = getStoredPreference(PREFERENCE_KEYS.pixelStyle); const storedStatus = getStoredPreference(PREFERENCE_KEYS.pixelStyle);
const currentStatus = storedStatus const currentStatus = storedStatus
|| document.documentElement.getAttribute(PIXEL_STYLE_ATTRIBUTE); || document.documentElement.dataset.pixelStyle;
const newStatus = currentStatus === "true" ? "false" : "true"; const newStatus = currentStatus === "true" ? "false" : "true";
document.documentElement.setAttribute(PIXEL_STYLE_ATTRIBUTE, newStatus); document.documentElement.dataset.pixelStyle = newStatus;
setStoredPreference(PREFERENCE_KEYS.pixelStyle, newStatus); setStoredPreference(PREFERENCE_KEYS.pixelStyle, newStatus);
window.TerminalTheme?.syncPixelFont?.(); window.TerminalTheme?.syncPixelFont?.();
}); });
+2 -2
View File
@@ -7,10 +7,10 @@
width: calc(var(--size-theme-font) * 1.85); width: calc(var(--size-theme-font) * 1.85);
} }
html[pixel_style='false'] .icon-regular { html[data-pixel-style='false'] .icon-regular {
display: inline; display: inline;
} }
html[pixel_style='true'] .icon-pixel { html[data-pixel-style='true'] .icon-pixel {
display: inline; display: inline;
} }
+2 -2
View File
@@ -31,11 +31,11 @@ html {
scrollbar-gutter: stable; scrollbar-gutter: stable;
} }
html[pixel_style='true'] { html[data-pixel-style='true'] {
--family-theme-font: 'Fusion Pixel 12px Prop zh_hans'; --family-theme-font: 'Fusion Pixel 12px Prop zh_hans';
} }
html[system_font_enable='true']:not([pixel_style='true']) { html[data-system-font-enabled='true']:not([data-pixel-style='true']) {
--family-theme-font: system-ui; --family-theme-font: system-ui;
} }
+2 -2
View File
@@ -27,7 +27,7 @@
--color-theme-scrollbar: color-mix(in srgb, var(--color-theme-foreground) 45%, var(--color-theme-background)) transparent; --color-theme-scrollbar: color-mix(in srgb, var(--color-theme-foreground) 45%, var(--color-theme-background)) transparent;
} }
html[theme-color-scheme] { html[data-theme-color-scheme] {
--color-theme-tag: var(--color-theme-accent); --color-theme-tag: var(--color-theme-accent);
--color-theme-title: var(--color-theme-accent); --color-theme-title: var(--color-theme-accent);
--color-theme-meta: var(--color-theme-foreground); --color-theme-meta: var(--color-theme-foreground);
@@ -122,7 +122,7 @@ $color-schemes: (
); );
@each $scheme-name, $scheme-colors in $color-schemes { @each $scheme-name, $scheme-colors in $color-schemes {
html[theme-color-scheme="#{$scheme-name}"] { html[data-theme-color-scheme="#{$scheme-name}"] {
@each $role, $color in $scheme-colors { @each $role, $color in $scheme-colors {
--color-theme-#{$role}: #{$color}; --color-theme-#{$role}: #{$color};
} }
-2
View File
@@ -4,8 +4,6 @@ export const PREFERENCE_KEYS = Object.freeze({
pixelStyle: "pixel-style" pixelStyle: "pixel-style"
}); });
export const PIXEL_STYLE_ATTRIBUTE = "pixel_style";
export function getStoredPreference(key) { export function getStoredPreference(key) {
return window.TerminalTheme?.storage return window.TerminalTheme?.storage
? window.TerminalTheme.storage.get(key) ? window.TerminalTheme.storage.get(key)
+16 -16
View File
@@ -8,72 +8,72 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title th:if="${head == null}" th:text="${site.title}">Site title</title> <title th:if="${head == null}" th:text="${site.title}">Site title</title>
<th:block th:replace="~{modules/theme-preferences :: theme-preferences}"/> <th:block th:replace="~{modules/theme-preferences :: theme-preferences}"/>
<link no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/> <link data-no-pjax rel="stylesheet" th:href="@{/assets/dist/main.css?v={version}(version=${theme.spec.version})}"/>
<script no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script> <script data-no-pjax type="module" th:src="@{/assets/dist/main.js?v={version}(version=${theme.spec.version})}"></script>
<script no-pjax type="module" th:if="${theme.config.basic.pjax_enable}" <script data-no-pjax type="module" th:if="${theme.config.basic.pjax_enable}"
th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script> th:src="@{/assets/dist/libs/pjax.js?v={version}(version=${theme.spec.version})}"></script>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.media_max_width)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.media_max_width)}" th:inline="css">
:root { :root {
--width-theme-media-max: [(${theme.config.style.media_max_width})]; --width-theme-media-max: [(${theme.config.style.media_max_width})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.font_size)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.font_size)}" th:inline="css">
:root { :root {
--size-theme-font: [(${theme.config.style.font_size})]; --size-theme-font: [(${theme.config.style.font_size})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.line_height)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.line_height)}" th:inline="css">
:root { :root {
--height-theme-line: [(${theme.config.style.line_height})]; --height-theme-line: [(${theme.config.style.line_height})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.post_paragraph_spacing)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.post_paragraph_spacing)}" th:inline="css">
:root { :root {
--spacing-theme-post-paragraph: [(${theme.config.style.post_paragraph_spacing})]; --spacing-theme-post-paragraph: [(${theme.config.style.post_paragraph_spacing})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.post_meta_divider)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.post_meta_divider)}" th:inline="css">
:root { :root {
--content-theme-post-meta-divider: "[(${theme.config.style.post_meta_divider})]"; --content-theme-post-meta-divider: "[(${theme.config.style.post_meta_divider})]";
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.content_padding)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.content_padding)}" th:inline="css">
:root { :root {
--spacing-theme-container-padding: [(${theme.config.style.content_padding})]; --spacing-theme-container-padding: [(${theme.config.style.content_padding})];
--spacing-theme-container-padding-mobile: [(${theme.config.style.content_padding})]; --spacing-theme-container-padding-mobile: [(${theme.config.style.content_padding})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.list_marker)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.list_marker)}" th:inline="css">
:root { :root {
--list-theme-marker: [(${theme.config.style.list_marker})]; --list-theme-marker: [(${theme.config.style.list_marker})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.border_width)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.border_width)}" th:inline="css">
:root { :root {
--width-theme-border: [(${theme.config.style.border_width})]; --width-theme-border: [(${theme.config.style.border_width})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.border_style)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.border_style)}" th:inline="css">
:root { :root {
--style-theme-border: [(${theme.config.style.border_style})]; --style-theme-border: [(${theme.config.style.border_style})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.table_border_width)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.table_border_width)}" th:inline="css">
:root { :root {
--width-theme-table-border: [(${theme.config.style.table_border_width})]; --width-theme-table-border: [(${theme.config.style.table_border_width})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.table_border_style)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.table_border_style)}" th:inline="css">
:root { :root {
--style-theme-table-border: [(${theme.config.style.table_border_style})]; --style-theme-table-border: [(${theme.config.style.table_border_style})];
} }
</style> </style>
<style no-pjax th:if="${not #strings.isEmpty(theme.config.style.title_divider_style)}" th:inline="css"> <style data-no-pjax th:if="${not #strings.isEmpty(theme.config.style.title_divider_style)}" th:inline="css">
:root { :root {
--style-theme-title-divider: [(${theme.config.style.title_divider_style})]; --style-theme-title-divider: [(${theme.config.style.title_divider_style})];
} }
</style> </style>
<script no-pjax> <script data-no-pjax>
window.TerminalTheme?.applyCustomCss?.(); window.TerminalTheme?.applyCustomCss?.();
</script> </script>
<th:block <th:block
+9 -11
View File
@@ -1,5 +1,5 @@
<th:block th:fragment="theme-preferences"> <th:block th:fragment="theme-preferences">
<script no-pjax th:inline="javascript"> <script data-no-pjax th:inline="javascript">
window.TerminalThemeConfig = { window.TerminalThemeConfig = {
systemFontEnabled: /*[[${theme.config.style.system_font_enable}]]*/ false, systemFontEnabled: /*[[${theme.config.style.system_font_enable}]]*/ false,
darkTheme: /*[[${theme.config.style.dark_color_scheme}]]*/ "dark", darkTheme: /*[[${theme.config.style.dark_color_scheme}]]*/ "dark",
@@ -19,7 +19,7 @@
} }
}; };
</script> </script>
<script no-pjax> <script data-no-pjax>
(function () { (function () {
const config = window.TerminalThemeConfig || {}; const config = window.TerminalThemeConfig || {};
const root = document.documentElement; const root = document.documentElement;
@@ -113,8 +113,8 @@
} }
const actualMode = mode === "system" ? getSystemScheme() : mode; const actualMode = mode === "system" ? getSystemScheme() : mode;
root.setAttribute("data-color-scheme", actualMode); root.dataset.colorScheme = actualMode;
root.setAttribute("theme-color-scheme", getStoredColorScheme() || resolveTheme(actualMode)); root.dataset.themeColorScheme = getStoredColorScheme() || resolveTheme(actualMode);
} }
function applyColorScheme(scheme) { function applyColorScheme(scheme) {
@@ -125,23 +125,21 @@
} }
setStoredPreference(storageKeys.colorScheme, scheme); setStoredPreference(storageKeys.colorScheme, scheme);
root.setAttribute("theme-color-scheme", scheme); root.dataset.themeColorScheme = scheme;
} }
function syncSystemFont() { function syncSystemFont() {
root.setAttribute("system_font_enable", String(config.systemFontEnabled)); root.dataset.systemFontEnabled = String(config.systemFontEnabled);
} }
function syncPixelStyle() { function syncPixelStyle() {
const storedStatus = getStoredPreference(storageKeys.pixelStyle) root.dataset.pixelStyle = getStoredPreference(storageKeys.pixelStyle)
|| String(config.pixelStyle); || String(config.pixelStyle);
root.setAttribute("pixel_style", storedStatus);
} }
function syncPixelFont() { function syncPixelFont() {
const existingLink = document.getElementById("terminal-pixel-font"); const existingLink = document.getElementById("terminal-pixel-font");
const pixelEnabled = root.getAttribute("pixel_style") === "true"; const pixelEnabled = root.dataset.pixelStyle === "true";
if (!pixelEnabled && existingLink) { if (!pixelEnabled && existingLink) {
existingLink.remove(); existingLink.remove();
@@ -152,7 +150,7 @@
const link = document.createElement("link"); const link = document.createElement("link");
link.id = "terminal-pixel-font"; link.id = "terminal-pixel-font";
link.setAttribute("no-pjax", ""); link.dataset.noPjax = "";
link.rel = "stylesheet"; link.rel = "stylesheet";
link.href = config.pixelCssHref; link.href = config.pixelCssHref;
document.head.appendChild(link); document.head.appendChild(link);