/* * Copyright(c) 2011 * OJC Technologies * all rights reserved * @author: Jake Smith */ function CursorFunctor() { this.getEndOffset = function (text, startI) { var endI, testChar; endI = startI + 1; testChar = text.substring(startI, endI); while (true) { if (this.isBreakingWhitespace(testChar)) { endI = startI + 1; startI = endI; testChar = text.substring(startI, startI + 1); } else { break; } } return endI; } this.getStartOffset = function(text, startI) { var endI, testChar; endI = startI + 1; testChar = text.substring(startI, endI); while (true) { if (this.isBreakingWhitespace(testChar)) { startI = endI - 1; endI = startI; testChar = text.substring(endI - 1, endI); } else { break; } } return startI; } this.isNodeCollapsed = function(n) { var rect; rect = n.getBoundingClientRect(); return rect.width == 0; } this.isToPreserveWhitespace = function(givenNode) { var tag, nodeParent; tag = givenNode.tagName; if (tag == "BODY") { return false; } else if (tag == "PRE") { return true; } else { nodeParent = givenNode.parentNode; return this.isToPreserveWhitespace(nodeParent); } } this.isWhitespace = function(text_in) { var pattern = /^\s+$/; if (text_in.match(pattern)) { return true; } return false; } this.isBreakingWhitespace = function(text_in) { var pattern = /[^\u00A0]/; if (text_in.match(pattern) && this.isWhitespace(text_in)) { return true; } return false; } this.isNBSP = function(text_in) { var pattern = /\u00A0/; if (text_in.match(pattern) && this.isWhitespace(text_in)) { return true; } return false; } this.getNodeFollowing = function(givenNode) { var nextSib, leafTest, parent; parent = givenNode.parentNode; nextSib = givenNode.nextSibling; if (givenNode.tagName == 'BODY') { return null; } else if (nextSib == null) { return this.getNodeFollowing(parent); } else { leafTest = this.testForLeafL2R(nextSib); if (leafTest != null) { return leafTest; } else { throw new Error("supposedly impossible state in " + " CursorFunctor.getNodeFollowing(givenNode)"); //return this.getNodeFollowing(nextSib); } } } this.getNodePreceding = function(givenNode) { var prevSib, leafTest, parent; parent = givenNode.parentNode; prevSib = givenNode.previousSibling; if (givenNode.tagName == 'BODY') { return null; } else if (prevSib == null) { return this.getNodePreceding(parent); } else { leafTest = this.testForLeafR2L(prevSib); if (leafTest != null) { return leafTest; } else { throw new Error("supposedly impossible state in " + " CursorFunctor.getNodeFollowing(givenNode)"); //return this.getNodeFollowing(nextSib); } } } //-----------------------// // "Private" functions // //-----------------------// this.testForLeafL2R = function(someNode) { var i, leafTest, child; if (someNode.childNodes.length == 0) { // is a leaf! return someNode; } for (i = 0; i < someNode.childNodes.length; i += 1) { child = someNode.childNodes[i]; leafTest = this.testForLeafL2R(child); if (leafTest != null) { return leafTest; } } return null; } this.testForLeafR2L = function(someNode) { var i, leafTest, child; if (someNode.childNodes.length == 0) { // is a leaf! return someNode; } for (i = someNode.childNodes.length - 1; i >= 0; i -= 1) { child = someNode.childNodes[i]; leafTest = this.testForLeafR2L(child); if (leafTest != null) { return leafTest; } } return null; } } // CursorFunctor()