Scoped absolute paths in querySelector/All

2013-09-06 Thread Brett Zamir
I am wondering whether thought has been given to allowing selectors 
beginning with  for use with querySelector/All such as:


  .sharedClass

Sometimes one wishes to resolve a precise hierarchy but relative to the 
current element. Currently, to do so with precision, one must either 
specify a path starting from the document root, a unique ID, or use 
other scripting methods.


Having such an option would provide the same convenience for precise 
access as XPath or to a lesser extent, as JSON, allow.


The following document illustrates the concern--in this case, where we 
are trying to get only the second span relative to #div1 but without 
needing to specify or be aware of the whole document hierarchy:


!DOCTYPE htmlhtmlheadmeta charset=utf-8 //headbody

div id=div1
span class=sharedClassFirst/span
div
span class=sharedClassSecond/span
/div
/div

script

  var s = document.querySelector('#div1');

  // The following gets both spans instead of the one directly inside 
that we want

  var relativeSelector = s.querySelectorAll('.sharedClass');
  alert(relativeSelector.length); // 2

  // The following gets the desired span, but required us to specify 
the full path or redundantly express the path to our parent again

  var absoluteSelector = s.querySelectorAll('#div1  .sharedClass');
  alert(absoluteSelector[0].textContent); // First

/script/body/html

(Although the use case would be far less common, a path might also begin 
with  when run against documents to allow usage with XML documents 
where a path was desired relative to the root but without committing to 
a specific root element.)


It would also be nice to be able to modify the element or 
elements--e.g., if there were a removeSelector(sel) analogue to the 
ability to use delete obj.prop.prop2 on a JSON object, a 
replaceSelector(sel, el) convenience method, etc..


Best,
Brett



Re: Scoped absolute paths in querySelector/All

2013-09-06 Thread Boris Zbarsky

On 9/6/13 9:13 PM, Brett Zamir wrote:

I am wondering whether thought has been given to allowing selectors
beginning with  for use with querySelector/All


There have, as have selectors like :scope  .sharedClass.  Gecko had 
an implementation of the :scope version of this at one point, but people 
couldn't make up their mind as to what they want here, so we removed it 
again until it's clearer what the API will actually look like.


-Boris



Re: Scoped absolute paths in querySelector/All

2013-09-06 Thread Tab Atkins Jr.
On Fri, Sep 6, 2013 at 6:13 PM, Brett Zamir bret...@gmail.com wrote:
 I am wondering whether thought has been given to allowing selectors
 beginning with  for use with querySelector/All such as:

   .sharedClass

 Sometimes one wishes to resolve a precise hierarchy but relative to the
 current element. Currently, to do so with precision, one must either specify
 a path starting from the document root, a unique ID, or use other scripting
 methods.

Selectors API 2 provides this ability through the .find() and
.findAll() functions:
http://dev.w3.org/2006/webapi/selectors-api2/#findelements-relative.

 (Although the use case would be far less common, a path might also begin
 with  when run against documents to allow usage with XML documents where
 a path was desired relative to the root but without committing to a specific
 root element.)

That's what the :root pseudoclass is for, by the way:
http://dev.w3.org/csswg/selectors/#the-root-pseudo

 It would also be nice to be able to modify the element or elements--e.g., if
 there were a removeSelector(sel) analogue to the ability to use delete
 obj.prop.prop2 on a JSON object, a replaceSelector(sel, el) convenience
 method, etc..

I don't understand what this would do, from your description.

~TJ