Identifier selector for element.querySelector?

I am writing an application where I store relative references to elements as a CSS selector suitable for navigating to querySelector. When I want to keep a relative reference to the "base" element, I would like to have a selector that will return the same element.

What is an identifier selector for the querySelector method for Element , as defined in level 1 APIs , which satisfies the following

 var identitySelector = "<this is the selector I want to know>" for (int i = 0; i < document.all.length; i++) { var elem = document.all[i]; if (elem !== elem.querySelector(identitySelector)) { throw identitySelector + " is not the identity selector"; } } 

Update:

Since this selector is not available, I am going to wrap my querySelector call with a function that returns a context element if the selector is equal to the :root pseudo- :root .

 var identitySelector = ":root" var querySelectorProxy = function(elem, selector) { return selector === ':root' ? elem: elem.querySelector(identitySelector); } for (int i = 0; i < document.all.length; i++) { var elem = document.all[i]; if (elem !== querySelectorProxy(elem, selector)) { throw identitySelector + " is not the identity selector"; } } 
+4
source share
1 answer

No.


John Resig provided a link to the discussion on his blog Thoughts on querySelectorAll .

Its beef is that although you can search for any node element, the selector you pass takes into account the entire document. (Personally, I think this is the right approach.) Because of this, he hoped that an identity selector would appear, as you describe.

jQuery (Sizzle) uses the hack bit using the element identifier (or assigning a temporary one if it is not available) and restores the selector to add this identifier selector.

Something like that:

 var identitySelector = "<this is the selector I want to know>" for (int i = 0; i < document.all.length; i++) { var elem = document.all[i], needs_id = false; if( !elem.id ) { needs_id = true; elem.id = '___some___obscure___value___'; } if (elem !== elem.querySelector( '#' + elem.id )) { throw identitySelector + " is not the identity selector"; } if( needs_id ) { elem.id = null; } } 

... although I do not know what this will do here.

+3
source

All Articles