Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / sdk / ResourceUtils.js
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  */
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.resourceTreeModel.inspectedPageURL())
66         return url.trimURL("");
67
68     var parsedURL = WebInspector.resourceTreeModel.inspectedPageURL().asParsedURL();
69     var lastPathComponent = parsedURL ? parsedURL.lastPathComponent : parsedURL;
70     var index = WebInspector.resourceTreeModel.inspectedPageURL().indexOf(lastPathComponent);
71     if (index !== -1 && index + lastPathComponent.length === WebInspector.resourceTreeModel.inspectedPageURL().length) {
72         var baseURL = WebInspector.resourceTreeModel.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             if (typeof columnNumber !== "undefined")
152                 urlNode.columnNumber = columnNumber;
153         }
154
155         return urlNode;
156     }
157
158     return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
159 }
160
161 /**
162  * @param {string} url
163  * @param {string=} linkText
164  * @param {string=} classes
165  * @param {boolean=} isExternal
166  * @param {string=} tooltipText
167  * @return {!Element}
168  */
169 WebInspector.linkifyURLAsNode = function(url, linkText, classes, isExternal, tooltipText)
170 {
171     if (!linkText)
172         linkText = url;
173     classes = (classes ? classes + " " : "");
174     classes += isExternal ? "webkit-html-external-link" : "webkit-html-resource-link";
175
176     var a = document.createElement("a");
177     var href = sanitizeHref(url);
178     if (href !== null)
179         a.href = href;
180     a.className = classes;
181     if (typeof tooltipText === "undefined")
182         a.title = url;
183     else if (typeof tooltipText !== "string" || tooltipText.length)
184         a.title = tooltipText;
185     a.textContent = linkText.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
186     if (isExternal)
187         a.setAttribute("target", "_blank");
188
189     return a;
190 }
191
192 /**
193  * @param {string} url
194  * @param {number=} lineNumber
195  * @return {string}
196  */
197 WebInspector.formatLinkText = function(url, lineNumber)
198 {
199     var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
200     if (typeof lineNumber === "number")
201         text += ":" + (lineNumber + 1);
202     return text;
203 }
204
205 /**
206  * @param {string} url
207  * @param {number=} lineNumber
208  * @param {string=} classes
209  * @param {string=} tooltipText
210  * @return {!Element}
211  */
212 WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
213 {
214     var linkText = WebInspector.formatLinkText(url, lineNumber);
215     var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
216     anchor.lineNumber = lineNumber;
217     return anchor;
218 }
219
220 /**
221  * @param {!WebInspector.NetworkRequest} request
222  * @return {!Element}
223  */
224 WebInspector.linkifyRequestAsNode = function(request)
225 {
226     var anchor = WebInspector.linkifyURLAsNode(request.url);
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 }