Find specific scopes in a file


I need find all source.css (<style>) scopes in an HTML file. Is that possible? How?

Use-case: Want to support autoprefixing CSS that is embedded in HTML for atom-autoprefixer without the user having to manually select the CSS, which is how it currently works.


If you are in a package you should be able to do that by combining first-mate scope selector and tokens retrieved from the tokenized buffer:

You’ll have to add first-mate as a dependency of your package:

"dependencies": {
  "first-mate": "2.x"

Then in your code something like this should work:

{ScopeSelector} = require 'first-mate'

tokensForSelector = (tokenizedBuffer, selector) ->
  matchingTokens = []
  for {tokens} in tokenizedBuffer.tokenizedLines
      for token in tokens
        matchingTokens.push token if selector.matches(token.scopes)

editor = atom.workspace.getActiveEditor()
selector = new ScopeSelector('.source.css')
tokens = tokensForSelector(editor.displayBuffer.tokenizedBuffer, selector)

This should give you an array with all the tokens whose scope matches .source.css.