mirror of
https://github.com/Sevichecc/Urara-Blog.git
synced 2025-05-02 16:19:30 +08:00
219 lines
6.1 KiB
Text
219 lines
6.1 KiB
Text
import { matchName, iconDefaults } from '../icon/index.mjs';
|
|
|
|
const matchChar = /^[a-f0-9]+(-[a-f0-9]+)*$/;
|
|
function validateIconProps(item, fix) {
|
|
for (const key in item) {
|
|
const attr = key;
|
|
const value = item[attr];
|
|
const type = typeof value;
|
|
if (type === "undefined") {
|
|
delete item[attr];
|
|
continue;
|
|
}
|
|
switch (key) {
|
|
case "body":
|
|
case "parent":
|
|
if (type !== "string") {
|
|
return key;
|
|
}
|
|
break;
|
|
case "hFlip":
|
|
case "vFlip":
|
|
case "hidden":
|
|
if (type !== "boolean") {
|
|
if (fix) {
|
|
delete item[attr];
|
|
} else {
|
|
return key;
|
|
}
|
|
}
|
|
break;
|
|
case "width":
|
|
case "height":
|
|
case "left":
|
|
case "top":
|
|
case "rotate":
|
|
case "inlineHeight":
|
|
case "inlineTop":
|
|
case "verticalAlign":
|
|
if (type !== "number") {
|
|
if (fix) {
|
|
delete item[attr];
|
|
} else {
|
|
return key;
|
|
}
|
|
}
|
|
break;
|
|
default:
|
|
if (type === "object") {
|
|
if (fix) {
|
|
delete item[attr];
|
|
} else {
|
|
return key;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
function validateIconSet(obj, options) {
|
|
const fix = !!(options && options.fix);
|
|
if (typeof obj !== "object" || obj === null || typeof obj.icons !== "object" || !obj.icons) {
|
|
throw new Error("Bad icon set");
|
|
}
|
|
const data = obj;
|
|
if (options && typeof options.prefix === "string") {
|
|
data.prefix = options.prefix;
|
|
} else if (typeof data.prefix !== "string" || !data.prefix.match(matchName)) {
|
|
throw new Error("Invalid prefix");
|
|
}
|
|
if (options && typeof options.provider === "string") {
|
|
data.provider = options.provider;
|
|
} else if (data.provider !== void 0) {
|
|
const value = data.provider;
|
|
if (typeof value !== "string" || value !== "" && !value.match(matchName)) {
|
|
if (fix) {
|
|
delete data.provider;
|
|
} else {
|
|
throw new Error("Invalid provider");
|
|
}
|
|
}
|
|
}
|
|
const icons = data.icons;
|
|
Object.keys(icons).forEach((name) => {
|
|
if (!name.match(matchName)) {
|
|
if (fix) {
|
|
delete icons[name];
|
|
return;
|
|
}
|
|
throw new Error(`Invalid icon name: "${name}"`);
|
|
}
|
|
const item = icons[name];
|
|
if (typeof item !== "object" || item === null || typeof item.body !== "string") {
|
|
if (fix) {
|
|
delete icons[name];
|
|
return;
|
|
}
|
|
throw new Error(`Invalid icon: "${name}"`);
|
|
}
|
|
const key = typeof item.parent === "string" ? "parent" : validateIconProps(item, fix);
|
|
if (key !== null) {
|
|
if (fix) {
|
|
delete icons[name];
|
|
return;
|
|
}
|
|
throw new Error(`Invalid property "${key}" in icon "${name}"`);
|
|
}
|
|
});
|
|
if (data.not_found !== void 0 && !(data.not_found instanceof Array)) {
|
|
if (fix) {
|
|
delete data.not_found;
|
|
} else {
|
|
throw new Error("Invalid not_found list");
|
|
}
|
|
}
|
|
if (!Object.keys(data.icons).length && !(data.not_found && data.not_found.length)) {
|
|
throw new Error("Icon set is empty");
|
|
}
|
|
if (data.aliases !== void 0) {
|
|
if (typeof data.aliases !== "object" || data.aliases === null) {
|
|
if (fix) {
|
|
delete data.aliases;
|
|
} else {
|
|
throw new Error("Invalid aliases list");
|
|
}
|
|
}
|
|
}
|
|
if (typeof data.aliases === "object") {
|
|
let validateAlias = function(name, iteration) {
|
|
if (validatedAliases.has(name)) {
|
|
return !failedAliases.has(name);
|
|
}
|
|
const item = aliases[name];
|
|
if (iteration > 5 || typeof item !== "object" || item === null || typeof item.parent !== "string" || !name.match(matchName)) {
|
|
if (fix) {
|
|
delete aliases[name];
|
|
failedAliases.add(name);
|
|
return false;
|
|
}
|
|
throw new Error(`Invalid icon alias: "${name}"`);
|
|
}
|
|
const parent = item.parent;
|
|
if (data.icons[parent] === void 0) {
|
|
if (aliases[parent] === void 0 || !validateAlias(parent, iteration + 1)) {
|
|
if (fix) {
|
|
delete aliases[name];
|
|
failedAliases.add(name);
|
|
return false;
|
|
}
|
|
throw new Error(`Missing parent icon for alias "${name}`);
|
|
}
|
|
}
|
|
if (fix && item.body !== void 0) {
|
|
delete item.body;
|
|
}
|
|
const key = item.body !== void 0 ? "body" : validateIconProps(item, fix);
|
|
if (key !== null) {
|
|
if (fix) {
|
|
delete aliases[name];
|
|
failedAliases.add(name);
|
|
return false;
|
|
}
|
|
throw new Error(`Invalid property "${key}" in alias "${name}"`);
|
|
}
|
|
validatedAliases.add(name);
|
|
return true;
|
|
};
|
|
const aliases = data.aliases;
|
|
const validatedAliases = /* @__PURE__ */ new Set();
|
|
const failedAliases = /* @__PURE__ */ new Set();
|
|
Object.keys(aliases).forEach((name) => {
|
|
validateAlias(name, 0);
|
|
});
|
|
if (fix && !Object.keys(data.aliases).length) {
|
|
delete data.aliases;
|
|
}
|
|
}
|
|
Object.keys(iconDefaults).forEach((prop) => {
|
|
const expectedType = typeof iconDefaults[prop];
|
|
const actualType = typeof data[prop];
|
|
if (actualType !== "undefined" && actualType !== expectedType) {
|
|
throw new Error(`Invalid value type for "${prop}"`);
|
|
}
|
|
});
|
|
if (data.chars !== void 0) {
|
|
if (typeof data.chars !== "object" || data.chars === null) {
|
|
if (fix) {
|
|
delete data.chars;
|
|
} else {
|
|
throw new Error("Invalid characters map");
|
|
}
|
|
}
|
|
}
|
|
if (typeof data.chars === "object") {
|
|
const chars = data.chars;
|
|
Object.keys(chars).forEach((char) => {
|
|
if (!matchChar.exec(char) || typeof chars[char] !== "string") {
|
|
if (fix) {
|
|
delete chars[char];
|
|
return;
|
|
}
|
|
throw new Error(`Invalid character "${char}"`);
|
|
}
|
|
const target = chars[char];
|
|
if (!data.icons[target] && (!data.aliases || !data.aliases[target])) {
|
|
if (fix) {
|
|
delete chars[char];
|
|
return;
|
|
}
|
|
throw new Error(`Character "${char}" points to missing icon "${target}"`);
|
|
}
|
|
});
|
|
if (fix && !Object.keys(data.chars).length) {
|
|
delete data.chars;
|
|
}
|
|
}
|
|
return data;
|
|
}
|
|
|
|
export { matchChar, validateIconSet };
|