Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
Add "contains" module
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jun 9, 2014
1 parent 1bb97e9 commit e437be2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ if (typeof selector !== 'undefined') {
api.find = selector.find;
}

module contains from './contains'; extend($, contains);
module mode from './mode'; extend($, mode);
module noconflict from './noconflict'; extend($, noconflict);
module type from './type'; extend($, type);
Expand Down
32 changes: 32 additions & 0 deletions src/contains.js
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 };
8 changes: 1 addition & 7 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,7 @@ function isAttachedToDocument(element) {
if (element === window || element === document) {
return true;
}
var container = element.ownerDocument.documentElement;
if (container.contains) {
return container.contains(element);
} else if (container.compareDocumentPosition) {
return !(container.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_DISCONNECTED);
}
return false;
return $.contains(element.ownerDocument.documentElement, element);
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<script src="spec/array.js"></script>
<script src="spec/attr.js"></script>
<script src="spec/class.js"></script>
<script src="spec/contains.js"></script>
<script src="spec/css.js"></script>
<script src="spec/data.js"></script>
<script src="spec/dom.js"></script>
Expand Down
26 changes: 26 additions & 0 deletions test/spec/contains.js
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;
});

});

2 comments on commit e437be2

@tomByrer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful additions, cheers.

@webpro
Copy link
Owner Author

@webpro webpro commented on e437be2 Jun 9, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @tomByrer, appreciated!

Please sign in to comment.