Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / bindings / 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     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 = createDocumentFragment();
100     var linkStringRegEx = /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|data:|www\.)[\w$\-_+*'=\|\/\\(){}[\]^%@&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({^%@&#~]/;
101
102     while (string) {
103         var linkString = linkStringRegEx.exec(string);
104         if (!linkString)
105             break;
106
107         linkString = linkString[0];
108         var linkIndex = string.indexOf(linkString);
109         var nonLink = string.substring(0, linkIndex);
110         container.appendChild(createTextNode(nonLink));
111
112         var title = linkString;
113         var realURL = (linkString.startsWith("www.") ? "http://" + linkString : linkString);
114         var parsedURL = new WebInspector.ParsedURL(realURL);
115         var splitResult = WebInspector.ParsedURL.splitLineAndColumn(parsedURL.lastPathComponent);
116         var linkNode;
117         if (splitResult) {
118             var link = realURL.substring(0, realURL.length - parsedURL.lastPathComponent.length + splitResult.url.length);
119             linkNode = linkifier(title, link, splitResult.lineNumber, splitResult.columnNumber);
120         } else
121             linkNode = linkifier(title, realURL);
122
123         container.appendChild(linkNode);
124         string = string.substring(linkIndex + linkString.length, string.length);
125     }
126
127     if (string)
128         container.appendChild(createTextNode(string));
129
130     return container;
131 }
132
133 /**
134  * @param {string} string
135  * @return {!DocumentFragment}
136  */
137 WebInspector.linkifyStringAsFragment = function(string)
138 {
139     /**
140      * @param {string} title
141      * @param {string} url
142      * @param {number=} lineNumber
143      * @param {number=} columnNumber
144      * @return {!Node}
145      */
146     function linkifier(title, url, lineNumber, columnNumber)
147     {
148         var isExternal = !WebInspector.resourceForURL(url) && !WebInspector.workspace.uiSourceCodeForURL(url);
149         var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined, isExternal);
150         if (typeof lineNumber !== "undefined") {
151             urlNode.lineNumber = lineNumber;
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 = createElement("a");
178     var href = sanitizeHref(url);
179     if (href !== null)
180         a.href = href;
181     a.className = classes;
182     if (typeof tooltipText === "undefined")
183         a.title = url;
184     else if (typeof tooltipText !== "string" || tooltipText.length)
185         a.title = tooltipText;
186     a.textContent = linkText.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
187     if (isExternal)
188         a.setAttribute("target", "_blank");
189
190     return a;
191 }
192
193 /**
194  * @param {string} url
195  * @param {number=} lineNumber
196  * @return {string}
197  */
198 WebInspector.formatLinkText = function(url, lineNumber)
199 {
200     var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
201     if (typeof lineNumber === "number")
202         text += ":" + (lineNumber + 1);
203     return text;
204 }
205
206 /**
207  * @param {string} url
208  * @param {number=} lineNumber
209  * @param {string=} classes
210  * @param {string=} tooltipText
211  * @return {!Element}
212  */
213 WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
214 {
215     var linkText = WebInspector.formatLinkText(url, lineNumber);
216     var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
217     anchor.lineNumber = lineNumber;
218     return anchor;
219 }
220
221 /**
222  * @param {!WebInspector.NetworkRequest} request
223  * @return {!Element}
224  */
225 WebInspector.linkifyRequestAsNode = function(request)
226 {
227     var anchor = WebInspector.linkifyURLAsNode(request.url);
228     anchor.requestId = request.requestId;
229     return anchor;
230 }