Skip to content

Commit b1dfacb

Browse files
refactor(utils): extract isExternalUrl helper from cc-link
Move the URL-origin check out of cc-link as a private method into a shared utility, so other components (cc-search-bar) can reuse it.
1 parent f8c9d69 commit b1dfacb

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

src/components/cc-link/cc-link.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { css, html, LitElement } from 'lit';
22
import { classMap } from 'lit/directives/class-map.js';
33
import { ifDefined } from 'lit/directives/if-defined.js';
44
import { iconRemixExternalLinkLine as externalLinkIcon, iconRemixDownloadLine } from '../../assets/cc-remix.icons.js';
5-
import { isStringEmpty } from '../../lib/utils.js';
5+
import { isExternalUrl, isStringEmpty } from '../../lib/utils.js';
66
import { skeletonStyles } from '../../styles/skeleton.js';
77
import { i18n } from '../../translations/translation.js';
88
import '../cc-icon/cc-icon.js';
@@ -83,24 +83,6 @@ export class CcLink extends LitElement {
8383
this._title = '';
8484
}
8585

86-
/**
87-
* Checks if a given URL points to a different origin than the current page.
88-
* If the URL is invalid, it is considered as a different origin for security.
89-
*
90-
* @param {string} rawUrl - The URL to check.
91-
* @returns {boolean} True if the URL points to a different origin, false otherwise.
92-
* @private
93-
*/
94-
_isDifferentOrigin(rawUrl) {
95-
try {
96-
const url = new URL(rawUrl, location.href);
97-
return url.origin !== location.origin;
98-
} catch {
99-
// Consider bad URLs as different origin
100-
return true;
101-
}
102-
}
103-
10486
/**
10587
* Determines the appropriate title for the link
10688
*
@@ -140,7 +122,7 @@ export class CcLink extends LitElement {
140122

141123
render() {
142124
const href = this.href != null && !this.skeleton ? this.href : null;
143-
const isDifferentOrigin = this._isDifferentOrigin(href);
125+
const isDifferentOrigin = isExternalUrl(href);
144126
const target = isDifferentOrigin ? '_blank' : null;
145127
const rel = isDifferentOrigin ? 'noreferrer' : null;
146128
const disableExternalIcon = this.disableExternalLinkIcon || this.mode !== 'default';

src/lib/utils.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,22 @@ export function clampNumber(number, min, max) {
255255
return Math.min(Math.max(number, min ?? -Infinity), max ?? Infinity);
256256
}
257257

258+
/**
259+
* Checks if the given URL points to a different origin than the current page.
260+
* Invalid URLs are considered external for security.
261+
*
262+
* @param {string} rawUrl
263+
* @return {boolean}
264+
*/
265+
export function isExternalUrl(rawUrl) {
266+
try {
267+
const url = new URL(rawUrl, location.href);
268+
return url.origin !== location.origin;
269+
} catch {
270+
return true;
271+
}
272+
}
273+
258274
/**
259275
* @param {string} string
260276
* @return {boolean}

0 commit comments

Comments
 (0)