Ticket #297 (closed defect: fixed)

Opened 3 months ago

Last modified 3 months ago

MochiKit.DOM.getFirstParentByTagAndClassName throws error if no match is found

Reported by: cederberg@gmail.com Assigned to: cederberg@gmail.com
Priority: normal Milestone: MochiKit 1.4
Component: component1 Version:
Severity: normal Keywords:
Cc:

Description

If no matching parent DOM node is found by MochiKit.DOM.getFirstParentByTagAndClassName(), an error with message "elem.className has no properties" will be thrown. This is caused by a missing check for a null elem.parentNode in the iteration.

...
while (elem && elem.tagName) {
    // XXX: oops, elem.parentNode might be null...
    elem = elem.parentNode;
    if (tagName == '*' && className === null) {
        return elem;
    }
    // XXX: exception might be thrown by the following line
    classList = elem.className.split(' ');

Change History

03/26/08 07:20:07 changed by cederberg@gmail.com

It seems like a simple check for a null elem.parentNode is not enough. Some parentNodes are not HTML nodes, but document and similar. The following works for me:

while (elem && elem.tagName &&
       typeof(elem.parentNode.className) === "string") {

But perhaps it would be better to increment elem at the end of the loop instead of at the beginning.

03/26/08 11:12:22 changed by bob@redivi.com

it would probably be best to increment elem at the end of the loop, but I think the typeof() check may still be necessary, no?

03/26/08 11:57:06 changed by cederberg@gmail.com

  • status changed from new to closed.
  • resolution set to fixed.

Fixed in [1346]. After refactoring, the code logic is easier to follow.

03/27/08 06:24:36 changed by cederberg@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

There's a bug in the [1346] fix. It stops the iteration on any DOM node that has an empty className string.

        while (elem && elem.tagName && elem.className) {

should be

        while (elem && elem.tagName && typeof(elem.className) == "string") {

03/27/08 06:25:18 changed by cederberg@gmail.com

  • owner changed from somebody to cederberg@gmail.com.
  • status changed from reopened to new.

03/27/08 09:37:31 changed by cederberg@gmail.com

  • status changed from new to closed.
  • resolution set to fixed.

Fixed again in [1347].