@kaynooo/cli-page 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/cli.js +343 -15
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -15,11 +15,15 @@ bun install -g @kaynooo/cli-page
|
|
|
15
15
|
|
|
16
16
|
Targets are HTML files or URLs already served: `page` never starts or builds an app. The widths are 1440 and 400 unless `-w 1280,768,375` says otherwise. PNGs go next to a file, into `-o <dir>`, or under `~/.cache/kaynooo-page` for a URL.
|
|
17
17
|
|
|
18
|
+
A page behind a login takes `--cookie name=value`, `-H "Authorization: Bearer <token>"` (sent to the target's origin only, never to the third-party scripts and fonts it loads) or `--storage-state <file>`, a Playwright storage state with cookies and localStorage. Both commands take them; a malformed value is refused without being echoed.
|
|
19
|
+
|
|
18
20
|
What `check` reports:
|
|
19
21
|
|
|
22
|
+
- `redirect`: the page landed on another URL, a login page say, so every other line is about that page. `diff` says so on stderr instead.
|
|
20
23
|
- `overflow`: the page scrolls sideways; the up to three elements where it starts, with their right edge.
|
|
21
24
|
- `console`: console errors and uncaught exceptions. `request`: requests that failed or answered 4xx/5xx. Both once per target, at the first width they show at.
|
|
22
25
|
- `font`: text drawn in another font than its `font-family` asks for, naming the font Chromium drew it with (CDP `CSS.getPlatformFontsForNode`): a named family that is not available, or a generic one such as `system-ui` resolving to a monospace font.
|
|
26
|
+
- `contrast`: text under WCAG AA, 4.5:1 or 3:1 for large text, with both colours and the ratio (measured by `@kaynooo/audit-engine`); text over an image, a gradient or a filter cannot be measured and is left out.
|
|
23
27
|
- `tap-target`: a control under 44px wide or tall below 600px wide, under 24px otherwise; a link inside a sentence is exempt, a checkbox counts its label.
|
|
24
28
|
- `padding`: text touching an edge its box draws, a border side or a background unlike what is behind it.
|
|
25
29
|
- `image`: an image that finished loading with nothing to draw. `layout-shift`: a cumulative layout shift score above 0.1, naming the element that moved most.
|
package/dist/cli.js
CHANGED
|
@@ -352,6 +352,9 @@ function marked(mark, text, stream, detail, env = process6.env) {
|
|
|
352
352
|
}
|
|
353
353
|
return `${head} ${text}${detail === undefined ? "" : ` ${colors.dim(detail)}`}`;
|
|
354
354
|
}
|
|
355
|
+
function warn(text, detail) {
|
|
356
|
+
console.error(marked("warn", text, process6.stderr, detail));
|
|
357
|
+
}
|
|
355
358
|
function problem(text, detail) {
|
|
356
359
|
console.error(marked("problem", text, process6.stderr, detail));
|
|
357
360
|
}
|
|
@@ -1436,6 +1439,273 @@ var init_src = __esm(() => {
|
|
|
1436
1439
|
init_workspace_root();
|
|
1437
1440
|
});
|
|
1438
1441
|
|
|
1442
|
+
// ../../node_modules/.bun/@kaynooo+audit-engine@0.1.1/node_modules/@kaynooo/audit-engine/src/inpage.ts
|
|
1443
|
+
function install() {
|
|
1444
|
+
if (window.__audit)
|
|
1445
|
+
return;
|
|
1446
|
+
window.__audit = {
|
|
1447
|
+
cssPath(el) {
|
|
1448
|
+
const parts = [];
|
|
1449
|
+
for (let node = el;node && node !== document.documentElement; node = node.parentElement) {
|
|
1450
|
+
if (node.id && document.querySelectorAll(`#${CSS.escape(node.id)}`).length === 1) {
|
|
1451
|
+
parts.unshift(`#${CSS.escape(node.id)}`);
|
|
1452
|
+
break;
|
|
1453
|
+
}
|
|
1454
|
+
const tag = node.localName;
|
|
1455
|
+
const siblings = node.parentElement ? [...node.parentElement.children].filter((child) => child.localName === tag) : [];
|
|
1456
|
+
parts.unshift(siblings.length > 1 ? `${tag}:nth-of-type(${siblings.indexOf(node) + 1})` : tag);
|
|
1457
|
+
}
|
|
1458
|
+
return parts.join(" > ");
|
|
1459
|
+
},
|
|
1460
|
+
snippet(el) {
|
|
1461
|
+
return (/^<[^>]*>/.exec(el.outerHTML)?.[0] ?? `<${el.localName}>`).slice(0, 200);
|
|
1462
|
+
},
|
|
1463
|
+
ownText(el) {
|
|
1464
|
+
let text = "";
|
|
1465
|
+
for (const node of el.childNodes) {
|
|
1466
|
+
if (node.nodeType === Node.TEXT_NODE)
|
|
1467
|
+
text += node.textContent ?? "";
|
|
1468
|
+
}
|
|
1469
|
+
return text.replace(/\s+/g, " ").trim();
|
|
1470
|
+
},
|
|
1471
|
+
visible(el) {
|
|
1472
|
+
const style = getComputedStyle(el);
|
|
1473
|
+
const rect = el.getBoundingClientRect();
|
|
1474
|
+
return style.display !== "none" && style.visibility !== "hidden" && Number(style.opacity) > 0 && rect.width > 0 && rect.height > 0;
|
|
1475
|
+
}
|
|
1476
|
+
};
|
|
1477
|
+
}
|
|
1478
|
+
async function installHelpers(page) {
|
|
1479
|
+
await page.evaluate(install);
|
|
1480
|
+
}
|
|
1481
|
+
|
|
1482
|
+
// ../../node_modules/.bun/@kaynooo+audit-engine@0.1.1/node_modules/@kaynooo/audit-engine/src/contrast.ts
|
|
1483
|
+
function measure(cap) {
|
|
1484
|
+
const helpers = window.__audit;
|
|
1485
|
+
const canvas = document.createElement("canvas");
|
|
1486
|
+
canvas.width = 1;
|
|
1487
|
+
canvas.height = 1;
|
|
1488
|
+
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
1489
|
+
const viaCanvas = (value) => {
|
|
1490
|
+
if (!ctx)
|
|
1491
|
+
return null;
|
|
1492
|
+
ctx.fillStyle = "#000";
|
|
1493
|
+
ctx.fillStyle = value;
|
|
1494
|
+
const onBlack = ctx.fillStyle;
|
|
1495
|
+
ctx.fillStyle = "#fff";
|
|
1496
|
+
ctx.fillStyle = value;
|
|
1497
|
+
if (ctx.fillStyle !== onBlack)
|
|
1498
|
+
return null;
|
|
1499
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
1500
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
1501
|
+
const [r = 0, g = 0, b = 0, a = 0] = ctx.getImageData(0, 0, 1, 1).data;
|
|
1502
|
+
return [r, g, b, a / 255];
|
|
1503
|
+
};
|
|
1504
|
+
const parse = (value) => {
|
|
1505
|
+
if (!value || value === "transparent")
|
|
1506
|
+
return [0, 0, 0, 0];
|
|
1507
|
+
const match = /^rgba?\(([^)]+)\)$/.exec(value);
|
|
1508
|
+
if (match) {
|
|
1509
|
+
const parts = (match[1] ?? "").split(/[\s,/]+/).filter(Boolean).map((part) => Number.parseFloat(part));
|
|
1510
|
+
return [parts[0] ?? 0, parts[1] ?? 0, parts[2] ?? 0, parts[3] ?? 1];
|
|
1511
|
+
}
|
|
1512
|
+
return viaCanvas(value);
|
|
1513
|
+
};
|
|
1514
|
+
const over = (top, bottom) => {
|
|
1515
|
+
const a = top[3];
|
|
1516
|
+
return [top[0] * a + bottom[0] * (1 - a), top[1] * a + bottom[1] * (1 - a), top[2] * a + bottom[2] * (1 - a), 1];
|
|
1517
|
+
};
|
|
1518
|
+
const luminance = (rgb) => {
|
|
1519
|
+
const channel = (value) => {
|
|
1520
|
+
const c = value / 255;
|
|
1521
|
+
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
|
|
1522
|
+
};
|
|
1523
|
+
return 0.2126 * channel(rgb[0]) + 0.7152 * channel(rgb[1]) + 0.0722 * channel(rgb[2]);
|
|
1524
|
+
};
|
|
1525
|
+
const ratio = (a, b) => {
|
|
1526
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
1527
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
1528
|
+
};
|
|
1529
|
+
const css = (rgb) => `rgb(${Math.round(rgb[0])}, ${Math.round(rgb[1])}, ${Math.round(rgb[2])})`;
|
|
1530
|
+
const MEDIA = new Set(["IMG", "PICTURE", "VIDEO", "CANVAS", "IFRAME", "OBJECT", "EMBED", "svg"]);
|
|
1531
|
+
const isMedia = (node) => MEDIA.has(node.tagName);
|
|
1532
|
+
const solid = (style) => {
|
|
1533
|
+
if (style.backgroundImage && style.backgroundImage !== "none")
|
|
1534
|
+
return "background-image";
|
|
1535
|
+
if (style.filter && style.filter !== "none")
|
|
1536
|
+
return "filter";
|
|
1537
|
+
if (style.mixBlendMode && style.mixBlendMode !== "normal")
|
|
1538
|
+
return "blend-mode";
|
|
1539
|
+
return Number.parseFloat(style.opacity) < 1 ? "opacity" : null;
|
|
1540
|
+
};
|
|
1541
|
+
const pseudoBackground = (node) => {
|
|
1542
|
+
for (const which of ["::before", "::after"]) {
|
|
1543
|
+
const style = getComputedStyle(node, which);
|
|
1544
|
+
if (!style.content || style.content === "none" || style.position !== "absolute" && style.position !== "fixed")
|
|
1545
|
+
continue;
|
|
1546
|
+
if (!(Number.parseFloat(style.zIndex) < 0))
|
|
1547
|
+
continue;
|
|
1548
|
+
if (style.top !== "0px" || style.right !== "0px" || style.bottom !== "0px" || style.left !== "0px")
|
|
1549
|
+
continue;
|
|
1550
|
+
if (style.backgroundImage && style.backgroundImage !== "none")
|
|
1551
|
+
return "background-image";
|
|
1552
|
+
const color = parse(style.backgroundColor);
|
|
1553
|
+
if (color && color[3] > 0)
|
|
1554
|
+
return color;
|
|
1555
|
+
}
|
|
1556
|
+
return null;
|
|
1557
|
+
};
|
|
1558
|
+
const ground = (() => {
|
|
1559
|
+
const probe = document.createElement("div");
|
|
1560
|
+
probe.style.backgroundColor = "Canvas";
|
|
1561
|
+
document.documentElement.append(probe);
|
|
1562
|
+
const color = parse(getComputedStyle(probe).backgroundColor);
|
|
1563
|
+
probe.remove();
|
|
1564
|
+
return color && color[3] === 1 ? color : [255, 255, 255, 1];
|
|
1565
|
+
})();
|
|
1566
|
+
const compose = (stack) => {
|
|
1567
|
+
const layers = [];
|
|
1568
|
+
for (const node of stack) {
|
|
1569
|
+
if (isMedia(node))
|
|
1570
|
+
return "media";
|
|
1571
|
+
const style = getComputedStyle(node);
|
|
1572
|
+
const reason = solid(style);
|
|
1573
|
+
if (reason)
|
|
1574
|
+
return reason;
|
|
1575
|
+
const pseudo = pseudoBackground(node);
|
|
1576
|
+
if (typeof pseudo === "string")
|
|
1577
|
+
return pseudo;
|
|
1578
|
+
if (pseudo) {
|
|
1579
|
+
layers.push(pseudo);
|
|
1580
|
+
if (pseudo[3] === 1)
|
|
1581
|
+
break;
|
|
1582
|
+
}
|
|
1583
|
+
const color = parse(style.backgroundColor);
|
|
1584
|
+
if (color && color[3] > 0) {
|
|
1585
|
+
layers.push(color);
|
|
1586
|
+
if (color[3] === 1)
|
|
1587
|
+
break;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
let base = ground;
|
|
1591
|
+
for (const layer of layers.toReversed())
|
|
1592
|
+
base = over(layer, base);
|
|
1593
|
+
return base;
|
|
1594
|
+
};
|
|
1595
|
+
const textRect = (el) => {
|
|
1596
|
+
const range = document.createRange();
|
|
1597
|
+
for (const child of el.childNodes) {
|
|
1598
|
+
if (child.nodeType !== Node.TEXT_NODE || (child.textContent ?? "").trim() === "")
|
|
1599
|
+
continue;
|
|
1600
|
+
range.selectNodeContents(child);
|
|
1601
|
+
for (const rect of range.getClientRects()) {
|
|
1602
|
+
if (rect.width > 0 && rect.height > 0)
|
|
1603
|
+
return rect;
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
return null;
|
|
1607
|
+
};
|
|
1608
|
+
const background = (el) => {
|
|
1609
|
+
let rect = textRect(el);
|
|
1610
|
+
if (!rect)
|
|
1611
|
+
return "offscreen";
|
|
1612
|
+
if (rect.top < 0 || rect.bottom > innerHeight) {
|
|
1613
|
+
scrollTo({ top: scrollY + rect.top + rect.height / 2 - innerHeight / 2, left: scrollX, behavior: "instant" });
|
|
1614
|
+
rect = textRect(el);
|
|
1615
|
+
}
|
|
1616
|
+
if (!rect)
|
|
1617
|
+
return "offscreen";
|
|
1618
|
+
const x = rect.left + rect.width / 2;
|
|
1619
|
+
const y = rect.top + rect.height / 2;
|
|
1620
|
+
if (x < 0 || y < 0 || x >= innerWidth || y >= innerHeight)
|
|
1621
|
+
return "offscreen";
|
|
1622
|
+
const stack = document.elementsFromPoint(x, y);
|
|
1623
|
+
const index = stack.indexOf(el);
|
|
1624
|
+
if (index === -1) {
|
|
1625
|
+
if (getComputedStyle(el).pointerEvents !== "none")
|
|
1626
|
+
return "covered";
|
|
1627
|
+
const chain = [];
|
|
1628
|
+
for (let node = el;node; node = node.parentElement)
|
|
1629
|
+
chain.push(node);
|
|
1630
|
+
return compose(chain);
|
|
1631
|
+
}
|
|
1632
|
+
for (const front of stack.slice(0, index)) {
|
|
1633
|
+
if (el.contains(front) || front.contains(el))
|
|
1634
|
+
continue;
|
|
1635
|
+
const style = getComputedStyle(front);
|
|
1636
|
+
if (isMedia(front) || style.backgroundImage && style.backgroundImage !== "none" || (parse(style.backgroundColor)?.[3] ?? 0) > 0)
|
|
1637
|
+
return "covered";
|
|
1638
|
+
}
|
|
1639
|
+
return compose(stack.slice(index));
|
|
1640
|
+
};
|
|
1641
|
+
const inactive = (el) => el.closest(':disabled, [aria-disabled="true"], [inert]') !== null;
|
|
1642
|
+
const startX = scrollX;
|
|
1643
|
+
const startY = scrollY;
|
|
1644
|
+
const report = { checked: 0, failures: [], undecidable: [], truncated: false };
|
|
1645
|
+
try {
|
|
1646
|
+
for (const el of document.body.querySelectorAll("*"))
|
|
1647
|
+
check(el);
|
|
1648
|
+
} finally {
|
|
1649
|
+
scrollTo({ top: startY, left: startX, behavior: "instant" });
|
|
1650
|
+
}
|
|
1651
|
+
return report;
|
|
1652
|
+
function undecided(item) {
|
|
1653
|
+
if (report.undecidable.length < cap)
|
|
1654
|
+
report.undecidable.push(item);
|
|
1655
|
+
else
|
|
1656
|
+
report.truncated = true;
|
|
1657
|
+
}
|
|
1658
|
+
function check(el) {
|
|
1659
|
+
const text = helpers.ownText(el);
|
|
1660
|
+
if (!text || !helpers.visible(el) || inactive(el))
|
|
1661
|
+
return;
|
|
1662
|
+
const box = el.getBoundingClientRect();
|
|
1663
|
+
if (box.width <= 1 || box.height <= 1)
|
|
1664
|
+
return;
|
|
1665
|
+
const style = getComputedStyle(el);
|
|
1666
|
+
const fill = style.webkitTextFillColor && style.webkitTextFillColor !== style.color ? style.webkitTextFillColor : style.color;
|
|
1667
|
+
const fg = parse(fill);
|
|
1668
|
+
if (!fg)
|
|
1669
|
+
return;
|
|
1670
|
+
const base = { selector: helpers.cssPath(el), snippet: helpers.snippet(el), text: text.slice(0, 120) };
|
|
1671
|
+
if (el instanceof SVGElement) {
|
|
1672
|
+
undecided({ ...base, fg: style.fill, reason: "svg" });
|
|
1673
|
+
return;
|
|
1674
|
+
}
|
|
1675
|
+
if (fg[3] === 0) {
|
|
1676
|
+
undecided({ ...base, fg: fill, reason: "gradient-text" });
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
report.checked++;
|
|
1680
|
+
const bg = background(el);
|
|
1681
|
+
if (typeof bg === "string") {
|
|
1682
|
+
undecided({ ...base, fg: css(fg), reason: bg });
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
const color = fg[3] < 1 ? over(fg, bg) : fg;
|
|
1686
|
+
const size = Number.parseFloat(style.fontSize) || 16;
|
|
1687
|
+
const large = size >= 24 || Number.parseInt(style.fontWeight, 10) >= 700 && size >= 18.66;
|
|
1688
|
+
const required = large ? 3 : 4.5;
|
|
1689
|
+
const measured = ratio(color, bg);
|
|
1690
|
+
if (measured >= required)
|
|
1691
|
+
return;
|
|
1692
|
+
if (style.textShadow && style.textShadow !== "none") {
|
|
1693
|
+
undecided({ ...base, fg: css(color), reason: "text-shadow" });
|
|
1694
|
+
return;
|
|
1695
|
+
}
|
|
1696
|
+
if (report.failures.length >= cap) {
|
|
1697
|
+
report.truncated = true;
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
report.failures.push({ ...base, fg: css(color), bg: css(bg), ratio: Math.floor(measured * 100) / 100, required, large });
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
async function measureContrast(page, options = {}) {
|
|
1704
|
+
await installHelpers(page);
|
|
1705
|
+
return page.evaluate(measure, options.cap ?? 200);
|
|
1706
|
+
}
|
|
1707
|
+
var init_contrast = () => {};
|
|
1708
|
+
|
|
1439
1709
|
// src/inpage.ts
|
|
1440
1710
|
async function source(file) {
|
|
1441
1711
|
let code = sources.get(file);
|
|
@@ -1507,8 +1777,35 @@ function targetOf(given, out) {
|
|
|
1507
1777
|
const name = basename(path).replace(HTML_EXT, "");
|
|
1508
1778
|
return { label: given, url: pathToFileURL(path).href, stem: join7(out === undefined ? dirname4(path) : resolve4(out), name) };
|
|
1509
1779
|
}
|
|
1510
|
-
|
|
1511
|
-
const
|
|
1780
|
+
function sessionOf(values) {
|
|
1781
|
+
const cookies = (values.cookie ?? []).map((cookie) => {
|
|
1782
|
+
const at = cookie.indexOf("=");
|
|
1783
|
+
if (at < 1)
|
|
1784
|
+
badUsage("--cookie takes name=value");
|
|
1785
|
+
return { name: cookie.slice(0, at), value: cookie.slice(at + 1) };
|
|
1786
|
+
});
|
|
1787
|
+
const headers = {};
|
|
1788
|
+
for (const header of values.header ?? []) {
|
|
1789
|
+
if (!HEADER_LINE.test(header))
|
|
1790
|
+
badUsage('-H takes "Name: value"');
|
|
1791
|
+
const at = header.indexOf(":");
|
|
1792
|
+
headers[header.slice(0, at).trim().toLowerCase()] = header.slice(at + 1).trim();
|
|
1793
|
+
}
|
|
1794
|
+
const storageState = values["storage-state"];
|
|
1795
|
+
if (storageState !== undefined && !existsSync3(storageState))
|
|
1796
|
+
fail(`--storage-state ${storageState} does not exist`);
|
|
1797
|
+
return { cookies, headers, ...storageState === undefined ? {} : { storageState } };
|
|
1798
|
+
}
|
|
1799
|
+
async function open(browser, url, width, session, prepare) {
|
|
1800
|
+
const context = await browser.newContext({ viewport: { width, height: 900 }, deviceScaleFactor: 1, hasTouch: width < PHONE_WIDTH, isMobile: false, ...session.storageState === undefined ? {} : { storageState: session.storageState } });
|
|
1801
|
+
if (HTTP_URL.test(url)) {
|
|
1802
|
+
if (session.cookies.length > 0)
|
|
1803
|
+
await context.addCookies(session.cookies.map((cookie) => ({ ...cookie, url })));
|
|
1804
|
+
if (Object.keys(session.headers).length > 0) {
|
|
1805
|
+
const { origin } = new URL(url);
|
|
1806
|
+
await context.route((requested) => requested.origin === origin, async (route) => route.continue({ headers: { ...route.request().headers(), ...session.headers } }));
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1512
1809
|
const page = await context.newPage();
|
|
1513
1810
|
await prepare?.(page);
|
|
1514
1811
|
await page.goto(url, { waitUntil: "load", timeout: 30000 });
|
|
@@ -1516,7 +1813,15 @@ async function open(browser, url, width, prepare) {
|
|
|
1516
1813
|
await page.waitForLoadState("networkidle", { timeout: 5000 });
|
|
1517
1814
|
} catch {}
|
|
1518
1815
|
await inPage(page, "measure", "fontsReady");
|
|
1519
|
-
return { context, page };
|
|
1816
|
+
return { context, page, landed: page.url() };
|
|
1817
|
+
}
|
|
1818
|
+
function redirectedTo(asked, landed) {
|
|
1819
|
+
const bare = (url) => {
|
|
1820
|
+
const parsed = new URL(url);
|
|
1821
|
+
parsed.hash = "";
|
|
1822
|
+
return parsed.href;
|
|
1823
|
+
};
|
|
1824
|
+
return bare(asked) === bare(landed) ? undefined : landed;
|
|
1520
1825
|
}
|
|
1521
1826
|
async function shoot(page, path) {
|
|
1522
1827
|
const options = { ...path === undefined ? {} : { path }, fullPage: true, animations: "disabled", caret: "hide" };
|
|
@@ -1526,15 +1831,21 @@ async function shoot(page, path) {
|
|
|
1526
1831
|
return page.screenshot(options);
|
|
1527
1832
|
}
|
|
1528
1833
|
}
|
|
1529
|
-
var HTTP_URL, HTML_EXT, UNSAFE, EDGE_DASHES, PHONE_WIDTH = 600, SYSTEM_CHROMES;
|
|
1834
|
+
var HTTP_URL, HEADER_LINE, HTML_EXT, UNSAFE, EDGE_DASHES, PHONE_WIDTH = 600, SYSTEM_CHROMES, SESSION_OPTIONS;
|
|
1530
1835
|
var init_browser = __esm(() => {
|
|
1531
1836
|
init_src();
|
|
1532
1837
|
init_inpage();
|
|
1533
1838
|
HTTP_URL = /^https?:\/\//;
|
|
1839
|
+
HEADER_LINE = /^[\w!#$%&'*+.^`|~-]+:/;
|
|
1534
1840
|
HTML_EXT = /\.html?$/;
|
|
1535
1841
|
UNSAFE = /[^\w.-]+/g;
|
|
1536
1842
|
EDGE_DASHES = /^-+|-+$/g;
|
|
1537
1843
|
SYSTEM_CHROMES = ["/usr/bin/chromium", "/usr/bin/chromium-browser", "/usr/bin/google-chrome-stable", "/usr/bin/google-chrome", "/usr/bin/brave", "/usr/bin/brave-browser"];
|
|
1844
|
+
SESSION_OPTIONS = {
|
|
1845
|
+
cookie: { type: "string", multiple: true, description: "name=value, a cookie for a URL target's site, repeatable" },
|
|
1846
|
+
header: { type: "string", short: "H", multiple: true, description: `"Name: value", a header sent with every request to a URL target's origin and nowhere else, repeatable` },
|
|
1847
|
+
"storage-state": { type: "string", description: "a Playwright storage state file (cookies and localStorage) every page starts from" }
|
|
1848
|
+
};
|
|
1538
1849
|
});
|
|
1539
1850
|
|
|
1540
1851
|
// src/fonts.ts
|
|
@@ -1666,11 +1977,15 @@ function listen(page, into) {
|
|
|
1666
1977
|
into.push({ kind: "request", message: `${response.request().method()} ${shortUrl(response.url())} answered ${response.status()}` });
|
|
1667
1978
|
});
|
|
1668
1979
|
}
|
|
1980
|
+
async function contrastFindings(page) {
|
|
1981
|
+
const { failures } = await measureContrast(page);
|
|
1982
|
+
return failures.map(({ selector, fg, bg, ratio, required }) => ({ kind: "contrast", selector, message: `${fg} on ${bg}, ${ratio.toFixed(2)}:1, needs ${required}:1` }));
|
|
1983
|
+
}
|
|
1669
1984
|
async function check(args) {
|
|
1670
1985
|
const { values, positionals } = parse(args, USAGE, OPTIONS, {
|
|
1671
1986
|
positionals: ["<file|url>..."],
|
|
1672
1987
|
exits: EXITS,
|
|
1673
|
-
examples: [["page check mockup.html", "mockup-1440.png and mockup-400.png beside it, then the defects"], "page check http://localhost:5173/settings -w 1280,768,375 -o shots", "page check index.html --skip font,padding --json"]
|
|
1988
|
+
examples: [["page check mockup.html", "mockup-1440.png and mockup-400.png beside it, then the defects"], ["page check http://localhost:5173/settings --cookie session=<value>", "a page behind a login"], "page check http://localhost:5173/settings -w 1280,768,375 -o shots", "page check index.html --skip font,padding --json"]
|
|
1674
1989
|
});
|
|
1675
1990
|
const widths = widthsOf(values.widths);
|
|
1676
1991
|
const skipped = new Set(values.skip?.split(",").map((kind) => kind.trim()) ?? []);
|
|
@@ -1678,6 +1993,7 @@ async function check(args) {
|
|
|
1678
1993
|
if (unknown !== undefined)
|
|
1679
1994
|
badUsage(`--skip takes ${KINDS.join(", ")}, not ${unknown}`);
|
|
1680
1995
|
const targets = positionals.map((given) => targetOf(given, values.out));
|
|
1996
|
+
const session = sessionOf(values);
|
|
1681
1997
|
const shots = [];
|
|
1682
1998
|
const findings = [];
|
|
1683
1999
|
const failed = [];
|
|
@@ -1690,7 +2006,7 @@ async function check(args) {
|
|
|
1690
2006
|
const heard = [];
|
|
1691
2007
|
let loaded;
|
|
1692
2008
|
try {
|
|
1693
|
-
loaded = await open(browser, target.url, width, async (page) => {
|
|
2009
|
+
loaded = await open(browser, target.url, width, session, async (page) => {
|
|
1694
2010
|
listen(page, heard);
|
|
1695
2011
|
await beforeLoad(page, "measure", "watchShifts");
|
|
1696
2012
|
});
|
|
@@ -1701,14 +2017,17 @@ async function check(args) {
|
|
|
1701
2017
|
`)) : message });
|
|
1702
2018
|
break;
|
|
1703
2019
|
}
|
|
1704
|
-
const { context, page } = loaded;
|
|
2020
|
+
const { context, page, landed } = loaded;
|
|
2021
|
+
const redirect = redirectedTo(target.url, landed);
|
|
2022
|
+
if (redirect !== undefined)
|
|
2023
|
+
heard.unshift({ kind: "redirect", message: `landed on ${shortUrl(redirect)}` });
|
|
1705
2024
|
const file = `${target.stem}-${width}.png`;
|
|
1706
2025
|
mkdirSync2(dirname5(file), { recursive: true });
|
|
1707
2026
|
await shoot(page, file);
|
|
1708
2027
|
shots.push({ target: target.label, width, path: file });
|
|
1709
2028
|
if (!values.json)
|
|
1710
2029
|
console.log(path(file));
|
|
1711
|
-
const found = [...heard, ...await inPage(page, "measure", "measureDom", width < PHONE_WIDTH), ...skipped.has("font") ? [] : await fontFindings(page)];
|
|
2030
|
+
const found = [...heard, ...await inPage(page, "measure", "measureDom", width < PHONE_WIDTH), ...skipped.has("font") ? [] : await fontFindings(page), ...skipped.has("contrast") ? [] : await contrastFindings(page)];
|
|
1712
2031
|
await context.close();
|
|
1713
2032
|
for (const finding of found) {
|
|
1714
2033
|
const key = `${finding.kind}|${finding.selector ?? ""}|${finding.message}`;
|
|
@@ -1737,25 +2056,28 @@ async function check(args) {
|
|
|
1737
2056
|
}
|
|
1738
2057
|
var KINDS, ONCE, URL_CAP = 160, EXITS, USAGE, OPTIONS;
|
|
1739
2058
|
var init_check = __esm(() => {
|
|
2059
|
+
init_contrast();
|
|
1740
2060
|
init_src();
|
|
1741
2061
|
init_browser();
|
|
1742
2062
|
init_fonts();
|
|
1743
2063
|
init_inpage();
|
|
1744
2064
|
init_report2();
|
|
1745
|
-
KINDS = ["overflow", "console", "request", "font", "tap-target", "padding", "image", "layout-shift"];
|
|
1746
|
-
ONCE = new Set(["console", "request", "font"]);
|
|
2065
|
+
KINDS = ["redirect", "overflow", "console", "request", "font", "contrast", "tap-target", "padding", "image", "layout-shift"];
|
|
2066
|
+
ONCE = new Set(["redirect", "console", "request", "font", "contrast"]);
|
|
1747
2067
|
EXITS = [[0, "nothing found"], [1, "a defect was found"], [2, "a usage error, or a target that would not load"]];
|
|
1748
2068
|
USAGE = `page check <file|url>...
|
|
1749
2069
|
|
|
1750
2070
|
Opens each HTML file or URL in a local Chromium at every width, writes a full-page PNG per width (next to a file as <name>-<width>.png, into --out, or the cache folder for a URL) and prints its path, then one line per defect: <target> <width>px <kind> <selector>: <measure>.
|
|
1751
|
-
Kinds: overflow (the page scrolls sideways; the elements where it starts), console (errors and uncaught exceptions), request (failed, or answered 4xx/5xx), font (text drawn in another font than its stack asks for, naming the one drawn), tap-target (under 44px below ${PHONE_WIDTH}px wide, under 24px otherwise; a link inside a sentence is exempt), padding (text touching the border or background edge of its box), image (did not load), layout-shift (a score above 0.1).
|
|
1752
|
-
It never starts or builds an app: give it a file or a URL already served.
|
|
2071
|
+
Kinds: redirect (the page landed elsewhere, a login page say, so what follows is about that page), overflow (the page scrolls sideways; the elements where it starts), console (errors and uncaught exceptions), request (failed, or answered 4xx/5xx), font (text drawn in another font than its stack asks for, naming the one drawn), contrast (text under WCAG AA, 4.5:1 or 3:1 for large text; text over an image, a gradient or a filter is left out), tap-target (under 44px below ${PHONE_WIDTH}px wide, under 24px otherwise; a link inside a sentence is exempt), padding (text touching the border or background edge of its box), image (did not load), layout-shift (a score above 0.1).
|
|
2072
|
+
It never starts or builds an app: give it a file or a URL already served. A page behind a login takes --cookie, -H or --storage-state; a header goes to the target's origin alone.
|
|
2073
|
+
Chromium comes from PAGE_CHROMIUM, ~/.cache/ms-playwright or /usr/bin, never downloaded.
|
|
1753
2074
|
|
|
1754
2075
|
Exits 0 when nothing is found, 1 when something is, 2 on a usage error or when a target would not load.`;
|
|
1755
2076
|
OPTIONS = {
|
|
1756
2077
|
widths: { type: "string", short: "w", description: "viewport widths, comma separated (1440,400)" },
|
|
1757
2078
|
out: { type: "string", short: "o", description: "folder the PNGs go to instead of next to each file" },
|
|
1758
2079
|
skip: { type: "string", description: `kinds left out, comma separated: ${KINDS.join(", ")}` },
|
|
2080
|
+
...SESSION_OPTIONS,
|
|
1759
2081
|
json: jsonOption("{shots: [{target, width, path}], findings: [{target, width, kind, selector, message}], failed: [{target, error}]}")
|
|
1760
2082
|
};
|
|
1761
2083
|
});
|
|
@@ -1768,8 +2090,11 @@ __export(exports_diff, {
|
|
|
1768
2090
|
import { mkdirSync as mkdirSync3 } from "fs";
|
|
1769
2091
|
import { dirname as dirname6 } from "path";
|
|
1770
2092
|
import process20 from "process";
|
|
1771
|
-
async function capture(browser,
|
|
1772
|
-
const { context, page } = await open(browser, url, width);
|
|
2093
|
+
async function capture(browser, target, width, session) {
|
|
2094
|
+
const { context, page, landed } = await open(browser, target.url, width, session);
|
|
2095
|
+
const redirect = redirectedTo(target.url, landed);
|
|
2096
|
+
if (redirect !== undefined)
|
|
2097
|
+
warn(`${target.label} landed on ${redirect} at ${width}px`, "the shot is of that page");
|
|
1773
2098
|
try {
|
|
1774
2099
|
return await shoot(page);
|
|
1775
2100
|
} finally {
|
|
@@ -1790,6 +2115,7 @@ async function diff(args) {
|
|
|
1790
2115
|
const threshold = wholeNumber(values.threshold, "threshold", THRESHOLD, 0);
|
|
1791
2116
|
const mockup = targetOf(mockupGiven, values.out);
|
|
1792
2117
|
const built = targetOf(builtGiven, values.out);
|
|
2118
|
+
const session = sessionOf(values);
|
|
1793
2119
|
const results = [];
|
|
1794
2120
|
const findings = [];
|
|
1795
2121
|
const path = role("path", process20.stdout);
|
|
@@ -1797,7 +2123,7 @@ async function diff(args) {
|
|
|
1797
2123
|
try {
|
|
1798
2124
|
const blank = await browser.newPage();
|
|
1799
2125
|
for (const width of widths) {
|
|
1800
|
-
const [left, right] = await Promise.all([capture(browser, mockup
|
|
2126
|
+
const [left, right] = await Promise.all([capture(browser, mockup, width, session), capture(browser, built, width, session)]);
|
|
1801
2127
|
const compared = await inPage(blank, "compare", "compareImages", { mockup: left.toString("base64"), built: right.toString("base64"), threshold: Math.min(threshold, 255) });
|
|
1802
2128
|
const side = `${built.stem}-side-${width}.png`;
|
|
1803
2129
|
const overlay = `${built.stem}-diff-${width}.png`;
|
|
@@ -1834,6 +2160,7 @@ var init_diff = __esm(() => {
|
|
|
1834
2160
|
USAGE2 = `page diff <mockup> <built>
|
|
1835
2161
|
|
|
1836
2162
|
Shoots the mockup and the built page (HTML files or URLs) full-page at every width, then writes two PNGs per width next to the built file, into --out, or the cache folder for a URL: <name>-side-<width>.png, the mockup left and the built page right, and <name>-diff-<width>.png, the built page faded with every differing pixel in red. The differing regions are boxed and numbered on both, then printed one line each: <built> <width>px region <n>: <width>\xD7<height>px at <x>,<y>.
|
|
2163
|
+
A page behind a login takes --cookie, -H or --storage-state, as page check does; a page landing elsewhere is warned about on stderr.
|
|
1837
2164
|
A pixel differs when a colour channel moves by more than --threshold (${THRESHOLD} of 255); a lone differing pixel in an 8px cell is taken for antialiasing and left out.
|
|
1838
2165
|
|
|
1839
2166
|
Exits 0 when both look the same at every width, 1 when a region differs or a page would not load, 2 on a usage error.`;
|
|
@@ -1841,6 +2168,7 @@ Exits 0 when both look the same at every width, 1 when a region differs or a pag
|
|
|
1841
2168
|
widths: { type: "string", short: "w", description: "viewport widths, comma separated (1440,400)" },
|
|
1842
2169
|
out: { type: "string", short: "o", description: "folder the PNGs go to instead of next to the built file" },
|
|
1843
2170
|
threshold: { type: "string", description: `largest per-channel difference still counted as the same pixel, 0 to 255 (${THRESHOLD})` },
|
|
2171
|
+
...SESSION_OPTIONS,
|
|
1844
2172
|
json: jsonOption("[{width, side, overlay, mockupHeight, builtHeight, differing, regions: [{x, y, width, height}]}]")
|
|
1845
2173
|
};
|
|
1846
2174
|
});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaynooo/cli-page",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.3",
|
|
5
5
|
"description": "page: shoots HTML files or URLs at several widths in a local Chromium and reports their layout defects one line each, or diffs a built page against its mockup.",
|
|
6
6
|
"author": "Kaynooo <contact@kaynooo.fr>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
"page": "$npm_execpath src/cli.ts"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
+
"@kaynooo/audit-engine": "0.1.1",
|
|
41
42
|
"playwright-core": "1.64.1"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|