[whatwg] Proposal: Adding methods like getElementById and getElementsByTagName to DocumentFragments
simonp at opera.com
Thu Oct 10 04:06:58 PDT 2013
On Thu, 10 Oct 2013 02:40:30 +0200, Glenn Maynard <glenn at zewt.org> wrote:
> On Wed, Oct 9, 2013 at 7:02 PM, Boris Zbarsky <bzbarsky at mit.edu> wrote:
>> On 6/28/13 10:01 PM, Boris Zbarsky wrote:
>>> On 6/28/13 5:06 PM, Tab Atkins Jr. wrote:
>>>> getElementById("foo") is just querySelector("#foo")
>>> This is actually false. For example, getElementById("foo:bar") is just
>>> querySelector("#foo\\:bar"), which is ... nonobvious.
>> And today someone asked me how to do the equivalent of
>> getElementById("\n") with querySelector. That one is even more
A newline isn't conforming in id="" in HTML, so it's not a case we need to
> But it's already been suggested--by you--that we need a function to
> CSS-escape a string, which seems to solve the that problem trivially (for
> I often do things like saving an element's elem.dataset.someId, and then
> finding the element again later by saying
> container.querySelector('[data-some-id="' + saved_id + '"]'. (That lets
> find the element later, even if it's been replaced by a new element,
> doesn't work if I just save a reference.) That would help there, too,
> since I wouldn't need to make sure that my IDs don't need to be escaped.
That wouldn't actually need CSS ident escaping, but CSS string escaping.
The former would *work*, though, I guess, but is technically overkill.
I grepped through webdevdata.org's 2013 june data set for querySelector
and querySelectorAll and $ to get an idea about what people are doing:
Maybe needs to escape as string:
$('#mainMenu > ul > li > ul > li > a[href="' + theMenu.split('?') ...
$("li.zone7-li[data-id='" + id + "']")
$('.flex-control-nav li#left div[id="'+slider.currentSlide+'"]')
Maybe needs to escape as ident:
.querySelectorAll('.' + className)
.querySelector('#bet_event' + eid)
.querySelector('#' + sections[sec].id + ' .d' +
$('#'+id+' ul li .item-thumbnail')
$("#" + hide + "_header")
$('#' + id + '_ed')
$('li[id = ' + textId + ']', $slideshow3485780.context)
$('[n_id='+allN_id+'] .notificationContainer a span')
$('.recommend > .bd.b_con ul[city="'+city1+'"]')
(The above is just a small subset of some interesting cases.)
I didn't see a single case that actually used an escaping utility.
Searching for code that uses Mathias' cssesc gives only one file that uses
it in github:
I did however find more instances (528) by searching for "escapeSelector":
So, in cluclusion, it appears that there is *some* demand for this. The
common case is escaping as ident. An API to escape as ident could be used
for escaping strings, too. In order to not make people think more than
just remembering to escape at all, it might be a good idea to just have
one API to serve both cases, e.g. CSS.escape(foo).
I don't think that adding an API to escape a CSS ident means that it's a
good idea to not have e.g. getElementById on DocumentFragment. Most people
don't escape their stuff, so only providing a selector API that requires
escaping seems like the net effect would be more buggy code.
More information about the whatwg