Skip to content

Commit 9ca0afa

Browse files
Ensure descendantOf doesn’t try to compare an element with a non-element. [close prototypejs#152]
1 parent bc8a84c commit 9ca0afa

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/prototype/dom/dom.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,20 +2137,23 @@
21372137
**/
21382138
function descendantOf_DOM(element, ancestor) {
21392139
element = $(element), ancestor = $(ancestor);
2140+
if (!element || !ancestor) return false;
21402141
while (element = element.parentNode)
21412142
if (element === ancestor) return true;
21422143
return false;
21432144
}
21442145

21452146
function descendantOf_contains(element, ancestor) {
21462147
element = $(element), ancestor = $(ancestor);
2148+
if (!element || !ancestor) return false;
21472149
// Some nodes, like `document`, don't have the "contains" method.
21482150
if (!ancestor.contains) return descendantOf_DOM(element, ancestor);
21492151
return ancestor.contains(element) && ancestor !== element;
21502152
}
21512153

21522154
function descendantOf_compareDocumentPosition(element, ancestor) {
21532155
element = $(element), ancestor = $(ancestor);
2156+
if (!element || !ancestor) return false;
21542157
return (element.compareDocumentPosition(ancestor) & 8) === 8;
21552158
}
21562159

test/unit/tests/dom.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ suite('DOM', function () {
867867
assert(!$('ancestor').descendantOf($('child')),
868868
'#ancestor should not be descendant of child');
869869

870+
assert(!$('child').descendantOf($('non-existent-thing')), 'cannot be a descendant of a non-element');
871+
assert(!Element.descendantOf('non-existent-thing', $('ancestor')), 'non-element cannot be a descendant of anything');
872+
870873
assert($('great-grand-child').descendantOf('ancestor'), 'great-grand-child < ancestor');
871874
assert($('grand-child').descendantOf('ancestor'), 'grand-child < ancestor');
872875
assert($('great-grand-child').descendantOf('grand-child'), 'great-grand-child < grand-child');

0 commit comments

Comments
 (0)