css-variable-lsp 0.3.2

A fast, Rust-based Language Server Protocol implementation for CSS Variables
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use regex::Regex;
use std::sync::LazyLock;

use crate::dom_tree::DomTree;
use crate::types::{CssVariable, DOMNodeInfo};

/// Memoized regex patterns for specificity calculation
static PSEUDO_ELEMENT_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"::[a-zA-Z-]+").unwrap());
static ID_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"#[a-zA-Z0-9_-]+").unwrap());
static CLASS_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\.[a-zA-Z0-9_-]+").unwrap());
static ATTR_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r#"\[[^\]'"']*\]"#).unwrap());
static NOT_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r":not\((?:[^()]|\([^)]*\))+\)").unwrap());
static IS_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r":is\((?:[^()]|\([^)]*\))+\)").unwrap());
static WHERE_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r":where\((?:[^()]|\([^)]*\))+\)").unwrap());
static PSEUDO_CLASS_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r":[a-zA-Z-]+(\([^)]*\))?").unwrap());

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Specificity {
    pub ids: u32,
    pub classes: u32,
    pub elements: u32,
}

impl Specificity {
    pub fn new(ids: u32, classes: u32, elements: u32) -> Self {
        Self {
            ids,
            classes,
            elements,
        }
    }
}

/// Extract the argument of a top-level pseudo-class call with parentheses.
/// Uses a depth tracker to handle nested parentheses correctly.
///
/// # Arguments
/// * `selector` - The CSS selector string
/// * `prefix` - The prefix bytes to match (e.g., b":is(" or b":not(")
///
/// # Returns
/// The content between the opening '(' and its matching closing ')', or None if not found.
fn extract_pseudo_arg<'a>(selector: &'a str, prefix: &[u8]) -> Option<&'a str> {
    let bytes = selector.as_bytes();
    let len = bytes.len();
    let prefix_len = prefix.len();

    for i in 0..len {
        if bytes[i] != b':' || i + prefix_len > len {
            continue;
        }
        let mut match_prefix = true;
        for j in 0..prefix_len {
            if bytes[i + j] != prefix[j] {
                match_prefix = false;
                break;
            }
        }
        if !match_prefix {
            continue;
        }

        // Found prefix at position i. Now find matching ) at depth 1.
        let mut depth = 1;
        let mut pos = i + prefix_len;
        while pos < len {
            match bytes[pos] {
                b'(' => {
                    depth += 1;
                }
                b')' => {
                    depth -= 1;
                    if depth == 0 {
                        return Some(&selector[i + prefix_len..pos]);
                    }
                }
                _ => {}
            }
            pos += 1;
        }
        return None;
    }
    None
}

/// Extract the argument of a top-level :is() call.
fn extract_is_arg(selector: &str) -> Option<&str> {
    extract_pseudo_arg(selector, b":is(")
}

/// Extract the argument of a top-level :not() call.
fn extract_not_arg(selector: &str) -> Option<&str> {
    extract_pseudo_arg(selector, b":not(")
}

/// Calculate the specificity of a pseudo-class argument (:not, :is, :where).
/// Per CSS spec: :not(.foo, #bar) → take max specificity across comma-separated arguments.
fn specificity_of_pseudo_argument(arg: &str) -> Specificity {
    let parts: Vec<&str> = arg
        .split(',')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .collect();
    if parts.is_empty() {
        return Specificity::new(0, 0, 0);
    }
    let mut max_ids: u32 = 0;
    let mut max_classes: u32 = 0;
    let mut max_elements: u32 = 0;
    for part in parts {
        let spec = specificity_of_selector_part(part);
        max_ids = max_ids.max(spec.ids);
        max_classes = max_classes.max(spec.classes);
        max_elements = max_elements.max(spec.elements);
    }
    Specificity::new(max_ids, max_classes, max_elements)
}

