Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed `contentScriptFile` validation, cleaned `contentScript` validation

Added `data` scheme to `contentScriptFile` validation
  • Loading branch information...
commit 6ee14fd3839fff38c86ba77d3a14a232ed84d500 1 parent cb55370
@ZER0 ZER0 authored
View
68 packages/api-utils/lib/content/loader.js
@@ -7,10 +7,15 @@
"use strict";
const { EventEmitter } = require('../events');
-const { validateOptions, getTypeOf } = require('../api-utils');
+const { validateOptions } = require('../api-utils');
const { URL } = require('../url');
const file = require('../file');
+// Returns `null` if `value` is `null` or `undefined`, otherwise `value`.
+function ensureNull(value) {
+ return value == null ? null : value;
+}
+
// map of property validations
const valid = {
contentURL: {
@@ -27,36 +32,38 @@ const valid = {
},
contentScriptFile: {
is: ['undefined', 'null', 'string', 'array'],
- map: function(value) 'undefined' === getTypeOf(value) ? null : value,
+ map: ensureNull,
ok: function(value) {
- if (getTypeOf(value) === 'array') {
- // Make sure every item is a local file URL.
- return value.every(function (item) {
- try {
- return URL(item).scheme === 'resource'
- }
- catch(e) {
- return false;
- }
- });
- }
- return true;
+ if (value === null)
+ return true;
+
+ value = [].concat(value);
+
+ // Make sure every item is a local file URL.
+ return value.every(function (item) {
+ try {
+ return ~['resource', 'data'].indexOf(URL(item).scheme);
+ }
+ catch(e) {
+ return false;
+ }
+ });
+
},
- msg: 'The `contentScriptFile` option must be a local URL or an array of'
- + 'URLs.'
+ msg: 'The `contentScriptFile` option must be a local URL or an array of URLs.'
},
contentScript: {
is: ['undefined', 'null', 'string', 'array'],
- map: function(value) 'undefined' === getTypeOf(value) ? null : value,
- ok: function(value) 'array' !== getTypeOf(value) ? true :
- value.every(function(item) 'string' === getTypeOf(item))
- ,
+ map: ensureNull,
+ ok: function(value) { return !Array.isArray(value) ||
+ value.every(function(item) { return typeof item === 'string' });
+ },
msg: 'The `contentScript` option must be a string or an array of strings.'
},
contentScriptWhen: {
is: ['string'],
- ok: function(value) ['start', 'ready', 'end'].indexOf(value) >= 0,
- map: function(value) {
+ ok: function(value) { return ~['start', 'ready', 'end'].indexOf(value) },
+ map: function(value) {
return value || 'end';
},
msg: 'The `contentScriptWhen` option must be either "start", "ready" or "end".'
@@ -113,10 +120,10 @@ const Loader = EventEmitter.compose({
_contentURL: null,
/**
* When to load the content scripts.
- * Possible values are "end" (default), which loads them once all page
- * contents have been loaded, "ready", which loads them once DOM nodes are
- * ready (ie like DOMContentLoaded event), and "start", which loads them once
- * the `window` object for the page has been created, but before any scripts
+ * Possible values are "end" (default), which loads them once all page
+ * contents have been loaded, "ready", which loads them once DOM nodes are
+ * ready (ie like DOMContentLoaded event), and "start", which loads them once
+ * the `window` object for the page has been created, but before any scripts
* specified by the page have been loaded.
* Property change emits `propertyChange` event on instance with this key
* and new value.
@@ -126,8 +133,8 @@ const Loader = EventEmitter.compose({
set contentScriptWhen(value) {
value = validate(value, valid.contentScriptWhen);
if (value !== this._contentScriptWhen) {
- this._emit('propertyChange', {
- contentScriptWhen: this._contentScriptWhen = value
+ this._emit('propertyChange', {
+ contentScriptWhen: this._contentScriptWhen = value
});
}
},
@@ -142,7 +149,7 @@ const Loader = EventEmitter.compose({
set contentScriptFile(value) {
value = validate(value, valid.contentScriptFile);
if (value != this._contentScriptFile) {
- this._emit('propertyChange', {
+ this._emit('propertyChange', {
contentScriptFile: this._contentScriptFile = value
});
}
@@ -159,11 +166,10 @@ const Loader = EventEmitter.compose({
value = validate(value, valid.contentScript);
if (value != this._contentScript) {
this._emit('propertyChange', {
- contentScript: this._contentScript = value
+ contentScript: this._contentScript = value
});
}
},
_contentScript: null
});
exports.Loader = Loader;
-
View
9 packages/api-utils/tests/test-content-loader.js
@@ -85,7 +85,7 @@ exports['test:contentURL'] = function(test) {
emitted,
'must not emit `propertyChange` if same value is set'
);
-
+
loader.removeListener('propertyChange', listener);
loader.contentURL = value = 'about:blank';
test.assertEqual(
@@ -198,8 +198,7 @@ exports['test:contentScriptFile'] = function(test) {
test.fail('must throw when wrong value is set');
} catch(e) {
test.assertEqual(
- 'The `contentScriptFile` option must be a local URL or an array of'
- + 'URLs.',
+ 'The `contentScriptFile` option must be a local URL or an array of URLs.',
e.message
);
}
@@ -209,8 +208,7 @@ exports['test:contentScriptFile'] = function(test) {
test.fail('must throw when wrong value is set');
} catch(e) {
test.assertEqual(
- 'The `contentScriptFile` option must be a local URL or an array of'
- + 'URLs.',
+ 'The `contentScriptFile` option must be a local URL or an array of URLs.',
e.message
);
}
@@ -226,4 +224,3 @@ exports['test:contentScriptFile'] = function(test) {
loader.contentScriptFile
);
};
-
Please sign in to comment.
Something went wrong with that request. Please try again.