Skip to content

Finding arbitrary HTML nodes that have specific attributes with specific value #976

Closed Answered by HerringtonDarkholme
hitochan777 asked this question in Q&A
Discussion options

You must be logged in to vote

I mean you can avoid using $TAG as a tag name in pattern and use YAML to directly find the attribute. Then find the element having that attribute. All these steps can be composed by YAML rules.

This is done by several step:

  1. Use Pattern Object to find class="foo" https://ast-grep.github.io/guide/rule-config/atomic-rule.html#pattern
    this is the critical step that we avoid the $TAG
  2. Use kind: element to find the HTML element and has to find the element containing the attribute. (But note it does not work since has by default only searches direct children)
  3. Fine-tune the pattern, stopBy can instruct ast-grep to find descendants. In this case, we want to only find elements that have attributes …

Replies: 1 comment 6 replies

Comment options

@hitochan777
Comment options

@HerringtonDarkholme
Comment options

Answer selected by hitochan777
@HerringtonDarkholme
Comment options

@hitochan777
Comment options

@HerringtonDarkholme
Comment options

@hitochan777
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants