2015-05-19 15:20: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-19 15:20:00 +03:00
|
|
|
/*
|
2015-05-27 00:04:59 +03:00
|
|
|
* ImageObjectProcessor
|
2015-05-19 15:20:00 +03:00
|
|
|
* Implementation of Image Object processing algorithms from:
|
2015-05-27 00:04:59 +03:00
|
|
|
* http://www.w3.org/TR/appmanifest/#image-object-and-its-members
|
2015-05-19 15:20:00 +03:00
|
|
|
*
|
2016-02-03 03:47:51 +03:00
|
|
|
* This is intended to be used in conjunction with ManifestProcessor.jsm
|
2015-05-19 15:20:00 +03:00
|
|
|
*
|
|
|
|
* Creates an object to process Image Objects as defined by the
|
|
|
|
* W3C specification. This is used to process things like the
|
|
|
|
* icon member and the splash_screen member.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
*
|
2015-05-20 17:58:00 +03:00
|
|
|
* .process(aManifest, aBaseURL, aMemberName);
|
2015-05-19 15:20:00 +03:00
|
|
|
*
|
|
|
|
*/
|
2018-12-18 23:38:43 +03:00
|
|
|
/* exported EXPORTED_SYMBOLS*/
|
|
|
|
/* globals Components */
|
|
|
|
"use strict";
|
2015-05-27 00:04:59 +03:00
|
|
|
|
2019-01-17 21:18:31 +03:00
|
|
|
const { XPCOMUtils } = ChromeUtils.import(
|
|
|
|
"resource://gre/modules/XPCOMUtils.jsm"
|
|
|
|
);
|
|
|
|
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
|
2018-05-26 03:02:29 +03:00
|
|
|
|
2018-12-18 23:38:43 +03:00
|
|
|
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
|
2015-05-19 15:20:00 +03:00
|
|
|
|
2019-07-09 05:33:39 +03:00
|
|
|
function ImageObjectProcessor(aErrors, aExtractor, aBundle) {
|
|
|
|
this.errors = aErrors;
|
2015-05-20 17:58:00 +03:00
|
|
|
this.extractor = aExtractor;
|
2019-07-09 05:33:39 +03:00
|
|
|
this.domBundle = aBundle;
|
2015-05-20 17:58:00 +03:00
|
|
|
}
|
2015-05-19 15:20:00 +03:00
|
|
|
|
|
|
|
// Static getters
|
2015-05-27 00:04:59 +03:00
|
|
|
Object.defineProperties(ImageObjectProcessor, {
|
2018-12-18 23:38:43 +03:00
|
|
|
decimals: {
|
|
|
|
get() {
|
2015-05-19 15:20:00 +03:00
|
|
|
return /^\d+$/;
|
2018-12-18 23:38:43 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
anyRegEx: {
|
|
|
|
get() {
|
|
|
|
return new RegExp("any", "i");
|
|
|
|
},
|
2015-05-19 15:20:00 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2015-05-27 00:04:59 +03:00
|
|
|
ImageObjectProcessor.prototype.process = function(
|
2015-05-20 17:58:00 +03:00
|
|
|
aManifest,
|
|
|
|
aBaseURL,
|
|
|
|
aMemberName
|
2015-05-19 15:20:00 +03:00
|
|
|
) {
|
|
|
|
const spec = {
|
2018-12-18 23:38:43 +03:00
|
|
|
objectName: "manifest",
|
2015-05-19 15:20:00 +03:00
|
|
|
object: aManifest,
|
|
|
|
property: aMemberName,
|
2018-12-18 23:38:43 +03:00
|
|
|
expectedType: "array",
|
|
|
|
trim: false,
|
2015-05-19 15:20:00 +03:00
|
|
|
};
|
2019-07-09 05:33:39 +03:00
|
|
|
const { domBundle, extractor, errors } = this;
|
2015-05-19 15:20:00 +03:00
|
|
|
const images = [];
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-05-19 15:20:00 +03:00
|
|
|
if (Array.isArray(value)) {
|
|
|
|
// Filter out images whose "src" is not useful.
|
|
|
|
value
|
2019-07-09 05:33:39 +03:00
|
|
|
.filter((item, index) => !!processSrcMember(item, aBaseURL, index))
|
2015-05-19 15:20:00 +03:00
|
|
|
.map(toImageObject)
|
|
|
|
.forEach(image => images.push(image));
|
|
|
|
}
|
|
|
|
return images;
|
|
|
|
|
|
|
|
function toImageObject(aImageSpec) {
|
|
|
|
return {
|
2018-12-18 23:38:43 +03:00
|
|
|
src: processSrcMember(aImageSpec, aBaseURL),
|
|
|
|
type: processTypeMember(aImageSpec),
|
|
|
|
sizes: processSizesMember(aImageSpec),
|
2015-05-19 15:20:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function processTypeMember(aImage) {
|
|
|
|
const charset = {};
|
|
|
|
const hadCharset = {};
|
|
|
|
const spec = {
|
2018-12-18 23:38:43 +03:00
|
|
|
objectName: "image",
|
2015-05-19 15:20:00 +03:00
|
|
|
object: aImage,
|
2018-12-18 23:38:43 +03:00
|
|
|
property: "type",
|
|
|
|
expectedType: "string",
|
|
|
|
trim: true,
|
2015-05-19 15:20:00 +03:00
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
let value = extractor.extractValue(spec);
|
2015-05-19 15:20:00 +03:00
|
|
|
if (value) {
|
2018-12-18 23:39:42 +03:00
|
|
|
value = Services.netUtils.parseRequestContentType(
|
|
|
|
value,
|
|
|
|
charset,
|
|
|
|
hadCharset
|
|
|
|
);
|
2015-05-19 15:20:00 +03:00
|
|
|
}
|
|
|
|
return value || undefined;
|
|
|
|
}
|
|
|
|
|
2019-07-09 05:33:39 +03:00
|
|
|
function processSrcMember(aImage, aBaseURL, index) {
|
2015-05-19 15:20:00 +03:00
|
|
|
const spec = {
|
2019-07-09 05:33:39 +03:00
|
|
|
objectName: aMemberName,
|
2015-05-19 15:20:00 +03:00
|
|
|
object: aImage,
|
2018-12-18 23:38:43 +03:00
|
|
|
property: "src",
|
|
|
|
expectedType: "string",
|
|
|
|
trim: false,
|
2015-05-19 15:20:00 +03:00
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-05-19 15:20:00 +03:00
|
|
|
let url;
|
|
|
|
if (value && value.length) {
|
|
|
|
try {
|
|
|
|
url = new URL(value, aBaseURL).href;
|
2019-07-09 05:33:39 +03:00
|
|
|
} catch (e) {
|
|
|
|
const warn = domBundle.formatStringFromName(
|
|
|
|
"ManifestImageURLIsInvalid",
|
|
|
|
[aMemberName, index, "src", value]
|
|
|
|
);
|
|
|
|
errors.push({ warn });
|
|
|
|
}
|
2015-05-19 15:20:00 +03:00
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function processSizesMember(aImage) {
|
|
|
|
const sizes = new Set();
|
|
|
|
const spec = {
|
2018-12-18 23:38:43 +03:00
|
|
|
objectName: "image",
|
2015-05-19 15:20:00 +03:00
|
|
|
object: aImage,
|
2018-12-18 23:38:43 +03:00
|
|
|
property: "sizes",
|
|
|
|
expectedType: "string",
|
|
|
|
trim: true,
|
2015-05-19 15:20:00 +03:00
|
|
|
};
|
2015-05-20 17:58:00 +03:00
|
|
|
const value = extractor.extractValue(spec);
|
2015-05-19 15:20:00 +03:00
|
|
|
if (value) {
|
|
|
|
// Split on whitespace and filter out invalid values.
|
|
|
|
value
|
|
|
|
.split(/\s+/)
|
|
|
|
.filter(isValidSizeValue)
|
2016-04-14 17:53:00 +03:00
|
|
|
.reduce((collector, size) => collector.add(size), sizes);
|
2015-05-19 15:20:00 +03:00
|
|
|
}
|
2016-04-14 17:53:00 +03:00
|
|
|
return sizes.size ? Array.from(sizes).join(" ") : undefined;
|
2015-05-19 15:20:00 +03:00
|
|
|
// Implementation of HTML's link@size attribute checker.
|
|
|
|
function isValidSizeValue(aSize) {
|
|
|
|
const size = aSize.toLowerCase();
|
2015-05-27 00:04:59 +03:00
|
|
|
if (ImageObjectProcessor.anyRegEx.test(aSize)) {
|
2015-05-19 15:20:00 +03:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-18 23:38:43 +03:00
|
|
|
if (!size.includes("x") || size.indexOf("x") !== size.lastIndexOf("x")) {
|
2015-05-19 15:20:00 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Split left of x for width, after x for height.
|
2018-12-18 23:38:43 +03:00
|
|
|
const widthAndHeight = size.split("x");
|
2015-05-19 15:20:00 +03:00
|
|
|
const w = widthAndHeight.shift();
|
2018-12-18 23:38:43 +03:00
|
|
|
const h = widthAndHeight.join("x");
|
|
|
|
const validStarts = !w.startsWith("0") && !h.startsWith("0");
|
2015-05-27 00:04:59 +03:00
|
|
|
const validDecimals = ImageObjectProcessor.decimals.test(w + h);
|
2015-05-19 15:20:00 +03:00
|
|
|
return validStarts && validDecimals;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-12-18 23:38:43 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ImageObjectProcessor"]; // jshint ignore:line
|