[whatwg] document.head

On Sun, Sep 20, 2009 at 8:51 PM, Juriy Zaytsev <kangax.dev at gmail.com> wrote:
>
> On Sep 20, 2009, at 10:29 PM, Garrett Smith wrote:
>
>> On Sun, Sep 20, 2009 at 2:47 PM, Michael A. Puls II
>> <shadow2531 at gmail.com> wrote:
>>>
>>> On Sun, 20 Sep 2009 16:15:11 -0400, Joseph Pecoraro <joepeck02 at gmail.com>
>>> wrote:
>>>
>>>> On Sep 20, 2009, at 3:57 PM, Michael A. Puls II wrote:
>>>>>
>>>>> I think it'd be cool to have to complement document.documentElement and
>>>>> document.body.
>>>>
>>>> On Sep 20, 2009, at 4: 00PM, Juriy Zaytsev wrote:
>>>>>
>>>>> Surely better than abominable ?
>>>>> `document.getElementsByTagName('head')[0]` :)
>>>>
>>>> I agree. Unfortunately that is the most popular method I've seen. ?There
>>>> are better solutions, but they haven't caught on. ?I think a smarter
>>>> idea
>>>> would be to look at the children of the <html> element. ?Something like
>>>> this
>>>> almost always works:
>>>>
>>>> ?var head = document.documentElement.firstChild
>>>
>>> It wasn't very long ago though that in Opera for example, head wasn't
>>> guaranteed to be the first child of the documentElement. But, that'll
>>> work
>>> now and is pretty good.
>>>
>>
>> The documentElement.firstChild cannot be expected to be head. It could
>> be a text node. For example:-
>>
>> <html>
>> ?<head>
>> ...
>>
>> the first child node of HTML looks like a textNode with the value
>> "\n\n\u0020\u0020".
>>
>> document.getElementsByTagName("head")[0] could be expected to produce
>> (in a valid HTML document) a result that is more consistent than
>> document.firstChild.
>
> That was exactly my thought when `firstChild` was mentioned here first time.
> I was suspecting IE to return textnode but couldn't reproduce it in either
> 6, 7 or 8. I also tried inserting comment in between:
>
> <html>
> ?<!--// foo -->
> ?<head>
> ?...
>
> but HEAD was still reported as first child.
>

Wow, that's bizarre.

Result:
Opera 10, Chrome 2, Safari 4
#comment, test1
HEAD,
#comment, test 2
BODY,

IE 5.5+, Firefox 3.5, Seamonkey 1.1
HEAD,
BODY,

Blackberry Storm 9000
#comment, test1
HEAD,
#comment, test 2
BODY,
#comment, test 3


The first result seems nearly right, except for the last comment "test
3". I believe the Blackberry result is the correct one, though I can't
cite a reference that backs this up.

example document:
<!doctype html>
<html>
<!-- test -->
<!-- test2 -->
<head>
<title>documentElement.childNodes</title>
<script type="text/javascript">
onload = function() {
  var childNodesArray = [];
  var childNodes = document.documentElement.childNodes;
  for(var i = 0; i < childNodes.length; i++) {
    childNodesArray[i] = childNodes[i].nodeName;
  }
  document.body.firstChild.data = childNodesArray.join("\n");
};
</script>
</head>
<!-- test 4 -->
<body style="white-space:pre">-</body>
<!-- test 5 -->
</html>

Garrett

Received on Sunday, 20 September 2009 21:42:14 UTC