refactor: normalize custom data attributes
This commit is contained in:
+12
-12
@@ -63,7 +63,7 @@ function insertStyle(dom, parentElement, selectors) {
|
||||
if (old) {
|
||||
parentElement.removeChild(old);
|
||||
} else {
|
||||
let styles = parentElement.querySelectorAll('style:not([no-pjax])');
|
||||
let styles = parentElement.querySelectorAll('style:not([data-no-pjax])');
|
||||
styles.forEach((s) => {
|
||||
if (s) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
@@ -114,7 +114,7 @@ function insertInlineScript(dom, parentElement, selectors) {
|
||||
if (old) {
|
||||
parentElement.removeChild(old);
|
||||
} 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) => {
|
||||
if (s) {
|
||||
if (djb2(s.innerHTML) === hash) {
|
||||
@@ -125,7 +125,7 @@ function insertInlineScript(dom, parentElement, selectors) {
|
||||
}
|
||||
const newScript = renewElement(script);
|
||||
newScript.innerHTML = script.innerHTML;
|
||||
newScript.setAttribute("data-hash", hash);
|
||||
newScript.dataset.hash = hash;
|
||||
parentElement.appendChild(newScript);
|
||||
}
|
||||
});
|
||||
@@ -134,17 +134,17 @@ function insertInlineScript(dom, parentElement, selectors) {
|
||||
|
||||
// 这个函数将请求的页面 head 中 script 和 link 都插入进来合成新页面
|
||||
function insertResourcesToHead(dom, parentElement) {
|
||||
insertStyleLink(dom, parentElement, 'head link[rel="stylesheet"]:not([no-pjax])');
|
||||
insertStyle(dom, parentElement, 'head style:not([no-pjax])');
|
||||
insertScript(dom, parentElement, 'head script[src]:not([no-pjax])');
|
||||
insertInlineScript(dom, parentElement, 'head script:not([src]):not([no-pjax])');
|
||||
insertStyleLink(dom, parentElement, 'head link[rel="stylesheet"]:not([data-no-pjax])');
|
||||
insertStyle(dom, parentElement, 'head style:not([data-no-pjax])');
|
||||
insertScript(dom, parentElement, 'head script[src]:not([data-no-pjax])');
|
||||
insertInlineScript(dom, parentElement, 'head script:not([src]):not([data-no-pjax])');
|
||||
}
|
||||
|
||||
// 这个函数将请求的页面 footer 中 script 和 link 都插入进来合成新页面
|
||||
function insertResourcesToFooter(dom, parentElement) {
|
||||
insertStyleLink(dom, parentElement, 'footer link[rel="stylesheet"][data-pjax]:not([no-pjax])');
|
||||
insertScript(dom, parentElement, 'footer script[src][data-pjax]:not([no-pjax])');
|
||||
insertInlineScript(dom, parentElement, 'footer script:not([src])[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([data-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);
|
||||
// 处理替换的内容中存在脚本的情况
|
||||
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) => {
|
||||
let code = element.text || element.textContent || element.innerHTML || "";
|
||||
let parent = element.parentNode;
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
} from "../utils/theme-preferences.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) {
|
||||
for (const rule of cssRules) {
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
import {syncColorSchemePicker} from "./color-scheme-picker.js";
|
||||
import {
|
||||
PIXEL_STYLE_ATTRIBUTE,
|
||||
PREFERENCE_KEYS,
|
||||
getStoredPreference,
|
||||
setStoredPreference
|
||||
} from "../utils/theme-preferences.js";
|
||||
import {getStoredPreference, PREFERENCE_KEYS, setStoredPreference} from "../utils/theme-preferences.js";
|
||||
|
||||
function registerSearchButton() {
|
||||
const searchButton = document.querySelector('[data-header-action="open-search"]');
|
||||
@@ -22,7 +17,7 @@ function registerThemeToggle() {
|
||||
if (!toggle || typeof window.applyTheme !== "function") return;
|
||||
|
||||
toggle.addEventListener("click", function () {
|
||||
const currentScheme = document.documentElement.getAttribute("data-color-scheme");
|
||||
const currentScheme = document.documentElement.dataset.colorScheme;
|
||||
const nextMode = currentScheme === "dark" ? "light" : "dark";
|
||||
|
||||
window.applyTheme(nextMode, {clearColorScheme: true});
|
||||
@@ -38,10 +33,10 @@ function registerPixelToggle() {
|
||||
pixelToggle.addEventListener("click", function () {
|
||||
const storedStatus = getStoredPreference(PREFERENCE_KEYS.pixelStyle);
|
||||
const currentStatus = storedStatus
|
||||
|| document.documentElement.getAttribute(PIXEL_STYLE_ATTRIBUTE);
|
||||
|| document.documentElement.dataset.pixelStyle;
|
||||
const newStatus = currentStatus === "true" ? "false" : "true";
|
||||
|
||||
document.documentElement.setAttribute(PIXEL_STYLE_ATTRIBUTE, newStatus);
|
||||
document.documentElement.dataset.pixelStyle = newStatus;
|
||||
setStoredPreference(PREFERENCE_KEYS.pixelStyle, newStatus);
|
||||
window.TerminalTheme?.syncPixelFont?.();
|
||||
});
|
||||
|
||||
@@ -7,10 +7,10 @@
|
||||
width: calc(var(--size-theme-font) * 1.85);
|
||||
}
|
||||
|
||||
html[pixel_style='false'] .icon-regular {
|
||||
html[data-pixel-style='false'] .icon-regular {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
html[pixel_style='true'] .icon-pixel {
|
||||
html[data-pixel-style='true'] .icon-pixel {
|
||||
display: inline;
|
||||
}
|
||||
@@ -31,11 +31,11 @@ html {
|
||||
scrollbar-gutter: stable;
|
||||
}
|
||||
|
||||
html[pixel_style='true'] {
|
||||
html[data-pixel-style='true'] {
|
||||
--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;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
--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-title: var(--color-theme-accent);
|
||||
--color-theme-meta: var(--color-theme-foreground);
|
||||
@@ -122,7 +122,7 @@ $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 {
|
||||
--color-theme-#{$role}: #{$color};
|
||||
}
|
||||
|
||||
@@ -4,8 +4,6 @@ export const PREFERENCE_KEYS = Object.freeze({
|
||||
pixelStyle: "pixel-style"
|
||||
});
|
||||
|
||||
export const PIXEL_STYLE_ATTRIBUTE = "pixel_style";
|
||||
|
||||
export function getStoredPreference(key) {
|
||||
return window.TerminalTheme?.storage
|
||||
? window.TerminalTheme.storage.get(key)
|
||||
|
||||
Reference in New Issue
Block a user