2015-05-20 17:58:00 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2015-05-27 00:04:59 +03:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
2015-05-20 17:58:00 +03:00
|
|
|
/*
|
|
|
|
* Helper functions extract values from manifest members
|
2019-07-09 05:33:39 +03:00
|
|
|
* and reports conformance errors.
|
2015-05-20 17:58:00 +03:00
|
|
|
*/
|
2018-12-18 23:38:43 +03:00
|
|
|
"use strict";
|
2015-05-20 17:58:00 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
2018-05-26 03:02:29 +03:00
|
|
|
|
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["InspectorUtils"]);
|
2018-01-11 07:38:00 +03:00
|
|
|
|
2019-07-09 05:33:39 +03:00
|
|
|
function ValueExtractor(errors, aBundle) {
|
|
|
|
this.errors = errors;
|
2016-02-03 03:47:51 +03:00
|
|
|
this.domBundle = aBundle;
|
2015-05-20 17:58:00 +03:00
|
|
|
}
|
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
ValueExtractor.prototype = {
|
2015-05-20 17:58:00 +03:00
|
|
|
// This function takes a 'spec' object and destructures
|
|
|
|
// it to extract a value. If the value is of th wrong type, it
|
|
|
|
// warns the developer and returns undefined.
|
|
|
|
// expectType: is the type of a JS primitive (string, number, etc.)
|
|
|
|
// object: is the object from which to extract the value.
|
|
|
|
// objectName: string used to construct the developer warning.
|
|
|
|
// property: the name of the property being extracted.
|
|
|
|
// trim: boolean, if the value should be trimmed (used by string type).
|
2019-08-15 15:55:35 +03:00
|
|
|
// throwTypeError: boolean, throw a TypeError if the type is incorrect.
|
|
|
|
extractValue(options) {
|
|
|
|
const {
|
|
|
|
expectedType,
|
|
|
|
object,
|
|
|
|
objectName,
|
|
|
|
property,
|
|
|
|
throwTypeError,
|
|
|
|
trim,
|
|
|
|
} = options;
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = object[property];
|
|
|
|
const isArray = Array.isArray(value);
|
|
|
|
// We need to special-case "array", as it's not a JS primitive.
|
2018-12-18 23:38:43 +03:00
|
|
|
const type = isArray ? "array" : typeof value;
|
2015-05-20 17:58:00 +03:00
|
|
|
if (type !== expectedType) {
|
2018-12-18 23:38:43 +03:00
|
|
|
if (type !== "undefined") {
|
2019-07-09 05:33:39 +03:00
|
|
|
const warn = this.domBundle.formatStringFromName(
|
|
|
|
"ManifestInvalidType",
|
|
|
|
[objectName, property, expectedType]
|
2019-06-11 18:51:51 +03:00
|
|
|
);
|
2019-07-09 05:33:39 +03:00
|
|
|
this.errors.push({ warn });
|
2019-08-15 15:55:35 +03:00
|
|
|
if (throwTypeError) {
|
|
|
|
throw new TypeError(warn);
|
|
|
|
}
|
2015-05-20 17:58:00 +03:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
// Trim string and returned undefined if the empty string.
|
2018-12-18 23:38:43 +03:00
|
|
|
const shouldTrim = expectedType === "string" && value && trim;
|
2015-05-20 17:58:00 +03:00
|
|
|
if (shouldTrim) {
|
|
|
|
return value.trim() || undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
extractColorValue(spec) {
|
|
|
|
const value = this.extractValue(spec);
|
|
|
|
let color;
|
2018-01-11 07:38:00 +03:00
|
|
|
if (InspectorUtils.isValidCSSColor(value)) {
|
2019-03-20 17:44:29 +03:00
|
|
|
const rgba = InspectorUtils.colorToRGBA(value);
|
|
|
|
color = "#" + ((rgba.r << 16) | (rgba.g << 8) | rgba.b).toString(16);
|
2015-05-27 00:04:59 +03:00
|
|
|
} else if (value) {
|
2019-07-09 05:33:39 +03:00
|
|
|
const warn = this.domBundle.formatStringFromName(
|
|
|
|
"ManifestInvalidCSSColor",
|
|
|
|
[spec.property, value]
|
2019-06-11 18:51:51 +03:00
|
|
|
);
|
2019-07-09 05:33:39 +03:00
|
|
|
this.errors.push({ warn });
|
2015-05-20 17:58:00 +03:00
|
|
|
}
|
|
|
|
return color;
|
2018-12-18 23:38:43 +03:00
|
|
|
},
|
2019-04-11 04:35:21 +03:00
|
|
|
extractLanguageValue(spec) {
|
|
|
|
let langTag;
|
|
|
|
const value = this.extractValue(spec);
|
|
|
|
if (value !== undefined) {
|
|
|
|
try {
|
|
|
|
langTag = Intl.getCanonicalLocales(value)[0];
|
|
|
|
} catch (err) {
|
2019-07-09 05:33:39 +03:00
|
|
|
const warn = this.domBundle.formatStringFromName(
|
|
|
|
"ManifestLangIsInvalid",
|
|
|
|
[spec.property, value]
|
2019-06-11 18:51:51 +03:00
|
|
|
);
|
2019-07-09 05:33:39 +03:00
|
|
|
this.errors.push({ warn });
|
2019-04-11 04:35:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return langTag;
|
|
|
|
},
|
2015-05-20 17:58:00 +03:00
|
|
|
};
|
2018-12-18 23:38:43 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ValueExtractor"]; // jshint ignore:line
|