• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/**
32 * @interface
33 */
34WebInspector.LinkifierFormatter = function()
35{
36}
37
38WebInspector.LinkifierFormatter.prototype = {
39    /**
40     * @param {!Element} anchor
41     * @param {!WebInspector.UILocation} uiLocation
42     */
43    formatLiveAnchor: function(anchor, uiLocation) { }
44}
45
46/**
47 * @constructor
48 * @param {!WebInspector.LinkifierFormatter=} formatter
49 */
50WebInspector.Linkifier = function(formatter)
51{
52    this._formatter = formatter || new WebInspector.Linkifier.DefaultFormatter(WebInspector.Linkifier.MaxLengthForDisplayedURLs);
53    this._liveLocations = [];
54}
55
56WebInspector.Linkifier.prototype = {
57    /**
58     * @param {string} sourceURL
59     * @param {number} lineNumber
60     * @param {number=} columnNumber
61     * @param {string=} classes
62     * @return {?Element}
63     */
64    linkifyLocation: function(sourceURL, lineNumber, columnNumber, classes)
65    {
66        var rawLocation = WebInspector.debuggerModel.createRawLocationByURL(sourceURL, lineNumber, columnNumber || 0);
67        if (!rawLocation)
68            return WebInspector.linkifyResourceAsNode(sourceURL, lineNumber, classes);
69        return this.linkifyRawLocation(rawLocation, classes);
70    },
71
72    /**
73     * @param {!WebInspector.DebuggerModel.Location} rawLocation
74     * @param {string=} classes
75     * @return {?Element}
76     */
77    linkifyRawLocation: function(rawLocation, classes)
78    {
79        var script = WebInspector.debuggerModel.scriptForId(rawLocation.scriptId);
80        if (!script)
81            return null;
82        var anchor = WebInspector.linkifyURLAsNode("", "", classes, false);
83        var liveLocation = script.createLiveLocation(rawLocation, this._updateAnchor.bind(this, anchor));
84        this._liveLocations.push(liveLocation);
85        return anchor;
86    },
87
88    /**
89     * @param {?CSSAgent.StyleSheetId} styleSheetId
90     * @param {!WebInspector.CSSLocation} rawLocation
91     * @param {string=} classes
92     * @return {?Element}
93     */
94    linkifyCSSLocation: function(styleSheetId, rawLocation, classes)
95    {
96        var anchor = WebInspector.linkifyURLAsNode("", "", classes, false);
97        var liveLocation = WebInspector.cssModel.createLiveLocation(styleSheetId, rawLocation, this._updateAnchor.bind(this, anchor));
98        if (!liveLocation)
99            return null;
100        this._liveLocations.push(liveLocation);
101        return anchor;
102    },
103
104    reset: function()
105    {
106        for (var i = 0; i < this._liveLocations.length; ++i)
107            this._liveLocations[i].dispose();
108        this._liveLocations = [];
109    },
110
111    /**
112     * @param {!Element} anchor
113     * @param {!WebInspector.UILocation} uiLocation
114     */
115    _updateAnchor: function(anchor, uiLocation)
116    {
117        anchor.preferredPanel = "sources";
118        anchor.href = sanitizeHref(uiLocation.uiSourceCode.originURL());
119        anchor.uiSourceCode = uiLocation.uiSourceCode;
120        anchor.lineNumber = uiLocation.lineNumber;
121        anchor.columnNumber = uiLocation.columnNumber;
122        this._formatter.formatLiveAnchor(anchor, uiLocation);
123    }
124}
125
126/**
127 * @constructor
128 * @implements {WebInspector.LinkifierFormatter}
129 * @param {number=} maxLength
130 */
131WebInspector.Linkifier.DefaultFormatter = function(maxLength)
132{
133    this._maxLength = maxLength;
134}
135
136WebInspector.Linkifier.DefaultFormatter.prototype = {
137    /**
138     * @param {!Element} anchor
139     * @param {!WebInspector.UILocation} uiLocation
140     */
141    formatLiveAnchor: function(anchor, uiLocation)
142    {
143        var text = uiLocation.linkText();
144        if (this._maxLength)
145            text = text.trimMiddle(this._maxLength);
146        anchor.textContent = text;
147
148        var titleText = uiLocation.uiSourceCode.originURL();
149        if (typeof uiLocation.lineNumber === "number")
150            titleText += ":" + (uiLocation.lineNumber + 1);
151        anchor.title = titleText;
152    },
153
154    __proto__: WebInspector.LinkifierFormatter.prototype
155}
156
157/**
158 * @constructor
159 * @extends {WebInspector.Linkifier.DefaultFormatter}
160 */
161WebInspector.Linkifier.DefaultCSSFormatter = function()
162{
163    WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs);
164}
165
166WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30;
167
168WebInspector.Linkifier.DefaultCSSFormatter.prototype = {
169    /**
170     * @param {!Element} anchor
171     * @param {!WebInspector.UILocation} uiLocation
172     */
173    formatLiveAnchor: function(anchor, uiLocation)
174    {
175        WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation);
176        anchor.classList.add("webkit-html-resource-link");
177        anchor.setAttribute("data-uncopyable", anchor.textContent);
178        anchor.textContent = "";
179    },
180    __proto__: WebInspector.Linkifier.DefaultFormatter.prototype
181}
182
183/**
184 * The maximum number of characters to display in a URL.
185 * @const
186 * @type {number}
187 */
188WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;
189