/// Calculate specificity for a single selector part (no comma-separated handling).
/// Handles nested :not(), :is(), :where() by extracting and processing their arguments.
fn specificity_of_selector_part(selector: &str) -> Specificity {
    let selector = selector.trim();
    if selector.is_empty() || selector == "*" {
        return Specificity::new(0, 0, 0);
    }
    let mut working = selector.to_string();

    let pseudo_elements = PSEUDO_ELEMENT_RE.find_iter(&working).count() as u32;
    working = PSEUDO_ELEMENT_RE.replace_all(&working, "").to_string();

    // Recursively handle nested :not() in this argument.
    let not_arg = extract_not_arg(selector);
    let (extra_ids, extra_classes, extra_elements) = if let Some(arg) = not_arg {
        let spec = specificity_of_pseudo_argument(arg);
        (spec.ids, spec.classes, spec.elements)
    } else {
        (0, 0, 0)
    };
    // Remove :not() blocks
    working = NOT_RE.replace_all(&working, "").to_string();

    // Recursively handle nested :is() in this argument.
    // :is() adds specificity of its argument per CSS spec.
    let is_arg = extract_is_arg(selector);
    let (is_ids, is_classes, is_elements) = if let Some(arg) = is_arg {
        let spec = specificity_of_pseudo_argument(arg);
        (spec.ids, spec.classes, spec.elements)
    } else {
        (0, 0, 0)
    };
    // Remove :is() blocks so they aren't counted as pseudo-classes
    working = IS_RE.replace_all(&working, "").to_string();

    // :where() has zero specificity per CSS spec - it's transparent.
    // Remove :where() blocks without processing their arguments.
    working = WHERE_RE.replace_all(&working, "").to_string();

    let ids = ID_RE.find_iter(&working).count() as u32;
    working = ID_RE.replace_all(&working, "").to_string();

    let classes = CLASS_RE.find_iter(&working).count() as u32;
    working = CLASS_RE.replace_all(&working, "").to_string();

    working = ATTR_RE.replace_all(&working, "").to_string();

    working = PSEUDO_CLASS_RE.replace_all(&working, "").to_string();

    let mut elements = pseudo_elements + extra_elements + is_elements;
    working = working.replace(['>', '+', '~', ' '], " ");
    for part in working.split_whitespace() {
        if !part.is_empty() && part != "*" {
            elements += 1;
        }
    }
    Specificity::new(
        ids + extra_ids + is_ids,
        classes + extra_classes + is_classes,
        elements,
    )
}

pub fn calculate_specificity(selector: &str) -> Specificity {
    let selector = selector.trim();
    if selector.is_empty() || selector == "*" {
        return Specificity::new(0, 0, 0);
    }

    let selectors: Vec<&str> = selector
        .split(',')
        .map(|s| s.trim())
        .filter(|s| !s.is_empty())
        .collect();
    // Only split by comma for top-level selector lists (e.g., "div, .foo")
    // Don't split for :not(), :is(), etc. with comma-separated arguments -
    // those are handled separately by extract_not_arg and specificity_of_pseudo_argument
    if selectors.len() > 1
        && !selector.contains(":not(")
        && !selector.contains(":is(")
        && !selector.contains(":where(")
    {
        let mut best = Specificity::new(0, 0, 0);
        for sel in selectors {
            let spec = calculate_specificity(sel);
            if compare_specificity(spec, best) > 0 {
                best = spec;
            }
        }
        return best;
    }

    let mut working = selector.to_string();

    let pseudo_elements = PSEUDO_ELEMENT_RE.find_iter(&working).count() as u32;
    working = PSEUDO_ELEMENT_RE.replace_all(&working, "").to_string();

    // Per CSS spec: :not() adds specificity of its argument.
    // Extract and add :not() specificity BEFORE counting IDs/classes in remaining selector.
    let not_arg = extract_not_arg(selector);
    let (not_ids, not_classes, not_elements) = if let Some(arg) = not_arg {
        let spec = specificity_of_pseudo_argument(arg);
        (spec.ids, spec.classes, spec.elements)
    } else {
        (0, 0, 0)
    };
    // Remove :not() blocks so they aren't double-counted by ID/class regexes
    // Handles nested parens via (?:[^()]|\([^)]*\))+
    working = NOT_RE.replace_all(&working, "").to_string();

    // Per CSS spec: :is() adds specificity of its argument.
    // Extract and add :is() specificity BEFORE counting IDs/classes in remaining selector.
    // Only do this if :not() is NOT present at top level (handled by specificity_of_pseudo_argument).
    let is_arg = if not_arg.is_none() {
        extract_is_arg(selector)
    } else {
        None
    };
    let (is_ids, is_classes, is_elements) = if let Some(arg) = is_arg {
        let spec = specificity_of_pseudo_argument(arg);
        (spec.ids, spec.classes, spec.elements)
    } else {
        (0, 0, 0)
    };
    // Remove :is() blocks so they aren't double-counted
    working = IS_RE.replace_all(&working, "").to_string();

    // Per CSS spec: :where() has ZERO specificity - it's completely transparent
    // Remove :where() blocks so they aren't double-counted
    working = WHERE_RE.replace_all(&working, "").to_string();

    let ids = ID_RE.find_iter(&working).count() as u32;
    working = ID_RE.replace_all(&working, "").to_string();

    let classes = CLASS_RE.find_iter(&working).count() as u32;
    working = CLASS_RE.replace_all(&working, "").to_string();

    let attrs = ATTR_RE.find_iter(&working).count() as u32;
    working = ATTR_RE.replace_all(&working, "").to_string();

    let pseudo_classes = PSEUDO_CLASS_RE.find_iter(&working).count() as u32;
    working = PSEUDO_CLASS_RE.replace_all(&working, "").to_string();

    let mut elements = pseudo_elements;
    working = working.replace(['>', '+', '~', ' '], " ");
    for part in working.split_whitespace() {
        if !part.is_empty() && part != "*" {
            elements += 1;
        }
    }

    Specificity::new(
        ids + not_ids + is_ids,
        classes + attrs + pseudo_classes + not_classes + is_classes,
        elements + not_elements + is_elements,
    )
}

