gecko-dev/dom/manifest/ImageObjectProcessor.jsm

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

166 строки
4.4 KiB
JavaScript
Исходник Обычный вид История

/* 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/*
* ImageObjectProcessor
* Implementation of Image Object processing algorithms from:
* http://www.w3.org/TR/appmanifest/#image-object-and-its-members
*
* This is intended to be used in conjunction with ManifestProcessor.jsm
*
* 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:
*
* .process(aManifest, aBaseURL, aMemberName);
*
*/
/* exported EXPORTED_SYMBOLS*/
/* globals Components */
"use strict";
Bug 1514594: Part 3 - Change ChromeUtils.import API. *** Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8 This changes the behavior of ChromeUtils.import() to return an exports object, rather than a module global, in all cases except when `null` is passed as a second argument, and changes the default behavior not to pollute the global scope with the module's exports. Thus, the following code written for the old model: ChromeUtils.import("resource://gre/modules/Services.jsm"); is approximately the same as the following, in the new model: var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm"); Since the two behaviors are mutually incompatible, this patch will land with a scripted rewrite to update all existing callers to use the new model rather than the old. *** Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs This was done using the followng script: https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm *** Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D16747 *** Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16748 *** Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16749 *** Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs *** Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D16750 --HG-- extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895 extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
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");
XPCOMUtils.defineLazyGlobalGetters(this, ["URL"]);
function ImageObjectProcessor(aErrors, aExtractor, aBundle) {
this.errors = aErrors;
this.extractor = aExtractor;
this.domBundle = aBundle;
}
// Static getters
Object.defineProperties(ImageObjectProcessor, {
decimals: {
get() {
return /^\d+$/;
},
},
anyRegEx: {
get() {
return new RegExp("any", "i");
},
},
});
ImageObjectProcessor.prototype.process = function(
aManifest,
aBaseURL,
aMemberName
) {
const spec = {
objectName: "manifest",
object: aManifest,
property: aMemberName,
expectedType: "array",
trim: false,
};
const { domBundle, extractor, errors } = this;
const images = [];
const value = extractor.extractValue(spec);
if (Array.isArray(value)) {
// Filter out images whose "src" is not useful.
value
.filter((item, index) => !!processSrcMember(item, aBaseURL, index))
.map(toImageObject)
.forEach(image => images.push(image));
}
return images;
function toImageObject(aImageSpec) {
return {
src: processSrcMember(aImageSpec, aBaseURL),
type: processTypeMember(aImageSpec),
sizes: processSizesMember(aImageSpec),
};
}
function processTypeMember(aImage) {
const charset = {};
const hadCharset = {};
const spec = {
objectName: "image",
object: aImage,
property: "type",
expectedType: "string",
trim: true,
};
let value = extractor.extractValue(spec);
if (value) {
value = Services.netUtils.parseRequestContentType(
value,
charset,
hadCharset
);
}
return value || undefined;
}
function processSrcMember(aImage, aBaseURL, index) {
const spec = {
objectName: aMemberName,
object: aImage,
property: "src",
expectedType: "string",
trim: false,
};
const value = extractor.extractValue(spec);
let url;
if (value && value.length) {
try {
url = new URL(value, aBaseURL).href;
} catch (e) {
const warn = domBundle.formatStringFromName(
"ManifestImageURLIsInvalid",
[aMemberName, index, "src", value]
);
errors.push({ warn });
}
}
return url;
}
function processSizesMember(aImage) {
const sizes = new Set();
const spec = {
objectName: "image",
object: aImage,
property: "sizes",
expectedType: "string",
trim: true,
};
const value = extractor.extractValue(spec);
if (value) {
// Split on whitespace and filter out invalid values.
value
.split(/\s+/)
.filter(isValidSizeValue)
.reduce((collector, size) => collector.add(size), sizes);
}
return sizes.size ? Array.from(sizes).join(" ") : undefined;
// Implementation of HTML's link@size attribute checker.
function isValidSizeValue(aSize) {
const size = aSize.toLowerCase();
if (ImageObjectProcessor.anyRegEx.test(aSize)) {
return true;
}
if (!size.includes("x") || size.indexOf("x") !== size.lastIndexOf("x")) {
return false;
}
// Split left of x for width, after x for height.
const widthAndHeight = size.split("x");
const w = widthAndHeight.shift();
const h = widthAndHeight.join("x");
const validStarts = !w.startsWith("0") && !h.startsWith("0");
const validDecimals = ImageObjectProcessor.decimals.test(w + h);
return validStarts && validDecimals;
}
}
};
var EXPORTED_SYMBOLS = ["ImageObjectProcessor"]; // jshint ignore:line