Debian Codegraph @

closure-library/closure/goog/dom/uri.js

raw
1 // Copyright 2017 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.module('goog.dom.uri');
16
17 const Const = goog.require('goog.string.Const');
18 const TagName = goog.require('goog.dom.TagName');
19 const uncheckedconversions = goog.require('goog.html.uncheckedconversions');
20 const {createElement} = goog.require('goog.dom');
21 const {setAnchorHref} = goog.require('goog.dom.safe');
22
23 /**
24 * Normalizes a URL by assigning it to an anchor element and reading back href.
25 *
26 * This converts relative URLs to absolute, and cleans up whitespace.
27 * @param {string} uri A string containing a URI.
28 * @return {string} Normalized, absolute form of uri.
29 */
30 function normalizeUri(uri) {
31 const anchor = createElement(TagName.A);
32 // This is safe even though the URL might be untrustworthy.
33 // The SafeURL is only used to set the href of an HTMLAnchorElement
34 // that is never added to the DOM. Therefore, the user cannot navigate
35 // to this URL.
36 const safeUrl =
37 uncheckedconversions.safeUrlFromStringKnownToSatisfyTypeContract(
38 Const.from('This URL is never added to the DOM'), uri);
39 setAnchorHref(anchor, safeUrl);
40 return anchor.href;
41 }
42 exports.normalizeUri = normalizeUri;