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)
101 event.stopImmediatePropagation();
102 event.preventDefault();
103 if (fallbackHref && WebInspector.Linkifier.handleLink(fallbackHref, fallbackLineNumber))
106 WebInspector.Revealer.reveal(this);
108 a.addEventListener("click", clickHandler.bind(revealable), false);
112 WebInspector.Linkifier.prototype = {
114 * @param {!WebInspector.Target} target
115 * @param {string} sourceURL
116 * @param {number} lineNumber
117 * @param {number=} columnNumber
118 * @param {string=} classes
121 linkifyLocation: function(target, sourceURL, lineNumber, columnNumber, classes)
123 var rawLocation = target.debuggerModel.createRawLocationByURL(sourceURL, lineNumber, columnNumber || 0);
125 return WebInspector.linkifyResourceAsNode(sourceURL, lineNumber, classes);
126 return this.linkifyRawLocation(rawLocation, classes);
130 * @param {!WebInspector.DebuggerModel.Location} rawLocation
131 * @param {string=} classes
134 linkifyRawLocation: function(rawLocation, classes)
136 // FIXME: this check should not be here.
137 var script = rawLocation.target().debuggerModel.scriptForId(rawLocation.scriptId);
140 var anchor = this._createAnchor(classes);
141 var liveLocation = rawLocation.createLiveLocation(this._updateAnchor.bind(this, anchor));
142 this._liveLocations.push(liveLocation);
147 * @param {?CSSAgent.StyleSheetId} styleSheetId
148 * @param {!WebInspector.CSSLocation} rawLocation
149 * @param {string=} classes
152 linkifyCSSLocation: function(styleSheetId, rawLocation, classes)
154 var anchor = this._createAnchor(classes);
155 var liveLocation = rawLocation.createLiveLocation(styleSheetId, this._updateAnchor.bind(this, anchor));
158 this._liveLocations.push(liveLocation);
163 * @param {string=} classes
166 _createAnchor: function(classes)
168 var anchor = document.createElement("a");
169 anchor.className = (classes || "") + " webkit-html-resource-link";
172 * @param {?Event} event
174 function clickHandler(event)
176 event.stopImmediatePropagation();
177 event.preventDefault();
178 if (!anchor.__uiLocation)
180 if (WebInspector.Linkifier.handleLink(anchor.__uiLocation.uiSourceCode.url, anchor.__uiLocation.lineNumber))
182 WebInspector.Revealer.reveal(anchor.__uiLocation);
184 anchor.addEventListener("click", clickHandler, false);
190 for (var i = 0; i < this._liveLocations.length; ++i)
191 this._liveLocations[i].dispose();
192 this._liveLocations = [];
196 * @param {!Element} anchor
197 * @param {!WebInspector.UILocation} uiLocation
199 _updateAnchor: function(anchor, uiLocation)
201 anchor.__uiLocation = uiLocation;
202 this._formatter.formatLiveAnchor(anchor, uiLocation);
208 * @implements {WebInspector.LinkifierFormatter}
209 * @param {number=} maxLength
211 WebInspector.Linkifier.DefaultFormatter = function(maxLength)
213 this._maxLength = maxLength;
216 WebInspector.Linkifier.DefaultFormatter.prototype = {
218 * @param {!Element} anchor
219 * @param {!WebInspector.UILocation} uiLocation
221 formatLiveAnchor: function(anchor, uiLocation)
223 var text = uiLocation.linkText();
225 text = text.trimMiddle(this._maxLength);
226 anchor.textContent = text;
228 var titleText = uiLocation.uiSourceCode.originURL();
229 if (typeof uiLocation.lineNumber === "number")
230 titleText += ":" + (uiLocation.lineNumber + 1);
231 anchor.title = titleText;
237 * @extends {WebInspector.Linkifier.DefaultFormatter}
239 WebInspector.Linkifier.DefaultCSSFormatter = function()
241 WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs);
244 WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30;
246 WebInspector.Linkifier.DefaultCSSFormatter.prototype = {
248 * @param {!Element} anchor
249 * @param {!WebInspector.UILocation} uiLocation
251 formatLiveAnchor: function(anchor, uiLocation)
253 WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation);
254 anchor.classList.add("webkit-html-resource-link");
255 anchor.setAttribute("data-uncopyable", anchor.textContent);
256 anchor.textContent = "";
258 __proto__: WebInspector.Linkifier.DefaultFormatter.prototype
262 * The maximum number of characters to display in a URL.
266 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;
271 WebInspector.Linkifier.LinkHandler = function()
275 WebInspector.Linkifier.LinkHandler.prototype = {
277 * @param {string} url
278 * @param {number=} lineNumber
281 handleLink: function(url, lineNumber) {}
285 * @param {!WebInspector.Target} target
286 * @param {string} scriptId
287 * @param {number} lineNumber
288 * @param {number=} columnNumber
290 WebInspector.Linkifier.liveLocationText = function(target, scriptId, lineNumber, columnNumber)
292 var script = target.debuggerModel.scriptForId(scriptId);
295 var uiLocation = script.rawLocationToUILocation(lineNumber, columnNumber);
296 return uiLocation.linkText();