This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* @module contains | ||
*/ | ||
|
||
/** | ||
* Test whether an element contains another element in the DOM. | ||
* | ||
* @param {Element} container The element that may contain the other element. | ||
* @param {Element} element The element that may be a descendant of the other element. | ||
* @return {Boolean} Whether the `container` element contains the `element`. | ||
* @example | ||
* $.contains(parentElement, childElement); | ||
* ➤ true/false | ||
*/ | ||
|
||
function contains(container, element) { | ||
if(!container || !element || container === element) { | ||
return false; | ||
} else if (container.contains) { | ||
return container.contains(element); | ||
} else if (container.compareDocumentPosition) { | ||
return !(container.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_DISCONNECTED); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
/* | ||
* Export interface | ||
*/ | ||
|
||
export { contains }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
describe('contains', function() { | ||
|
||
it('should recognize descendant element is contained by other element', function() { | ||
var container = document, | ||
element = $('#testFragment')[0]; | ||
expect($.contains(container, element)).to.be.true; | ||
}); | ||
|
||
it('should recognize descendant element is contained by other element (within detached fragment)', function() { | ||
var container = $('<div><span></span></div>'), | ||
element = container.find('span')[0]; | ||
expect($.contains(container[0], element)).to.be.true; | ||
}); | ||
|
||
it('should recognize ancestor element is not contained by other element', function() { | ||
var container = document, | ||
element = $('#testFragment')[0]; | ||
expect($.contains(element, container)).to.be.false; | ||
}); | ||
|
||
it('should recognize element is not contained by itself', function() { | ||
var element = $('#testFragment')[0]; | ||
expect($.contains(element, element)).to.be.false; | ||
}); | ||
|
||
}); |
e437be2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Useful additions, cheers.
e437be2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @tomByrer, appreciated!