Performance optimizations for Element#descendantOf. Costliness should no longer be dependent on the difference in depth between the parent and the child. [Andrew Dupont]

This commit is contained in:
Andrew Dupont 2007-11-01 15:11:52 +00:00
parent 916cbbaf4c
commit 7cf17c7bec
3 changed files with 45 additions and 3 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Performance optimizations for Element#descendantOf. Costliness should no longer be dependent on the difference in depth between the parent and the child. [Andrew Dupont]
* Apply the workaround for the Firefox "blinking element" opacity=1 bug only to Firefox 1.5. [Thomas Fuchs]
* Add event.stopped, a boolean that is set to `true` when Event#stop is called. [Andrew Dupont, sam]

View File

@ -358,6 +358,20 @@ Element.Methods = {
descendantOf: function(element, ancestor) {
element = $(element), ancestor = $(ancestor);
if (element.compareDocumentPosition)
return (element.compareDocumentPosition(ancestor) & 24) === 8;
if (element.sourceIndex) {
var e = element.sourceIndex, a = ancestor.sourceIndex,
nextAncestor = ancestor.nextSibling;
if (!nextAncestor) {
do { ancestor = ancestor.parentNode; }
while (!(nextAncestor = ancestor.nextSibling) && ancestor.parentNode);
}
if (nextAncestor) return (e > a && e < nextAncestor.sourceIndex);
}
while (element = element.parentNode)
if (element == ancestor) return true;
return false;

View File

@ -319,7 +319,13 @@
</p>
<p id="test-full">content</p>
<div id="ancestor"><div id="child"><div><div id="great-grand-child"></div></div></div></div>
<div id="ancestor">
<div id="child">
<div id="grand-child">
<div id="great-grand-child"></div>
</div></div><!-- intentional formatting; don't change this line -->
<div id="sibling"><div id="grand-sibling"></div></div>
</div>
<div id="not-in-the-family"></div>
<div id="insertions-container"><div id="insertions-main"><p>some content.</p></div></div>
@ -1003,8 +1009,28 @@
testDescendantOf: function() {with(this) {
assert($('child').descendantOf('ancestor'));
assert($('child').descendantOf($('ancestor')));
assert($('great-grand-child').descendantOf('ancestor'));
assert(!$('great-grand-child').descendantOf('not-in-the-family'));
assert(!$('ancestor').descendantOf($('child')));
assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor');
assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor');
assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child');
assert($('grand-child').descendantOf('child'), 'grand-child < child');
assert($('great-grand-child').descendantOf('child'), 'great-grand-child < child');
window.debug = true;
assert($('sibling').descendantOf('ancestor'), 'sibling < ancestor');
assert($('grand-sibling').descendantOf('sibling'), 'grand-sibling < sibling');
assert($('grand-sibling').descendantOf('ancestor'), 'grand-sibling < ancestor');
assert($('grand-sibling').descendantOf(document.body), 'grand-sibling < body');
window.debug = false;
assert(!$('great-grand-child').descendantOf('great-grand-child'), 'great-grand-child < great-grand-child');
assert(!$('great-grand-child').descendantOf('sibling'), 'great-grand-child < sibling');
assert(!$('sibling').descendantOf('child'), 'sibling < child');
assert(!$('great-grand-child').descendantOf('not-in-the-family'), 'great-grand-child < not-in-the-family');
assert(!$('child').descendantOf('not-in-the-family'), 'child < not-in-the-family');
}},
testChildOf: function() {with(this) {