pub fn compare_specificity(a: Specificity, b: Specificity) -> i32 {
    if a.ids != b.ids {
        return if a.ids > b.ids { 1 } else { -1 };
    }
    if a.classes != b.classes {
        return if a.classes > b.classes { 1 } else { -1 };
    }
    if a.elements != b.elements {
        return if a.elements > b.elements { 1 } else { -1 };
    }
    0
}

pub fn format_specificity(spec: Specificity) -> String {
    format!("({},{},{})", spec.ids, spec.classes, spec.elements)
}

pub fn matches_context(
    definition_selector: &str,
    usage_context: &str,
    dom_tree: Option<&DomTree>,
    dom_node: Option<&DOMNodeInfo>,
) -> bool {
    if let (Some(tree), Some(node)) = (dom_tree, dom_node) {
        if let Some(node_index) = node.node_index {
            return tree.matches_selector(node_index, definition_selector);
        }
    }

    let def_trim = definition_selector.trim();
    let usage_trim = usage_context.trim();

    if def_trim == ":root" {
        return true;
    }

    if def_trim == usage_trim {
        return true;
    }

    let def_parts: Vec<&str> = def_trim.split(&[' ', '>', '+', '~'][..]).collect();
    let usage_parts: Vec<&str> = usage_trim.split(&[' ', '>', '+', '~'][..]).collect();

    def_parts.iter().any(|def_part| {
        usage_parts.iter().any(|usage_part| {
            !def_part.is_empty()
                && !usage_part.is_empty()
                && (usage_part.contains(def_part) || def_part.contains(usage_part))
        })
    })
}

