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 */ 34 WebInspector.LinkifierFormatter = function() 35 { 36 } 37 38 WebInspector.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 */ 50 WebInspector.Linkifier = function(formatter) 51 { 52 this._formatter = formatter || new WebInspector.Linkifier.DefaultFormatter(WebInspector.Linkifier.MaxLengthForDisplayedURLs); 53 this._liveLocations = []; 54 } 55 56 WebInspector.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 * @return {?Element} 92 */ 93 linkifyCSSLocation: function(styleSheetId, rawLocation) 94 { 95 var anchor = WebInspector.linkifyURLAsNode("", "", "", false); 96 var liveLocation = WebInspector.cssModel.createLiveLocation(styleSheetId, rawLocation, this._updateAnchor.bind(this, anchor)); 97 if (!liveLocation) 98 return null; 99 this._liveLocations.push(liveLocation); 100 return anchor; 101 }, 102 103 reset: function() 104 { 105 for (var i = 0; i < this._liveLocations.length; ++i) 106 this._liveLocations[i].dispose(); 107 this._liveLocations = []; 108 }, 109 110 /** 111 * @param {Element} anchor 112 * @param {WebInspector.UILocation} uiLocation 113 */ 114 _updateAnchor: function(anchor, uiLocation) 115 { 116 anchor.preferredPanel = "scripts"; 117 anchor.href = sanitizeHref(uiLocation.uiSourceCode.originURL()); 118 anchor.uiSourceCode = uiLocation.uiSourceCode; 119 anchor.lineNumber = uiLocation.lineNumber; 120 anchor.columnNumber = uiLocation.columnNumber; 121 this._formatter.formatLiveAnchor(anchor, uiLocation); 122 } 123 } 124 125 /** 126 * @constructor 127 * @implements {WebInspector.LinkifierFormatter} 128 * @param {number=} maxLength 129 */ 130 WebInspector.Linkifier.DefaultFormatter = function(maxLength) 131 { 132 this._maxLength = maxLength; 133 } 134 135 WebInspector.Linkifier.DefaultFormatter.prototype = { 136 /** 137 * @param {Element} anchor 138 * @param {WebInspector.UILocation} uiLocation 139 */ 140 formatLiveAnchor: function(anchor, uiLocation) 141 { 142 var text = uiLocation.linkText(); 143 if (this._maxLength) 144 text = text.trimMiddle(this._maxLength); 145 anchor.textContent = text; 146 147 var titleText = uiLocation.uiSourceCode.originURL(); 148 if (typeof uiLocation.lineNumber === "number") 149 titleText += ":" + (uiLocation.lineNumber + 1); 150 anchor.title = titleText; 151 }, 152 153 __proto__: WebInspector.LinkifierFormatter.prototype 154 } 155 156 /** 157 * @constructor 158 * @extends {WebInspector.Linkifier.DefaultFormatter} 159 */ 160 WebInspector.Linkifier.DefaultCSSFormatter = function() 161 { 162 WebInspector.Linkifier.DefaultFormatter.call(this, WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs); 163 } 164 165 WebInspector.Linkifier.DefaultCSSFormatter.MaxLengthForDisplayedURLs = 30; 166 167 WebInspector.Linkifier.DefaultCSSFormatter.prototype = { 168 /** 169 * @param {Element} anchor 170 * @param {WebInspector.UILocation} uiLocation 171 */ 172 formatLiveAnchor: function(anchor, uiLocation) 173 { 174 WebInspector.Linkifier.DefaultFormatter.prototype.formatLiveAnchor.call(this, anchor, uiLocation); 175 anchor.classList.add("webkit-html-resource-link"); 176 anchor.setAttribute("data-uncopyable", anchor.textContent); 177 anchor.textContent = ""; 178 }, 179 __proto__: WebInspector.Linkifier.DefaultFormatter.prototype 180 } 181 182 /** 183 * The maximum number of characters to display in a URL. 184 * @const 185 * @type {number} 186 */ 187 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150; 188