⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utilities.js

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 JS
📖 第 1 页 / 共 3 页
字号:
/* * Copyright (C) 2007 Apple Inc.  All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1.  Redistributions of source code must retain the above copyright *     notice, this list of conditions and the following disclaimer.  * 2.  Redistributions in binary form must reproduce the above copyright *     notice, this list of conditions and the following disclaimer in the *     documentation and/or other materials provided with the distribution.  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of *     its contributors may be used to endorse or promote products derived *     from this software without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */Object.type = function(obj, win){    if (obj === null)        return "null";    var type = typeof obj;    if (type !== "object" && type !== "function")        return type;    win = win || window;    if (obj instanceof win.Node)        return "node";    if (obj instanceof win.String)        return "string";    if (obj instanceof win.Array)        return "array";    if (obj instanceof win.Boolean)        return "boolean";    if (obj instanceof win.Number)        return "number";    if (obj instanceof win.Date)        return "date";    if (obj instanceof win.RegExp)        return "regexp";    if (obj instanceof win.Error)        return "error";    return type;}Object.hasProperties = function(obj){    if (typeof obj === "undefined" || typeof obj === "null")        return false;    for (var name in obj)        return true;    return false;}Object.describe = function(obj, abbreviated){    var type1 = Object.type(obj);    var type2 = Object.prototype.toString.call(obj).replace(/^\[object (.*)\]$/i, "$1");    switch (type1) {    case "object":    case "node":        return type2;    case "array":        return "[" + obj.toString() + "]";    case "string":        if (obj.length > 100)            return "\"" + obj.substring(0, 100) + "\u2026\"";        return "\"" + obj + "\"";    case "function":        var objectText = String(obj);        if (!/^function /.test(objectText))            objectText = (type2 == "object") ? type1 : type2;        else if (abbreviated)            objectText = /.*/.exec(obj)[0].replace(/ +$/g, "");        return objectText;    case "regexp":        return String(obj).replace(/([\\\/])/g, "\\$1").replace(/\\(\/[gim]*)$/, "$1").substring(1);    default:        return String(obj);    }}Object.sortedProperties = function(obj){    var properties = [];    for (var prop in obj)        properties.push(prop);    properties.sort();    return properties;}Function.prototype.bind = function(thisObject){    var func = this;    var args = Array.prototype.slice.call(arguments, 1);    return function() { return func.apply(thisObject, args.concat(Array.prototype.slice.call(arguments, 0))) };}Node.prototype.rangeOfWord = function(offset, stopCharacters, stayWithinNode, direction){    var startNode;    var startOffset = 0;    var endNode;    var endOffset = 0;    if (!stayWithinNode)        stayWithinNode = this;    if (!direction || direction === "backward" || direction === "both") {        var node = this;        while (node) {            if (node === stayWithinNode) {                if (!startNode)                    startNode = stayWithinNode;                break;            }            if (node.nodeType === Node.TEXT_NODE) {                var start = (node === this ? (offset - 1) : (node.nodeValue.length - 1));                for (var i = start; i >= 0; --i) {                    if (stopCharacters.indexOf(node.nodeValue[i]) !== -1) {                        startNode = node;                        startOffset = i + 1;                        break;                    }                }            }            if (startNode)                break;            node = node.traversePreviousNode(false, stayWithinNode);        }        if (!startNode) {            startNode = stayWithinNode;            startOffset = 0;        }    } else {        startNode = this;        startOffset = offset;    }    if (!direction || direction === "forward" || direction === "both") {        node = this;        while (node) {            if (node === stayWithinNode) {                if (!endNode)                    endNode = stayWithinNode;                break;            }            if (node.nodeType === Node.TEXT_NODE) {                var start = (node === this ? offset : 0);                for (var i = start; i < node.nodeValue.length; ++i) {                    if (stopCharacters.indexOf(node.nodeValue[i]) !== -1) {                        endNode = node;                        endOffset = i;                        break;                    }                }            }            if (endNode)                break;            node = node.traverseNextNode(false, stayWithinNode);        }        if (!endNode) {            endNode = stayWithinNode;            endOffset = stayWithinNode.nodeType === Node.TEXT_NODE ? stayWithinNode.nodeValue.length : stayWithinNode.childNodes.length;        }    } else {        endNode = this;        endOffset = offset;    }    var result = this.ownerDocument.createRange();    result.setStart(startNode, startOffset);    result.setEnd(endNode, endOffset);    return result;}Element.prototype.removeStyleClass = function(className) {    // Test for the simple case before using a RegExp.    if (this.className === className) {        this.className = "";        return;    }    this.removeMatchingStyleClasses(className.escapeForRegExp());}Element.prototype.removeMatchingStyleClasses = function(classNameRegex){    var regex = new RegExp("(^|\\s+)" + classNameRegex + "($|\\s+)");    if (regex.test(this.className))        this.className = this.className.replace(regex, " ");}Element.prototype.addStyleClass = function(className) {    if (className && !this.hasStyleClass(className))        this.className += (this.className.length ? " " + className : className);}Element.prototype.hasStyleClass = function(className) {    if (!className)        return false;    // Test for the simple case before using a RegExp.    if (this.className === className)        return true;    var regex = new RegExp("(^|\\s)" + className.escapeForRegExp() + "($|\\s)");    return regex.test(this.className);}Node.prototype.enclosingNodeOrSelfWithNodeNameInArray = function(nameArray){    for (var node = this; node && !objectsAreSame(node, this.ownerDocument); node = node.parentNode)        for (var i = 0; i < nameArray.length; ++i)            if (node.nodeName.toLowerCase() === nameArray[i].toLowerCase())                return node;    return null;}Node.prototype.enclosingNodeOrSelfWithNodeName = function(nodeName){    return this.enclosingNodeOrSelfWithNodeNameInArray([nodeName]);}Node.prototype.enclosingNodeOrSelfWithClass = function(className){    for (var node = this; node && !objectsAreSame(node, this.ownerDocument); node = node.parentNode)        if (node.nodeType === Node.ELEMENT_NODE && node.hasStyleClass(className))            return node;    return null;}Node.prototype.enclosingNodeWithClass = function(className){    if (!this.parentNode)        return null;    return this.parentNode.enclosingNodeOrSelfWithClass(className);}Element.prototype.query = function(query) {    return this.ownerDocument.evaluate(query, this, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;}Element.prototype.removeChildren = function(){    while (this.firstChild)         this.removeChild(this.firstChild);        }Element.prototype.isInsertionCaretInside = function(){    var selection = window.getSelection();    if (!selection.rangeCount || !selection.isCollapsed)        return false;    var selectionRange = selection.getRangeAt(0);    return selectionRange.startContainer === this || selectionRange.startContainer.isDescendant(this);}Element.prototype.__defineGetter__("totalOffsetLeft", function(){    var total = 0;    for (var element = this; element; element = element.offsetParent)        total += element.offsetLeft;    return total;});Element.prototype.__defineGetter__("totalOffsetTop", function(){    var total = 0;    for (var element = this; element; element = element.offsetParent)        total += element.offsetTop;    return total;});Element.prototype.firstChildSkippingWhitespace = firstChildSkippingWhitespace;Element.prototype.lastChildSkippingWhitespace = lastChildSkippingWhitespace;Node.prototype.isWhitespace = isNodeWhitespace;Node.prototype.nodeTypeName = nodeTypeName;Node.prototype.displayName = nodeDisplayName;Node.prototype.contentPreview = nodeContentPreview;Node.prototype.isAncestor = isAncestorNode;Node.prototype.isDescendant = isDescendantNode;Node.prototype.firstCommonAncestor = firstCommonNodeAncestor;Node.prototype.nextSiblingSkippingWhitespace = nextSiblingSkippingWhitespace;Node.prototype.previousSiblingSkippingWhitespace = previousSiblingSkippingWhitespace;Node.prototype.traverseNextNode = traverseNextNode;Node.prototype.traversePreviousNode = traversePreviousNode;Node.prototype.onlyTextChild = onlyTextChild;String.prototype.hasSubstring = function(string, caseInsensitive){    if (!caseInsensitive)        return this.indexOf(string) !== -1;    return this.match(new RegExp(string.escapeForRegExp(), "i"));}String.prototype.escapeCharacters = function(chars){    var foundChar = false;    for (var i = 0; i < chars.length; ++i) {        if (this.indexOf(chars.charAt(i)) !== -1) {            foundChar = true;            break;        }    }    if (!foundChar)        return this;    var result = "";    for (var i = 0; i < this.length; ++i) {        if (chars.indexOf(this.charAt(i)) !== -1)            result += "\\";        result += this.charAt(i);    }    return result;}String.prototype.escapeForRegExp = function(){    return this.escapeCharacters("^[]{}()\\.$*+?|");}String.prototype.escapeHTML = function(){    return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");}String.prototype.collapseWhitespace = function(){    return this.replace(/[\s\xA0]+/g, " ");}String.prototype.trimLeadingWhitespace = function(){    return this.replace(/^[\s\xA0]+/g, "");}String.prototype.trimTrailingWhitespace = function(){    return this.replace(/[\s\xA0]+$/g, "");}String.prototype.trimWhitespace = function(){    return this.replace(/^[\s\xA0]+|[\s\xA0]+$/g, "");}String.prototype.trimURL = function(baseURLDomain){    var result = this.replace(new RegExp("^http[s]?:\/\/", "i"), "");    if (baseURLDomain)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -