Skip to content

Commit

Permalink
capricorn86#887@patch: Fix parsing CSS comments with asterisks.
Browse files Browse the repository at this point in the history
  • Loading branch information
h-tuomola committed May 3, 2023
1 parent 0892e50 commit 71d57ac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/happy-dom/src/css/CSSParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import CSSMediaRule from './rules/CSSMediaRule';
import CSSContainerRule from './rules/CSSContainerRule';
import CSSSupportsRule from './rules/CSSSupportsRule';

const COMMENT_REGEXP = /\/\*[^*]*\*\//gm;
const COMMENT_REGEXP = /\/\*[\s\S]*?\*\//gm;

/**
* CSS parser.
Expand Down
18 changes: 17 additions & 1 deletion packages/happy-dom/test/css/CSSParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('CSSParser', () => {
const cssStyleSheet = new CSSStyleSheet();
const cssRules = CSSParser.parseFromString(cssStyleSheet, CSSParserInput);

expect(cssRules.length).toBe(8);
expect(cssRules.length).toBe(10);

// CSSStyleRule
expect((<CSSStyleRule>cssRules[0]).parentRule).toBe(null);
Expand Down Expand Up @@ -187,6 +187,22 @@ describe('CSSParser', () => {
expect(children6[0].style[0]).toBe('color');
expect(children6[0].style.color).toBe('green');
expect(children6[0].cssText).toBe('.container { color: green; }');

expect((<CSSStyleRule>cssRules[8]).parentRule).toBe(null);
expect((<CSSStyleRule>cssRules[8]).parentStyleSheet).toBe(cssStyleSheet);
expect((<CSSStyleRule>cssRules[8]).selectorText).toBe(':root');
expect((<CSSStyleRule>cssRules[8]).cssText).toBe(':root { --my-var: 10px; }');
expect((<CSSStyleRule>cssRules[8]).style.parentRule).toBe(cssRules[8]);
expect((<CSSStyleRule>cssRules[8]).style.length).toBe(1);
expect((<CSSStyleRule>cssRules[8]).style.cssText).toBe('--my-var: 10px;');

expect((<CSSStyleRule>cssRules[9]).parentRule).toBe(null);
expect((<CSSStyleRule>cssRules[9]).parentStyleSheet).toBe(cssStyleSheet);
expect((<CSSStyleRule>cssRules[9]).selectorText).toBe('.foo');
expect((<CSSStyleRule>cssRules[9]).cssText).toBe('.foo { color: red; }');
expect((<CSSStyleRule>cssRules[9]).style.parentRule).toBe(cssRules[9]);
expect((<CSSStyleRule>cssRules[9]).style.length).toBe(1);
expect((<CSSStyleRule>cssRules[9]).style.cssText).toBe('color: red;');
});
});
});
12 changes: 12 additions & 0 deletions packages/happy-dom/test/css/data/CSSParserInput.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default `
:host {
display: flex;
overflow: hidden;
Expand Down Expand Up @@ -57,4 +58,15 @@ export default `
color: green;
}
}
/*
* Multi-line comment with leading star
*/
:root {
--my-var: 10px;
}
/* Single-line comment */
.foo { color: red; }
`.trim();

0 comments on commit 71d57ac

Please sign in to comment.