ResourceUtils.js 7.6 KB

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