Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#10143 closed defect (fixed)

dojo.query uses the wrong document

Reported by: arv Owned by: alex
Priority: high Milestone: 1.4
Component: Query Version: 1.4.0b
Keywords: Cc: alex
Blocked By: Blocking:

Description

We are using a port of dojo.query in our js library and I found and fixed some issues. I haven't tested the latest copies of dojo.query so these might have been fixed already.

When using dojo.query on a different document the lookup of the element was done in the wrong document. This might have been an error in our port but I wanted to file the bug to ensure that this is not broken in Dojo.

DOM:

  <iframe name=ifr></iframe>
  <div id=iframe-test>
    <div id=if1>
      <div class=if2>
        <div id=if3></div>
      </div>
    </div>
  </div>

JS Unit Test:

function testCorrectDocumentInFrame() {
  var frameDocument = window.frames['ifr'].document;
  frameDocument.body.innerHTML =
      document.getElementById('iframe-test').innerHTML;

  var els = dojo.query('#if1 .if2 div', document);
  var frameEls = dojo.query('#if1 .if2 div', frameDocument);

  assertEquals(els.length, frameEls.length);
  assertEquals(1, frameEls.length);
  assertNotEquals(document.getElementById('if3'),
                  frameDocument.getElementById('if3'));
}

The fix was to get the owner document from the root node inside retFunc inside getElementsFunc:

        retFunc = function(root, arr) {
          var te = GET_OWNER_DOCUMENT(root).getElementById(query.id);
          if (!te || !filterFunc(te)) {
            return;
          }
          if (9 == root.nodeType) { // If root's a doc, we just return directly.
            return getArr(te, arr);
          } else { // otherwise check ancestry
            if (_isDescendant(te, root)) {
              return getArr(te, arr);
            }
          }
        }

Change History (3)

comment:1 Changed 10 years ago by alex

Status: newassigned

comment:2 Changed 9 years ago by James Burke

Resolution: fixed
Status: assignedclosed

Thanks for the report. It looks like that particular retFunc is already using ownerDocument: http://bugs.dojotoolkit.org/browser/dojo/trunk/_base/query.js?rev=20851#L990

and there is an iframe test in the dojo.query tests.

comment:3 Changed 9 years ago by bill

Milestone: tbd1.4
Note: See TracTickets for help on using tickets.