Unable to use querySelectorAll on inactive view


When I try to use atom.views.getView(editor).querySelectorAll('some class') on an inactive text editor it is returning empty array whereas it is working for the active text editor but only after i give a 2 second delay after the file is loaded (both editors have exactly same content). Can someone please tell how to do a querySelectorAll on inactive tabs and how to remove this delay?


Can you explain your use case? There are other (and most of the times, better) ways to do view operations for editor, but they are often use-case specific


I want to select lines based on specific tags so I have added custom scopes to the HTML grammar and now i want to extract the lines which contain that tags. so i want views of each text editor open to get those lines from each of the editor


The Atom text editor views do not contain the entire contents of the files being edited necessarily and can be cleared at any time for performance or other reasons. See the blog article here for more information on the rendering architecture in Atom:

So an inactive text editor view may not have any lines in it … or it may have some lines … or it may have all the lines. It depends on a bunch of different factors. Even an active text editor view may not have all lines in the file in it at any particular time.

So let’s take another step back. You said:

Why are you doing this? What problem are you trying to solve?


I am trying to work on a package css-spy which gives suggestions for classes based on the CSS files included in the HTML file. So I have to detect link tags to get the css files included. So i am doing so by adding custom scopes to link tag and the href attribute and to get them i am using getView.querySelectorAll


Is there any way i can get the DOM of an editor which might not be active?


I guess you are trying to query the DOM to avoid parsing the file yourself. This is not a supported feature. Dom only contain line that need to be displayed at the current scroll position, not the entire file.

Now you may still want to access the result of grammar tokenisation. That is still not a publicly supported feature. But I’ve seen it more often.

Access like this:

Iterate like this:

Maybe we do need a public api over theese.


We do not currently have any plans on allowing for dynamic grammars or injecting things into the tokenization process. We have some ideas for improving syntax highlighting and such but until we’re able to bring them to life, parsing the file yourself in an external process (like the linter tools do) is probably the best bet.


That’s not the use case I was pointing to. Doing a good parser is hard. And a lot of the the Works is already done. The API that would be interesting could be something as simple as read only access.

The simple things could be built on top of that. Find header in markdown file. Find external include in html file.

Or more complex, like find if a variable has been used before declaring. Maybe there’s a way to parse identifier and whether it appear on the lhs of an operator being assignment. And maybe there’s a way to be 80% correct without re-writting per language syntax.


Yes, I see your point. We’re still not planning on exposing a public API because we are constantly rewriting this stuff for perf reasons. We cannot yet commit to keeping it stable.


Is there any way to get the grammar of HTML when no HTML file is opened in atom. I am able to access it by using atom.grammars.grammarsByScopeName['text.html.basic'] only when some HTML file is loaded


I don’t know. Why do you want to do this?

We seem to be running into an X/Y problem.


I wanted to add some styling to make out classes from rest of the code which is why I need to add custom scopes to HTML grammar. I understood that it is better to parse the HTML file itself as the DOM querying is not a well supported feature. I will try to change my code to do this.


I wanted to achieve something similar a few days ago, I wanted to scan all the numbers in editor, and you want to scan all the links in the editor.

Instead of poking through the DOM, you could just create a regex and just do

var scriptRegex = /<script src="([\S ]+)"></script>/g
var textEditor = atom.workspace.getActiveTextEditor()

if (!textEditor) {
  throw new Error('no active text editor')
textEditor.scan(scriptRegex, function(match) {


Yeah I implemented that using regex and it is working fine now. I got the lines from buffer and parsed each line to match for the link tags.