ResourceUtils.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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=,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. var columnNumber;
  97. if (lineColumnMatch) {
  98. realURL = realURL.substring(0, realURL.length - lineColumnMatch[0].length);
  99. lineNumber = parseInt(lineColumnMatch[1], 10);
  100. // Immediately convert line and column to 0-based numbers.
  101. lineNumber = isNaN(lineNumber) ? undefined : lineNumber - 1;
  102. if (typeof(lineColumnMatch[3]) === "string") {
  103. columnNumber = parseInt(lineColumnMatch[3], 10);
  104. columnNumber = isNaN(columnNumber) ? undefined : columnNumber - 1;
  105. }
  106. }
  107. var linkNode = linkifier(title, realURL, lineNumber, columnNumber);
  108. container.appendChild(linkNode);
  109. string = string.substring(linkIndex + linkString.length, string.length);
  110. }
  111. if (string)
  112. container.appendChild(document.createTextNode(string));
  113. return container;
  114. }
  115. /**
  116. * @param {string} string
  117. * @return {DocumentFragment}
  118. */
  119. WebInspector.linkifyStringAsFragment = function(string)
  120. {
  121. /**
  122. * @param {string} title
  123. * @param {string} url
  124. * @param {number=} lineNumber
  125. * @param {number=} columnNumber
  126. * @return {Node}
  127. */
  128. function linkifier(title, url, lineNumber, columnNumber)
  129. {
  130. var isExternal = !WebInspector.resourceForURL(url) && !WebInspector.workspace.uiSourceCodeForURL(url);
  131. var urlNode = WebInspector.linkifyURLAsNode(url, title, undefined, isExternal);
  132. if (typeof lineNumber !== "undefined") {
  133. urlNode.lineNumber = lineNumber;
  134. urlNode.preferredPanel = "scripts";
  135. if (typeof columnNumber !== "undefined")
  136. urlNode.columnNumber = columnNumber;
  137. }
  138. return urlNode;
  139. }
  140. return WebInspector.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier);
  141. }
  142. /**
  143. * @param {string} url
  144. * @param {string=} linkText
  145. * @param {string=} classes
  146. * @param {boolean=} isExternal
  147. * @param {string=} tooltipText
  148. * @return {!Element}
  149. */
  150. WebInspector.linkifyURLAsNode = function(url, linkText, classes, isExternal, tooltipText)
  151. {
  152. if (!linkText)
  153. linkText = url;
  154. classes = (classes ? classes + " " : "");
  155. classes += isExternal ? "webkit-html-external-link" : "webkit-html-resource-link";
  156. var a = document.createElement("a");
  157. a.href = sanitizeHref(url);
  158. a.className = classes;
  159. if (typeof tooltipText === "undefined")
  160. a.title = url;
  161. else if (typeof tooltipText !== "string" || tooltipText.length)
  162. a.title = tooltipText;
  163. a.textContent = linkText.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
  164. if (isExternal)
  165. a.setAttribute("target", "_blank");
  166. return a;
  167. }
  168. /**
  169. * @param {string} url
  170. * @param {number=} lineNumber
  171. * @return {string}
  172. */
  173. WebInspector.formatLinkText = function(url, lineNumber)
  174. {
  175. var text = url ? WebInspector.displayNameForURL(url) : WebInspector.UIString("(program)");
  176. if (typeof lineNumber === "number")
  177. text += ":" + (lineNumber + 1);
  178. return text;
  179. }
  180. /**
  181. * @param {string} url
  182. * @param {number=} lineNumber
  183. * @param {string=} classes
  184. * @param {string=} tooltipText
  185. * @return {Element}
  186. */
  187. WebInspector.linkifyResourceAsNode = function(url, lineNumber, classes, tooltipText)
  188. {
  189. var linkText = WebInspector.formatLinkText(url, lineNumber);
  190. var anchor = WebInspector.linkifyURLAsNode(url, linkText, classes, false, tooltipText);
  191. anchor.lineNumber = lineNumber;
  192. return anchor;
  193. }
  194. /**
  195. * @param {WebInspector.NetworkRequest} request
  196. * @return {Element}
  197. */
  198. WebInspector.linkifyRequestAsNode = function(request)
  199. {
  200. var anchor = WebInspector.linkifyURLAsNode(request.url);
  201. anchor.preferredPanel = "network";
  202. anchor.requestId = request.requestId;
  203. return anchor;
  204. }
  205. /**
  206. * @param {string} content
  207. * @param {string} mimeType
  208. * @param {boolean} contentEncoded
  209. * @return {?string}
  210. */
  211. WebInspector.contentAsDataURL = function(content, mimeType, contentEncoded)
  212. {
  213. const maxDataUrlSize = 1024 * 1024;
  214. if (content == null || content.length > maxDataUrlSize)
  215. return null;
  216. return "data:" + mimeType + (contentEncoded ? ";base64," : ",") + content;
  217. }