243 lines
14 KiB
Python
243 lines
14 KiB
Python
# ⚠ 已作废(2026-11-03 antd 重写):本脚本量的是**手写规范**那套自造尺子
|
||
# (--ctl-h / --fs-* / 词汇表用词),尺子本身已随 theme/{metrics,palette,css-vars}.ts 删除。
|
||
# 现行两道闸:`node scripts/ui/classaudit.mjs`(源码用词)+
|
||
# `.venv/bin/python scripts/ui/visualcheck.py`(渲染结果)。口径见 docs/UI规范.md §15。
|
||
"""前端体检的度量口径(React 18 + Ant Design 5)。
|
||
|
||
只有这一份实现:audit.py 把这里的 JS 片段拼进 page.evaluate,
|
||
任何「怎么算错位、什么算控件」的定义都改在这里,不许散到脚本里。
|
||
|
||
三条口径(都是踩过坑之后定下来的,改之前先读):
|
||
|
||
1. **对齐按「视觉盒子」分组**。同一个盒子里的字段共用一条列轨道,才谈得上对齐;
|
||
带 padding 或左边框的子框(能力条、参考图框、折叠面板正文)是新盒子,
|
||
里面的字段自成一列,与外面的字段比左缘是误报(实测 AI 生成抽屉 9px)。
|
||
2. **行内工具条量中心线**,不量左缘:Ctlbar / CardActions / ChipBar 里字段横向排开,
|
||
x 天生不同,只有「标签的垂直中心 vs 它的控件的垂直中心」有意义。
|
||
3. **溢出豁免沿祖先链传递**:一个盒子只要位于 absolute / fixed 子树内,就不能再拿普通流里的
|
||
容器当它的边界(antd 抽屉 `getContainer={false}` + `rootStyle:absolute` 就踩在这里);
|
||
跨过 `position:fixed` 之后只与视口比。
|
||
4. **内容自定的高度不算控件档位**:textarea 按 rows 长高、滑杆轨道是 antd 的 12px、
|
||
标签/回显数字是文字。把它们算进档位,报表就永远挂着「第三档高度」。
|
||
剩下的自绘区尺寸(素材墙 tile、缩略图角标)走 audit.py 的 EXPECTED 白名单,
|
||
每条都要写理由,白名单只能是显式的,不能靠「看起来差不多」。
|
||
"""
|
||
|
||
W_DEFAULT = 1440
|
||
|
||
# 主题存在 localStorage 的 nex-the(stores/theme.ts 同一个键),直接种进 context 免地点开关
|
||
def storage(theme: str):
|
||
if theme not in ("light", "dark"):
|
||
return None
|
||
return {"origins": [{"origin": "http://localhost:5173",
|
||
"localStorage": [{"name": "nex-the", "value": theme}]}]}
|
||
|
||
|
||
# 能代表「控件外壳高度」的那层盒子
|
||
CTL = ('.ant-btn, .ant-select-selector, .ant-input, .ant-input-affix-wrapper, '
|
||
'.ant-input-number, .ant-picker, .ant-segmented, .ant-pagination, .nex-chip, .ant-switch, .nf-ctl > *')
|
||
|
||
# 控件的内层:天生比外壳矮 2px 或只有行盒高,不是外壳
|
||
INNER = ('.ant-select-selection-search-input, .ant-input-number-input, .ant-input-number-handler-wrap, '
|
||
'.ant-checkbox-inner, .ant-checkbox-input, .ant-radio-inner, .ant-segmented-item-label, '
|
||
'.ant-select-selection-item, .ant-select-selection-placeholder, .ant-btn-icon, .ant-tag, '
|
||
'.ant-badge, .ant-progress, .nex-chip-sm, .anticon, .ant-input-clear-icon, '
|
||
# 带前后缀的输入框:padding 在外层 affix wrapper 上,里层 input 只有行盒高(19px)
|
||
'.ant-input-affix-wrapper > .ant-input, .ant-input-prefix, .ant-input-suffix')
|
||
|
||
# 高度由内容决定的东西:不参与档位
|
||
NONCTL = ('textarea, .ant-input-textarea, .ant-mentions, .ant-slider, .nf-slider-value, '
|
||
'.nex-taggroup, .ant-progress, img, video, canvas, audio, .nex-chip-sm, '
|
||
# 满屏遮罩(ui/Overlay.tsx 的 Scrim):它是「点外面关掉」的那一面,
|
||
# 高度 = 视口高,与「控件有几档」无关;拿它去占白名单,还得证明它今天出场过
|
||
'.nex-scrim')
|
||
|
||
# 瞬时动画残影:它不是版面。量到 = 探针跑得比动画快,不是布局写坏了。
|
||
# antd 的点击水波纹 .ant-wave 挂在被点的按钮上、0.4s 就消失,播的时候尺度会胀出
|
||
# 宿主盒子,被 OVERFLOW 读成「横向溢出 55px」。真正的解法是 audit.py 的 settle()
|
||
# (等版面静止再量),这条只是兜底:谁手快点完就量,也不该因此去改布局。
|
||
TRANSIENT = '.ant-wave'
|
||
|
||
# 容器:栅格类与行内类分开(口径不同,见模块头)
|
||
SCOPE = '.nex-form, .nex-ctlbar-inner, .nex-card-actions, .ant-drawer-body, .ant-modal-body, .nex-chipbar'
|
||
INLINE = '.nex-ctlbar-inner, .nex-card-actions, .nex-chipbar'
|
||
FIELD = '.nex-field, .nex-field-block'
|
||
|
||
HELPERS = r"""
|
||
const INNER = `%INNER%`;
|
||
const CTL = `%CTL%`;
|
||
const NONCTL = `%NONCTL%`;
|
||
const isInner = (el) => el.matches(INNER) || !!el.closest(INNER);
|
||
const isNonCtl = (el) => el.matches(NONCTL) || !!el.closest(NONCTL);
|
||
const TRANSIENT = `%TRANSIENT%`;
|
||
const isTransient = (el) => el.matches(TRANSIENT) || !!el.closest(TRANSIENT);
|
||
const ANT = '.ant-select, .ant-input, .ant-input-affix-wrapper, .ant-input-number, .ant-picker, .ant-checkbox, .ant-radio, .ant-segmented, .ant-btn, .ant-switch, .ant-slider, .ant-mentions, .ant-upload, .ant-pagination, .ant-tabs, .ant-dropdown, .ant-modal, .ant-drawer, .ant-tour, .ant-popover';
|
||
const isAntd = (el) => !!el.closest(ANT) || el.classList.contains('ant-input') || el.classList.contains('ant-input-number-input');
|
||
const box = (el) => { const b = el.getBoundingClientRect(); return b.width ? {x: b.x, r: b.right, y: b.y, h: b.height, t: b.top, b: b.bottom} : null; };
|
||
/* 按 x 聚类(容差 24px):簇内极差才是「同一条轨道上错开了几 px」 */
|
||
const cluster = (vals) => {
|
||
const v = [...new Set(vals.map((n) => Math.round(n)))].sort((a, b) => a - b);
|
||
let worst = 0, at = null, i = 0;
|
||
while (i < v.length) {
|
||
let j = i;
|
||
while (j + 1 < v.length && v[j + 1] - v[i] <= 24) j++;
|
||
const sp = v[j] - v[i];
|
||
if (sp > worst) { worst = sp; at = [v[i], v[j]]; }
|
||
i = j + 1;
|
||
}
|
||
return worst ? {px: worst, at} : 0;
|
||
};
|
||
const clsOf = (el, n = 3) => (el.className || '').toString().trim().split(/\s+/).slice(0, n).join('.');
|
||
"""
|
||
|
||
NAKED = r"""(root) => {
|
||
const out = [];
|
||
document.querySelectorAll(root + ' input, ' + root + ' textarea, ' + root + ' select').forEach((el) => {
|
||
if (el.type === 'hidden' || el.type === 'file' || el.closest('.nex-file-input')) return;
|
||
if (isAntd(el)) return;
|
||
const b = el.getBoundingClientRect();
|
||
if (!b.width && !b.height) return;
|
||
out.push({tag: el.tagName.toLowerCase(), type: el.type || '', ph: (el.placeholder || '').slice(0, 14),
|
||
cls: clsOf(el, 2),
|
||
label: (el.closest('label')?.querySelector('.nf-label')?.textContent || '').trim().slice(0, 10)});
|
||
});
|
||
return out;
|
||
}"""
|
||
|
||
CONTAINER = r"""(root) => {
|
||
const out = [];
|
||
document.querySelectorAll(root + ' ' + '%SCOPE%').forEach((c) => {
|
||
const inline = c.matches(`%INLINE%`);
|
||
/* 字段按所在的视觉盒子分组:同一个盒子才共用列轨道;
|
||
有左内边距或左边框的祖先 = 换了个盒子,里面的字段自成一套对齐基准。 */
|
||
const boundary = (el) => {
|
||
let p = el.parentElement;
|
||
while (p && p !== c && c.contains(p)) {
|
||
const cs = getComputedStyle(p);
|
||
if (parseFloat(cs.paddingLeft) > 0 || parseFloat(cs.borderLeftWidth) > 0) return p;
|
||
p = p.parentElement;
|
||
}
|
||
return c;
|
||
};
|
||
const groups = new Map();
|
||
[...c.querySelectorAll(`%FIELD%`)].forEach((f) => {
|
||
if (f.querySelector(`%FIELD%`)) return; /* 只取叶子字段,容器不算 */
|
||
const k = boundary(f);
|
||
if (!groups.has(k)) groups.set(k, []);
|
||
groups.get(k).push(f);
|
||
});
|
||
const boxes = [...c.querySelectorAll(CTL)].filter((e) => !isInner(e) && !isNonCtl(e))
|
||
.map(box).filter((b) => b && b.h >= 18 && b.h <= 60 && b.x > 10);
|
||
const hs = [...new Set(boxes.map((b) => Math.round(b.h)))].sort((x, y) => x - y);
|
||
const key = (c.matches('.ant-drawer-body') ? 'drawer' : c.matches('.ant-modal-body') ? 'modal'
|
||
: c.matches('.nex-form') ? 'form' : c.matches('.nex-ctlbar-inner') ? 'ctlbar'
|
||
: c.matches('.nex-card-actions') ? 'cardactions' : 'chipbar');
|
||
groups.forEach((fields, bound) => {
|
||
const labels = fields.map((f) => box(f.querySelector('.nf-label'))).filter(Boolean);
|
||
const ctls = fields.map((f) => { const k = f.querySelector('.nf-ctl > *'); return k ? box(k) : null; }).filter(Boolean);
|
||
if (!labels.length && !ctls.length) return;
|
||
let dy = 0, dyAt = null;
|
||
if (inline) {
|
||
fields.forEach((f) => {
|
||
const l = f.querySelector('.nf-label'), k = f.querySelector('.nf-ctl > *');
|
||
if (!l || !k) return;
|
||
const lb = l.getBoundingClientRect(), kb = k.getBoundingClientRect();
|
||
if (!lb.width || !kb.width) return;
|
||
const d = Math.abs((lb.y + lb.height / 2) - (kb.y + kb.height / 2));
|
||
if (d > dy) { dy = Math.round(d * 10) / 10; dyAt = [l.textContent.trim().slice(0, 8), Math.round(lb.y + lb.height / 2), Math.round(kb.y + kb.height / 2)]; }
|
||
});
|
||
}
|
||
out.push({key, box: bound === c ? '' : clsOf(bound, 2),
|
||
y: Math.round(c.getBoundingClientRect().y), inline,
|
||
fields: fields.length, labels: labels.length,
|
||
lblX: inline ? 0 : cluster(labels.map((b) => b.x)), ctlX: inline ? 0 : cluster(ctls.map((b) => b.x)),
|
||
lblR: inline ? 0 : cluster(labels.map((b) => b.r)), ctlR: inline ? 0 : cluster(ctls.map((b) => b.r)),
|
||
dy: dy > 1 ? {px: dy, at: dyAt} : 0, h: hs.slice(0, 6), n: boxes.length});
|
||
});
|
||
});
|
||
return out;
|
||
}"""
|
||
|
||
OVERFLOW = r"""(root) => {
|
||
const rootEl = document.querySelector(root === '*' ? 'body' : root);
|
||
const out = [];
|
||
/* 自己会横滚的容器(TableBox、素材墙…)不算溢出:内容超宽是设计的一部分 */
|
||
const scrolls = (el) => { const s = getComputedStyle(el); return /auto|scroll/.test(s.overflowX) || /auto|scroll/.test(s.overflow); };
|
||
document.querySelectorAll(root === '*' ? '*' : root + ' *').forEach((el) => {
|
||
if (isInner(el) || isNonCtl(el) || isTransient(el)) return;
|
||
const cs = getComputedStyle(el);
|
||
if (cs.display === 'none' || cs.visibility === 'hidden' || +cs.opacity === 0) return;
|
||
const b = el.getBoundingClientRect();
|
||
if (!b.width || !b.height) return;
|
||
let p = el.parentElement, limit = null, name = '';
|
||
/* absolute / fixed 是「故意脱离这一层盒子」的:抽屉、角标、悬浮钮都靠它逃出裁剪。
|
||
只准和最近的定位祖先(没有就到视口)比,和普通的 flex 子容器比必误报。
|
||
关键是这个豁免要**沿祖先链往下传**:抽屉本体是 position:absolute 挂在页面里,
|
||
它的静态子节点(.ant-drawer-content)也跟着脱离了普通流——
|
||
不传就会拿页面某个 min-w-0 的 flex 子容器当边界,误报「抽屉溢出 177px」。 */
|
||
let escapes = cs.position === 'absolute' || cs.position === 'fixed';
|
||
while (p && p !== document.body) {
|
||
if (scrolls(p)) return;
|
||
/* display:contents 的祖先自己不长盒子(rect 是 0×0),拿它当边界会误报整屏溢出 */
|
||
const pcs = getComputedStyle(p);
|
||
if (pcs.display === 'contents') { p = p.parentElement; continue; }
|
||
const positioned = pcs.position !== 'static';
|
||
if (escapes && !positioned) { p = p.parentElement; continue; }
|
||
const pb = p.getBoundingClientRect();
|
||
if (b.right - pb.right > 1 || pb.x - b.x > 1) { limit = p; name = clsOf(p, 2) || p.tagName.toLowerCase(); break; }
|
||
/* 跨过 fixed 之后整棵子树只受视口裁剪;跨过 absolute 之后只受更上面的定位祖先裁剪 */
|
||
if (pcs.position === 'fixed') break;
|
||
if (pcs.position === 'absolute') escapes = true;
|
||
p = p.parentElement;
|
||
}
|
||
if (!limit) {
|
||
if (b.right - document.documentElement.clientWidth > 1) { out.push({w: Math.round(b.right - document.documentElement.clientWidth), tag: el.tagName.toLowerCase(), cls: clsOf(el), txt: (el.textContent || '').trim().slice(0, 16), in: 'viewport'}); return; }
|
||
if (el.scrollWidth - el.clientWidth > 1 && !scrolls(el) && el.children.length) { out.push({w: el.scrollWidth - el.clientWidth, tag: el.tagName.toLowerCase(), cls: clsOf(el), txt: (el.textContent || '').trim().slice(0, 16), in: 'self'}); return; }
|
||
return;
|
||
}
|
||
const over = Math.round(Math.max(b.right - limit.getBoundingClientRect().right, limit.getBoundingClientRect().x - b.x));
|
||
out.push({w: over, tag: el.tagName.toLowerCase(), cls: clsOf(el), txt: (el.textContent || '').trim().slice(0, 16), in: name.slice(0, 28)});
|
||
});
|
||
const seen = new Set();
|
||
return out.filter((x) => { const k = x.tag + x.cls + x.in; if (seen.has(k)) return false; seen.add(k); return true; })
|
||
.sort((a, b) => b.w - a.w).slice(0, 10);
|
||
}"""
|
||
|
||
TIERS = r"""(root) => {
|
||
const out = [];
|
||
document.querySelectorAll(root + ' ' + CTL + ', button').forEach((el) => {
|
||
if (isInner(el) || isNonCtl(el) || el.closest('.ant-checkbox')) return;
|
||
const b = el.getBoundingClientRect();
|
||
if (!b.height || !b.width || b.bottom < 0 || b.top > innerHeight) return;
|
||
out.push({h: Math.round(b.height), w: Math.round(b.width),
|
||
cls: el.tagName.toLowerCase() + (el.className ? '.' + clsOf(el, 4) : '')});
|
||
});
|
||
return out;
|
||
}"""
|
||
|
||
|
||
def js(body: str) -> str:
|
||
return (HELPERS.replace('%INNER%', INNER).replace('%CTL%', CTL).replace('%NONCTL%', NONCTL).replace('%TRANSIENT%', TRANSIENT)
|
||
+ body.replace('%SCOPE%', SCOPE).replace('%INLINE%', INLINE).replace('%FIELD%', FIELD))
|
||
|
||
|
||
def off(c, k):
|
||
v = c.get(k)
|
||
return v["px"] if isinstance(v, dict) else 0
|
||
|
||
|
||
def flagged(c, max_tiers=2):
|
||
return (off(c, "ctlX") > 1 or off(c, "lblX") > 1 or off(c, "ctlR") > 1
|
||
or off(c, "lblR") > 1 or off(c, "dy") > 0 or len(c["h"]) > max_tiers)
|
||
|
||
|
||
async def login(pg):
|
||
"""真登录一次(拿 localStorage 里的 token),后面所有路由都按登录态量。"""
|
||
await pg.goto("/login")
|
||
await pg.fill("#login-user", "admin")
|
||
await pg.fill("#login-pass", "admin1234")
|
||
async with pg.expect_response(lambda r: "/api/auth/login" in r.url, timeout=180000) as info:
|
||
await pg.click(".ant-btn-primary")
|
||
await info.value
|
||
await pg.wait_for_timeout(1000)
|