hackers-painters / katana-parser

A CSS parsing library in pure C99
http://hackers-painters.github.io/katana-parser
MIT License
180 stars 40 forks source link

Matching selectors #23

Open RossComputerGuy opened 5 years ago

RossComputerGuy commented 5 years ago

I'm working on a function for matching CSS selectors and I seem to be having some issues trying to write this function. This is my current code but it doesn't match correctly. Does anyone know of an easier way of matching or one that works? I also tried looking for a function in the Katana source code but I didn't find a matching function.

KatanaStyleRule* rds_theme_find_byrule(rds_theme_t* theme, const char* strrule) {
  KatanaOutput* css = katana_parse(strrule, strlen(strrule), KatanaParserModeSelector);
  if (css == NULL) return NULL;
  for (int i = 0; i < theme->css->stylesheet->rules.length; i++) {
    if (((KatanaRule*)theme->css->stylesheet->rules.data[i])->type == KatanaRuleStyle) {
      KatanaStyleRule* rule = (KatanaStyleRule*)theme->css->stylesheet->rules.data[i];
      if (rule->selectors->length != css->selectors->length) continue;
      int match = 1;
      for (int x = 0; x < css->selectors->length; x++) {
        KatanaSelector* asel = (KatanaSelector*)rule->selectors->data[x];
        KatanaSelector* bsel = (KatanaSelector*)css->selectors->data[x];

        if ((asel->match == bsel->match) == KatanaSelectorMatchTag) {
          if (asel->tag->prefix == NULL && bsel->tag->prefix == NULL) {
            if (!!strcmp(asel->tag->local, bsel->tag->local)) {
              match = 0;
            }
          } else {
            if (!!strcmp(asel->tag->prefix, bsel->tag->prefix)) {
              match = 0;
            }
            if (!!strcmp(asel->tag->local, bsel->tag->local)) {
              match = 0;
            }
          }
        }

        while (asel != NULL && bsel != NULL) {
          if (asel->match == bsel->match) {
            if (asel->match == KatanaSelectorMatchId || asel->match == KatanaSelectorMatchClass) {
              if (!!strcmp(asel->data->value, bsel->data->value)) match = 0;
            }
          } else {
            match = 0;
            break;
          }
          asel = asel->tagHistory;
          bsel = bsel->tagHistory;
        }
      }
      if (!match) continue;
      katana_destroy_output(css);
      return rule;
    }
  }
  katana_destroy_output(css);
  return NULL;
}