| * Copyright 2018 Google LLC |
| * SPDX-License-Identifier: Apache-2.0 |
| const SAFE_URL_PATTERN = /^(https?:\/\/|mailto:|[^:/?#]*(?:[/?#]|$))/i; |
| * Wraps a string to be used as a URL. An error is thrown if the string cannot |
| private readonly _url: string; |
| constructor(url: string) { |
| if (!SAFE_URL_PATTERN.test(url)) { |
| throw new Error(`URL not marked as safe: ${url}`); |
| export const _testOnly_SafeUrl = SafeUrl; |
| * Get the string representation of the safe URL. |
| export function safeTypesBridge(value: unknown, type: string): unknown { |
| // If the value is being bound to a URL, ensure the value is wrapped in the |
| // SafeUrl type first. If the URL is not safe, allow the SafeUrl constructor |
| if (value instanceof SafeUrl) { |
| } else if (typeof value === 'string') { |
| safeValue = new SafeUrl(value); |
| return safeValue.toString(); |
| // If the value is being bound to a string or a constant, then the string |
| if (type === 'STRING' || type === 'CONSTANT') { |
| throw new Error(`Refused to bind value as ${type}: ${value}`); |