The issue you're encountering is due to how the regular expression and your replacement function are interacting. Specifically, the values are not being wrapped in <span class="value">...</span> because the if (value) condition in your replacement function is not being triggered as expected.
Understanding the Problem
In your highlightCSS function, you have:
const regex = /(.*?)(?=\s*{)|([\w-]*)(?=:)|:\s*(.*?)(?=;)/g;
const highlighted = code.replace(regex, (match, selector, property, value) => {
if (selector) {
return `<span class="selector">${selector}</span>`;
}
if (property) {
return `<span class="property">${property}</span>`;
}
if (value) {
return `<span class="value">${value}</span>`;
}
return match;
});
Here, you're using a regular expression with alternation (|) that has three parts:
(.*?)(?=\s*{) - Captures selectors (e.g., body, .class, #id).
([\w-]*)(?=:) - Captures properties (e.g., color, margin-top).
:\s*(.*?)(?=;) - Captures values (e.g., #fff, 10px).
The core issue is with how the alternation and capturing groups work in regular expressions in conjunction with your replacement function.
Why Values Aren't Being Wrapped
-
Alternation in Regex: In a regex with alternation like A|B|C, only one of the patterns A, B, or C will match at a given position in the string. So, for each match, only one of selector, property, or value will be populated; the others will be undefined.
-
Falsy Values in JavaScript: In JavaScript, the condition in an if statement will evaluate to false if the expression is undefined, null, 0, NaN, '' (empty string), or false.
-
Undefined vs. Empty Strings: When a capturing group doesn't participate in the match (i.e., when its part of the regex wasn't used), it is undefined. When it matches an empty string, the captured value is '' (an empty string).
-
Your if Conditions: In your replacement function, you check if (selector), if (property), and if (value). When value is undefined, it doesn't enter the if (value) block, which is expected. However, even when value is captured (e.g., #000), the condition might still fail if value is an empty string or another falsy value.
The Solution
To ensure that your replacement function correctly handles all cases—including when the captured value is an empty string—you should explicitly check whether value is not undefined rather than relying on its truthiness.
Here's how you can adjust your code:
const highlighted = code.replace(regex, (match, selector, property, value) => {
if (selector !== undefined) {
return `<span class="selector">${selector}</span>`;
}
if (property !== undefined) {
return `<span class="property">${property}</span>`;
}
if (value !== undefined) {
return `<span class="value">${value}</span>`;
}
return match;
});
By checking if the variables are strictly not undefined, you ensure that even if value is an empty string (which is falsy), the condition will still pass if the capturing group was part of the match.
Updated Function
Here's the updated highlightCSS function with the fix:
function highlightCSS(element) {
const code = element.textContent;
const regex = /(.*?)(?=\s*{)|([\w-]*)(?=:)|:\s*(.*?)(?=;)/g;
const highlighted = code.replace(regex, (match, selector, property, value) => {
if (selector !== undefined) {
return `<span class="selector">${selector}</span>`;
}
if (property !== undefined) {
return `<span class="property">${property}</span>`;
}
if (value !== undefined) {
return `<span class="value">${value}</span>`;
}
return match;
});
element.innerHTML = highlighted;
}
Explanation
-
selector !== undefined: This condition checks if the selector variable was captured. If it wasn't (undefined), the condition fails.
-
Same for property and value: By explicitly checking for undefined, we handle all cases where the capturing group is present, regardless of the actual content.
Result
With this change, your values will now be correctly wrapped in <span class="value">...</span>. Here's what the highlighted code will look like in your DevTools:
<pre><span class="selector">body</span> {
<span class="property">background-color</span>: <span class="value">#000</span>;
<span class="property">margin</span>: <span class="value">0</span>;
<span class="property">padding</span>: <span class="value">0</span>;
<span class="property">width</span>: <span class="value">100vw</span>;
<span class="property">height</span>: <span class="value">100vh</span>;
}
<span class="selector">.color::red</span> {
}
</pre>
Additional Suggestions
-
Use Non-Capturing Groups When Appropriate: If you have parts of your regex that are purely for matching and don't need to capture their contents, you can use non-capturing groups (?:...) to simplify your groups.
-
Improving the Regex: Depending on the complexity of the CSS you're parsing, you might need a more robust regex. For example, handling comments, nested rules, or more complex selectors and values.
-
Testing Edge Cases: Ensure you test your function with various CSS snippets, including those with empty properties or values, to verify that the regex and replacement function handle all cases correctly.
Summary
The issue was caused by the if conditions not properly accounting for undefined values resulting from the alternation in your regex. By adjusting your conditions to explicitly check for undefined, you ensure that your replacement function correctly wraps the values, even if they are empty strings.