/// Sort variables by cascade rules (winner first):
/// !important > inline > specificity > source order (later wins)
pub fn sort_by_cascade(variables: &mut [CssVariable]) {
    variables.sort_by(|a, b| {
        if a.important != b.important {
            return if a.important {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            };
        }

        if a.inline != b.inline {
            return if a.inline {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            };
        }

        let spec_a = calculate_specificity(&a.selector);
        let spec_b = calculate_specificity(&b.selector);
        let spec_cmp = compare_specificity(spec_a, spec_b);
        if spec_cmp != 0 {
            return if spec_cmp > 0 {
                std::cmp::Ordering::Less
            } else {
                std::cmp::Ordering::Greater
            };
        }

        b.source_position.cmp(&a.source_position)
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_specificity_calculation() {
        let root = calculate_specificity(":root");
        assert_eq!(root.ids, 0);
        assert_eq!(root.classes, 1);
        assert_eq!(root.elements, 0);
        assert_eq!(format_specificity(root), "(0,1,0)");
    }

    #[test]
    fn element_selector_specificity() {
        let div = calculate_specificity("div");
        assert_eq!(div.ids, 0);
        assert_eq!(div.classes, 0);
        assert_eq!(div.elements, 1);
    }

    #[test]
    fn class_selector_specificity() {
        let class = calculate_specificity(".button");
        assert_eq!(class.ids, 0);
        assert_eq!(class.classes, 1);
        assert_eq!(class.elements, 0);
    }

    #[test]
    fn id_selector_specificity() {
        let id = calculate_specificity("#main");
        assert_eq!(id.ids, 1);
        assert_eq!(id.classes, 0);
        assert_eq!(id.elements, 0);
    }

    #[test]
    fn complex_selector_specificity() {
        let spec = calculate_specificity("div.button#submit");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 1);
    }

    #[test]
    fn specificity_comparison() {
        let root = calculate_specificity(":root");
        let div = calculate_specificity("div");
        let cls = calculate_specificity(".button");
        let id = calculate_specificity("#main");

        assert_eq!(compare_specificity(div, root), -1);
        assert_eq!(compare_specificity(cls, div), 1);
        assert_eq!(compare_specificity(id, cls), 1);
        assert_eq!(compare_specificity(root, root), 0);
    }

    #[test]
    fn context_matching_basics() {
        assert!(matches_context(":root", "div", None, None));
        assert!(matches_context("div", "div", None, None));
        assert!(matches_context(":root", ".button", None, None));
    }

    #[test]
    fn not_with_class_specificity() {
        // :not(.foo) has specificity of .foo → (0,1,0)
        let spec = calculate_specificity(":not(.foo)");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn not_with_id_specificity() {
        // :not(#bar) has specificity of #bar → (1,0,0)
        let spec = calculate_specificity(":not(#bar)");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 0);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn not_with_multiple_args_specificity() {
        // :not(.foo, #bar) → take max across args → (1,1,0)
        let spec = calculate_specificity(":not(.foo, #bar)");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn not_with_complex_selector_specificity() {
        // :not(.foo.bar) → specificity of .foo.bar = 2 classes → (0,2,0)
        let spec = calculate_specificity(":not(.foo.bar)");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 2);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn not_with_element_specificity() {
        // :not(div) → (0,0,1)
        let spec = calculate_specificity(":not(div)");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 0);
        assert_eq!(spec.elements, 1);
    }

    #[test]
    fn not_preserves_other_selectors() {
        // .foo:not(#bar) → (1,1,0)
        let spec = calculate_specificity(".foo:not(#bar)");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn not_nested_is_specificity() {
        // :not(:is(.foo)) should return specificity of .foo → (0,1,0)
        // The :not() takes specificity of its argument :is(.foo),
        // which in turn takes specificity of .foo
        let spec = calculate_specificity(":not(:is(.foo))");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn where_zero_specificity() {
        // :where() has zero specificity per CSS spec - it's transparent
        let spec = calculate_specificity(":where(.foo)");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 0);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn where_nested_in_not() {
        // :not(:where(.foo)) should return zero specificity from :where()
        let spec = calculate_specificity(":not(:where(.foo))");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 0);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn is_with_class_specificity() {
        // :is(.foo) should return specificity of .foo → (0,1,0)
        let spec = calculate_specificity(":is(.foo)");
        assert_eq!(spec.ids, 0);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn is_with_id_specificity() {
        // :is(#bar) should return specificity of #bar → (1,0,0)
        let spec = calculate_specificity(":is(#bar)");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 0);
        assert_eq!(spec.elements, 0);
    }

    #[test]
    fn is_with_multiple_args_specificity() {
        // :is(.foo, #bar) → take max across args → (1,1,0)
        let spec = calculate_specificity(":is(.foo, #bar)");
        assert_eq!(spec.ids, 1);
        assert_eq!(spec.classes, 1);
        assert_eq!(spec.elements, 0);
    }
}