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

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

Issue 2644753002: DevTools: untruncate links on copy (Closed)
Patch Set: ac Created 3 years, 8 months 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/front_end/components/Linkifier.js
diff --git a/third_party/WebKit/Source/devtools/front_end/components/Linkifier.js b/third_party/WebKit/Source/devtools/front_end/components/Linkifier.js
index 6184cc95d5d4eb1254e5cddb10c6985f96a1c68c..ff079047175022dfd0c4ee9040454ea792be76dc 100644
--- a/third_party/WebKit/Source/devtools/front_end/components/Linkifier.js
+++ b/third_party/WebKit/Source/devtools/front_end/components/Linkifier.js
@@ -303,13 +303,8 @@ Components.Linkifier = class {
return;
Components.Linkifier._bindUILocation(anchor, uiLocation);
- var text = uiLocation.linkText();
- var info = Components.Linkifier._linkInfo(anchor);
- info.originalLinkText = text;
- text = text.replace(/([a-f0-9]{7})[a-f0-9]{13}[a-f0-9]*/g, '$1\u2026');
- if (this._maxLength)
- text = text.trimMiddle(this._maxLength);
- anchor.textContent = text;
+ var text = uiLocation.linkText(true /* skipTrim */);
+ Components.Linkifier._setTrimmedText(anchor, text, this._maxLength);
var titleText = uiLocation.uiSourceCode.url();
if (typeof uiLocation.lineNumber === 'number')
@@ -396,9 +391,7 @@ Components.Linkifier = class {
link.title = title;
if (href)
link.href = href;
- link.textContent = text;
- if (maxLength)
- link.textContent = link.textContent.trimMiddle(maxLength);
+ Components.Linkifier._setTrimmedText(link, text, maxLength);
link[Components.Linkifier._infoSymbol] = {
icon: null,
enableDecorator: false,
@@ -408,8 +401,7 @@ Components.Linkifier = class {
lineNumber: null,
columnNumber: null,
revealable: null,
- fallback: null,
- originalLinkText: text
+ fallback: null
};
if (!preventClick)
link.addEventListener('click', Components.Linkifier._handleClick, false);
@@ -419,12 +411,70 @@ Components.Linkifier = class {
}
/**
- * @param {?Element} link
- * @return {?string}
+ * @param {!Element} link
+ * @param {string} text
+ * @param {number=} maxLength
+ */
+ static _setTrimmedText(link, text, maxLength) {
+ link.removeChildren();
+ if (maxLength && text.length > maxLength) {
+ var middleSplit = splitMiddle(text, maxLength);
+ appendTextWithoutHashes(middleSplit[0]);
+ appendHiddenText(middleSplit[1]);
+ appendTextWithoutHashes(middleSplit[2]);
+ } else {
+ appendTextWithoutHashes(text);
+ }
+
+ /**
+ * @param {string} string
+ */
+ function appendHiddenText(string) {
+ var ellipsisNode = link.createChild('span', 'devtools-link-ellipsis').createTextChild('\u2026');
+ ellipsisNode[Components.Linkifier._untruncatedNodeTextSymbol] = string;
+ }
+
+ /**
+ * @param {string} string
+ */
+ function appendTextWithoutHashes(string) {
+ var hashSplit = TextUtils.TextUtils.splitStringByRegexes(string, [/[a-f0-9]{20,}/g]);
+ for (var match of hashSplit) {
+ if (match.regexIndex === -1) {
+ link.createTextChild(match.value);
+ } else {
+ link.createTextChild(match.value.substring(0, 7));
+ appendHiddenText(match.value.substring(7));
+ }
+ }
+ }
+
+ /**
+ * @param {string} string
+ * @param {number} maxLength
+ * @return {!Array<string>}
+ */
+ function splitMiddle(string, maxLength) {
+ var leftIndex = Math.floor(maxLength / 2);
+ var rightIndex = string.length - Math.ceil(maxLength / 2) + 1;
+
+ // Do not truncate between characters that use multiple code points (emojis).
+ if (string.codePointAt(rightIndex - 1) >= 0x10000) {
+ rightIndex++;
+ leftIndex++;
+ }
+ if (leftIndex > 0 && string.codePointAt(leftIndex - 1) >= 0x10000)
+ leftIndex--;
+ return [string.substring(0, leftIndex), string.substring(leftIndex, rightIndex), string.substring(rightIndex)];
+ }
+ }
+
+ /**
+ * @param {!Node} node
+ * @return {string}
*/
- static originalLinkText(link) {
- var info = this._linkInfo(link);
- return info ? info.originalLinkText : null;
+ static untruncatedNodeText(node) {
+ return node[Components.Linkifier._untruncatedNodeTextSymbol] || node.textContent;
}
/**
@@ -549,6 +599,7 @@ Components.Linkifier._decorator = null;
Components.Linkifier._sourceCodeAnchors = Symbol('Linkifier.anchors');
Components.Linkifier._infoSymbol = Symbol('Linkifier.info');
+Components.Linkifier._untruncatedNodeTextSymbol = Symbol('Linkifier.untruncatedNodeText');
/**
* @typedef {{
@@ -560,8 +611,7 @@ Components.Linkifier._infoSymbol = Symbol('Linkifier.info');
* lineNumber: ?number,
* columnNumber: ?number,
* revealable: ?Object,
- * fallback: ?Element,
- * originalLinkText: string
+ * fallback: ?Element
* }}
*/
Components._LinkInfo;

Powered by Google App Engine
This is Rietveld 408576698