Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [#1308] Fix a bug of multiple fallbacks of custom property #1309

Merged
merged 4 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ export default class CSSStyleDeclarationElementStyle {
let newValue = value;
let match;

while ((match = regexp.exec(value)) !== null) {
while ((match = regexp.exec(newValue)) !== null) {
// Fallback value - E.g. var(--my-var, #FFFFFF)
if (match[2] !== undefined) {
newValue = newValue.replace(match[0], cssVariables[match[2]] || match[3]);
Expand Down
30 changes: 30 additions & 0 deletions packages/happy-dom/test/window/BrowserWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,36 @@ describe('BrowserWindow', () => {
expect(computedStyle.color).toBe('');
});

it('Returns values defined by a CSS variables when multiple fallbacks are used', () => {
const div = <IHTMLElement>document.createElement('div');
const divStyle = document.createElement('style');

divStyle.innerHTML = `
div {
color: var(--my-var, var(--my-background, pink));

--color1: red;
--result1: var(--color1, var(--unknown, var(--unknown, green)));
--result2: var(--unknown, var(--color1, var(--unknown, green)));
--result3: var(--unknown, var(--unknown, var(--color1, green)));

--result4: var(--unknown, var(--unknown, var(--unknown, var(--unknown, white))));
}
`;

document.body.appendChild(divStyle);
document.body.appendChild(div);

const computedStyle = window.getComputedStyle(div);
expect(computedStyle.color).toBe('pink');

expect(computedStyle.getPropertyValue('--result1')).toBe('red');
expect(computedStyle.getPropertyValue('--result2')).toBe('red');
expect(computedStyle.getPropertyValue('--result2')).toBe('red');

expect(computedStyle.getPropertyValue('--result4')).toBe('white');
});

it('Returns a CSSStyleDeclaration object with computed styles containing "rem" and "em" measurement values converted to pixels.', () => {
const parent = <IHTMLElement>document.createElement('div');
const element = <IHTMLElement>document.createElement('span');
Expand Down