<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>117836</bug_id>
          
          <creation_ts>2013-06-20 08:21:17 -0700</creation_ts>
          <short_desc>REGRESSION(r149652): accessing items in .children via id doesn&apos;t work when element is not rooted in DOM tree</short_desc>
          <delta_ts>2013-08-02 15:47:17 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>DOM</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          <dependson>115584</dependson>
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Antoine Quint">graouts</reporter>
          <assigned_to name="Ryosuke Niwa">rniwa</assigned_to>
          <cc>benjamin</cc>
    
    <cc>commit-queue</cc>
    
    <cc>esprehn+autocc</cc>
    
    <cc>kling</cc>
    
    <cc>koivisto</cc>
    
    <cc>rniwa</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>902157</commentid>
    <comment_count>0</comment_count>
      <attachid>205092</attachid>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 08:21:17 -0700</bug_when>
    <thetext>Created attachment 205092
Reduced testcase

Take an element such as this one:

&lt;div&gt;
    &lt;div id=foo&gt;&lt;/div&gt;
&lt;/div&gt;

If you call `.children.foo` on this element when it&apos;s not yet in the DOM tree, you&apos;ll get null, while the same code once the element is rooted in the DOM tree gets a reference to the &lt;div id=foo&gt; element.

This is only failing in WebKit nightlies and works fine on Safari 6.0.5.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902158</commentid>
    <comment_count>1</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 08:21:52 -0700</bug_when>
    <thetext>&lt;rdar://problem/14118228&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902163</commentid>
    <comment_count>2</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 08:43:43 -0700</bug_when>
    <thetext>I think the problem is coming from HTMLCollection::namedItem() and this code right at the start of the method:

    ContainerNode* root = rootContainerNode();
    if (name.isEmpty() || !root)
        return 0;

This was added in http://trac.webkit.org/changeset/138195 to fix https://webkit.org/b/105324.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902164</commentid>
    <comment_count>3</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 08:46:38 -0700</bug_when>
    <thetext>Cc&apos;ing Antti who committed the change that changed the behaviour here. Looking at the spec at http://www.w3.org/TR/dom/#dom-htmlcollection-nameditem, I don&apos;t see why we would perform such a check, it seems perfectly valid to operate without a root:

&quot;The namedItem(key) method must return the first element in the collection that falls into one of the following categories:

    - It is an a, applet, area, embed, form, frame, frameset, iframe, img, or object element, in the HTML namespace, with a name attribute equal to key, or,
    - It is an element with an ID equal to key.

If no such elements are found, then the method must return null.&quot;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902181</commentid>
    <comment_count>4</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 09:30:05 -0700</bug_when>
    <thetext>Actually, the case we hit is the final &quot;else&quot; clause in this chunk of code:

        TreeScope* treeScope = root-&gt;treeScope();
        Element* candidate = 0;
        if (treeScope-&gt;hasElementWithId(name.impl())) {
            if (!treeScope-&gt;containsMultipleElementsWithId(name))
                candidate = treeScope-&gt;getElementById(name);
        } else if (treeScope-&gt;hasElementWithName(name.impl())) {
            if (!treeScope-&gt;containsMultipleElementsWithName(name)) {
                candidate = treeScope-&gt;getElementByName(name);
                if (candidate &amp;&amp; type() == DocAll &amp;&amp; (!candidate-&gt;isHTMLElement() || !nameShouldBeVisibleInDocumentAll(toHTMLElement(candidate))))
                    candidate = 0;
            }
        } else
            return 0;

So the treeScope is not returning true for `treeScope-&gt;hasElementWithId(name.impl())`, which is surprising in this case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902184</commentid>
    <comment_count>5</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 09:34:32 -0700</bug_when>
    <thetext>Indeed, the treeScope&apos;s m_elementsById map is empty.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902187</commentid>
    <comment_count>6</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 09:42:44 -0700</bug_when>
    <thetext>Element::updateIdForTreeScope() is where the map of element IDs is populated, and it&apos;s not yet been called at the time we&apos;re trying to access the namedItem in our test case. We need to be sure we have an ID map before calling namedItem(), although this may need to happen in other places too. Maybe this should happen in TreeScope::hasElementWithId().</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902204</commentid>
    <comment_count>7</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 10:38:26 -0700</bug_when>
    <thetext>Actually, this may have regressed with http://trac.webkit.org/changeset/149652 which fixed http://webkit.org/b/115584.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902225</commentid>
    <comment_count>8</comment_count>
    <who name="Antoine Quint">graouts</who>
    <bug_when>2013-06-20 11:24:22 -0700</bug_when>
    <thetext>So the regression is indeed coming from http://trac.webkit.org/changeset/149652. The problem, I think, is that we don&apos;t get to register this id in the treeScope due to this early return in Element::insertedInto():

    if (!insertionPoint-&gt;isInTreeScope())
        return InsertionDone;

As we hit this, we can&apos;t make the call to:

    updateIdForTreeScope(newScope, nullAtom, idValue);

later on in the function. This needs to happen for the treeScope&apos;s m_elementsById to be populated with the expected elements when we eventually call HTMLCollection::namedItem().</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902229</commentid>
    <comment_count>9</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2013-06-20 11:32:12 -0700</bug_when>
    <thetext>If a node is not a descendent of the tree scope, then it shouldn&apos;t be in the hash map.  Instead, we need to go through the slow path.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902310</commentid>
    <comment_count>10</comment_count>
      <attachid>205119</attachid>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2013-06-20 14:02:29 -0700</bug_when>
    <thetext>Created attachment 205119
