51 lignes
3.0 KiB
JavaScript
51 lignes
3.0 KiB
JavaScript
// Contrôle de format des maîtres : chaque SVG doit rester dans le sous-ensemble qui passe
|
|
// tel quel en PDF iOS et en VectorDrawable Android. Refuse filtre, masque, texte, image,
|
|
// clipPath, dégradé radial ou en objectBoundingBox, transformation autre que translate,
|
|
// rotate ou scale, et toute taille autre que 200 x 200.
|
|
// node illustrations-mobiles/controle.mjs (sortie 1 au moindre refus)
|
|
// Canari : un SVG fabriqué avec un filtre doit être refusé, sinon le contrôle lui-même est cassé.
|
|
import { readdirSync, readFileSync } from 'node:fs';
|
|
import { dirname, join } from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const ici = dirname(fileURLToPath(import.meta.url));
|
|
// Illustrations en 200, pictogrammes de la barre d'onglets en 24.
|
|
const DOSSIERS = [['sources', 200], ['onglets', 24]];
|
|
const ELEMENTS = new Set(['svg', 'defs', 'g', 'path', 'linearGradient', 'stop']);
|
|
const ATTRS = new Set(['xmlns', 'width', 'height', 'viewBox', 'd', 'fill', 'fill-opacity', 'stroke',
|
|
'stroke-width', 'stroke-linecap', 'stroke-linejoin', 'transform', 'id', 'gradientUnits',
|
|
'x1', 'y1', 'x2', 'y2', 'offset', 'stop-color', 'fill-rule']);
|
|
|
|
export function defauts(svg, taille = 200) {
|
|
const out = [];
|
|
for (const [, nom, attrs] of svg.matchAll(/<([A-Za-z][\w:-]*)([^>]*)>/g)) {
|
|
if (!ELEMENTS.has(nom)) { out.push(`élément <${nom}>`); continue; }
|
|
for (const [, a, v] of attrs.matchAll(/([\w:-]+)="([^"]*)"/g)) {
|
|
if (!ATTRS.has(a)) out.push(`attribut ${a} sur <${nom}>`);
|
|
if (a === 'transform' && !/^((translate|rotate|scale)\([^)]*\)\s*)+$/.test(v)) out.push(`transform ${v}`);
|
|
if (a === 'gradientUnits' && v !== 'userSpaceOnUse') out.push(`gradientUnits ${v}`);
|
|
if (a === 'fill' && v.startsWith('url(') && !/^url\(#g\d+\)$/.test(v)) out.push(`fill ${v}`);
|
|
}
|
|
if (nom === 'linearGradient' && !/gradientUnits="userSpaceOnUse"/.test(attrs)) out.push('dégradé sans userSpaceOnUse');
|
|
}
|
|
if (!new RegExp(`<svg[^>]*width="${taille}" height="${taille}" viewBox="0 0 ${taille} ${taille}"`).test(svg)) out.push(`gabarit autre que ${taille} x ${taille}`);
|
|
for (const [, r] of svg.matchAll(/fill-rule="([^"]*)"/g)) if (r !== 'evenodd') out.push(`fill-rule ${r}`);
|
|
if (/<style|style="/.test(svg)) out.push('style CSS');
|
|
return out;
|
|
}
|
|
|
|
const canari = '<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200"><defs><filter id="f"/></defs><path d="M0 0H1Z" fill="#000"/></svg>';
|
|
if (defauts(canari).length === 0) { console.error('canari accepté : le contrôle est cassé'); process.exit(2); }
|
|
|
|
let ko = 0, total = 0;
|
|
for (const [sous, taille] of DOSSIERS) {
|
|
const dir = join(ici, sous);
|
|
for (const f of readdirSync(dir).filter((x) => x.endsWith('.svg')).sort()) {
|
|
total++;
|
|
const d = defauts(readFileSync(join(dir, f), 'utf8'), taille);
|
|
if (d.length) { ko++; console.error(`${sous}/${f} : ${[...new Set(d)].join(', ')}`); }
|
|
}
|
|
}
|
|
console.log(`canari refusé (${defauts(canari)[0]}), ${total} maîtres contrôlés, ${ko} refusés`);
|
|
process.exit(ko ? 1 : 0);
|