throw an error when comments in CSS contain expressions (#5156)

* throw an error when comments in CSS contain expressions

* Change files

Co-authored-by: Chris Holt <chhol@microsoft.com>
This commit is contained in:
John Kreitlow 2021-09-09 20:09:53 -07:00 коммит произвёл GitHub
Родитель 9cbba55c5e
Коммит 54a90cb9b3
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 14 добавлений и 2 удалений

Просмотреть файл

@ -1,3 +1,4 @@
/* eslint-env node */
/* eslint-disable @typescript-eslint/explicit-function-return-type, @typescript-eslint/typedef */
/**
@ -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)
@ -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"
);
}

Просмотреть файл

@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Remove commented CSS",
"packageName": "@microsoft/fast-components",
"email": "john.kreitlow@microsoft.com",
"dependentChangeType": "patch"
}