Use imported SafeTypes
This change removes usages of global Gerrit.SafeTypes replaces them with direct import. Change-Id: Ida8233178bf3bd50948986a14518adb5f30ae78f
This commit is contained in:
@@ -14,16 +14,12 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
(function(window) {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
window.Gerrit = window.Gerrit || {};
|
|
||||||
|
|
||||||
/** @polymerBehavior Gerrit.SafeTypes */
|
|
||||||
Gerrit.SafeTypes = {};
|
|
||||||
|
|
||||||
const SAFE_URL_PATTERN = /^(https?:\/\/|mailto:|[^:/?#]*(?:[/?#]|$))/i;
|
const SAFE_URL_PATTERN = /^(https?:\/\/|mailto:|[^:/?#]*(?:[/?#]|$))/i;
|
||||||
|
|
||||||
|
/** @polymerBehavior Gerrit.SafeTypes */
|
||||||
|
export const SafeTypes = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps a string to be used as a URL. An error is thrown if the string cannot
|
* Wraps a string to be used as a URL. An error is thrown if the string cannot
|
||||||
* be considered safe.
|
* be considered safe.
|
||||||
@@ -31,7 +27,7 @@
|
|||||||
* @constructor
|
* @constructor
|
||||||
* @param {string} url the unwrapped, potentially unsafe URL.
|
* @param {string} url the unwrapped, potentially unsafe URL.
|
||||||
*/
|
*/
|
||||||
Gerrit.SafeTypes.SafeUrl = function(url) {
|
SafeTypes.SafeUrl = function(url) {
|
||||||
if (!SAFE_URL_PATTERN.test(url)) {
|
if (!SAFE_URL_PATTERN.test(url)) {
|
||||||
throw new Error(`URL not marked as safe: ${url}`);
|
throw new Error(`URL not marked as safe: ${url}`);
|
||||||
}
|
}
|
||||||
@@ -43,20 +39,20 @@
|
|||||||
*
|
*
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
Gerrit.SafeTypes.SafeUrl.prototype.asString = function() {
|
SafeTypes.SafeUrl.prototype.asString = function() {
|
||||||
return this._url;
|
return this._url;
|
||||||
};
|
};
|
||||||
|
|
||||||
Gerrit.SafeTypes.safeTypesBridge = function(value, type) {
|
SafeTypes.safeTypesBridge = function(value, type) {
|
||||||
// If the value is being bound to a URL, ensure the value is wrapped in the
|
// 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
|
// SafeUrl type first. If the URL is not safe, allow the SafeUrl constructor
|
||||||
// to surface the error.
|
// to surface the error.
|
||||||
if (type === 'URL') {
|
if (type === 'URL') {
|
||||||
let safeValue = null;
|
let safeValue = null;
|
||||||
if (value instanceof Gerrit.SafeTypes.SafeUrl) {
|
if (value instanceof SafeTypes.SafeUrl) {
|
||||||
safeValue = value;
|
safeValue = value;
|
||||||
} else if (typeof value === 'string') {
|
} else if (typeof value === 'string') {
|
||||||
safeValue = new Gerrit.SafeTypes.SafeUrl(value);
|
safeValue = new SafeTypes.SafeUrl(value);
|
||||||
}
|
}
|
||||||
if (safeValue) {
|
if (safeValue) {
|
||||||
return safeValue.asString();
|
return safeValue.asString();
|
||||||
@@ -72,4 +68,11 @@
|
|||||||
// Otherwise fail.
|
// Otherwise fail.
|
||||||
throw new Error(`Refused to bind value as ${type}: ${value}`);
|
throw new Error(`Refused to bind value as ${type}: ${value}`);
|
||||||
};
|
};
|
||||||
})(window);
|
|
||||||
|
// 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;
|
||||||
|
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ limitations under the License.
|
|||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import '../../test/common-test-setup.js';
|
import '../../test/common-test-setup.js';
|
||||||
import './safe-types-behavior.js';
|
|
||||||
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
|
import {Polymer} from '@polymer/polymer/lib/legacy/polymer-fn.js';
|
||||||
|
import {SafeTypes} from './safe-types-behavior.js';
|
||||||
suite('gr-tooltip-behavior tests', () => {
|
suite('gr-tooltip-behavior tests', () => {
|
||||||
let element;
|
let element;
|
||||||
let sandbox;
|
let sandbox;
|
||||||
@@ -40,7 +40,7 @@ suite('gr-tooltip-behavior tests', () => {
|
|||||||
suiteSetup(() => {
|
suiteSetup(() => {
|
||||||
Polymer({
|
Polymer({
|
||||||
is: 'safe-types-element',
|
is: 'safe-types-element',
|
||||||
behaviors: [Gerrit.SafeTypes],
|
behaviors: [SafeTypes],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,12 +79,12 @@ suite('gr-tooltip-behavior tests', () => {
|
|||||||
|
|
||||||
suite('safeTypesBridge', () => {
|
suite('safeTypesBridge', () => {
|
||||||
function acceptsString(value, type) {
|
function acceptsString(value, type) {
|
||||||
assert.equal(Gerrit.SafeTypes.safeTypesBridge(value, type),
|
assert.equal(SafeTypes.safeTypesBridge(value, type),
|
||||||
value);
|
value);
|
||||||
}
|
}
|
||||||
|
|
||||||
function rejects(value, type) {
|
function rejects(value, type) {
|
||||||
assert.throws(() => { Gerrit.SafeTypes.safeTypesBridge(value, type); });
|
assert.throws(() => { SafeTypes.safeTypesBridge(value, type); });
|
||||||
}
|
}
|
||||||
|
|
||||||
test('accepts valid URL strings', () => {
|
test('accepts valid URL strings', () => {
|
||||||
@@ -99,7 +99,7 @@ suite('gr-tooltip-behavior tests', () => {
|
|||||||
test('accepts SafeUrl values', () => {
|
test('accepts SafeUrl values', () => {
|
||||||
const url = '/abc/123';
|
const url = '/abc/123';
|
||||||
const safeUrl = new element.SafeUrl(url);
|
const safeUrl = new element.SafeUrl(url);
|
||||||
assert.equal(Gerrit.SafeTypes.safeTypesBridge(safeUrl, 'URL'), url);
|
assert.equal(SafeTypes.safeTypesBridge(safeUrl, 'URL'), url);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('rejects non-string or non-SafeUrl types', () => {
|
test('rejects non-string or non-SafeUrl types', () => {
|
||||||
|
|||||||
@@ -25,18 +25,18 @@ import './gr-app-init.js';
|
|||||||
import './font-roboto-local-loader.js';
|
import './font-roboto-local-loader.js';
|
||||||
import '../scripts/bundled-polymer.js';
|
import '../scripts/bundled-polymer.js';
|
||||||
import 'polymer-resin/standalone/polymer-resin.js';
|
import 'polymer-resin/standalone/polymer-resin.js';
|
||||||
import '../behaviors/safe-types-behavior/safe-types-behavior.js';
|
|
||||||
import './gr-app-element.js';
|
import './gr-app-element.js';
|
||||||
import './change-list/gr-embed-dashboard/gr-embed-dashboard.js';
|
import './change-list/gr-embed-dashboard/gr-embed-dashboard.js';
|
||||||
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
|
import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
|
||||||
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
|
import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
|
||||||
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
|
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
|
||||||
import {htmlTemplate} from './gr-app_html.js';
|
import {htmlTemplate} from './gr-app_html.js';
|
||||||
|
import {SafeTypes} from '../behaviors/safe-types-behavior/safe-types-behavior.js';
|
||||||
|
|
||||||
security.polymer_resin.install({
|
security.polymer_resin.install({
|
||||||
allowedIdentifierPrefixes: [''],
|
allowedIdentifierPrefixes: [''],
|
||||||
reportHandler: security.polymer_resin.CONSOLE_LOGGING_REPORT_HANDLER,
|
reportHandler: security.polymer_resin.CONSOLE_LOGGING_REPORT_HANDLER,
|
||||||
safeTypesBridge: Gerrit.SafeTypes.safeTypesBridge,
|
safeTypesBridge: SafeTypes.safeTypesBridge,
|
||||||
});
|
});
|
||||||
|
|
||||||
/** @extends Polymer.Element */
|
/** @extends Polymer.Element */
|
||||||
|
|||||||
@@ -17,10 +17,11 @@
|
|||||||
import '../scripts/bundled-polymer.js';
|
import '../scripts/bundled-polymer.js';
|
||||||
|
|
||||||
import 'polymer-resin/standalone/polymer-resin.js';
|
import 'polymer-resin/standalone/polymer-resin.js';
|
||||||
import '../behaviors/safe-types-behavior/safe-types-behavior.js';
|
|
||||||
import '@polymer/iron-test-helpers/iron-test-helpers.js';
|
import '@polymer/iron-test-helpers/iron-test-helpers.js';
|
||||||
import './test-router.js';
|
import './test-router.js';
|
||||||
import moment from 'moment/src/moment.js';
|
import moment from 'moment/src/moment.js';
|
||||||
|
import {SafeTypes} from '../behaviors/safe-types-behavior/safe-types-behavior.js';
|
||||||
|
|
||||||
self.moment = moment;
|
self.moment = moment;
|
||||||
security.polymer_resin.install({
|
security.polymer_resin.install({
|
||||||
allowedIdentifierPrefixes: [''],
|
allowedIdentifierPrefixes: [''],
|
||||||
@@ -35,7 +36,7 @@ security.polymer_resin.install({
|
|||||||
JSON.stringify(args));
|
JSON.stringify(args));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
safeTypesBridge: Gerrit.SafeTypes.safeTypesBridge,
|
safeTypesBridge: SafeTypes.safeTypesBridge,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Default implementations of 'fixture' and 'stub' methods in
|
// Default implementations of 'fixture' and 'stub' methods in
|
||||||
|
|||||||
Reference in New Issue
Block a user