Skip to content

Commit

Permalink
throw an error when comments in CSS contain expressions (microsoft#5156)
Browse files Browse the repository at this point in the history
* throw an error when comments in CSS contain expressions

* Change files

Co-authored-by: Chris Holt <chhol@microsoft.com>
  • Loading branch information
radium-v and chrisdholt authored Sep 10, 2021
1 parent 9cbba55 commit 54a90cb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions build/transform-fragments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-env node */
/* eslint-disable @typescript-eslint/explicit-function-return-type, @typescript-eslint/typedef */

/**
Expand All @@ -15,7 +16,7 @@ export function transformHTMLFragment(data) {
* Reduces extra spaces in CSS tagged templates.
*
* Breakdown of this regex:
* (?:\s*\/\*(?:.|\s)+?\*\/\s*) Remove comments (non-capturing)
* (?:\s*\/\*(?:[\s\S])+?\*\/\s*) Remove comments (non-capturing)
* (?:;)\s+(?=\}) Remove semicolons and spaces followed by property list end (non-capturing)
* \s+(?=\{) Remove spaces before property list start (non-capturing)
* (?<=:)\s+ Remove spaces after property declarations (non-capturing)
Expand All @@ -25,8 +26,12 @@ export function transformHTMLFragment(data) {
* @returns string
*/
export function transformCSSFragment(data) {
if (/\/\*(?![\s\S]*\*\/)[\s\S]*/g.test(data)) {
throw new Error("Unterminated comment found in CSS tagged template literal");
}

return data.replace(
/(?:\s*\/\*(?:.|\s)+?\*\/\s*)|(?:;)\s+(?=\})|\s+(?=\{)|(?<=:)\s+|\s*([{};,])\s*/g,
/(?:\s*\/\*(?:[\s\S])+?\*\/\s*)|(?:;)\s+(?=\})|\s+(?=\{)|(?<=:)\s+|\s*([{};,])\s*/g,
"$1"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Remove commented CSS",
"packageName": "@microsoft/fast-components",
"email": "john.kreitlow@microsoft.com",
"dependentChangeType": "patch"
}

0 comments on commit 54a90cb

Please sign in to comment.