Fixes the bug</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902400</commentid>
    <comment_count>11</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2013-06-20 19:48:12 -0700</bug_when>
    <thetext>Taking it over.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>902418</commentid>
    <comment_count>12</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2013-06-20 20:51:05 -0700</bug_when>
    <thetext>Committed r151821: &lt;http://trac.webkit.org/changeset/151821&gt;</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="0"
              isprivate="0"
          >
            <attachid>205092</attachid>
            <date>2013-06-20 08:21:17 -0700</date>
            <delta_ts>2013-06-20 08:21:17 -0700</delta_ts>
            <desc>Reduced testcase</desc>
            <filename>test.html</filename>
            <type>text/html</type>
            <size>335</size>
            <attacher name="Antoine Quint">graouts</attacher>
            
              <data encoding="base64">PHNjcmlwdD4KCiAgICB2YXIgZGl2ID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgiZGl2Iik7CiAg
ICBkaXYuaW5uZXJIVE1MID0gIjxkaXYgaWQ9Zm9vPjwvZGl2PiI7CgogICAgY29uc29sZS5sb2co
IkJlZm9yZSBhcHBlbmRpbmciLCAhIWRpdi5jaGlsZHJlbi5mb28pOwoKICAgIHdpbmRvdy5hZGRF
dmVudExpc3RlbmVyKCJET01Db250ZW50TG9hZGVkIiwgZnVuY3Rpb24oKSB7CiAgICAgICAgZG9j
dW1lbnQuYm9keS5hcHBlbmRDaGlsZChkaXYpOwogICAgICAgIGNvbnNvbGUubG9nKCJBZnRlciBh
cHBlbmRpbmciLCAhIWRpdi5jaGlsZHJlbi5mb28pOwogICAgfSk7Cgo8L3NjcmlwdD4=
</data>

          </attachment>
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>205119</attachid>
            <date>2013-06-20 14:02:29 -0700</date>
            <delta_ts>2013-06-20 20:35:45 -0700</delta_ts>
            <desc>Fixes the bug</desc>
            <filename>bug-117836-20130620140038.patch</filename>
            <type>text/plain</type>
            <size>4298</size>
            <attacher name="Ryosuke Niwa">rniwa</attacher>
            
              <data encoding="base64">SW5kZXg6IFNvdXJjZS9XZWJDb3JlL0NoYW5nZUxvZwo9PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0tLSBTb3VyY2UvV2Vi
Q29yZS9DaGFuZ2VMb2cJKHJldmlzaW9uIDE1MTc5OSkKKysrIFNvdXJjZS9XZWJDb3JlL0NoYW5n
ZUxvZwkod29ya2luZyBjb3B5KQpAQCAtMSwzICsxLDIwIEBACisyMDEzLTA2LTIwICBSeW9zdWtl
IE5pd2EgIDxybml3YUB3ZWJraXQub3JnPgorCisgICAgICAgIFJFR1JFU1NJT046IGFjY2Vzc2lu
ZyBpdGVtcyBpbiAuY2hpbGRyZW4gdmlhIGlkIGRvZXNuJ3Qgd29yayB3aGVuIGVsZW1lbnQgaXMg
bm90IHJvb3RlZCBpbiBET00gdHJlZQorICAgICAgICBodHRwczovL2J1Z3Mud2Via2l0Lm9yZy9z
aG93X2J1Zy5jZ2k/aWQ9MTE3ODM2CisKKyAgICAg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0ZXIgb24gZWxlbWVudC5jaGls
ZHJlbiB3b3JrcyBldmVuIHdoZW4gZWxlbWVudCBpcyBub3QgaW4gdGhlIGRvY3VtZW50Iik7CisK
K2Z1bmN0aW9uIGNyZWF0ZUVsZW1lbnRXaXRoSWQoaWQpIHsKKyAgICB2YXIgc3BhbiA9IGRvY3Vt
ZW50LmNyZWF0ZUVsZW1lbnQoJ3NwYW4nKTsKKyAgICBzcGFuLmlkID0gJ2Zvbyc7CisgICAgcmV0
dXJuIHNwYW47Cit9CisKK3ZhciBlbGVtZW50ID0gZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgnZGl2
Jyk7CitzaG91bGRCZVVuZGVmaW5lZCgiZWxlbWVudC5jaGlsZHJlblsnZm9vJ10iKTsKK3Nob3Vs
ZEJlKCJzcGFuID0gY3JlYXRlRWxlbWVudFdpdGhJZCgnZm9vJyk7IGVsZW1lbnQuYXBwZW5kQ2hp
bGQoc3Bhbik7IGVsZW1lbnQuY2hpbGRyZW5bJ2ZvbyddIiwgInNwYW4iKTsKK3Nob3VsZEJlVW5k
ZWZpbmVkKCJkb2N1bWVudC5hbGxbJ2ZvbyddIik7CitzaG91bGRCZSgiZG9jdW1lbnQuYm9keS5h
cHBlbmRDaGlsZChzcGFuKTsgZG9jdW1lbnQuYWxsWydmb28nXSIsICJzcGFuIik7CisKKzwvc2Ny
aXB0PgorPHNjcmlwdCBzcmM9Ii4uL2pzL3Jlc291cmNlcy9qcy10ZXN0LXBvc3QuanMiPjwvc2Ny
aXB0PgorPC9ib2R5PgorPC9odG1sPgo=
</data>
<flag name="review"
          id="226589"
          type_id="1"
          status="+"
          setter="benjamin"
    />
          </attachment>
      

    </bug>

</bugzilla>