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

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

Issue 2510193003: [DevTools] Move link click and context menu handling to Linkifier. (Closed)
Patch Set: rework 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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 return; 124 return;
125 125
126 Common.Revealer.reveal(this); 126 Common.Revealer.reveal(this);
127 } 127 }
128 a.addEventListener('click', clickHandler.bind(revealable), false); 128 a.addEventListener('click', clickHandler.bind(revealable), false);
129 return a; 129 return a;
130 } 130 }
131 131
132 /** 132 /**
133 * @param {!Element} anchor 133 * @param {!Element} anchor
134 * @return {?Workspace.UILocation} uiLocation
135 */
136 static uiLocationByAnchor(anchor) {
137 return anchor[Components.Linkifier._uiLocationSymbol];
138 }
139
140 /**
141 * @param {!Element} anchor
142 * @param {!Workspace.UILocation} uiLocation 134 * @param {!Workspace.UILocation} uiLocation
143 */ 135 */
144 static _bindUILocation(anchor, uiLocation) { 136 static _bindUILocation(anchor, uiLocation) {
145 anchor[Components.Linkifier._uiLocationSymbol] = uiLocation; 137 anchor[Components.Linkifier._uiLocationSymbol] = uiLocation;
146 if (!uiLocation) 138 if (!uiLocation)
147 return; 139 return;
148 var uiSourceCode = uiLocation.uiSourceCode; 140 var uiSourceCode = uiLocation.uiSourceCode;
149 var sourceCodeAnchors = uiSourceCode[Components.Linkifier._sourceCodeAnchors ]; 141 var sourceCodeAnchors = uiSourceCode[Components.Linkifier._sourceCodeAnchors ];
150 if (!sourceCodeAnchors) { 142 if (!sourceCodeAnchors) {
151 sourceCodeAnchors = new Set(); 143 sourceCodeAnchors = new Set();
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 if (!url) { 443 if (!url) {
452 var element = createElementWithClass('span', className); 444 var element = createElementWithClass('span', className);
453 element.textContent = text || Common.UIString('(unknown)'); 445 element.textContent = text || Common.UIString('(unknown)');
454 return element; 446 return element;
455 } 447 }
456 448
457 var linkText = text || Bindings.displayNameForURL(url); 449 var linkText = text || Bindings.displayNameForURL(url);
458 if (typeof lineNumber === 'number' && !text) 450 if (typeof lineNumber === 'number' && !text)
459 linkText += ':' + (lineNumber + 1); 451 linkText += ':' + (lineNumber + 1);
460 452
461 var anchor = createElementWithClass('a', className); 453 var link = createElementWithClass('a', className);
462 if (!url.trim().toLowerCase().startsWith('javascript:')) { 454 if (!url.trim().toLowerCase().startsWith('javascript:')) {
463 anchor.href = url; 455 link.href = url;
464 anchor.classList.add('webkit-html-resource-link'); 456 link.classList.add('webkit-html-resource-link');
457 link[Components.Linkifier._linkSymbol] = true;
458 link.addEventListener('click', Components.Linkifier._handleClick, false);
465 } 459 }
466 anchor.title = linkText !== url ? url : ''; 460 link.title = linkText !== url ? url : '';
467 anchor.textContent = linkText.trimMiddle(150); 461 link.textContent = linkText.trimMiddle(150);
468 anchor.lineNumber = lineNumber; 462 link.lineNumber = lineNumber;
469 anchor.columnNumber = columnNumber; 463 link.columnNumber = columnNumber;
470 return anchor; 464 return link;
465 }
466
467 /**
468 * @param {!Event} event
469 */
470 static _handleClick(event) {
471 var link = /** @type {!Element} */ (event.currentTarget);
472 event.consume(true);
473 if (link.preventFollow || UI.isBeingEdited(/** @type {!Node} */ (event.targe t)))
474 return;
475 if (Components.openAnchorLocationRegistry.dispatch({url: link.href, lineNumb er: link.lineNumber}))
476 return;
477 var actions = Components.Linkifier._linkActions(link);
478 if (actions.length)
479 actions[0].handler();
lushnikov 2016/11/18 23:43:35 nit: consider actions[0].handler.call(null) to avo
dgozman 2016/11/19 02:09:47 Done.
480 }
481
482 /**
483 * @param {?Element} link
484 * @return {?Array<{title: string, handler: function()}>}
lushnikov 2016/11/18 23:43:35 you probably want to return !Array here - otherwis
dgozman 2016/11/19 02:09:47 Nice one! Done.
485 */
486 static _linkActions(link) {
487 var url = null;
488 var uiLocation = null;
489 var isLiveLink = false;
490 if (link && link[Components.Linkifier._uiLocationSymbol]) {
491 uiLocation = /** @type {!Workspace.UILocation} */ (link[Components.Linkifi er._uiLocationSymbol]);
492 url = uiLocation.uiSourceCode.contentURL();
493 isLiveLink = true;
494 } else if (link && link.href) {
495 url = link.href;
496 var uiSourceCode = Workspace.workspace.uiSourceCodeForURL(url);
497 uiLocation = uiSourceCode ? uiSourceCode.uiLocation(link.lineNumber || 0, link.columnNumber || 0) : null;
498 isLiveLink = false;
499 } else {
500 return null;
501 }
502
503 var result = [];
504 if (uiLocation)
505 result.push({title: Common.UIString('Open'), handler: () => Common.Reveale r.reveal(uiLocation)});
506
507 var resource = Bindings.resourceForURL(url);
508 if (resource) {
509 result.push({
510 title: Common.UIString.capitalize('Open ^link in Application ^panel'),
511 handler: () => Common.Revealer.reveal(resource)
512 });
513 }
514
515 var request = SDK.NetworkLog.requestForURL(url);
516 if (request) {
517 result.push({
518 title: Common.UIString.capitalize('Open ^request in Network ^panel'),
519 handler: () => Common.Revealer.reveal(request)
520 });
521 }
522
523 if (resource || !isLiveLink) {
524 result.push({title: UI.openLinkExternallyLabel(), handler: () => Inspector FrontendHost.openInNewTab(url)});
525 result.push({title: UI.copyLinkAddressLabel(), handler: () => InspectorFro ntendHost.copyText(url)});
526 }
527
528 return result;
471 } 529 }
472 }; 530 };
473 531
474 /** @type {!Set<!Components.Linkifier>} */ 532 /** @type {!Set<!Components.Linkifier>} */
475 Components.Linkifier._instances = new Set(); 533 Components.Linkifier._instances = new Set();
476 /** @type {?Components.LinkDecorator} */ 534 /** @type {?Components.LinkDecorator} */
477 Components.Linkifier._decorator = null; 535 Components.Linkifier._decorator = null;
478 536
479 Components.Linkifier._iconSymbol = Symbol('Linkifier.iconSymbol'); 537 Components.Linkifier._iconSymbol = Symbol('Linkifier.iconSymbol');
480 Components.Linkifier._enableDecoratorSymbol = Symbol('Linkifier.enableIconsSymbo l'); 538 Components.Linkifier._enableDecoratorSymbol = Symbol('Linkifier.enableIconsSymbo l');
481 Components.Linkifier._sourceCodeAnchors = Symbol('Linkifier.anchors'); 539 Components.Linkifier._sourceCodeAnchors = Symbol('Linkifier.anchors');
482 Components.Linkifier._uiLocationSymbol = Symbol('uiLocation'); 540 Components.Linkifier._uiLocationSymbol = Symbol('uiLocation');
483 Components.Linkifier._fallbackAnchorSymbol = Symbol('fallbackAnchor'); 541 Components.Linkifier._fallbackAnchorSymbol = Symbol('fallbackAnchor');
484 Components.Linkifier._liveLocationSymbol = Symbol('liveLocation'); 542 Components.Linkifier._liveLocationSymbol = Symbol('liveLocation');
543 Components.Linkifier._linkSymbol = Symbol('Linkifier.link');
485 544
486 /** 545 /**
487 * The maximum number of characters to display in a URL. 546 * The maximum number of characters to display in a URL.
488 * @const 547 * @const
489 * @type {number} 548 * @type {number}
490 */ 549 */
491 Components.Linkifier.MaxLengthForDisplayedURLs = 150; 550 Components.Linkifier.MaxLengthForDisplayedURLs = 150;
492 551
493 /** 552 /**
494 * The maximum length before strings are considered too long for finding URLs. 553 * The maximum length before strings are considered too long for finding URLs.
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
580 * @param {number=} lineNumber 639 * @param {number=} lineNumber
581 * @param {number=} columnNumber 640 * @param {number=} columnNumber
582 * @return {!Node} 641 * @return {!Node}
583 */ 642 */
584 function linkifier(title, url, lineNumber, columnNumber) { 643 function linkifier(title, url, lineNumber, columnNumber) {
585 return Components.Linkifier.linkifyURL(url, title, undefined, lineNumber, co lumnNumber); 644 return Components.Linkifier.linkifyURL(url, title, undefined, lineNumber, co lumnNumber);
586 } 645 }
587 646
588 return Components.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier ); 647 return Components.linkifyStringAsFragmentWithCustomLinkifier(string, linkifier );
589 }; 648 };
649
650 /**
651 * @implements {UI.ContextMenu.Provider}
652 * @unrestricted
653 */
654 Components.Linkifier.LinkContextMenuProvider = class {
655 /**
656 * @override
657 * @param {!Event} event
658 * @param {!UI.ContextMenu} contextMenu
659 * @param {!Object} target
660 */
661 appendApplicableItems(event, contextMenu, target) {
662 var targetNode = /** @type {!Node} */ (target);
663 while (targetNode && !targetNode[Components.Linkifier._linkSymbol] &&
664 !targetNode[Components.Linkifier._uiLocationSymbol])
665 targetNode = targetNode.parentNodeOrShadowHost();
666 var link = /** @type {?Element} */ (targetNode);
667 var actions = Components.Linkifier._linkActions(link);
668 if (!actions)
669 return;
670 for (var action of actions)
671 contextMenu.appendItem(action.title, action.handler);
672 }
673 };
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698