Home | History | Annotate | Download | only in front_end
      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     return WebInspector.resourceTreeModel.resourceForURL(url);
     38 }
     39 
     40 /**
     41  * @param {function(WebInspector.Resource)} callback
     42  */
     43 WebInspector.forAllResources = function(callback)
     44 {
     45      WebInspector.resourceTreeModel.forAllResources(callback);
     46 }
     47 
     48 /**
     49  * @param {string} url
     50  * @return {string}
     51  */
     52 WebInspector.displayNameForURL = function(url)
     53 {
     54     if (!url)
     55         return "";
     56 
     57     var resource = WebInspector.resourceForURL(url);
     58     if (resource)
     59         return resource.displayName;
     60 
     61     var uiSourceCode = WebInspector.workspace.uiSourceCodeForURL(url);
     62     if (uiSourceCode)
     63         return uiSourceCode.displayName();
     64 
     65     if (!WebInspector.inspectedPageURL)
     66         return url.trimURL("");
     67 
     68     var parsedURL = WebInspector.inspectedPageURL.asParsedURL();
     69     var lastPathComponent = parsedURL ? parsedURL.lastPathComponent : parsedURL;
     70     var index = WebInspector.inspectedPageURL.indexOf(lastPathComponent);
     71     if (index !== -1 && index + lastPathComponent.length === WebInspector.inspectedPageURL.length) {
     72         var baseURL = WebInspector.inspectedPageURL.substring(0, index);
     73         if (url.startsWith(baseURL))
     74             return url.substring(index);
     75     }
     76 
     77     if (!parsedURL)
     78         return url;
     79 
     80     var displayName = url.trimURL(parsedURL.host);
     81     return displayName === "/" ? parsedURL.host + "/" : displayName;
     82 }
     83 
     84 /**
     85  * @param {string} string
     86  * @param {function(string,string,number=,number=):Node} linkifier
     87  * @return {DocumentFragment}
     88  */
     89 WebInspector.linkifyStringAsFragmentWithCustomLinkifier = function(string, linkifier)
     90 {
     91     var container = document.createDocumentFragment();
     92     var linkStringRegEx = /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|data:|www\.)[\w$\-_+*'=\|\/\\(){}[\]^%@&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({^%@&#~]/;
     93     var lineColumnRegEx = /:(\d+)(:(\d+))?$/;
     94 
     95     while (string) {
     96         var linkString = linkStringRegEx.exec(string);
     97         if (!linkString)
     98             break;
     99 
    100         linkString = linkString[0];
    101         var linkIndex = string.indexOf(linkString);
    102         var nonLink = string.substring(0, linkIndex);
    103         container.appendChild(document.createTextNode(nonLink));
    104 
    105         var title = linkString;
    106         var realURL = (linkString.startsWith("www.") ? "http://" + linkString : linkString);
    107         var lineColumnMatch = lineColumnRegEx.exec(realURL);
    108         var lineNumber;
    109         var columnNumber;
    110         if (lineColumnMatch) {
    111             realURL = realURL.substring(0, realURL.length - lineColumnMatch[0].length);
    112             lineNumber = parseInt(lineColumnMatch[1], 10);
    113             // Immediately convert line and column to 0-based numbers.
    114             lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1;
    115             if (typeof(lineColumnMatch[3]) === "string") {
    116                 columnNumber = parseInt(lineColumnMatch[3], 10);
    117                 columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1;
    118             }
    119         }
    120 
    121         var linkNode = linkifier(title, realURL, lineNumber, columnNumber);
    122         container.appendChild(linkNode);
    123         string = string.substring(linkIndex + linkString.length, string.length);
    124     }
    125 
    126     if (string)
    127         container.appendChild(document.createTextNode(string));
    128 
    129     return container;
    130 }
    131 
    132 /**
    133  * @param {string} string
    134  * @return {DocumentFragment}
    135  */
    136 WebInspector.linkifyStringAsFragment = function(string)
    137 {
    138     /**
    139      * @param {string} title
    140      * @param {string} url
    141      * @param {number=} lineNumber
    142      * @param {number=} columnNumber
    143      * @return {Node}
    144      */
    145     function linkifier(title, url, lineNumber, columnNumber)
    146     {
    147         var isExternal = !WebInspector.resourceForURL(url) && !WebInspector.workspace.uiSourceCodeForURL(url);
    148         var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined, isExternal);
    149         if (typeof lineNumber !== "undefined") {
    150             urlNode.lineNumber = lineNumber;
    151             urlNode.preferredPanel = "scripts";
    152             if (typeof columnNumber !== "undefined")
    153                 urlNode.columnNumber = columnNumber;
    154         }
    155 
    156         return urlNode;
    157     }
    158 
    159     return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
    160 }
    161 
    162 /**
    163  * @param {string} url
    164  * @param {string=} linkText
    165  * @param {string=} classes
    166  * @param {boolean=} isExternal
    167  * @param {string=} tooltipText
    168  * @return {!Element}
    169  */
    170 WebInspector.linkifyURLAsNode = function(url, linkText, classes, isExternal, tooltipText)
    171 {
    172     if (!linkText)
    173         linkText = url;
    174     classes = (classes ? classes + " " : "");
    175     classes += isExternal ? "webkit-html-external-link" : "webkit-html-resource-link";
    176 
    177     var a = document.createElement("a");
    178     a.href = sanitizeHref(url);
    179     a.className = classes;
    180     if (typeof tooltipText === "undefined")
    181         a.title = url;
    182     else if (typeof tooltipText !== "string" || tooltipText.length)
    183         a.title = tooltipText;
    184     a.textContent = linkText.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
    185     if (isExternal)
    186         a.setAttribute("target", "_blank");
    187 
    188     return a;
    189 }
    190 
    191 /**
    192  * @param {string} url
    193  * @param {number=} lineNumber
    194  * @return {string}
    195  */
    196 WebInspector.formatLinkText = function(url, lineNumber)
    197 {
    198     var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
    199     if (typeof lineNumber === "number")
    200         text += ":" + (lineNumber + 1);
    201     return text;
    202 }
    203 
    204 /**
    205  * @param {string} url
    206  * @param {number=} lineNumber
    207  * @param {string=} classes
    208  * @param {string=} tooltipText
    209  * @return {Element}
    210  */
    211 WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
    212 {
    213     var linkText = WebInspector.formatLinkText(url, lineNumber);
    214     var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
    215     anchor.lineNumber = lineNumber;
    216     return anchor;
    217 }
    218 
    219 /**
    220  * @param {WebInspector.NetworkRequest} request
    221  * @return {Element}
    222  */
    223 WebInspector.linkifyRequestAsNode = function(request)
    224 {
    225     var anchor = WebInspector.linkifyURLAsNode(request.url);
    226     anchor.preferredPanel = "network";
    227     anchor.requestId  = request.requestId;
    228     return anchor;
    229 }
    230 
    231 /**
    232  * @param {string} content
    233  * @param {string} mimeType
    234  * @param {boolean} contentEncoded
    235  * @return {?string}
    236  */
    237 WebInspector.contentAsDataURL = function(content, mimeType, contentEncoded)
    238 {
    239     const maxDataUrlSize = 1024 * 1024;
    240     if (content == null || content.length > maxDataUrlSize)
    241         return null;
    242 
    243     return "data:" + mimeType + (contentEncoded ? ";base64," : ",") + content;
    244 }
    245