[prev in list] [next in list] [prev in thread] [next in thread]
List: kupu-checkins
Subject: [kupu-checkins] r5530 - in kupu/branch/guido-playground: common kte
From: guido () codespeak ! net
Date: 2004-07-11 16:47:44
Message-ID: 20040711164744.8E0E95AFD2 () thoth ! codespeak ! net
[Download RAW message or body]
Author: guido
Date: Sun Jul 11 18:47:44 2004
New Revision: 5530
Modified:
kupu/branch/guido-playground/common/kupuhelpers.js
kupu/branch/guido-playground/kte/kupute.js
kupu/branch/guido-playground/tests/test_kupuhelpers.js
Log:
Basic stuff works in Mozilla now, pressing enter breaks IE still.
Modified: kupu/branch/guido-playground/common/kupuhelpers.js
==============================================================================
--- kupu/branch/guido-playground/common/kupuhelpers.js (original)
+++ kupu/branch/guido-playground/common/kupuhelpers.js Sun Jul 11 18:47:44 2004
@@ -1196,10 +1196,18 @@
// JavaScript has a friggin' blink() function, but not for string stripping...
String.prototype.strip = function() {
+ /* returns a stripped version of this */
var stripspace = /^\s*([\s\S]*?)\s*$/;
return stripspace.exec(this)[1];
};
+String.prototype.rstrip = function() {
+ /* returns an rstripped version of this */
+ var stripspace = /^([\s\S]*?)\s*$/;
+ return stripspace.exec(this)[1];
+};
+
+
//----------------------------------------------------------------------------
// Exceptions
//----------------------------------------------------------------------------
Modified: kupu/branch/guido-playground/kte/kupute.js
==============================================================================
--- kupu/branch/guido-playground/kte/kupute.js (original)
+++ kupu/branch/guido-playground/kte/kupute.js Sun Jul 11 18:47:44 2004
@@ -226,6 +226,27 @@
this.logMessage('Editor initialized');
};
+ this._initializeEventHandlers = function() {
+ /* attach the event handlers to the iframe */
+ addEventHandler(this.getInnerDocument(), "click", this.updateStateHandler, this);
+ addEventHandler(this.getInnerDocument(), "keyup", this.updateStateHandler, this);
+ addEventHandler(this.getInnerDocument(), "keydown", this.processKeyDown, this);
+ addEventHandler(this.getInnerDocument(), "keypress", this.processKeyPress, this);
+ };
+
+ this.processKeyDown = function(event) {
+ if (event.keyCode == 13) {
+ this.handleEnterPress();
+ event.preventDefault();
+ };
+ };
+
+ this.processKeyPress = function(event) {
+ if (event.keyCode == 13) {
+ event.preventDefault();
+ };
+ };
+
this._updateStateHandler = function() {
if (this.getBrowserName() == 'IE') {
this._saveSelection();
@@ -234,18 +255,22 @@
this._colorCurrentLine();
};
- this._handleEnterPress = function() {
+ this.handleEnterPress = function() {
/* add a new line */
- // XXX Not working yet...
+ var doc = this.getInnerDocument();
var currNode = this.getSelectedNode();
var linediv = this.getNearestParentOfType(currNode, 'div');
-
- var oldlinecontent = linediv.childNodes[0].nodeValue;
-
- oldlinecontent = rstrip(oldlinecontent);
+
+ // get the offset of the selection inside the div
+ var offset = this.getDivSelectionOffset(currNode, linediv);
+ var oldlinecontent = this.colorizer.getText(linediv);
+
+ // remove any empty lines (containing just a space)
+ oldlinecontent = oldlinecontent.rstrip();
if (offset == 1 && !oldlinecontent) {
offset = 0;
};
+ // create a new line
var newlinecontent = '';
if (oldlinecontent.length > offset) {
newlinecontent = oldlinecontent.substr(offset);
@@ -254,17 +279,50 @@
if (!oldlinecontent) {
oldlinecontent = '\xa0';
};
- linediv.replaceChild(this.getInnerDocument().createTextNode(oldlinecontent),
- linediv.childNodes[0]);
-
- this.processLineDiv(linediv);
-
- // now add a new linediv
- // XXX does nextSibling always return the next direct child of the parent?
- var newlinediv = this.addNewLineDiv(linediv, newlinecontent);
-
+ while (linediv.hasChildNodes()) {
+ linediv.removeChild(linediv.firstChild);
+ };
+
+ if (!newlinecontent.length > 0) {
+ newlinecontent = ' ';
+ };
+
+ // now a bit of a filthy trick: since the DOM doesn't provide an
+ // insertAfter method we swap the old and new content and use
+ // insertBefore
+ linediv.appendChild(doc.createTextNode(newlinecontent));
+ this.colorizer.colorizeLine(linediv);
+
+ var newlinediv = doc.createElement('div');
+ newlinediv.appendChild(doc.createTextNode(oldlinecontent));
+ this.colorizer.colorizeLine(newlinediv);
+
+ linediv.parentNode.insertBefore(newlinediv, linediv);
+
// and select it
- this.placeSelectionInDiv(newlinediv, 0);
+ var selection = this.getSelection();
+ selection.selectNodeContents(linediv.childNodes[0]);
+ selection.collapse();
+ };
+
+ this.getDivSelectionOffset = function(selNode, div) {
+ /* return the selection offset from node's start */
+ var sel = this.getSelection();
+ var offset = sel.startOffset();
+ var startNode = sel.startNode();
+ // startNode can be a TextNode, which we don't check
+ if (startNode.nodeType == 3) {
+ startNode = startNode.parentNode;
+ };
+ // we can assume a div with only spans with only text content
+ for (var i=0; i < div.childNodes.length; i++) {
+ var child = div.childNodes[i];
+ if (child == startNode) {
+ return offset;
+ };
+ offset += this.colorizer.getText(child).length;
+ };
+ return -1;
};
this._colorCurrentLine = function() {
Modified: kupu/branch/guido-playground/tests/test_kupuhelpers.js
==============================================================================
--- kupu/branch/guido-playground/tests/test_kupuhelpers.js (original)
+++ kupu/branch/guido-playground/tests/test_kupuhelpers.js Sun Jul 11 18:47:44 2004
@@ -79,6 +79,32 @@
this.assertEquals(str.strip(), "tu quoque Brute filie mee");
};
+ this.testStringRstrip = function() {
+ // an empty string
+ var str = "";
+ this.assertEquals(str.rstrip(), str);
+ // a string only containg whitespace
+ str = " \n \t ";
+ this.assertEquals(str.rstrip(), "");
+ // a string not containg any whitespaces
+ str = "foo"
+ this.assertEquals(str.rstrip(), str);
+ // a word wrapped around whitespace
+ str = "\n foo \t ";
+ this.assertEquals(str.rstrip(), "\n foo");
+ // a single character wrapped in whitespace
+ str = "\n\t a \t\n";
+ this.assertEquals(str.rstrip(), "\n\t a");
+ // a string containing whitespace in the middle
+ str = "foo bar baz";
+ this.assertEquals(str.rstrip(), str);
+ // a string containing spaces around it and in it
+ str = " \t foo bar\n baz ";
+ this.assertEquals(str.rstrip(), " \t foo bar\n baz");
+ str = " tu quoque Brute filie mee ";
+ this.assertEquals(str.rstrip(), " tu quoque Brute filie mee");
+ };
+
this.testLoadDictFromXML = function() {
var dict = loadDictFromXML(document, 'xmlisland');
this.assertEquals(dict['foo'], 'bar');
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic