Skip to content
This repository has been archived by the owner on Aug 5, 2021. It is now read-only.

Matching selectors #23

Open
RossComputerGuy opened this issue Jul 2, 2019 · 0 comments
Open

Matching selectors #23

RossComputerGuy opened this issue Jul 2, 2019 · 0 comments

Comments

@RossComputerGuy
Copy link

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;
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant