Home | History | Annotate | Download | only in bindings
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
      3  * Copyright (C) 2007 Matt Lilek (pewtermoose (at) gmail.com).
      4  * Copyright (C) 2009 Joseph Pecoraro
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  *
     10  * 1.  Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  * 2.  Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     16  *     its contributors may be used to endorse or promote products derived
     17  *     from this software without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /**
     32  * @param {string} url
     33  * @return {?WebInspector.Resource}
     34  */
     35 WebInspector.resourceForURL = function(url)
     36 {
     37     var targets = WebInspector.targetManager.targets();
     38     for (var i = 0; i < targets.length; ++i) {
     39         var resource = targets[i].resourceTreeModel.resourceForURL(url);
     40         if (resource)
     41             return resource;
     42     }
     43     return null;
     44 }
     45 
     46 /**
     47  * @param {function(!WebInspector.Resource)} callback
     48  */
     49 WebInspector.forAllResources = function(callback)
     50 {
     51     var targets = WebInspector.targetManager.targets();
     52     for (var i = 0; i < targets.length; ++i)
     53         targets[i].resourceTreeModel.forAllResources(callback);
     54 }
     55 
     56 /**
     57  * @param {string} url
     58  * @return {string}
     59  */
     60 WebInspector.displayNameForURL = function(url)
     61 {
     62     if (!url)
     63         return "";
     64 
     65     var resource = WebInspector.resourceForURL(url);
     66     if (resource)
     67         return resource.displayName;
     68 
     69     var uiSourceCode = WebInspector.workspace.uiSourceCodeForURL(url);
     70     if (uiSourceCode)
     71         return uiSourceCode.displayName();
     72 
     73     if (!WebInspector.targetManager.inspectedPageURL())
     74         return url.trimURL("");
     75 
     76     var parsedURL = WebInspector.targetManager.inspectedPageURL().asParsedURL();
     77     var lastPathComponent = parsedURL ? parsedURL.lastPathComponent : parsedURL;
     78     var index = WebInspector.targetManager.inspectedPageURL().indexOf(lastPathComponent);
     79     if (index !== -1 && index + lastPathComponent.length === WebInspector.targetManager.inspectedPageURL().length) {
     80         var baseURL = WebInspector.targetManager.inspectedPageURL().substring(0, index);
     81         if (url.startsWith(baseURL))
     82             return url.substring(index);
     83     }
     84 
     85     if (!parsedURL)
     86         return url;
     87 
     88     var displayName = url.trimURL(parsedURL.host);
     89     return displayName === "/" ? parsedURL.host + "/" : displayName;
     90 }
     91 
     92 /**
     93  * @param {string} string
     94  * @param {function(string,string,number=,number=):!Node} linkifier
     95  * @return {!DocumentFragment}
     96  */
     97 WebInspector.linkifyStringAsFragmentWithCustomLinkifier = function(string, linkifier)
     98 {
     99     var container = document.createDocumentFragment();
    100     var linkStringRegEx = /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|data:|www\.)[\w$\-_+*'=\|\/\\(){}[\]^%@&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({^%@&#~]/;
    101     var lineColumnRegEx = /:(\d+)(:(\d+))?$/;
    102 
    103     while (string) {
    104         var linkString = linkStringRegEx.exec(string);
    105         if (!linkString)
    106             break;
    107 
    108         linkString = linkString[0];
    109         var linkIndex = string.indexOf(linkString);
    110         var nonLink = string.substring(0, linkIndex);
    111         container.appendChild(document.createTextNode(nonLink));
    112 
    113         var title = linkString;
    114         var realURL = (linkString.startsWith("www.") ? "http://" + linkString : linkString);
    115         var lineColumnMatch = lineColumnRegEx.exec(realURL);
    116         var lineNumber;
    117         var columnNumber;
    118         if (lineColumnMatch) {
    119             realURL = realURL.substring(0, realURL.length - lineColumnMatch[0].length);
    120             lineNumber = parseInt(lineColumnMatch[1], 10);
    121             // Immediately convert line and column to 0-based numbers.
    122             lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1;
    123             if (typeof(lineColumnMatch[3]) === "string") {
    124                 columnNumber = parseInt(lineColumnMatch[3], 10);
    125                 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1;
    126             }
    127         }
    128 
    129         var linkNode = linkifier(title, realURL, lineNumber, columnNumber);
    130         container.appendChild(linkNode);
    131         string = string.substring(linkIndex + linkString.length, string.length);
    132     }
    133 
    134     if (string)
    135         container.appendChild(document.createTextNode(string));
    136 
    137     return container;
    138 }
    139 
    140 /**
    141  * @param {string} string
    142  * @return {!DocumentFragment}
    143  */
    144 WebInspector.linkifyStringAsFragment = function(string)
    145 {
    146     /**
    147      * @param {string} title
    148      * @param {string} url
    149      * @param {number=} lineNumber
    150      * @param {number=} columnNumber
    151      * @return {!Node}
    152      */
    153     function linkifier(title, url, lineNumber, columnNumber)
    154     {
    155         var isExternal = !WebInspector.resourceForURL(url) && !WebInspector.workspace.uiSourceCodeForURL(url);
    156         var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined, isExternal);
    157         if (typeof lineNumber !== "undefined") {
    158             urlNode.lineNumber = lineNumber;
    159             if (typeof columnNumber !== "undefined")
    160                 urlNode.columnNumber = columnNumber;
    161         }
    162 
    163         return urlNode;
    164     }
    165 
    166     return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
    167 }
    168 
    169 /**
    170  * @param {string} url
    171  * @param {string=} linkText
    172  * @param {string=} classes
    173  * @param {boolean=} isExternal
    174  * @param {string=} tooltipText
    175  * @return {!Element}
    176  */
    177 WebInspector.linkifyURLAsNode = function(url, linkText, classes, isExternal, tooltipText)
    178 {
    179     if (!linkText)
    180         linkText = url;
    181     classes = (classes ? classes + " " : "");
    182     classes += isExternal ? "webkit-html-external-link" : "webkit-html-resource-link";
    183 
    184     var a = document.createElement("a");
    185     var href = sanitizeHref(url);
    186     if (href !== null)
    187         a.href = href;
    188     a.className = classes;
    189     if (typeof tooltipText === "undefined")
    190         a.title = url;
    191     else if (typeof tooltipText !== "string" || tooltipText.length)
    192         a.title = tooltipText;
    193     a.textContent = linkText.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
    194     if (isExternal)
    195         a.setAttribute("target", "_blank");
    196 
    197     return a;
    198 }
    199 
    200 /**
    201  * @param {string} url
    202  * @param {number=} lineNumber
    203  * @return {string}
    204  */
    205 WebInspector.formatLinkText = function(url, lineNumber)
    206 {
    207     var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
    208     if (typeof lineNumber === "number")
    209         text += ":" + (lineNumber + 1);
    210     return text;
    211 }
    212 
    213 /**
    214  * @param {string} url
    215  * @param {number=} lineNumber
    216  * @param {string=} classes
    217  * @param {string=} tooltipText
    218  * @return {!Element}
    219  */
    220 WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
    221 {
    222     var linkText = WebInspector.formatLinkText(url, lineNumber);
    223     var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
    224     anchor.lineNumber = lineNumber;
    225     return anchor;
    226 }
    227 
    228 /**
    229  * @param {!WebInspector.NetworkRequest} request
    230  * @return {!Element}
    231  */
    232 WebInspector.linkifyRequestAsNode = function(request)
    233 {
    234     var anchor = WebInspector.linkifyURLAsNode(request.url);
    235     anchor.requestId = request.requestId;
    236     return anchor;
    237 }
    238