2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 WebInspector.LinkifierFormatter = function()
38 WebInspector.LinkifierFormatter.prototype = {
40 * @param {!Element} anchor
41 * @param {!WebInspector.UILocation} uiLocation
43 formatLiveAnchor: function(anchor, uiLocation) { }
48 * @param {!WebInspector.LinkifierFormatter=} formatter
50 WebInspector.Linkifier = function(formatter)
52 this._formatter = formatter || new WebInspector.Linkifier.DefaultFormatter(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
53 this._liveLocations = [];
57 * @param {!WebInspector.Linkifier.LinkHandler} handler
59 WebInspector.Linkifier.setLinkHandler = function(handler)
61 WebInspector.Linkifier._linkHandler = handler;
66 * @param {number=} lineNumber
69 WebInspector.Linkifier.handleLink = function(url, lineNumber)
71 if (!WebInspector.Linkifier._linkHandler)
73 return WebInspector.Linkifier._linkHandler.handleLink(url, lineNumber)
77 * @param {!Object} revealable
78 * @param {string} text
79 * @param {string} fallbackHref
80 * @param {number=} fallbackLineNumber
81 * @param {string=} title
82 * @param {string=} classes
85 WebInspector.Linkifier.linkifyUsingRevealer = function(revealable, text, fallbackHref, fallbackLineNumber, title, classes)
87 var a = document.createElement("a");
88 a.className = (classes || "") + " webkit-html-resource-link";
89 a.textContent = text.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
90 a.title = title || text;
91 a.href = fallbackHref;
92 a.lineNumber = fallbackLineNumber;
95 * @param {?Event} event
98 function clickHandler(event)
101 if (WebInspector.Linkifier.handleLink(fallbackHref, fallbackLineNumber))
104 WebInspector.Revealer.reveal(this);
106 a.addEventListener("click", clickHandler.bind(revealable), false);
110 WebInspector.Linkifier.prototype = {
112 * @param {string} sourceURL
113 * @param {number} lineNumber
114 * @param {number=} columnNumber
115 * @param {string=} classes
118 linkifyLocation: function(sourceURL, lineNumber, columnNumber, classes)
120 var rawLocation = WebInspector.debuggerModel.createRawLocationByURL(sourceURL, lineNumber, columnNumber || 0);
122 return WebInspector.linkifyResourceAsNode(sourceURL, lineNumber, classes);
123 return this.linkifyRawLocation(rawLocation, classes);
127 * @param {!WebInspector.DebuggerModel.Location} rawLocation
128 * @param {string=} classes
131 linkifyRawLocation: function(rawLocation, classes)
133 var script = WebInspector.debuggerModel.scriptForId(rawLocation.scriptId);
136 var anchor = this._createAnchor(classes);
137 var liveLocation = script.createLiveLocation(rawLocation, this._updateAnchor.bind(this, anchor));
138 this._liveLocations.push(liveLocation);
143 * @param {?CSSAgent.StyleSheetId} styleSheetId
144 * @param {!WebInspector.CSSLocation} rawLocation
145 * @param {string=} classes
148 linkifyCSSLocation: function(styleSheetId, rawLocation, classes)
150 var anchor = this._createAnchor(classes);
151 var liveLocation = WebInspector.cssModel.createLiveLocation(styleSheetId, rawLocation, this._updateAnchor.bind(this, anchor));
154 this._liveLocations.push(liveLocation);
159 * @param {string=} classes
162 _createAnchor: function(classes)
164 var anchor = document.createElement("a");
165 anchor.className = (classes || "") + " webkit-html-resource-link";
168 * @param {?Event} event
170 function clickHandler(event)
173 if (!anchor.__uiLocation)
175 if (WebInspector.Linkifier.handleLink(anchor.__uiLocation.url(), anchor.__uiLocation.lineNumber))
177 WebInspector.Revealer.reveal(anchor.__uiLocation);
179 anchor.addEventListener("click", clickHandler, false);
185 for (var i = 0; i < this._liveLocations.length; ++i)
186 this._liveLocations[i].dispose();
187 this._liveLocations = [];
191 * @param {!Element} anchor
192 * @param {!WebInspector.UILocation} uiLocation
194 _updateAnchor: function(anchor, uiLocation)
196 anchor.__uiLocation = uiLocation;
197 this._formatter.formatLiveAnchor(anchor, uiLocation);
203 * @implements {WebInspector.LinkifierFormatter}
204 * @param {number=} maxLength
206 WebInspector.Linkifier.DefaultFormatter = function(maxLength)
208 this._maxLength = maxLength;
211 WebInspector.Linkifier.DefaultFormatter.prototype = {
213 * @param {!Element} anchor
214 * @param {!WebInspector.UILocation} uiLocation
216 formatLiveAnchor: function(anchor, uiLocation)
218 var text = uiLocation.linkText();
220 text = text.trimMiddle(this._maxLength);
221 anchor.textContent = text;
223 var titleText = uiLocation.uiSourceCode.originURL();
224 if (typeof uiLocation.lineNumber === "number")
225 titleText += ":" + (uiLocation.lineNumber + 1);
226 anchor.title = titleText;
232 * @extends {WebInspector.Linkifier.DefaultFormatter}
234 WebInspector.Linkifier.DefaultCSSFormatter = function()
236 WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs);
239 WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30;
241 WebInspector.Linkifier.DefaultCSSFormatter.prototype = {
243 * @param {!Element} anchor
244 * @param {!WebInspector.UILocation} uiLocation
246 formatLiveAnchor: function(anchor, uiLocation)
248 WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation);
249 anchor.classList.add("webkit-html-resource-link");
250 anchor.setAttribute("data-uncopyable", anchor.textContent);
251 anchor.textContent = "";
253 __proto__: WebInspector.Linkifier.DefaultFormatter.prototype
257 * The maximum number of characters to display in a URL.
261 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;
266 WebInspector.Linkifier.LinkHandler = function()
270 WebInspector.Linkifier.LinkHandler.prototype = {
272 * @param {string} url
273 * @param {number=} lineNumber
276 handleLink: function(url, lineNumber) {}