within-element
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -5,3 +5,3 @@ { | ||
"description": "check if an element is within the element", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -8,0 +8,0 @@ "dom", |
0.1.0 / 2014-06-17 | ||
================== | ||
* test: add some selectable text to the page | ||
* index: add Range object support | ||
0.0.1 / 2014-06-14 | ||
@@ -3,0 +9,0 @@ ================== |
11
index.js
@@ -5,3 +5,3 @@ | ||
* | ||
* @param {DOMElement} child - the element to check if it with within `parent` | ||
* @param {DOMElement|Range} child - the DOM element or Range to check if it's within `parent` | ||
* @param {DOMElement} parent - the parent node that `child` could be inside of | ||
@@ -13,2 +13,10 @@ * @return {Boolean} True if `child` is within `parent`. False otherwise. | ||
module.exports = function within (child, parent) { | ||
// don't throw if `child` is null | ||
if (!child) return false; | ||
// Range support | ||
if (child.commonAncestorContainer) child = child.commonAncestorContainer; | ||
else if (child.endContainer) child = child.endContainer; | ||
// traverse up the `parentNode` properties until `parent` is found | ||
var node = child; | ||
@@ -18,3 +26,4 @@ while (node = node.parentNode) { | ||
} | ||
return false; | ||
}; |
{ | ||
"name": "within-element", | ||
"description": "check if an element is within the element", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"keywords": [ | ||
@@ -6,0 +6,0 @@ "browser", |
Sorry, the diff of this file is not supported yet
2715
37