Linkifier.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2012 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following disclaimer
  12. * in the documentation and/or other materials provided with the
  13. * distribution.
  14. * * Neither the name of Google Inc. nor the names of its
  15. * contributors may be used to endorse or promote products derived from
  16. * this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * @interface
  32. */
  33. WebInspector.LinkifierFormatter = function()
  34. {
  35. }
  36. WebInspector.LinkifierFormatter.prototype = {
  37. /**
  38. * @param {Element} anchor
  39. * @param {WebInspector.UILocation} uiLocation
  40. */
  41. formatLiveAnchor: function(anchor, uiLocation) { }
  42. }
  43. /**
  44. * @constructor
  45. * @param {WebInspector.LinkifierFormatter=} formatter
  46. */
  47. WebInspector.Linkifier = function(formatter)
  48. {
  49. this._formatter = formatter || new WebInspector.Linkifier.DefaultFormatter(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
  50. this._liveLocations = [];
  51. }
  52. WebInspector.Linkifier.prototype = {
  53. /**
  54. * @param {string} sourceURL
  55. * @param {number} lineNumber
  56. * @param {number=} columnNumber
  57. * @param {string=} classes
  58. * @return {Element}
  59. */
  60. linkifyLocation: function(sourceURL, lineNumber, columnNumber, classes)
  61. {
  62. var rawLocation = WebInspector.debuggerModel.createRawLocationByURL(sourceURL, lineNumber, columnNumber || 0);
  63. if (!rawLocation)
  64. return WebInspector.linkifyResourceAsNode(sourceURL, lineNumber, classes);
  65. return this.linkifyRawLocation(rawLocation, classes);
  66. },
  67. /**
  68. * @param {WebInspector.DebuggerModel.Location} rawLocation
  69. * @param {string=} classes
  70. * @return {Element}
  71. */
  72. linkifyRawLocation: function(rawLocation, classes)
  73. {
  74. var script = WebInspector.debuggerModel.scriptForId(rawLocation.scriptId);
  75. if (!script)
  76. return null;
  77. var anchor = WebInspector.linkifyURLAsNode("", "", classes, false);
  78. var liveLocation = script.createLiveLocation(rawLocation, this._updateAnchor.bind(this, anchor));
  79. this._liveLocations.push(liveLocation);
  80. return anchor;
  81. },
  82. /**
  83. * @param {CSSAgent.StyleSheetId} styleSheetId
  84. * @param {WebInspector.CSSLocation} rawLocation
  85. * @return {?Element}
  86. */
  87. linkifyCSSLocation: function(styleSheetId, rawLocation)
  88. {
  89. var anchor = WebInspector.linkifyURLAsNode("", "", "", false);
  90. var liveLocation = WebInspector.cssModel.createLiveLocation(styleSheetId, rawLocation, this._updateAnchor.bind(this, anchor));
  91. if (!liveLocation)
  92. return null;
  93. this._liveLocations.push(liveLocation);
  94. return anchor;
  95. },
  96. reset: function()
  97. {
  98. for (var i = 0; i < this._liveLocations.length; ++i)
  99. this._liveLocations[i].dispose();
  100. this._liveLocations = [];
  101. },
  102. /**
  103. * @param {Element} anchor
  104. * @param {WebInspector.UILocation} uiLocation
  105. */
  106. _updateAnchor: function(anchor, uiLocation)
  107. {
  108. anchor.preferredPanel = "scripts";
  109. anchor.href = sanitizeHref(uiLocation.uiSourceCode.originURL());
  110. anchor.uiSourceCode = uiLocation.uiSourceCode;
  111. anchor.lineNumber = uiLocation.lineNumber;
  112. anchor.columnNumber = uiLocation.columnNumber;
  113. this._formatter.formatLiveAnchor(anchor, uiLocation);
  114. }
  115. }
  116. /**
  117. * @constructor
  118. * @implements {WebInspector.LinkifierFormatter}
  119. * @param {number=} maxLength
  120. */
  121. WebInspector.Linkifier.DefaultFormatter = function(maxLength)
  122. {
  123. this._maxLength = maxLength;
  124. }
  125. WebInspector.Linkifier.DefaultFormatter.prototype = {
  126. /**
  127. * @param {Element} anchor
  128. * @param {WebInspector.UILocation} uiLocation
  129. */
  130. formatLiveAnchor: function(anchor, uiLocation)
  131. {
  132. var text = uiLocation.linkText();
  133. if (this._maxLength)
  134. text = text.trimMiddle(this._maxLength);
  135. anchor.textContent = text;
  136. var titleText = uiLocation.uiSourceCode.originURL();
  137. if (typeof uiLocation.lineNumber === "number")
  138. titleText += ":" + (uiLocation.lineNumber + 1);
  139. anchor.title = titleText;
  140. },
  141. __proto__: WebInspector.LinkifierFormatter.prototype
  142. }
  143. /**
  144. * @constructor
  145. * @extends {WebInspector.Linkifier.DefaultFormatter}
  146. */
  147. WebInspector.Linkifier.DefaultCSSFormatter = function()
  148. {
  149. WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs);
  150. }
  151. WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30;
  152. WebInspector.Linkifier.DefaultCSSFormatter.prototype = {
  153. /**
  154. * @param {Element} anchor
  155. * @param {WebInspector.UILocation} uiLocation
  156. */
  157. formatLiveAnchor: function(anchor, uiLocation)
  158. {
  159. WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation);
  160. anchor.classList.add("webkit-html-resource-link");
  161. anchor.setAttribute("data-uncopyable", anchor.textContent);
  162. anchor.textContent = "";
  163. },
  164. __proto__: WebInspector.Linkifier.DefaultFormatter.prototype
  165. }
  166. /**
  167. * The maximum number of characters to display in a URL.
  168. * @const
  169. * @type {number}
  170. */
  171. WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;