Skip to content

Instantly share code, notes, and snippets.

@james-jlo-long
Created October 1, 2015 15:16
Show Gist options
  • Save james-jlo-long/720f69e56bacce5dd85b to your computer and use it in GitHub Desktop.
Save james-jlo-long/720f69e56bacce5dd85b to your computer and use it in GitHub Desktop.
Just enough of a framework to get DOM nodes common ancestors - based on https://gist.github.com/benpickles/4059636
var dom = (function () {
'use strict';
var dom = {};
function isElement(element) {
return element instanceof HTMLElement;
}
function parents(element) {
var parents = [];
while (isElement(element)) {
parents.push(element);
element = element.parentNode;
}
return parents;
}
function common() {
var elements = util.Array.slice(arguments);
if (elements.length === 1 && util.Array.isIterable(elements[0])) {
elements = util.Array.from(elements[0]);
}
return util.Array.common.apply(
undefined,
util.Array.map(elements, parents)
)[0];
}
util.Object.assign(dom, {
isElement: isElement,
parents: parents,
common: common
});
return dom;
}());
<!DOCTYPE html>
<html>
<head>
<title>Common Ancestor Tests</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.10.0.css">
<script src="./util.js"></script>
<script src="./dom.js"></script>
</head>
<body>
<div id="qunit"></div>
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
<script src="./test.js"></script>
</body>
</html>
function createElement(parent) {
var elem = document.createElement("div")
if (parent) {
parent.appendChild(elem)
}
return elem
}
var root = createElement() // .
var elem1 = createElement(root) // ├── elem1
var elem2 = createElement(elem1) // | ├── elem2
var elem3 = createElement(elem1) // | └── elem3
var elem4 = createElement(elem3) // | └── elem4
var elem5 = createElement(root) // └── elem5
test("same parent", function() {
ok(dom.common(elem1, elem5) === root)
})
test("same parent, simulating NodeList", function() {
ok(dom.common([elem1, elem5]) === root)
})
test("same parent but deeper", function() {
ok(dom.common(elem4, elem5) === root)
})
test("direct child of the other", function() {
ok(dom.common(elem1, elem2) === elem1)
})
test("grandchild of the other", function() {
ok(dom.common(elem1, elem4) === elem1)
})
test("when there's no common ancestor", function() {
var separate = createElement();
ok(dom.common(elem3, separate) === undefined)
})
var util = (function () {
'use strict';
var util = {
Array: {},
Function: {},
Object: {}
};
function common(array1) {
var arrays = slice(arguments, 1);
return unique(arrays.reduce(function (array1, array2) {
var array = util.Array.from(array2);
return util.Array.from(array1).filter(function (entry) {
return array.indexOf(entry) > -1;
});
}, util.Array.from(array1)));
}
function getClass(object) {
return toString(object).slice(8, -1);
}
function identity(x) {
return x;
}
function isFunction(func) {
return typeof func === 'function' && getClass(func) === 'Function';
}
function isIterable(array) {
return array ? typeof array.length === 'number' : false;
}
function slice(array, start, end) {
return Array.prototype.slice.call(array, start, end);
}
function toString(object) {
return Object.prototype.toString.call(object);
}
function unique(array) {
return util.Array.from(array).reduce(function (prev, curr) {
if (prev.indexOf(curr) < 0) {
prev.push(curr);
}
return prev;
}, []);
}
util.Array.map = Array.map || function (array, handler, context) {
return Array.prototype.map.call(array, handler, context);
};
util.Array.from = Array.from || function (array, map, context) {
return util.Array.map(array, isFunction(map) ? map : identity, context);
};
util.Object.assign = Object.assign || function (source) {
slice(arguments, 1).forEach(function (object) {
Object.keys(object).forEach(function (key) {
source[key] = object[key];
});
});
return source;
};
util.Object.assign(util.Array, {
common: common,
isIterable: isIterable,
slice: slice,
unique: unique
});
util.Object.assign(util.Function, {
identity: identity,
isFunction: isFunction
});
util.Object.assign(util.Object, {
getClass: getClass,
toString: toString
});
return util;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment