From ef366ab34fe21a594199ddefc85eef1acf7ca422 Mon Sep 17 00:00:00 2001 From: Dmitrii Filippov Date: Fri, 3 Apr 2020 18:23:05 +0200 Subject: [PATCH] Use imported SafeTypes This change removes usages of global Gerrit.SafeTypes replaces them with direct import. Change-Id: Ida8233178bf3bd50948986a14518adb5f30ae78f --- .../safe-types-behavior.js | 105 +++++++++--------- .../safe-types-behavior_test.html | 10 +- polygerrit-ui/app/elements/gr-app.js | 4 +- polygerrit-ui/app/test/common-test-setup.js | 5 +- 4 files changed, 64 insertions(+), 60 deletions(-) diff --git a/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior.js b/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior.js index 23f2290ac5..ec7a9f4317 100644 --- a/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior.js +++ b/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior.js @@ -14,62 +14,65 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -(function(window) { - 'use strict'; - window.Gerrit = window.Gerrit || {}; +const SAFE_URL_PATTERN = /^(https?:\/\/|mailto:|[^:/?#]*(?:[/?#]|$))/i; - /** @polymerBehavior Gerrit.SafeTypes */ - Gerrit.SafeTypes = {}; +/** @polymerBehavior Gerrit.SafeTypes */ +export const SafeTypes = {}; - const SAFE_URL_PATTERN = /^(https?:\/\/|mailto:|[^:/?#]*(?:[/?#]|$))/i; +/** + * Wraps a string to be used as a URL. An error is thrown if the string cannot + * be considered safe. + * + * @constructor + * @param {string} url the unwrapped, potentially unsafe URL. + */ +SafeTypes.SafeUrl = function(url) { + if (!SAFE_URL_PATTERN.test(url)) { + throw new Error(`URL not marked as safe: ${url}`); + } + this._url = url; +}; - /** - * Wraps a string to be used as a URL. An error is thrown if the string cannot - * be considered safe. - * - * @constructor - * @param {string} url the unwrapped, potentially unsafe URL. - */ - Gerrit.SafeTypes.SafeUrl = function(url) { - if (!SAFE_URL_PATTERN.test(url)) { - throw new Error(`URL not marked as safe: ${url}`); +/** + * Get the string representation of the safe URL. + * + * @returns {string} + */ +SafeTypes.SafeUrl.prototype.asString = function() { + return this._url; +}; + +SafeTypes.safeTypesBridge = function(value, type) { + // 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 + // to surface the error. + if (type === 'URL') { + let safeValue = null; + if (value instanceof SafeTypes.SafeUrl) { + safeValue = value; + } else if (typeof value === 'string') { + safeValue = new SafeTypes.SafeUrl(value); } - this._url = url; - }; - - /** - * Get the string representation of the safe URL. - * - * @returns {string} - */ - Gerrit.SafeTypes.SafeUrl.prototype.asString = function() { - return this._url; - }; - - Gerrit.SafeTypes.safeTypesBridge = function(value, type) { - // 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 - // to surface the error. - if (type === 'URL') { - let safeValue = null; - if (value instanceof Gerrit.SafeTypes.SafeUrl) { - safeValue = value; - } else if (typeof value === 'string') { - safeValue = new Gerrit.SafeTypes.SafeUrl(value); - } - if (safeValue) { - return safeValue.asString(); - } + if (safeValue) { + return safeValue.asString(); } + } - // If the value is being bound to a string or a constant, then the string - // can be used as is. - if (type === 'STRING' || type === 'CONSTANT') { - return value; - } + // If the value is being bound to a string or a constant, then the string + // can be used as is. + if (type === 'STRING' || type === 'CONSTANT') { + return value; + } + + // Otherwise fail. + throw new Error(`Refused to bind value as ${type}: ${value}`); +}; + +// TODO(dmfilippov) Remove the following lines with assignments +// Plugins can use the behavior because it was accessible with +// the global Gerrit... variable. To avoid breaking changes in plugins +// temporary assign global variables. +window.Gerrit = window.Gerrit || {}; +window.Gerrit.SafeTypes = SafeTypes; - // Otherwise fail. - throw new Error(`Refused to bind value as ${type}: ${value}`); - }; -})(window); diff --git a/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior_test.html b/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior_test.html index 791dc4d28a..6fe44609e7 100644 --- a/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior_test.html +++ b/polygerrit-ui/app/behaviors/safe-types-behavior/safe-types-behavior_test.html @@ -31,8 +31,8 @@ limitations under the License.