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

#1192@patch: Adds support for fallback values when declaring a CSS va… #1203

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import CSSMeasurementConverter from '../measurement-converter/CSSMeasurementConv
import MediaQueryList from '../../../match-media/MediaQueryList.js';
import WindowBrowserSettingsReader from '../../../window/WindowBrowserSettingsReader.js';

const CSS_VARIABLE_REGEXP = /var\( *(--[^) ]+)\)/g;
const CSS_VARIABLE_REGEXP = /var\( *(--[^), ]+)\)|var\( *(--[^), ]+), *([^), ]+)\)/g;
const CSS_MEASUREMENT_REGEXP = /[0-9.]+(px|rem|em|vw|vh|%|vmin|vmax|cm|mm|in|pt|pc|Q)/g;

type IStyleAndElement = {
Expand Down Expand Up @@ -333,7 +333,12 @@ export default class CSSStyleDeclarationElementStyle {
let match;

while ((match = regexp.exec(value)) !== null) {
newValue = newValue.replace(match[0], cssVariables[match[1]] || '');
// Fallback value - E.g. var(--my-var, #FFFFFF)
if (match[2] !== undefined) {
newValue = newValue.replace(match[0], cssVariables[match[2]] || match[3]);
} else {
newValue = newValue.replace(match[0], cssVariables[match[1]] || '');
}
}

return newValue;
Expand Down
39 changes: 39 additions & 0 deletions packages/happy-dom/test/window/BrowserWindow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,45 @@ describe('BrowserWindow', () => {
expect(computedStyle.color).toBe('');
});

it('Returns values defined by a CSS variables when a fallback is used.', () => {
const parent = <IHTMLElement>document.createElement('div');
const element = <IHTMLElement>document.createElement('span');
const computedStyle = window.getComputedStyle(element);
const parentStyle = document.createElement('style');
const elementStyle = document.createElement('style');

browserFrame.page.setViewport({ width: 1024 });

parentStyle.innerHTML = `
html {
font: 14px "Times New Roman";
}

div {
--border-variable: 1px solid var(--color-variable, #000);
--font-variable: 1rem "Tahoma";
}
`;

elementStyle.innerHTML = `
span {
border: var(--border-variable);
font: var(--font-variable);
color: var(--invalid-variable);
}
`;

parent.appendChild(elementStyle);
parent.appendChild(element);

document.body.appendChild(parentStyle);
document.body.appendChild(parent);

expect(computedStyle.border).toBe('1px solid #000');
expect(computedStyle.font).toBe('14px "Tahoma"');
expect(computedStyle.color).toBe('');
});

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
Loading