Skip to main content

Bug Tracker

Side navigation

Ticket #2477: safari_contains_2477.diff


File safari_contains_2477.diff, 3.1 KB (added by ebartels, March 12, 2008 09:33PM UTC)

Reimpliment contains functionality to work with safari2

diff --git a/ui.sortable.js b/ui.sortable.js
index c93fcb7..91c48a8 100644
--- a/ui.sortable.js
+++ b/ui.sortable.js
@@ -1,10 +1,16 @@
 (function($) {
 
-	if (window.Node && Node.prototype && !Node.prototype.contains) {
-		Node.prototype.contains = function (arg) {
-			return !!(this.compareDocumentPosition(arg) & 16);
-		};
-	}
+	function contains(a, b) {
+		var safari2 = $.browser.safari && $.browser.version < 522;
+		if (a.contains && !safari2) {
+			return a.contains(b);
+		}
+		if (a.compareDocumentPosition)
+			return !!(a.compareDocumentPosition(b) & 16);
+		while (b = b.parentNode)
+			  if (b == a) return true;
+		return false;
+	};
 
 	$.fn.extend({
 		sortable: function(options) {
@@ -331,10 +337,10 @@
 
 			this.propagate("stop", e); //Call plugins and trigger callbacks
 			if(this.positionDOM != this.currentItem.prev()[0]) this.propagate("update", e);
-			if(!this.element[0].contains(this.currentItem[0])) { //Node was moved out of the current element
+			if(!contains(this.element[0], this.currentItem[0])) { //Node was moved out of the current element
 				this.propagate("remove", e);
 				for (var i = this.containers.length - 1; i >= 0; i--){
-					if(this.containers[i].element[0].contains(this.currentItem[0])) {
+					if(contains(this.containers[i].element[0], this.currentItem[0])) {
 						this.containers[i].propagate("update", e, this);
 						this.containers[i].propagate("receive", e, this);
 					}
@@ -376,8 +382,8 @@
 						this.intersectsWith(this.items[i]) //items must intersect
 					&& 	this.items[i].item[0] != this.currentItem[0] //cannot intersect with itself
 					&&	this.items[i].item[this.direction == 'down' ? 'prev' : 'next']()[0] != this.currentItem[0] //no useless actions that have been done before
-					&&	!this.currentItem[0].contains(this.items[i].item[0]) //no action if the item moved is the parent of the item checked
-					&& (this.options.type == 'semi-dynamic' ? !this.element[0].contains(this.items[i].item[0]) : true)
+					&&	!contains(this.currentItem[0], this.items[i].item[0]) //no action if the item moved is the parent of the item checked
+					&& (this.options.type == 'semi-dynamic' ? !contains(this.element[0], this.items[i].item[0]) : true)
 				) {
 					
 					this.rearrange(e, this.items[i]);
@@ -393,12 +399,12 @@
 					if(!this.containers[i].containerCache.over) {
 						
 
-						if(!this.containers[i].element[0].contains(this.currentItem[0])) {
+						if(!contains(this.containers[i].element[0], this.currentItem[0])) {
 							
 							//When entering a new container, we will find the item with the least distance and append our item near it
 							var dist = 10000; var itemWithLeastDistance = null; var base = this.positionAbs[this.containers[i].floating ? 'left' : 'top'];
 							for (var j = this.items.length - 1; j >= 0; j--) {
-								if(!this.containers[i].element[0].contains(this.items[j].item[0])) continue;
+								if(!contains(this.containers[i].element[0], this.items[j].item[0])) continue;
 								var cur = this.items[j][this.containers[i].floating ? 'left' : 'top'];
 								if(Math.abs(cur - base) < dist) {
 									dist = Math.abs(cur - base); itemWithLeastDistance = this.items[j];

Download in other formats:

Original Format