Skip to content

Commit ebb5856

Browse files
fix: Update version to 1.9.4 and enhance CSS comment handling in parser
1 parent edbfc86 commit ebb5856

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "css-linter"
3-
version = "1.9.3"
3+
version = "1.9.4"
44
edition = "2021"
55

66
[dependencies]

src/parsers/css_parser.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ pub fn extract_classes(css_content: &str) -> HashSet<ClassName> {
2929
let mut defined_classes: HashSet<ClassName> = HashSet::new();
3030
const DISABLE_RULE_FLAG: &str = "css-lint-disable-rule ";
3131
let mut skip_lines_remaining = 0;
32+
let mut in_multiline_comment = false;
3233

3334
for (index, line) in css_content.split('\n').enumerate() {
3435
let stripped_line = line.trim_start();
3536
let trimmed_line_indent = line.len() - stripped_line.len();
3637

38+
// Check for disable-rule directive before removing comments
3739
if stripped_line.starts_with("/*") {
3840
let comment_content = stripped_line
3941
.trim_start_matches("/*")
@@ -51,14 +53,17 @@ pub fn extract_classes(css_content: &str) -> HashSet<ClassName> {
5153
continue;
5254
}
5355

54-
if !stripped_line.contains('.') || contains_forbidden_characters(stripped_line) {
56+
// Remove comments from the line
57+
let line_without_comments = remove_css_comments(stripped_line, &mut in_multiline_comment);
58+
59+
if !line_without_comments.contains('.') || contains_forbidden_characters(&line_without_comments) {
5560
continue;
5661
}
5762

5863
let mut buffer: String = String::new();
5964
let mut is_class = false;
6065
let mut start_index = 0;
61-
for (column_index, symbol) in stripped_line.chars().enumerate() {
66+
for (column_index, symbol) in line_without_comments.chars().enumerate() {
6267
match symbol {
6368
'.' => {
6469
if !buffer.is_empty() && !is_first_char_numeric(&buffer) {
@@ -100,3 +105,28 @@ pub fn extract_classes(css_content: &str) -> HashSet<ClassName> {
100105

101106
defined_classes
102107
}
108+
109+
fn remove_css_comments(line: &str, in_comment: &mut bool) -> String {
110+
let mut result = String::new();
111+
let mut chars = line.chars().peekable();
112+
113+
while let Some(ch) = chars.next() {
114+
if *in_comment {
115+
// Inside a comment, look for */
116+
if ch == '*' && chars.peek() == Some(&'/') {
117+
chars.next(); // consume '/'
118+
*in_comment = false;
119+
}
120+
} else {
121+
// Outside a comment
122+
if ch == '/' && chars.peek() == Some(&'*') {
123+
chars.next(); // consume '*'
124+
*in_comment = true;
125+
} else {
126+
result.push(ch);
127+
}
128+
}
129+
}
130+
131+
result
132+
}

0 commit comments

Comments
 (0)