All files / packages/sds-metadata/src/processors/selector-processors base-processor.ts

0% Statements 0/8
0% Branches 0/3
0% Functions 0/3
0% Lines 0/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                     
import selectorParser from 'postcss-selector-parser';
import { SelectorProcessor } from '../../types.js';
import { Rule } from 'postcss';
 
/**
 * Base class for processing CSS selectors
 */
export abstract class BaseSelectorProcessor<T> implements SelectorProcessor<T> {
 
  protected rule: Rule | undefined;
  
  /**
   * @param skipEmptyRules - Whether to skip empty rules
   */
  constructor(protected skipEmptyRules: boolean = false) {
  }
 
  /**
   * Process a single selector
   * @param selector The CSS selector to process
   */
  processSelector(selector: string, rule?: Rule): void {
    this.rule = rule;
    if(this.skipEmptyRules && this.isEmptyRule()) {
      return;
    }
    selectorParser((selectors: any) => {
      selectors.walk((selector: any) => {
        this.processSelectorNode(selector);
      });
    }).processSync(selector);
  }
 
  isEmptyRule(): boolean {
    return this.rule?.nodes.filter((node: any) => node.type === 'decl').length === 0;
  }
 
  /**
   * Process a selector node
   * @param selector The selector node to process
   */
  protected processSelectorNode(selector: any): void {
    // To be implemented by subclasses
  }
 
  /**
   * Get the results of processing
   * @returns The selector map
   */
  abstract getResults(): T;
}