1/*
2 * Copyright (C) 2006, 2007, 2008 Apple Inc.  All rights reserved.
3 * Copyright (C) 2007 Matt Lilek (pewtermoose@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 */
35WebInspector.resourceForURL = function(url)
36{
37    return WebInspector.resourceTreeModel.resourceForURL(url);
38}
39
40/**
41 * @param {function(WebInspector.Resource)} callback
42 */
43WebInspector.forAllResources = function(callback)
44{
45     WebInspector.resourceTreeModel.forAllResources(callback);
46}
47
48/**
49 * @param {string} url
50 * @return {string}
51 */
52WebInspector.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=):Node} linkifier
87 * @return {DocumentFragment}
88 */
89WebInspector.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        if (lineColumnMatch) {
110            realURL = realURL.substring(0, realURL.length - lineColumnMatch[0].length);
111            lineNumber = parseInt(lineColumnMatch[1], 10);
112            lineNumber = isNaN(lineNumber) ? undefined : lineNumber;
113        }
114
115        var linkNode = linkifier(title, realURL, lineNumber);
116        container.appendChild(linkNode);
117        string = string.substring(linkIndex + linkString.length, string.length);
118    }
119
120    if (string)
121        container.appendChild(document.createTextNode(string));
122
123    return container;
124}
125
126/**
127 * @param {string} string
128 * @return {DocumentFragment}
129 */
130WebInspector.linkifyStringAsFragment = function(string)
131{
132    /**
133     * @param {string} title
134     * @param {string} url
135     * @param {number=} lineNumber
136     * @return {Node}
137     */
138    function linkifier(title, url, lineNumber)
139    {
140        var isExternal = !WebInspector.resourceForURL(url);
141        var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined, isExternal);
142        if (typeof(lineNumber) !== "undefined") {
143            urlNode.lineNumber = lineNumber;
144            urlNode.preferredPanel = "scripts";
145        }
146
147        return urlNode;
148    }
149
150    return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
151}
152
153/**
154 * @param {string} url
155 * @param {string=} linkText
156 * @param {string=} classes
157 * @param {boolean=} isExternal
158 * @param {string=} tooltipText
159 * @return {!Element}
160 */
161WebInspector.linkifyURLAsNode = function(url, linkText, classes, isExternal, tooltipText)
162{
163    if (!linkText)
164        linkText = url;
165    classes = (classes ? classes + " " : "");
166    classes += isExternal ? "webkit-html-external-link" : "webkit-html-resource-link";
167
168    var a = document.createElement("a");
169    a.href = sanitizeHref(url);
170    a.className = classes;
171    if (typeof tooltipText === "undefined")
172        a.title = url;
173    else if (typeof tooltipText !== "string" || tooltipText.length)
174        a.title = tooltipText;
175    a.textContent = linkText.centerEllipsizedToLength(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
176    if (isExternal)
177        a.setAttribute("target", "_blank");
178
179    return a;
180}
181
182/**
183 * @param {string} url
184 * @param {number=} lineNumber
185 * @return {string}
186 */
187WebInspector.formatLinkText = function(url, lineNumber)
188{
189    var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
190    if (typeof lineNumber === "number")
191        text += ":" + (lineNumber + 1);
192    return text;
193}
194
195/**
196 * @param {string} url
197 * @param {number=} lineNumber
198 * @param {string=} classes
199 * @param {string=} tooltipText
200 * @return {Element}
201 */
202WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
203{
204    var linkText = WebInspector.formatLinkText(url, lineNumber);
205    var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
206    anchor.lineNumber = lineNumber;
207    return anchor;
208}
209
210/**
211 * @param {WebInspector.NetworkRequest} request
212 * @param {string=} classes
213 * @return {Element}
214 */
215WebInspector.linkifyRequestAsNode = function(request, classes)
216{
217    var anchor = WebInspector.linkifyURLAsNode(request.url);
218    anchor.preferredPanel = "network";
219    anchor.requestId  = request.requestId;
220    return anchor;
221}
222
223/**
224 * @param {string} content
225 * @param {string} mimeType
226 * @param {boolean} contentEncoded
227 * @return {?string}
228 */
229WebInspector.contentAsDataURL = function(content, mimeType, contentEncoded)
230{
231    const maxDataUrlSize = 1024 * 1024;
232    if (content == null || content.length > maxDataUrlSize)
233        return null;
234
235    return "data:" + mimeType + (contentEncoded ? ";base64," : ",") + content;
236}
237