link.mjs 1021 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * @license
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import { unwrapUrlOrSanitize } from '../../builders/url_sanitizer';
  6. import { TrustedResourceUrl, unwrapResourceUrl } from '../../internals/resource_url_impl';
  7. const SAFE_URL_REL_VALUES = [
  8. 'alternate',
  9. 'author',
  10. 'bookmark',
  11. 'canonical',
  12. 'cite',
  13. 'help',
  14. 'icon',
  15. 'license',
  16. 'next',
  17. 'prefetch',
  18. 'dns-prefetch',
  19. 'prerender',
  20. 'preconnect',
  21. 'preload',
  22. 'prev',
  23. 'search',
  24. 'subresource',
  25. ];
  26. export function setHrefAndRel(link, url, rel) {
  27. if (url instanceof TrustedResourceUrl) {
  28. link.href = unwrapResourceUrl(url).toString();
  29. }
  30. else {
  31. if (SAFE_URL_REL_VALUES.indexOf(rel) === -1) {
  32. throw new Error(`TrustedResourceUrl href attribute required with rel="${rel}"`);
  33. }
  34. const sanitizedUrl = unwrapUrlOrSanitize(url);
  35. if (sanitizedUrl === undefined) {
  36. return;
  37. }
  38. link.href = sanitizedUrl;
  39. }
  40. link.rel = rel;
  41. }