Skip to content

Commit

Permalink
capricorn86#976@minor: Optimizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
malko committed Jul 17, 2023
1 parent 1052bbb commit 4054058
Showing 1 changed file with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
// PropName => \s*([^:;]+?)\s*:
// PropValue => \s*((?:[^(;]*?(?:\([^)]*\))?)*?) <- will match any non ';' char except inside (), nested parentheses are not supported
// !important => \s*(!important)?
// EndOfRule => \s*(?:$|;)
const SPLIT_RULES_REGEXP =
/\s*([^:;]+?)\s*:\s*((?:[^(;]*?(?:\([^)]*\))?)*?)\s*(!important)?\s*(?:$|;)/g;

/**
* CSS parser.
*/
Expand All @@ -12,19 +19,11 @@ export default class CSSStyleDeclarationCSSParser {
cssText: string,
callback: (name: string, value: string, important: boolean) => void
): void {
const rules = [
...cssText.matchAll(
// PropName => \s*([^:;]+?)\s*:
// PropValue => \s*((?:[^(;]*?(?:\([^)]*\))?)*?) <- will match any non ';' char except inside (), nested parentheses are not supported
// !important => \s*(!important)?
// EndOfRule => \s*(?:$|;)
/\s*([^:;]+?)\s*:\s*((?:[^(;]*?(?:\([^)]*\))?)*?)\s*(!important)?\s*(?:$|;)/g
)
];
rules.forEach(([, key, value, important]) => {
const rules = Array.from(cssText.matchAll(SPLIT_RULES_REGEXP));
for (const [, key, value, important] of rules) {
if (key && value) {
callback(key.trim(), value.trim(), !!important);
}
});
}
}
}

0 comments on commit 4054058

Please sign in to comment.