location.mjs 963 B

1234567891011121314151617181920212223242526272829303132333435
  1. /**
  2. * @license
  3. * SPDX-License-Identifier: Apache-2.0
  4. */
  5. import { unwrapUrlOrSanitize } from '../../builders/url_sanitizer';
  6. /**
  7. * setHref safely sets {@link Location.href} on the given {@link Location} with
  8. * given {@link Url}.
  9. */
  10. export function setHref(loc, url) {
  11. const sanitizedUrl = unwrapUrlOrSanitize(url);
  12. if (sanitizedUrl !== undefined) {
  13. loc.href = sanitizedUrl;
  14. }
  15. }
  16. /**
  17. * replace safely calls {@link Location.replace} on the given {@link Location}
  18. * with given {@link Url}.
  19. */
  20. export function replace(loc, url) {
  21. const sanitizedUrl = unwrapUrlOrSanitize(url);
  22. if (sanitizedUrl !== undefined) {
  23. loc.replace(sanitizedUrl);
  24. }
  25. }
  26. /**
  27. * assign safely calls {@link Location.assign} on the given {@link Location}
  28. * with given {@link Url}.
  29. */
  30. export function assign(loc, url) {
  31. const sanitizedUrl = unwrapUrlOrSanitize(url);
  32. if (sanitizedUrl !== undefined) {
  33. loc.assign(sanitizedUrl);
  34. }
  35. }