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

📄 utilities.js

📁 linux下开源浏览器WebKit的源码,市面上的很多商用浏览器都是移植自WebKit
💻 JS
📖 第 1 页 / 共 3 页
字号:
    return this.parentNode;}function onlyTextChild(ignoreWhitespace){    if (!this)        return null;    var firstChild = ignoreWhitespace ? firstChildSkippingWhitespace.call(this) : this.firstChild;    if (!firstChild || firstChild.nodeType !== Node.TEXT_NODE)        return null;    var sibling = ignoreWhitespace ? nextSiblingSkippingWhitespace.call(firstChild) : firstChild.nextSibling;    return sibling ? null : firstChild;}function nodeTitleInfo(hasChildren, linkify){    var info = {title: "", hasChildren: hasChildren};    switch (this.nodeType) {        case Node.DOCUMENT_NODE:            info.title = "Document";            break;        case Node.ELEMENT_NODE:            info.title = "<span class=\"webkit-html-tag\">&lt;" + this.nodeName.toLowerCase().escapeHTML();            if (this.hasAttributes()) {                for (var i = 0; i < this.attributes.length; ++i) {                    var attr = this.attributes[i];                    info.title += " <span class=\"webkit-html-attribute\"><span class=\"webkit-html-attribute-name\">" + attr.name.escapeHTML() + "</span>=&#8203;\"";                    var value = attr.value;                    if (linkify && (attr.name === "src" || attr.name === "href")) {                        var value = value.replace(/([\/;:\)\]\}])/g, "$1\u200B");                        info.title += linkify(attr.value, value, "webkit-html-attribute-value", this.nodeName.toLowerCase() == "a");                    } else {                        var value = value.escapeHTML();                        value = value.replace(/([\/;:\)\]\}])/g, "$1&#8203;");                        info.title += "<span class=\"webkit-html-attribute-value\">" + value + "</span>";                    }                    info.title += "\"</span>";                }            }            info.title += "&gt;</span>&#8203;";            // If this element only has a single child that is a text node,            // just show that text and the closing tag inline rather than            // create a subtree for them            var textChild = onlyTextChild.call(this, Preferences.ignoreWhitespace);            var showInlineText = textChild && textChild.textContent.length < Preferences.maxInlineTextChildLength;            if (showInlineText) {                info.title += "<span class=\"webkit-html-text-node\">" + textChild.nodeValue.escapeHTML() + "</span>&#8203;<span class=\"webkit-html-tag\">&lt;/" + this.nodeName.toLowerCase().escapeHTML() + "&gt;</span>";                info.hasChildren = false;            }            break;        case Node.TEXT_NODE:            if (isNodeWhitespace.call(this))                info.title = "(whitespace)";            else                info.title = "\"<span class=\"webkit-html-text-node\">" + this.nodeValue.escapeHTML() + "</span>\"";            break        case Node.COMMENT_NODE:            info.title = "<span class=\"webkit-html-comment\">&lt;!--" + this.nodeValue.escapeHTML() + "--&gt;</span>";            break;        case Node.DOCUMENT_TYPE_NODE:            info.title = "<span class=\"webkit-html-doctype\">&lt;!DOCTYPE " + this.nodeName;            if (this.publicId) {                info.title += " PUBLIC \"" + this.publicId + "\"";                if (this.systemId)                    info.title += " \"" + this.systemId + "\"";            } else if (this.systemId)                info.title += " SYSTEM \"" + this.systemId + "\"";            if (this.internalSubset)                info.title += " [" + this.internalSubset + "]";            info.title += "&gt;</span>";            break;        default:            info.title = this.nodeName.toLowerCase().collapseWhitespace().escapeHTML();    }    return info;}function getDocumentForNode(node) {    return node.nodeType == Node.DOCUMENT_NODE ? node : node.ownerDocument;}function parentNodeOrFrameElement(node) {    var parent = node.parentNode;    if (parent)        return parent;    return getDocumentForNode(node).defaultView.frameElement;}function isAncestorIncludingParentFrames(a, b) {    if (objectsAreSame(a, b))        return false;    for (var node = b; node; node = getDocumentForNode(node).defaultView.frameElement)        if (objectsAreSame(a, node) || isAncestorNode.call(a, node))            return true;    return false;}Number.secondsToString = function(seconds, formatterFunction, higherResolution){    if (!formatterFunction)        formatterFunction = String.sprintf;    var ms = seconds * 1000;    if (higherResolution && ms < 1000)        return formatterFunction("%.3fms", ms);    else if (ms < 1000)        return formatterFunction("%.0fms", ms);    if (seconds < 60)        return formatterFunction("%.2fs", seconds);    var minutes = seconds / 60;    if (minutes < 60)        return formatterFunction("%.1fmin", minutes);    var hours = minutes / 60;    if (hours < 24)        return formatterFunction("%.1fhrs", hours);    var days = hours / 24;    return formatterFunction("%.1f days", days);}Number.bytesToString = function(bytes, formatterFunction){    if (!formatterFunction)        formatterFunction = String.sprintf;    if (bytes < 1024)        return formatterFunction("%.0fB", bytes);    var kilobytes = bytes / 1024;    if (kilobytes < 1024)        return formatterFunction("%.2fKB", kilobytes);    var megabytes = kilobytes / 1024;    return formatterFunction("%.3fMB", megabytes);}Number.constrain = function(num, min, max){    if (num < min)        num = min;    else if (num > max)        num = max;    return num;}HTMLTextAreaElement.prototype.moveCursorToEnd = function(){    var length = this.value.length;    this.setSelectionRange(length, length);}Array.prototype.remove = function(value, onlyFirst){    if (onlyFirst) {        var index = this.indexOf(value);        if (index !== -1)            this.splice(index, 1);        return;    }    var length = this.length;    for (var i = 0; i < length; ++i) {        if (this[i] === value)            this.splice(i, 1);    }}function insertionIndexForObjectInListSortedByFunction(anObject, aList, aFunction){    // indexOf returns (-lowerBound - 1). Taking (-result - 1) works out to lowerBound.    return (-indexOfObjectInListSortedByFunction(anObject, aList, aFunction) - 1);}function indexOfObjectInListSortedByFunction(anObject, aList, aFunction){    var first = 0;    var last = aList.length - 1;    var floor = Math.floor;    var mid, c;    while (first <= last) {        mid = floor((first + last) / 2);        c = aFunction(anObject, aList[mid]);        if (c > 0)            first = mid + 1;        else if (c < 0)            last = mid - 1;        else {            //we return the first occurance of an item in the list.            while (mid > 0 && aFunction(anObject, aList[mid - 1]) === 0)                mid--;            return mid;        }    }    // By returning 1 less than the negative lower search bound, we can reuse this function    // for both indexOf and insertionIndexFor, with some simple arithmetic.    return (-first - 1);}String.sprintf = function(format){    return String.vsprintf(format, Array.prototype.slice.call(arguments, 1));}String.tokenizeFormatString = function(format){    var tokens = [];    var substitutionIndex = 0;    function addStringToken(str)    {        tokens.push({ type: "string", value: str });    }    function addSpecifierToken(specifier, precision, substitutionIndex)    {        tokens.push({ type: "specifier", specifier: specifier, precision: precision, substitutionIndex: substitutionIndex });    }    var index = 0;    for (var precentIndex = format.indexOf("%", index); precentIndex !== -1; precentIndex = format.indexOf("%", index)) {        addStringToken(format.substring(index, precentIndex));        index = precentIndex + 1;        if (format[index] === "%") {            addStringToken("%");            ++index;            continue;        }        if (!isNaN(format[index])) {            // The first character is a number, it might be a substitution index.            var number = parseInt(format.substring(index));            while (!isNaN(format[index]))                ++index;            // If the number is greater than zero and ends with a "$",            // then this is a substitution index.            if (number > 0 && format[index] === "$") {                substitutionIndex = (number - 1);                ++index;            }        }        var precision = -1;        if (format[index] === ".") {            // This is a precision specifier. If no digit follows the ".",            // then the precision should be zero.            ++index;            precision = parseInt(format.substring(index));            if (isNaN(precision))                precision = 0;            while (!isNaN(format[index]))                ++index;        }        addSpecifierToken(format[index], precision, substitutionIndex);        ++substitutionIndex;        ++index;    }    addStringToken(format.substring(index));    return tokens;}String.standardFormatters = {    d: function(substitution)    {        substitution = parseInt(substitution);        return !isNaN(substitution) ? substitution : 0;    },    f: function(substitution, token)    {        substitution = parseFloat(substitution);        if (substitution && token.precision > -1)            substitution = substitution.toFixed(token.precision);        return !isNaN(substitution) ? substitution : (token.precision > -1 ? Number(0).toFixed(token.precision) : 0);    },    s: function(substitution)    {        return substitution;    },};String.vsprintf = function(format, substitutions){    return String.format(format, substitutions, String.standardFormatters, "", function(a, b) { return a + b; }).formattedResult;}String.format = function(format, substitutions, formatters, initialValue, append){    if (!format || !substitutions || !substitutions.length)        return { formattedResult: append(initialValue, format), unusedSubstitutions: substitutions };    function prettyFunctionName()    {        return "String.format(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")";    }    function warn(msg)    {        console.warn(prettyFunctionName() + ": " + msg);    }    function error(msg)    {        console.error(prettyFunctionName() + ": " + msg);    }    var result = initialValue;    var tokens = String.tokenizeFormatString(format);    var usedSubstitutionIndexes = {};    for (var i = 0; i < tokens.length; ++i) {        var token = tokens[i];        if (token.type === "string") {            result = append(result, token.value);            continue;        }        if (token.type !== "specifier") {            error("Unknown token type \"" + token.type + "\" found.");            continue;        }        if (token.substitutionIndex >= substitutions.length) {            // If there are not enough substitutions for the current substitutionIndex            // just output the format specifier literally and move on.            error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped.");            result = append(result, "%" + (token.precision > -1 ? token.precision : "") + token.specifier);            continue;        }        usedSubstitutionIndexes[token.substitutionIndex] = true;        if (!(token.specifier in formatters)) {            // Encountered an unsupported format character, treat as a string.            warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");            result = append(result, substitutions[token.substitutionIndex]);            continue;        }        result = append(result, formatters[token.specifier](substitutions[token.substitutionIndex], token));    }    var unusedSubstitutions = [];    for (var i = 0; i < substitutions.length; ++i) {        if (i in usedSubstitutionIndexes)            continue;        unusedSubstitutions.push(substitutions[i]);    }    return { formattedResult: result, unusedSubstitutions: unusedSubstitutions };}

⌨️ 快捷键说明

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