Files
NapCatQQ/.pnpm-store/v3/files/dd/e760494b3359975336de875a215941b39a00d2ea6412a4f6b585d120d6660596c4642cc8574c341cfde37a54598e56d397289e2d1dcc38aeba97aef1a6dfbb
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

44 lines
1.2 KiB
Plaintext

import Attributor from './attributor.js';
function match(node: HTMLElement, prefix: string): string[] {
const className = node.getAttribute('class') || '';
return className
.split(/\s+/)
.filter((name) => name.indexOf(`${prefix}-`) === 0);
}
class ClassAttributor extends Attributor {
public static keys(node: HTMLElement): string[] {
return (node.getAttribute('class') || '')
.split(/\s+/)
.map((name) => name.split('-').slice(0, -1).join('-'));
}
public add(node: HTMLElement, value: any): boolean {
if (!this.canAdd(node, value)) {
return false;
}
this.remove(node);
node.classList.add(`${this.keyName}-${value}`);
return true;
}
public remove(node: HTMLElement): void {
const matches = match(node, this.keyName);
matches.forEach((name) => {
node.classList.remove(name);
});
if (node.classList.length === 0) {
node.removeAttribute('class');
}
}
public value(node: HTMLElement): any {
const result = match(node, this.keyName)[0] || '';
const value = result.slice(this.keyName.length + 1); // +1 for hyphen
return this.canAdd(node, value) ? value : '';
}
}
export default ClassAttributor;