Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(781)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/components/Linkifier.js

Issue 2487293002: DevTools: introduce sync checkmarks icons in StylesSidebarPane. (Closed)
Patch Set: DevTools: introduce icons in linkifier. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 17 matching lines...) Expand all
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @implements {WebInspector.TargetManager.Observer} 32 * @implements {WebInspector.TargetManager.Observer}
33 * @unrestricted 33 * @unrestricted
34 */ 34 */
35 WebInspector.Linkifier = class { 35 WebInspector.Linkifier = class {
36 /** 36 /**
37 * @param {number=} maxLengthForDisplayedURLs 37 * @param {number=} maxLengthForDisplayedURLs
38 * @param {boolean=} useLinkDecorator
38 */ 39 */
39 constructor(maxLengthForDisplayedURLs) { 40 constructor(maxLengthForDisplayedURLs, useLinkDecorator) {
40 this._maxLength = maxLengthForDisplayedURLs || WebInspector.Linkifier.MaxLen gthForDisplayedURLs; 41 this._maxLength = maxLengthForDisplayedURLs || WebInspector.Linkifier.MaxLen gthForDisplayedURLs;
41 /** @type {!Map<!WebInspector.Target, !Array<!Element>>} */ 42 /** @type {!Map<!WebInspector.Target, !Array<!Element>>} */
42 this._anchorsByTarget = new Map(); 43 this._anchorsByTarget = new Map();
43 /** @type {!Map<!WebInspector.Target, !WebInspector.LiveLocationPool>} */ 44 /** @type {!Map<!WebInspector.Target, !WebInspector.LiveLocationPool>} */
44 this._locationPoolByTarget = new Map(); 45 this._locationPoolByTarget = new Map();
46 this._useLinkDecorator = true;
47 WebInspector.Linkifier._instances.add(this);
45 WebInspector.targetManager.observeTargets(this); 48 WebInspector.targetManager.observeTargets(this);
46 } 49 }
47 50
48 /** 51 /**
52 * @param {!WebInspector.LinkDecorator} decorator
53 */
54 static setLinkDecorator(decorator) {
55 console.assert(!WebInspector.Linkifier._decorator, 'Cannot re-register icon provider.');
dgozman 2016/11/10 01:01:43 ... link decorator
lushnikov 2016/11/10 01:13:45 Done.
56 WebInspector.Linkifier._decorator = decorator;
57 decorator.addEventListener(WebInspector.LinkDecorator.Events.LinkIconChanged , onLinkIconChanged);
58 for (var linkifier of WebInspector.Linkifier._instances)
59 linkifier._updateAllAnchorDecorations();
60
61 /**
62 * @param {!WebInspector.Event} event
63 */
64 function onLinkIconChanged(event) {
65 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */(event.data);
66 var anchors = uiSourceCode[WebInspector.Linkifier._sourceCodeAnchors] || [ ];
67 for (var anchor of anchors)
dgozman 2016/11/10 01:01:43 s/anchor/link/g
lushnikov 2016/11/10 01:13:45 Done.
68 WebInspector.Linkifier._decorateLink(anchor);
dgozman 2016/11/10 01:01:43 _updateAnchorDecorations
lushnikov 2016/11/10 01:13:45 Done.
69 }
70 }
71
72 _updateAllAnchorDecorations() {
73 for (var target of this._anchorsByTarget.keysArray()) {
dgozman 2016/11/10 01:01:43 for (var anchors of this._anchorsByTarget.values()
lushnikov 2016/11/10 01:13:45 Done.
74 var anchors = this._anchorsByTarget.get(target);
75 for (var anchor of anchors)
76 WebInspector.Linkifier._decorateLink(anchor);
77 }
78 }
79
80 /**
49 * @param {?WebInspector.Linkifier.LinkHandler} handler 81 * @param {?WebInspector.Linkifier.LinkHandler} handler
50 */ 82 */
51 static setLinkHandler(handler) { 83 static setLinkHandler(handler) {
52 WebInspector.Linkifier._linkHandler = handler; 84 WebInspector.Linkifier._linkHandler = handler;
53 } 85 }
54 86
55 /** 87 /**
56 * @param {string} url 88 * @param {string} url
57 * @param {number=} lineNumber 89 * @param {number=} lineNumber
58 * @return {boolean} 90 * @return {boolean}
(...skipping 15 matching lines...) Expand all
74 */ 106 */
75 static linkifyUsingRevealer(revealable, text, fallbackHref, fallbackLineNumber , title, classes) { 107 static linkifyUsingRevealer(revealable, text, fallbackHref, fallbackLineNumber , title, classes) {
76 var a = createElement('a'); 108 var a = createElement('a');
77 a.className = (classes || '') + ' webkit-html-resource-link'; 109 a.className = (classes || '') + ' webkit-html-resource-link';
78 a.textContent = text.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayed URLs); 110 a.textContent = text.trimMiddle(WebInspector.Linkifier.MaxLengthForDisplayed URLs);
79 a.title = title || text; 111 a.title = title || text;
80 if (fallbackHref) { 112 if (fallbackHref) {
81 a.href = fallbackHref; 113 a.href = fallbackHref;
82 a.lineNumber = fallbackLineNumber; 114 a.lineNumber = fallbackLineNumber;
83 } 115 }
116
84 /** 117 /**
85 * @param {!Event} event 118 * @param {!Event} event
86 * @this {Object} 119 * @this {Object}
87 */ 120 */
88 function clickHandler(event) { 121 function clickHandler(event) {
89 event.stopImmediatePropagation(); 122 event.stopImmediatePropagation();
90 event.preventDefault(); 123 event.preventDefault();
91 if (fallbackHref && WebInspector.Linkifier.handleLink(fallbackHref, fallba ckLineNumber)) 124 if (fallbackHref && WebInspector.Linkifier.handleLink(fallbackHref, fallba ckLineNumber))
92 return; 125 return;
93 126
94 WebInspector.Revealer.reveal(this); 127 WebInspector.Revealer.reveal(this);
95 } 128 }
96 a.addEventListener('click', clickHandler.bind(revealable), false); 129 a.addEventListener('click', clickHandler.bind(revealable), false);
97 return a; 130 return a;
98 } 131 }
99 132
100 /** 133 /**
101 * @param {!Element} anchor 134 * @param {!Element} anchor
102 * @return {?WebInspector.UILocation} uiLocation 135 * @return {?WebInspector.UILocation} uiLocation
103 */ 136 */
104 static uiLocationByAnchor(anchor) { 137 static uiLocationByAnchor(anchor) {
105 return anchor[WebInspector.Linkifier._uiLocationSymbol]; 138 return anchor[WebInspector.Linkifier._uiLocationSymbol];
106 } 139 }
107 140
108 /** 141 /**
142 * @param {!Element} anchor
143 * @param {?WebInspector.UILocation} uiLocation
dgozman 2016/11/10 01:01:43 Let's not accept null.
lushnikov 2016/11/10 01:13:45 Done.
144 */
145 static _bindUILocation(anchor, uiLocation) {
146 anchor[WebInspector.Linkifier._uiLocationSymbol] = uiLocation;
147 if (!uiLocation)
148 return;
149 var uiSourceCode = uiLocation.uiSourceCode;
150 var sourceCodeAnchors = uiSourceCode[WebInspector.Linkifier._sourceCodeAncho rs];
151 if (!sourceCodeAnchors) {
152 sourceCodeAnchors = new Set();
153 uiSourceCode[WebInspector.Linkifier._sourceCodeAnchors] = sourceCodeAnchor s;
154 }
155 sourceCodeAnchors.add(anchor);
156 }
157
158 /**
159 * @param {!Element} anchor
160 */
161 static _unbindUILocation(anchor) {
162 if (!anchor[WebInspector.Linkifier._uiLocationSymbol])
163 return;
164
165 var uiSourceCode = anchor[WebInspector.Linkifier._uiLocationSymbol].uiSource Code;
dgozman 2016/11/10 01:01:43 nullify this.
lushnikov 2016/11/10 01:13:45 Done.
166 var sourceCodeAnchors = uiSourceCode[WebInspector.Linkifier._sourceCodeAncho rs];
167 if (sourceCodeAnchors)
168 sourceCodeAnchors.delete(anchor);
169 }
170
171 /**
109 * @param {!WebInspector.Target} target 172 * @param {!WebInspector.Target} target
110 * @param {string} scriptId 173 * @param {string} scriptId
111 * @param {number} lineNumber 174 * @param {number} lineNumber
112 * @param {number=} columnNumber 175 * @param {number=} columnNumber
113 * @return {string} 176 * @return {string}
114 */ 177 */
115 static liveLocationText(target, scriptId, lineNumber, columnNumber) { 178 static liveLocationText(target, scriptId, lineNumber, columnNumber) {
116 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target); 179 var debuggerModel = WebInspector.DebuggerModel.fromTarget(target);
117 if (!debuggerModel) 180 if (!debuggerModel)
118 return ''; 181 return '';
(...skipping 19 matching lines...) Expand all
138 /** 201 /**
139 * @override 202 * @override
140 * @param {!WebInspector.Target} target 203 * @param {!WebInspector.Target} target
141 */ 204 */
142 targetRemoved(target) { 205 targetRemoved(target) {
143 var locationPool = /** @type {!WebInspector.LiveLocationPool} */ (this._loca tionPoolByTarget.remove(target)); 206 var locationPool = /** @type {!WebInspector.LiveLocationPool} */ (this._loca tionPoolByTarget.remove(target));
144 locationPool.disposeAll(); 207 locationPool.disposeAll();
145 var anchors = this._anchorsByTarget.remove(target); 208 var anchors = this._anchorsByTarget.remove(target);
146 for (var anchor of anchors) { 209 for (var anchor of anchors) {
147 delete anchor[WebInspector.Linkifier._liveLocationSymbol]; 210 delete anchor[WebInspector.Linkifier._liveLocationSymbol];
211 WebInspector.Linkifier._unbindUILocation(anchor);
148 var fallbackAnchor = anchor[WebInspector.Linkifier._fallbackAnchorSymbol]; 212 var fallbackAnchor = anchor[WebInspector.Linkifier._fallbackAnchorSymbol];
149 if (fallbackAnchor) { 213 if (fallbackAnchor) {
150 anchor.href = fallbackAnchor.href; 214 anchor.href = fallbackAnchor.href;
151 anchor.lineNumber = fallbackAnchor.lineNumber; 215 anchor.lineNumber = fallbackAnchor.lineNumber;
152 anchor.title = fallbackAnchor.title; 216 anchor.title = fallbackAnchor.title;
153 anchor.className = fallbackAnchor.className; 217 anchor.className = fallbackAnchor.className;
154 anchor.textContent = fallbackAnchor.textContent; 218 anchor.textContent = fallbackAnchor.textContent;
155 delete anchor[WebInspector.Linkifier._fallbackAnchorSymbol]; 219 delete anchor[WebInspector.Linkifier._fallbackAnchorSymbol];
156 } 220 }
157 } 221 }
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 anchors.push(anchor); 338 anchors.push(anchor);
275 anchor[WebInspector.Linkifier._liveLocationSymbol] = liveLocation; 339 anchor[WebInspector.Linkifier._liveLocationSymbol] = liveLocation;
276 return anchor; 340 return anchor;
277 } 341 }
278 342
279 /** 343 /**
280 * @param {!WebInspector.Target} target 344 * @param {!WebInspector.Target} target
281 * @param {!Element} anchor 345 * @param {!Element} anchor
282 */ 346 */
283 disposeAnchor(target, anchor) { 347 disposeAnchor(target, anchor) {
284 delete anchor[WebInspector.Linkifier._uiLocationSymbol]; 348 WebInspector.Linkifier._unbindUILocation(anchor);
285 delete anchor[WebInspector.Linkifier._fallbackAnchorSymbol]; 349 delete anchor[WebInspector.Linkifier._fallbackAnchorSymbol];
286 var liveLocation = anchor[WebInspector.Linkifier._liveLocationSymbol]; 350 var liveLocation = anchor[WebInspector.Linkifier._liveLocationSymbol];
287 if (liveLocation) 351 if (liveLocation)
288 liveLocation.dispose(); 352 liveLocation.dispose();
289 delete anchor[WebInspector.Linkifier._liveLocationSymbol]; 353 delete anchor[WebInspector.Linkifier._liveLocationSymbol];
290 } 354 }
291 355
292 /** 356 /**
293 * @param {string=} classes 357 * @param {string=} classes
294 * @return {!Element} 358 * @return {!Element}
295 */ 359 */
296 _createAnchor(classes) { 360 _createAnchor(classes) {
297 var anchor = createElement('a'); 361 var anchor = createElement('a');
362 if (this._useLinkDecorator)
363 anchor[WebInspector.Linkifier._enableDecoratorSymbol] = true;
298 anchor.className = (classes || '') + ' webkit-html-resource-link'; 364 anchor.className = (classes || '') + ' webkit-html-resource-link';
299 365
300 /** 366 /**
301 * @param {!Event} event 367 * @param {!Event} event
302 */ 368 */
303 function clickHandler(event) { 369 function clickHandler(event) {
304 var uiLocation = anchor[WebInspector.Linkifier._uiLocationSymbol]; 370 var uiLocation = anchor[WebInspector.Linkifier._uiLocationSymbol];
305 if (!uiLocation) 371 if (!uiLocation)
306 return; 372 return;
307 373
(...skipping 10 matching lines...) Expand all
318 for (var target of this._anchorsByTarget.keysArray()) { 384 for (var target of this._anchorsByTarget.keysArray()) {
319 this.targetRemoved(target); 385 this.targetRemoved(target);
320 this.targetAdded(target); 386 this.targetAdded(target);
321 } 387 }
322 } 388 }
323 389
324 dispose() { 390 dispose() {
325 for (var target of this._anchorsByTarget.keysArray()) 391 for (var target of this._anchorsByTarget.keysArray())
326 this.targetRemoved(target); 392 this.targetRemoved(target);
327 WebInspector.targetManager.unobserveTargets(this); 393 WebInspector.targetManager.unobserveTargets(this);
394 WebInspector.Linkifier._instances.delete(this);
328 } 395 }
329 396
330 /** 397 /**
331 * @param {!Element} anchor 398 * @param {!Element} anchor
332 * @param {!WebInspector.LiveLocation} liveLocation 399 * @param {!WebInspector.LiveLocation} liveLocation
333 */ 400 */
334 _updateAnchor(anchor, liveLocation) { 401 _updateAnchor(anchor, liveLocation) {
402 WebInspector.Linkifier._unbindUILocation(anchor);
335 var uiLocation = liveLocation.uiLocation(); 403 var uiLocation = liveLocation.uiLocation();
404 WebInspector.Linkifier._bindUILocation(anchor, uiLocation);
336 if (!uiLocation) 405 if (!uiLocation)
337 return; 406 return;
338 anchor[WebInspector.Linkifier._uiLocationSymbol] = uiLocation;
339 this._formatLiveAnchor(anchor, uiLocation, liveLocation.isBlackboxed());
340 }
341 407
342 /**
343 * @param {!Element} anchor
344 * @param {!WebInspector.UILocation} uiLocation
345 * @param {boolean} isBlackboxed
346 */
347 _formatLiveAnchor(anchor, uiLocation, isBlackboxed) {
348 var text = uiLocation.linkText(); 408 var text = uiLocation.linkText();
349 text = text.replace(/([a-f0-9]{7})[a-f0-9]{13}[a-f0-9]*/g, '$1\u2026'); 409 text = text.replace(/([a-f0-9]{7})[a-f0-9]{13}[a-f0-9]*/g, '$1\u2026');
350 if (this._maxLength) 410 if (this._maxLength)
351 text = text.trimMiddle(this._maxLength); 411 text = text.trimMiddle(this._maxLength);
352 anchor.textContent = text; 412 anchor.textContent = text;
353 413
354 var titleText = uiLocation.uiSourceCode.url(); 414 var titleText = uiLocation.uiSourceCode.url();
355 if (typeof uiLocation.lineNumber === 'number') 415 if (typeof uiLocation.lineNumber === 'number')
356 titleText += ':' + (uiLocation.lineNumber + 1); 416 titleText += ':' + (uiLocation.lineNumber + 1);
357 anchor.title = titleText; 417 anchor.title = titleText;
418 anchor.classList.toggle('webkit-html-blackbox-link', liveLocation.isBlackbox ed());
419 WebInspector.Linkifier._decorateLink(anchor);
420 }
358 421
359 anchor.classList.toggle('webkit-html-blackbox-link', isBlackboxed); 422 /**
423 * @param {!Element} anchor
424 */
425 static _decorateLink(anchor) {
426 if (!anchor[WebInspector.Linkifier._enableDecoratorSymbol])
427 return;
428 var uiLocation = anchor[WebInspector.Linkifier._uiLocationSymbol];
429 if (!WebInspector.Linkifier._decorator || !uiLocation)
430 return;
431 var icon = anchor.querySelector('span[is=ui-icon]');
dgozman 2016/11/10 01:01:43 Put this under symbol instead.
lushnikov 2016/11/10 01:13:45 Done.
432 if (icon)
433 icon.remove();
434 var newIcon = WebInspector.Linkifier._decorator.linkIcon(uiLocation.uiSource Code);
dgozman 2016/11/10 01:01:43 reuse icon variable
lushnikov 2016/11/10 01:13:45 Done.
435 if (newIcon) {
436 newIcon.style.setProperty('margin-right', '2px');
437 anchor.insertBefore(newIcon, anchor.firstChild);
438 } else {
439 // if (uiLocation.uiSourceCode.name().endsWith("common.scss"))
dgozman 2016/11/10 01:01:43 :-)
lushnikov 2016/11/10 01:13:45 Done.
440 // debugger;
441 }
360 } 442 }
361 }; 443 };
362 444
445 /** @type {!Set<!WebInspector.Linkifier>} */
446 WebInspector.Linkifier._instances = new Set();
447 /** @type {?WebInspector.LinkDecorator} */
448 WebInspector.Linkifier._decorator = null;
449
450 WebInspector.Linkifier._enableDecoratorSymbol = Symbol('Linkifier.enableIconsSym bol');
451 WebInspector.Linkifier._sourceCodeAnchors = Symbol('Linkifier.anchors');
363 WebInspector.Linkifier._uiLocationSymbol = Symbol('uiLocation'); 452 WebInspector.Linkifier._uiLocationSymbol = Symbol('uiLocation');
364 WebInspector.Linkifier._fallbackAnchorSymbol = Symbol('fallbackAnchor'); 453 WebInspector.Linkifier._fallbackAnchorSymbol = Symbol('fallbackAnchor');
365 WebInspector.Linkifier._liveLocationSymbol = Symbol('liveLocation'); 454 WebInspector.Linkifier._liveLocationSymbol = Symbol('liveLocation');
366 455
367 /** 456 /**
368 * The maximum number of characters to display in a URL. 457 * The maximum number of characters to display in a URL.
369 * @const 458 * @const
370 * @type {number} 459 * @type {number}
371 */ 460 */
372 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150; 461 WebInspector.Linkifier.MaxLengthForDisplayedURLs = 150;
(...skipping 12 matching lines...) Expand all
385 474
386 WebInspector.Linkifier.LinkHandler.prototype = { 475 WebInspector.Linkifier.LinkHandler.prototype = {
387 /** 476 /**
388 * @param {string} url 477 * @param {string} url
389 * @param {number=} lineNumber 478 * @param {number=} lineNumber
390 * @return {boolean} 479 * @return {boolean}
391 */ 480 */
392 handleLink: function(url, lineNumber) {} 481 handleLink: function(url, lineNumber) {}
393 }; 482 };
394 483
484 /**
485 * @extends {WebInspector.EventTarget}
486 * @interface
487 */
488 WebInspector.LinkDecorator = function() {};
489
490 WebInspector.LinkDecorator.prototype = {
491 /**
492 * @param {!WebInspector.UISourceCode} uiSourceCode
493 * @return {?WebInspector.Icon}
494 */
495 linkIcon: function(uiSourceCode) {}
496 };
497
498 WebInspector.LinkDecorator.Events = {
499 LinkIconChanged: Symbol('LinkIconChanged')
500 };
395 501
396 /** 502 /**
397 * @param {string} string 503 * @param {string} string
398 * @param {function(string,string,number=,number=):!Node} linkifier 504 * @param {function(string,string,number=,number=):!Node} linkifier
399 * @return {!DocumentFragment} 505 * @return {!DocumentFragment}
400 */ 506 */
401 WebInspector.linkifyStringAsFragmentWithCustomLinkifier = function(string, linki fier) { 507 WebInspector.linkifyStringAsFragmentWithCustomLinkifier = function(string, linki fier) {
402 var container = createDocumentFragment(); 508 var container = createDocumentFragment();
403 var linkStringRegEx = 509 var linkStringRegEx =
404 /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|data:|www\.)[\w$\-_+*'=\|\/\\(){}[\]^% @&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({^%@&#~]/; 510 /(?:[a-zA-Z][a-zA-Z0-9+.-]{2,}:\/\/|data:|www\.)[\w$\-_+*'=\|\/\\(){}[\]^% @&#~,:;.!?]{2,}[\w$\-_+*=\|\/\\({^%@&#~]/;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
487 593
488 /** 594 /**
489 * @param {!WebInspector.NetworkRequest} request 595 * @param {!WebInspector.NetworkRequest} request
490 * @return {!Element} 596 * @return {!Element}
491 */ 597 */
492 WebInspector.linkifyRequestAsNode = function(request) { 598 WebInspector.linkifyRequestAsNode = function(request) {
493 var anchor = WebInspector.linkifyURLAsNode(request.url); 599 var anchor = WebInspector.linkifyURLAsNode(request.url);
494 anchor.requestId = request.requestId; 600 anchor.requestId = request.requestId;
495 return anchor; 601 return anchor;
496 }; 602 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698