[snowy] Multibrowser support and implement normal style



commit 0004a982eaecbe357976c7ad48d43c0432bc0950
Author: Brad Taylor <brad getcoded net>
Date:   Wed May 13 14:58:13 2009 -0400

    Multibrowser support and implement normal style
    
    Multibrowser support is untested, and I'm still looking into some buggy
    behavior with the normal style code.  I'm planning on testing with Safari now.
---
 site_media/js/funcooker.js |   42 +++++++++++++++++++++++++++++++++++++++---
 1 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/site_media/js/funcooker.js b/site_media/js/funcooker.js
index 2abacf4..bf58fae 100644
--- a/site_media/js/funcooker.js
+++ b/site_media/js/funcooker.js
@@ -33,7 +33,29 @@ var FunCooker = DUI.Class.create({
     },
 
     normalStyle: function() {
-        // TODO:
+        var range = this.getSelectionRange();
+        if (!range) {
+            return;
+        }
+
+        if (range.startContainer == range.endContainer) {
+            // This range has no formatting
+            return;
+        }
+
+        var iter = range.startContainer;
+        do {
+            iter = iter.nextSibling;
+            if (!iter) {
+                continue;
+            }
+
+            range.startContainer.nodeValue += iter.innerHTML;
+
+            $(iter).remove();
+        } while (iter != range.endContainer);
+
+        this.target.focus();
     },
 
     bold: function() {
@@ -91,7 +113,7 @@ var FunCooker = DUI.Class.create({
     },
 
     wrapSelection: function(wrapper) {
-        var range = this.getDocumentSelection();
+        var range = this.getSelectionRange();
         if (range) {
             // insure that the selection range is inside of the editor
             var parent = this.findParentById(range.commonAncestorContainer,
@@ -109,8 +131,22 @@ var FunCooker = DUI.Class.create({
     },
 
     getDocumentSelection: function() {
-        if ($.browser.mozilla) {
+        if (window.getSelection) {
+            return window.getSelection();
+        } else if (document.selection) { // IE
+            return document.selection.createRange();
+        }
+    },
+
+    getSelectionRange: function() {
+        var selection = this.getDocumentSelection();
+        if (selection.getRangeAt) {
             return window.getSelection().getRangeAt(0);
+        } else { // Safari
+            var range = document.createRange();
+            range.setStart(selection.anchorNode, selection.anchorOffset);
+            range.setEnd(selection.focusNode, selection.focusOffset);
+            return range;
         }
     },
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]