Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v13.0.3
Choose a base ref
...
head repository: capricorn86/happy-dom
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v13.0.4
Choose a head ref
  • 2 commits
  • 2 files changed
  • 1 contributor

Commits on Jan 13, 2024

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

    …riable (e.g. "var(--my-var, #FFF)").
    capricorn86 committed Jan 13, 2024
    Copy the full SHA
    59d01b6 View commit details
  2. Merge pull request #1203 from capricorn86/task/1192-support-fallback-…

    …value-of-css-variables
    
    #1192@patch: Adds support for fallback values when declaring a CSS va…
    capricorn86 authored Jan 13, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d4dd276 View commit details
Original file line number Diff line number Diff line change
@@ -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 = {
@@ -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;
39 changes: 39 additions & 0 deletions packages/happy-dom/test/window/BrowserWindow.test.ts
Original file line number Diff line number Diff line change
@@ -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');