Hi, tried to add this patch to the trac ticket that I raised but couldn't, so it is here instead. Sorry if I didn't follow the correct protocol;
8<---- Refactored isChildNode to use MochiKit.Base predicates. Changed logic in tests to guard against cases when the child or parent node is not part of the DOM. Added extra tests to test suite, verified working on FF2, IE7. diff -r 8d983df7eccc MochiKit/DOM.js --- a/MochiKit/DOM.js Wed Apr 30 17:16:32 2008 +1000 +++ b/MochiKit/DOM.js Mon May 05 11:48:42 2008 +1000 @@ -352,20 +352,25 @@ /** @id MochiKit.DOM.isChildNode */ isChildNode: function (node, maybeparent) { var self = MochiKit.DOM; - if (typeof(node) == "string") { - node = self.getElement(node); - } - if (typeof(maybeparent) == "string") { - maybeparent = self.getElement(maybeparent); - } - if (typeof(node) == 'undefined' || node === null) { - return false; - } - while (node !== self._document) { + // ensure we are dealing with DOM elements not IDs + node = self.getElement(node); + maybeparent = self.getElement(maybeparent); + var tagName = null; + + if (node && maybeparent && (node !== self._document)) { + while (node) { if (node === maybeparent) { - return true; + return true; } + + // not all nodes have tagNames, eg text nodes. + tagName = node.tagName && node.tagName.toUpperCase(); + if ((tagName === "BODY") || (tagName === "HTML")) { + break; + } + node = node.parentNode; + } } return false; }, diff -r 8d983df7eccc tests/test_MochiKit-DOM.html --- a/tests/test_MochiKit-DOM.html Wed Apr 30 17:16:32 2008 +1000 +++ b/tests/test_MochiKit-DOM.html Mon May 05 11:48:42 2008 +1000 @@ -4,8 +4,8 @@ <script type="text/javascript" src="../MochiKit/Base.js"></ script> <script type="text/javascript" src="../MochiKit/Iter.js"></ script> <script type="text/javascript" src="../MochiKit/DOM.js"></script> - <script type="text/javascript" src="../MochiKit/Style.js"></ script> - <script type="text/javascript" src="SimpleTest/SimpleTest.js"></ script> + <script type="text/javascript" src="../MochiKit/Style.js"></ script> + <script type="text/javascript" src="SimpleTest/SimpleTest.js"></ script> <link rel="stylesheet" type="text/css" href="SimpleTest/ test.css"> </head> <body> @@ -69,8 +69,8 @@ is( lst.join(" "), "original new", "callStack in correct order (abort)" ); o.blah(); is( lst.join(" "), "original new original new", "callStack in correct order (again)" ); - - + + is( escapeHTML("<>\"&bar"), "<>"&bar", "escapeHTML" ); // for emacs highlighting: " var isDOM = function (value, expected, message) { @@ -93,7 +93,7 @@ isDOM( d, '<span>word up<span/>Think Different</span>', 'insertSiblingNodesBefore' ); insertSiblingNodesAfter(d.childNodes[0], 'purple monkey', document.createElement('span')); - isDOM( d, '<span>word uppurple monkey<span/><span/>Think Different</span>', 'insertSiblingNodesAfter' ); + isDOM( d, '<span>word uppurple monkey<span/><span/>Think Different</span>', 'insertSiblingNodesAfter' ); d = createDOM("span"); isDOM( d, "<span/>", "createDOM empty" ); @@ -107,7 +107,7 @@ is( getNodeAttribute(d, 'baz'), "wibble", "createDOM attribute" ); removeNodeAttribute(d, "spam"); is( scrapeText(d), "onetwothree", "createDOM contents" ); - + isDOM( d, '<span baz="wibble" foo="bar">onetwothree</span>', "createDOM contents" ); d = createDOM("span", null, function (f) { @@ -136,7 +136,7 @@ domConverters.unregister("taco"); isDOM( d, "<span>Goddamn, I like pork tacos</span>", "createDOM with custom converter" ); - + is( escapeHTML(toHTML(SPAN(null))), escapeHTML(toHTML(createDOM("span", null))), @@ -148,8 +148,8 @@ var st = DIV(null, STRONG(null, "d"), "oor ", STRONG(null, "f", SPAN(null, "r"), "a"), "me"); is( scrapeText(st), "door frame", "scrape in-order" ); - - + + ok( !isUndefinedOrNull(getElement("test")), "getElement might work" ); ok( !isUndefinedOrNull($("test")), "$alias$$ CASH MONEY alias might work" ); @@ -171,10 +171,10 @@ toggleElementClass("bar", d); ok( d.className == "baz", "toggleElementClass: " + d.className); toggleElementClass("bar", d); - ok( hasElementClass(d, "baz", "bar"), + ok( hasElementClass(d, "baz", "bar"), "toggleElementClass 2: " + d.className); addElementClass(d, "bar"); - ok( hasElementClass(d, "baz", "bar"), + ok( hasElementClass(d, "baz", "bar"), "toggleElementClass 3: " + d.className); ok( addElementClass(d, "blah"), "addElementClass return"); ok( hasElementClass(d, "baz", "bar", "blah"), "addElementClass action"); @@ -221,7 +221,7 @@ "tr0 tr1", "getElementsByTagAndClassName found all tr tags" ); - + var oldDoc = document; var doc = MochiKit.MockDOM.createDocument(); is( currentDocument(), document, "currentDocument() correct" ); @@ -282,7 +282,7 @@ is( kv[0].join(","), "selempty,selempty2", "formContents names empty option values" ); is( kv[1].join(","), ",foo", "formContents empty option values" ); is( queryString("form_test2"), "selempty=&selempty2=foo", "queryString empty option values" ); - + var d = DIV(null, SPAN(), " \n\t", SPAN(), "foo", SPAN(), " "); is( d.childNodes.length, 6, "removeEmptyNodes test conditions correct" ); removeEmptyTextNodes(d); @@ -302,11 +302,14 @@ ok( isChildNode('child', document.body), "isChildNode of body"); ok( isChildNode($('child').firstChild, 'parentTwo'), "isChildNode text node"); + ok( !isChildNode( SPAN(), document.body), "isChildNode child not in DOM"); + ok( !isChildNode( SPAN(), 'child'), "isChildNode child not in DOM"); + + ok( !isChildNode( 'child', SPAN()), "isChildNode parent not in DOM"); + ok( true, "test suite finished!"); - - } catch (err) { - + var s = "test suite failure!\n"; var o = {}; var k = null; --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "MochiKit" group. To post to this group, send email to mochikit@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/mochikit?hl=en -~----------~----~----~----~------~----~------~--~---