1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/* 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);
*
*/
export function ImageObjectProcessor(aErrors, aExtractor, aBundle) {
this.errors = aErrors;
this.extractor = aExtractor;
this.domBundle = aBundle;
}
const iconPurposes = Object.freeze(["any", "maskable", "monochrome"]);
// 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)) {
value
.map(toImageObject)
// Filter out images that resulted in "failure", per spec.
.filter(image => image)
.forEach(image => images.push(image));
}
return images;
function toImageObject(aImageSpec, index) {
let img; // if "failure" happens below, we return undefined.
try {
// can throw
const src = processSrcMember(aImageSpec, aBaseURL, index);
// can throw
const purpose = processPurposeMember(aImageSpec, index);
const type = processTypeMember(aImageSpec);
const sizes = processSizesMember(aImageSpec);
img = {
src,
purpose,
type,
sizes,
};
} catch (err) {
/* Errors are collected by each process* function */
}
return img;
}
function processPurposeMember(aImage, index) {
const spec = {
objectName: "image",
object: aImage,
property: "purpose",
expectedType: "string",
trim: true,
throwTypeError: true,
};
// Type errors are treated at "any"...
let value;
try {
value = extractor.extractValue(spec);
} catch (err) {
return ["any"];
}
// Was only whitespace...
if (!value) {
return ["any"];
}
const keywords = value.split(/\s+/);
// Emtpy is treated as "any"...
if (keywords.length === 0) {
return ["any"];
}
// We iterate over keywords and classify them into:
const purposes = new Set();
const unknownPurposes = new Set();
const repeatedPurposes = new Set();
for (const keyword of keywords) {
const canonicalKeyword = keyword.toLowerCase();
if (purposes.has(canonicalKeyword)) {
repeatedPurposes.add(keyword);
continue;
}
iconPurposes.includes(canonicalKeyword)
? purposes.add(canonicalKeyword)
: unknownPurposes.add(keyword);
}
// Tell developer about unknown purposes...
if (unknownPurposes.size) {
const warn = domBundle.formatStringFromName(
"ManifestImageUnsupportedPurposes",
[aMemberName, index, [...unknownPurposes].join(" ")]
);
errors.push({ warn });
}
// Tell developer about repeated purposes...
if (repeatedPurposes.size) {
const warn = domBundle.formatStringFromName(
"ManifestImageRepeatedPurposes",
[aMemberName, index, [...repeatedPurposes].join(" ")]
);
errors.push({ warn });
}
if (purposes.size === 0) {
const warn = domBundle.formatStringFromName("ManifestImageUnusable", [
aMemberName,
index,
]);
errors.push({ warn });
throw new TypeError(warn);
}
return [...purposes];
}
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.io.parseRequestContentType(value, charset, hadCharset);
}
return value || undefined;
}
function processSrcMember(aImage, aBaseURL, index) {
const spec = {
objectName: aMemberName,
object: aImage,
property: "src",
expectedType: "string",
trim: false,
throwTypeError: true,
};
const value = extractor.extractValue(spec);
let url;
if (typeof value === "undefined" || value === "") {
// We throw here as the value is unusable,
// but it's not an developer error.
throw new TypeError();
}
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 });
throw e;
}
}
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) : 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;
}
}
};
|