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;
92 a.href = fallbackHref;
93 a.lineNumber = fallbackLineNumber;
96 * @param {?Event} event
99 function clickHandler(event)
102 if (fallbackHref && WebInspector.Linkifier.handleLink(fallbackHref, fallbackLineNumber))
105 WebInspector.Revealer.reveal(this);
107 a.addEventListener("click", clickHandler.bind(revealable), false);
111 WebInspector.Linkifier.prototype = {
113 * @param {string} sourceURL
114 * @param {number} lineNumber
115 * @param {number=} columnNumber
116 * @param {string=} classes
119 linkifyLocation: function(sourceURL, lineNumber, columnNumber, classes)
121 var rawLocation = WebInspector.debuggerModel.createRawLocationByURL(sourceURL, lineNumber, columnNumber || 0);
123 return WebInspector.linkifyResourceAsNode(sourceURL, lineNumber, classes);
124 return this.linkifyRawLocation(rawLocation, classes);
128 * @param {!WebInspector.DebuggerModel.Location} rawLocation
129 * @param {string=} classes
132 linkifyRawLocation: function(rawLocation, classes)
134 var script = WebInspector.debuggerModel.scriptForId(rawLocation.scriptId);
137 var anchor = this._createAnchor(classes);
138 var liveLocation = script.createLiveLocation(rawLocation, this._updateAnchor.bind(this, anchor));
139 this._liveLocations.push(liveLocation);
144 * @param {?CSSAgent.StyleSheetId} styleSheetId
145 * @param {!WebInspector.CSSLocation} rawLocation
146 * @param {string=} classes
149 linkifyCSSLocation: function(styleSheetId, rawLocation, classes)
151 var anchor = this._createAnchor(classes);
152 var liveLocation = WebInspector.cssModel.createLiveLocation(styleSheetId, rawLocation, this._updateAnchor.bind(this, anchor));
155 this._liveLocations.push(liveLocation);
160 * @param {string=} classes
163 _createAnchor: function(classes)
165 var anchor = document.createElement("a");
166 anchor.className = (classes || "") + " webkit-html-resource-link";
169 * @param {?Event} event
171 function clickHandler(event)
174 if (!anchor.__uiLocation)
176 if (WebInspector.Linkifier.handleLink(anchor.__uiLocation.url(), anchor.__uiLocation.lineNumber))
178 WebInspector.Revealer.reveal(anchor.__uiLocation);
180 anchor.addEventListener("click", clickHandler, false);
186 for (var i = 0; i < this._liveLocations.length; ++i)
187 this._liveLocations[i].dispose();
188 this._liveLocations = [];
192 * @param {!Element} anchor
193 * @param {!WebInspector.UILocation} uiLocation
195 _updateAnchor: function(anchor, uiLocation)
197 anchor.__uiLocation = uiLocation;
198 this._formatter.formatLiveAnchor(anchor, uiLocation);
204 * @implements {WebInspector.LinkifierFormatter}
205 * @param {number=} maxLength
207 WebInspector.Linkifier.DefaultFormatter = function(maxLength)
209 this._maxLength = maxLength;
212 WebInspector.Linkifier.DefaultFormatter.prototype = {
214 * @param {!Element} anchor
215 * @param {!WebInspector.UILocation} uiLocation
217 formatLiveAnchor: function(anchor, uiLocation)
219 var text = uiLocation.linkText();
221 text = text.trimMiddle(this._maxLength);
222 anchor.textContent = text;
224 var titleText = uiLocation.uiSourceCode.originURL();
225 if (typeof uiLocation.lineNumber === "number")
226 titleText += ":" + (uiLocation.lineNumber + 1);
227 anchor.title = titleText;
233 * @extends {WebInspector.Linkifier.DefaultFormatter}
235 WebInspector.Linkifier.DefaultCSSFormatter = function()
237 WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs);
240 WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30;
242 WebInspector.Linkifier.DefaultCSSFormatter.prototype = {
244 * @param {!Element} anchor
245 * @param {!WebInspector.UILocation} uiLocation
247 formatLiveAnchor: function(anchor, uiLocation)
249 WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation);
250 anchor.classList.add("webkit-html-resource-link");
251 anchor.setAttribute("data-uncopyable", anchor.textContent);
252 anchor.textContent = "";
254 __proto__: WebInspector.Linkifier.DefaultFormatter.prototype
258 * The maximum number of characters to display in a URL.
262 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;
267 WebInspector.Linkifier.LinkHandler = function()
271 WebInspector.Linkifier.LinkHandler.prototype = {
273 * @param {string} url
274 * @param {number=} lineNumber
277 handleLink: function(url, lineNumber) {}
281 * @param {string} scriptId
282 * @param {number} lineNumber
283 * @param {number=} columnNumber
285 WebInspector.Linkifier.liveLocationText = function(scriptId, lineNumber, columnNumber)
287 var script = WebInspector.debuggerModel.scriptForId(scriptId);
290 var uiLocation = script.rawLocationToUILocation(lineNumber, columnNumber);
291 return uiLocation.linkText();