--- /dev/null
+define([
+'libs/jquery-1.9.1.min',
+], function($) {
+
+'use strict';
+
+
+var nearestInDocumentOrder = function(selector, direction, element) {
+ var parents = $(element).parents(),
+ parent = parents.length ? $(parents[parents.length-1]) : element;
+
+ var adj = parent.find(selector).filter(function() {
+ return this.compareDocumentPosition(element) & (direction === 'above' ? Node.DOCUMENT_POSITION_FOLLOWING : Node.DOCUMENT_POSITION_PRECEDING);
+ });
+
+ if(adj.length) {
+ return adj[direction === 'above' ? adj.length-1 : 0];
+ }
+ return null;
+}
+
+return {
+ nearestInDocumentOrder: nearestInDocumentOrder
+};
+
+});
--- /dev/null
+define([
+'libs/chai',
+'modules/documentCanvas/canvas/utils'
+
+], function(chai, utils) {
+
+'use strict';
+
+var expect = chai.expect;
+
+
+describe('utils.nearestInDocumentOrder', function() {
+
+
+ var tests = [
+ ['return null if no match found',
+ '<span>\
+ <span></span>\
+ <div id="b">\
+ <span></span>\
+ </span>'
+ ],
+ ['returns nearest sibling if applicable',
+ '<div n1>\
+ <div n2></div>\
+ <div n3>\
+ <div id="a"></div>\
+ <div id="b"></div>\
+ <div id="c"></div>\
+ </div>\
+ <div n4></div>\
+ </div>'
+ ],
+ ['looks inside siblings children',
+ '<div>\
+ <div></div>\
+ <div>\
+ <div></div>\
+ <span>\
+ <div id="a"></div>\
+ </span>\
+ <div id="b"></div>\
+ <span>\
+ <div id="c"></div>\
+ </span>\
+ <div></div>\
+ </div>\
+ <div></div>\
+ </div>'
+ ]
+
+
+ ];
+
+ tests.forEach(function(test) {
+ var description = test[0],
+ html = test[1];
+ it(description, function() {
+ var dom = $(html),
+ a = dom.find('#a').length ? dom.find('#a')[0] : null,
+ b = dom.find('#b')[0],
+ c = dom.find('#c').length ? dom.find('#c')[0] : null;
+ expect(utils.nearestInDocumentOrder('div', 'above', b)).to.equal(a, 'above');
+ expect(utils.nearestInDocumentOrder('div', 'below', b)).to.equal(c, 'below');
+ });
+ });
+
+});
+
+});