Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions skills/reverse-engineering-saas/scripts/page/tokens.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// tokens.js: design tokens in use on the page.
// Paste the whole file into the browser's JavaScript tool. The value of the
// last expression is the result.
// window.__re.tokens({ root: '.selector' }) limits the frequency count to a subtree
// window.__re.tokens({ root: '.selector' }) resolves variables and counts styles in a subtree
// window.__re.tokens.element('.selector') computed style of one element (use for hover/focus states)
// Playwright browser_evaluate: wrap as () => { <file>; return window.__re.last; }
(() => {
Expand Down Expand Up @@ -61,12 +61,12 @@

// Resolves a color expression to the browser's computed rgb() form.
const probe = document.createElement('span');
const resolveColor = (expression) => {
const resolveColor = (expression, root) => {
// var() always passes CSS.supports, and resolves to the inherited color when invalid.
if (!expression || expression.includes('var(') || !CSS.supports('color', expression)) return null;
probe.style.color = '';
probe.style.color = expression;
document.body.appendChild(probe);
root.appendChild(probe);
const value = getComputedStyle(probe).color;
probe.remove();
return value;
Expand All @@ -79,14 +79,14 @@
const maxVariables = options.maxVariables || 300;

const { declared, unreadable } = collectDeclaredVariables();
const rootStyle = getComputedStyle(document.documentElement);
const rootStyle = getComputedStyle(root);
for (let i = 0; i < rootStyle.length; i += 1) {
const name = rootStyle[i];
if (name.startsWith('--') && !declared[name]) declared[name] = [{ selector: '(computed on :root)', value: rootStyle.getPropertyValue(name).trim() }];
if (name.startsWith('--') && !declared[name]) declared[name] = [{ selector: '(computed on scope)', value: rootStyle.getPropertyValue(name).trim() }];
}
const variables = Object.entries(declared).map(([name, declarations]) => {
const resolved = rootStyle.getPropertyValue(name).trim() || declarations[0].value;
return { name, resolved, color: resolveColor(resolved), declarations: declarations.slice(0, 4) };
const resolved = rootStyle.getPropertyValue(name).trim();
return { name, resolved, color: resolveColor(resolved, root), declarations: declarations.slice(0, 4) };
});
const variablesByValue = {};
for (const variable of variables) {
Expand Down
39 changes: 39 additions & 0 deletions tests/page-scripts.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,45 @@ test('tokens maps computed values back to CSS variables', async () => {
assert.equal(element.style.borderTopLeftRadius, '8px');
});

test('tokens resolves active body and component themes without using inactive declarations', async () => {
await page.evaluate(() => {
const style = document.createElement('style');
style.id = 'scope-token-style';
style.textContent = 'body.theme-dark { --color-primary: #112233; } .inactive-theme { --inactive-color: #abcdef; }';
document.head.append(style);
document.body.classList.add('theme-dark');
const scope = document.createElement('section');
scope.id = 'token-scope';
scope.style.cssText = '--color-primary: #445566; --inline-color: var(--color-primary); background: var(--inline-color);';
scope.textContent = 'Secret scoped content';
document.body.append(scope);
});
try {
const body = await page.evaluate(script('tokens.js'));
const find = (result, name) => result.variables.list.find((entry) => entry.name === name);
assert.equal(find(body, '--color-primary').color, 'rgb(17, 34, 51)');
assert.equal(find(body, '--inactive-color').resolved, '');
assert.equal(find(body, '--inactive-color').color, null);
const scoped = await page.evaluate(() => window.__re.tokens({ root: '#token-scope' }));
assert.equal(find(scoped, '--color-primary').color, 'rgb(68, 85, 102)');
assert.equal(find(scoped, '--inline-color').color, 'rgb(68, 85, 102)');
assert.ok(scoped.colors.find((entry) => entry.value === 'rgb(68, 85, 102)').variables.includes('--inline-color'));
await page.evaluate(() => document.body.classList.remove('theme-dark'));
const light = await page.evaluate(() => window.__re.tokens());
assert.equal(find(light, '--color-primary').color, 'rgb(123, 104, 238)');
for (const result of [body, scoped, light]) {
const output = JSON.stringify(result);
for (const secret of ['Secret scoped content', 'maria@cliente.com.br', 'Maria Souza']) assert.ok(!output.includes(secret));
}
} finally {
await page.evaluate(() => {
document.body.classList.remove('theme-dark');
document.getElementById('scope-token-style').remove();
document.getElementById('token-scope').remove();
});
}
});

test('api-shapes records endpoint templates and shapes without values', async () => {
await page.evaluate(script('api-shapes.js'));
await page.evaluate(() => window.__re.api.clear());
Expand Down