📄 kupuhelpers.js
字号:
if (selectAfterPlace) { // see MozillaSelection.replaceWithNode() for some comments about // selectAfterPlace var temprange = this.document.getDocument().body.createTextRange(); if (selectAfterPlace.nodeType == 1) { temprange.moveToElementText(selectAfterPlace); } else { temprange.moveToElementText(newnode); }; //temprange.moveEnd('character', -1); temprange.select(); }; }; this.selection = this.document.getDocument().selection; return newnode; }; this.startOffset = function() { var startoffset = 0; var selrange = this.selection.createRange(); var parent = selrange.parentElement(); var elrange = selrange.duplicate(); elrange.moveToElementText(parent); var tempstart = selrange.duplicate(); while (elrange.compareEndPoints('StartToStart', tempstart) < 0) { startoffset++; tempstart.moveStart('character', -1); }; return startoffset; }; this.endOffset = function() { var endoffset = 0; var selrange = this.selection.createRange(); var parent = selrange.parentElement(); var elrange = selrange.duplicate(); elrange.moveToElementText(parent); var tempend = selrange.duplicate(); while (elrange.compareEndPoints('EndToEnd', tempend) > 0) { endoffset++; tempend.moveEnd('character', 1); }; return endoffset; }; this.getContentLength = function() { var contentlength = 0; var range = this.selection.createRange().duplicate(); var startpoint = range.duplicate(); startpoint.collapse(); var endpoint = range.duplicate(); endpoint.collapse(false); while (!startpoint.isEqual(endpoint)) { startpoint.moveEnd('character', 1); startpoint.moveStart('character', 1); contentlength++; }; return contentlength; }; this.cutChunk = function(startOffset, endOffset) { /* cut a chunk of HTML from the selection this *should* return the chunk of HTML but doesn't yet */ var range = this.selection.createRange().duplicate(); range.moveStart('character', startOffset); range.moveEnd('character', -endOffset); range.pasteHTML(''); // XXX here it should return the chunk }; this.getElementLength = function(element) { /* returns the length of an element *including* 1 char for each child element this is defined on the selection since it returns results that can be used to work with selection offsets */ var length = 0; var range = this.selection.createRange().duplicate(); range.moveToElementText(element); range.moveStart('character', 1); range.moveEnd('character', -1); var endpoint = range.duplicate(); endpoint.collapse(false); range.collapse(); while (!range.isEqual(endpoint)) { range.moveEnd('character', 1); range.moveStart('character', 1); length++; }; return length; }; this.parentElement = function() { /* return the selected node (or the node containing the selection) */ // XXX this should be on a range object if (this.selection.type == 'Control') { return this.selection.createRange().item(0); } else { return this.selection.createRange().parentElement(); }; }; // deprecated alias of parentElement this.getSelectedNode = this.parentElement; this.moveStart = function(offset) { /* move the start of the selection */ var range = this.selection.createRange(); range.moveStart('character', offset); range.select(); }; this.moveEnd = function(offset) { /* moves the end of the selection */ var range = this.selection.createRange(); range.moveEnd('character', offset); range.select(); }; this.reset = function() { this.selection = this.document.getDocument().selection; }; this.cloneContents = function() { /* returns a document fragment with a copy of the contents */ var contents = this.selection.createRange().htmlText; var doc = this.document.getDocument(); var docfrag = doc.createElement('span'); docfrag.innerHTML = contents; return docfrag; }; this.containsNode = function(node) { var selected = this.selection.createRange(); if (this.selection.type.toLowerCase()=='text') { var range = doc.body.createTextRange(); range.moveToElementText(node); if (selected.compareEndPoints('StartToEnd', range) >= 0 || selected.compareEndPoints('EndToStart', range) <= 0) { return false; } return true; } else { for (var i = 0; i < selected.length; i++) { if (selected.item(i).contains(node)) { return true; } } return false; } }; this.toString = function() { return this.selection.createRange().htmlText; };};IESelection.prototype = new BaseSelection;/* ContextFixer, fixes a problem with the prototype based model When a method is called in certain particular ways, for instance when it is used as an event handler, the context for the method is changed, so 'this' inside the method doesn't refer to the object on which the method is defined (or to which it is attached), but for instance to the element on which the method was bound to as an event handler. This class can be used to wrap such a method, the wrapper has one method that can be used as the event handler instead. The constructor expects at least 2 arguments, first is a reference to the method, second the context (a reference to the object) and optionally it can cope with extra arguments, they will be passed to the method as arguments when it is called (which is a nice bonus of using this wrapper).*/function ContextFixer(func, context) { /* Make sure 'this' inside a method points to its class */ this.func = func; this.context = context; this.args = arguments; var self = this; this.execute = function() { /* execute the method */ var args = new Array(); // the first arguments will be the extra ones of the class for (var i=0; i < self.args.length - 2; i++) { args.push(self.args[i + 2]); }; // the last are the ones passed on to the execute method for (var i=0; i < arguments.length; i++) { args.push(arguments[i]); }; self.func.apply(self.context, args); };};/* Alternative implementation of window.setTimeout This is a singleton class, the name of the single instance of the object is 'timer_instance', which has one public method called registerFunction. This method takes at least 2 arguments: a reference to the function (or method) to be called and the timeout. Arguments to the function are optional arguments to the registerFunction method. Example: timer_instance.registerMethod(foo, 100, 'bar', 'baz'); will call the function 'foo' with the arguments 'bar' and 'baz' with a timeout of 100 milliseconds. Since the method doesn't expect a string but a reference to a function and since it can handle arguments that are resolved within the current namespace rather then in the global namespace, the method can be used to call methods on objects from within the object (so this.foo calls this.foo instead of failing to find this inside the global namespace) and since the arguments aren't strings which are resolved in the global namespace the arguments work as expected even inside objects.*/function Timer() { /* class that has a method to replace window.setTimeout */ this.lastid = 0; this.functions = {}; this.registerFunction = function(object, func, timeout) { /* register a function to be called with a timeout args: func - the function timeout - timeout in millisecs all other args will be passed 1:1 to the function when called */ var args = new Array(); for (var i=0; i < arguments.length - 3; i++) { args.push(arguments[i + 3]); } var id = this._createUniqueId(); this.functions[id] = new Array(object, func, args); setTimeout("timer_instance._handleFunction(" + id + ")", timeout); }; this._handleFunction = function(id) { /* private method that does the actual function call */ var obj = this.functions[id][0]; var func = this.functions[id][1]; var args = this.functions[id][2]; this.functions[id] = null; func.apply(obj, args); }; this._createUniqueId = function() { /* create a unique id to store the function by */ while (this.lastid in this.functions && this.functions[this.lastid]) { this.lastid++; if (this.lastid > 100000) { this.lastid = 0; } } return this.lastid; };};// create a timer instance in the global namespace, obviously this does some// polluting but I guess it's impossible to avoid...// OBVIOUSLY THIS VARIABLE SHOULD NEVER BE OVERWRITTEN!!!timer_instance = new Timer();// helper function on the Array object to test for containmentArray.prototype.contains = function(element, objectequality) { /* see if some value is in this */ for (var i=0; i < this.length; i++) { if (objectequality) { if (element === this[i]) { return true; }; } else { if (element == this[i]) { return true; }; }; }; return false;};// JavaScript has a friggin' blink() function, but not for string stripping...String.prototype.strip = function() { var stripspace = /^\s*([\s\S]*?)\s*$/; return stripspace.exec(this)[1];};//----------------------------------------------------------------------------// Exceptions//----------------------------------------------------------------------------// XXX don't know if this is the regular way to define exceptions in JavaScript?function Exception() { return;};// throw this as an exception inside an updateState handler to restart the// update, may be required in situations where updateState changes the structure// of the document (e.g. does a cleanup or so)UpdateStateCancelBubble = new Exception();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -