From a11b4bc15e3c6efa4b52894377ba0c90888cf2ee Mon Sep 17 00:00:00 2001 From: Jose Camargo Date: Mon, 19 Oct 2026 21:17:12 -0300 Subject: [PATCH] fix: resolve design tokens in the selected theme scope Signed-off-by: Jose Camargo --- .../scripts/page/tokens.js | 14 +++---- tests/page-scripts.test.mjs | 39 +++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/skills/reverse-engineering-saas/scripts/page/tokens.js b/skills/reverse-engineering-saas/scripts/page/tokens.js index 6934cce..a326d53 100644 --- a/skills/reverse-engineering-saas/scripts/page/tokens.js +++ b/skills/reverse-engineering-saas/scripts/page/tokens.js @@ -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 () => { ; return window.__re.last; } (() => { @@ -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; @@ -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) { diff --git a/tests/page-scripts.test.mjs b/tests/page-scripts.test.mjs index d240203..22a317d 100644 --- a/tests/page-scripts.test.mjs +++ b/tests/page-scripts.test.mjs @@ -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());