[kupu-checkins] r47378 - in kupu/trunk/kupu: common plone

duncan at codespeak.net duncan at codespeak.net
Wed Oct 10 13:29:25 CEST 2007


Author: duncan
Date: Wed Oct 10 13:29:24 2007
New Revision: 47378

Modified:
   kupu/trunk/kupu/common/kupudrawers.js
   kupu/trunk/kupu/common/kupuhelpers.js
   kupu/trunk/kupu/plone/plonelibrarytool.py
Log:
Work round missing Selection methods for Safari compatability

Modified: kupu/trunk/kupu/common/kupudrawers.js
==============================================================================
--- kupu/trunk/kupu/common/kupudrawers.js	(original)
+++ kupu/trunk/kupu/common/kupudrawers.js	Wed Oct 10 13:29:24 2007
@@ -440,7 +440,7 @@
             var there = preview.contentWindow.location.href;
         } catch(e) { return; }
 
-        if (here != there && !(/^about:/.test(there))) {
+        if (there && here != there && !(/^about:/.test(there))) {
             input.value = there;
         }
         this.showAnchors(currentAnchor());
@@ -926,8 +926,10 @@
 
         if (this.editor.getBrowserName() == 'IE') {
             newitemsnode = newitemsnode.cloneNode(true);
+            if (newbc) newbc = newbc.cloneNode(true);
         } else {
             newitemsnode = this.xmldata.importNode(newitemsnode, true);
+            if (newbc) newbc = this.xmldata.importNode(newbc, true);
         }
         if (newbc) {
             if (bcnode) {
@@ -1297,7 +1299,7 @@
         if (this.editor.getBrowserName() == 'IE') {
             resultlib = resultlib.cloneNode(true);
         } else {
-            this.xmldata.importNode(resultlib, true);
+            resultlib = this.xmldata.importNode(resultlib, true);
         }
         var libraries = this.xmldata.selectSingleNode("/libraries");
         libraries.appendChild(resultlib);
@@ -1398,10 +1400,20 @@
     }
 
     this.createContent = function() {
+        function getSel(sel, p, t) {
+            var nodes = p.getElementsByTagName(t);
+            for (var i = 0; i < nodes.length; i++) {
+                if (sel.containsNode(nodes[i])) {
+                    return nodes[i];
+                };
+            };
+        }
         var ed = this.editor;
+        var sel = ed.getSelection();
         var currnode = ed.getSelectedNode();
-        var currimg = ed.getNearestParentOfType(currnode, 'OBJECT') || ed.getNearestParentOfType(currnode, 'IMG');
-        this.selectedSrc = currimg.data||currimg.src||null;
+        var currimg = ed.getNearestParentOfType(currnode, 'OBJECT') || ed.getNearestParentOfType(currnode, 'IMG') ||
+                      getSel(sel, currnode, 'object') || getSel(sel, currnode, 'img');
+        this.selectedSrc = currimg?(currimg.data||currimg.src||null):null;
         this.options = {};
         if (currimg) {
             ed.getSelection().selectNodeContents(currimg);

Modified: kupu/trunk/kupu/common/kupuhelpers.js
==============================================================================
--- kupu/trunk/kupu/common/kupuhelpers.js	(original)
+++ kupu/trunk/kupu/common/kupuhelpers.js	Wed Oct 10 13:29:24 2007
@@ -460,13 +460,27 @@
 function MozillaSelection(document) {
     this.document = document;
     this.selection = document.getWindow().getSelection();
-    
+
+    this._createRange = function() {
+        return this.document.getDocument().createRange();
+    }
     this.selectNodeContents = function(node) {
         if (node && node.parentNode) {
             /* select the contents of a node */
-            this.selection.removeAllRanges();
-            this.selection.selectAllChildren(node);
-        }
+            var sel = this.selection;
+            sel.removeAllRanges();
+            if (sel.selectAllChildren) {
+                sel.selectAllChildren(node);
+            } else {
+                var range = this._createRange();
+                try {
+                    range.selectNode(node);
+                } catch (e) {
+                    range.selectNodeContents(node);
+                };
+                sel.addRange(range);
+            };
+        };
     };
 
     this.collapse = function(collapseToEnd) {
@@ -504,7 +518,7 @@
         var pos = range.startOffset;
 
         // make a new range for the new selection
-        var range = this.document.getDocument().createRange();
+        var range = this._createRange();
 
         if (container.nodeType == 3 && node.nodeType == 3) {
             // if we insert text in a textnode, do optimized insertion
@@ -616,9 +630,9 @@
         var aoffset = this.selection.anchorOffset;
         var onode = this.selection.focusNode;
         var ooffset = this.selection.focusOffset;
-        var arange = this.document.getDocument().createRange();
+        var arange = this._createRange();
         arange.setStart(anode, aoffset);
-        var orange = this.document.getDocument().createRange();
+        var orange = this._createRange();
         orange.setStart(onode, ooffset);
         return arange.compareBoundaryPoints('START_TO_START', orange) <= 0 ? anode : onode;
     };
@@ -672,9 +686,9 @@
         var aoffset = this.selection.anchorOffset;
         var onode = this.selection.focusNode;
         var ooffset = this.selection.focusOffset;
-        var arange = this.document.getDocument().createRange();
+        var arange = this._createRange();
         arange.setStart(anode, aoffset);
-        var orange = this.document.getDocument().createRange();
+        var orange = this._createRange();
         orange.setStart(onode, ooffset);
         return arange.compareBoundaryPoints('START_TO_START', orange) > 0 ? anode : onode;
     };
@@ -774,8 +788,8 @@
                 {
                     var parent1 = parent;
                     var parent2 = null;
-                    var range1 = this.document.getDocument().createRange();
-                    var range2 = this.document.getDocument().createRange();
+                    var range1 = this._createRange();
+                    var range2 = this._createRange();
                 
                     var parent2 = this.parentElementOfRange(this.selection.getRangeAt(i));
 
@@ -796,7 +810,7 @@
                         //just one node, which we don't want; but since parent1
                         //and parent2 are different, their range is not just
                         //one node
-                        var coverRange = this.document.getDocument().createRange();
+                        var coverRange = this._createRange();
                         coverRange.setStartBefore(parent1);
                         coverRange.setEndAfter(parent2);
                         parent = coverRange.commonAncestorContainer;
@@ -806,7 +820,7 @@
                         //just one node, which we don't want; but since parent1
                         //and parent2 are different, their range is not just
                         //one node
-                        var coverRange = this.document.getDocument().createRange();
+                        var coverRange = this._createRange();
                         coverRange.setStartBefore(parent2);
                         coverRange.setEndAfter(parent1);
                         parent = coverRange.commonAncestorContainer;                    
@@ -961,7 +975,18 @@
     };
 
     this.containsNode = function(node) {
-        return this.selection.containsNode(node, true);
+        var sel = this.selection;
+        if (sel.containsNode) {
+            return sel.containsNode(node, true);
+        } else {
+            // kludge it for safari
+            for(var i = 0; i < sel.rangeCount; i++ ) {
+                if( sel.getRangeAt(i).containsNode(node) ) {
+                    return true;
+                }
+            };
+            return false;
+        }
     };
 
     this.toString = function() {
@@ -991,6 +1016,8 @@
                 nodeRange.selectNodeContents(node);
             };
 
+            // selection end after node start and selection start
+            // before node end
             return this.compareBoundaryPoints(Range.END_TO_START, nodeRange) == -1 &&
                 this.compareBoundaryPoints(Range.START_TO_END, nodeRange) == 1;
         };
@@ -1003,6 +1030,21 @@
         };
         return false;
     };
+    if( !Range.prototype.containsNode ){
+        Range.prototype.containsNode = function(node) {
+            var nodeRange = node.ownerDocument.createRange();
+            try {
+                nodeRange.selectNode(node);
+            } catch (e) {
+                nodeRange.selectNodeContents(node);
+            };
+
+            // selection start not after node start and selection end
+            // not before node end.
+            return this.compareBoundaryPoints(Range.START_TO_START, nodeRange) != -1 &&
+                    this.compareBoundaryPoints(Range.END_TO_END, nodeRange) != 1;
+        };
+    };
 };
 
 MozillaSelection.prototype = new BaseSelection;

Modified: kupu/trunk/kupu/plone/plonelibrarytool.py
==============================================================================
--- kupu/trunk/kupu/plone/plonelibrarytool.py	(original)
+++ kupu/trunk/kupu/plone/plonelibrarytool.py	Wed Oct 10 13:29:24 2007
@@ -360,9 +360,12 @@
                 verno = getver(' rv:')
                 if verno:
                     return verno >= (1,3,1)
-#                 verno = getver(' AppleWebKit/')
-#                 if verno:
-#                     return verno >= (420,)
+                verno = getver(' AppleWebKit/')
+                if verno:
+                    return verno >= (523,6)
+                    verno = getver(' Safari/')
+                    if verno:
+                        return verno >= (522,12)
 
             verno = getver('MSIE')
             if verno:


More information about the kupu-checkins mailing list