Release Vulpes OS Preview 26 with Gecko 161.0 and Tundra 0.2.0
This commit is contained in:
parent
3a1fba4efe
commit
e241a42d2f
1 changed files with 1305 additions and 331 deletions
|
|
@ -3,6 +3,19 @@ function unescapeText(value) {
|
|||
return value.replace(/\\([nN,;\\])/g, (_, char) => /n/i.test(char) ? '\n' : char);
|
||||
}
|
||||
|
||||
// Split before decoding: escaped commas and semicolons belong to the field.
|
||||
function components(value, separator) {
|
||||
const result = [''];
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (value[i] === separator) result.push('');
|
||||
else {
|
||||
result[result.length - 1] += value[i];
|
||||
if (value[i] === '\\' && i + 1 < value.length) result[result.length - 1] += value[++i];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function readVCards(raw) {
|
||||
const cards = [];
|
||||
let card;
|
||||
|
|
@ -17,10 +30,11 @@ export function readVCards(raw) {
|
|||
if (!card) throw Error('INVALID_VCARD');
|
||||
if (/^END:VCARD$/i.test(line)) {
|
||||
if (!card.name.length) {
|
||||
const name = [...(card.givenName || []), ...(card.familyName || [])].join(' ');
|
||||
const name = ['honorificPrefix','givenName','additionalName','familyName','honorificSuffix']
|
||||
.flatMap(key => card[key] || []).join(' ');
|
||||
if (name) card.name.push(name);
|
||||
}
|
||||
if (!card.name.length && !card.tel.length && !card.email.length) throw Error('INVALID_VCARD');
|
||||
if (!card.name.length && !card.tel.length && !card.email.length && !card.org?.length && !card.adr?.length && !card.url?.length && !card.nickname?.length) throw Error('INVALID_VCARD');
|
||||
cards.push(card);
|
||||
card = null;
|
||||
continue;
|
||||
|
|
@ -32,17 +46,39 @@ export function readVCards(raw) {
|
|||
const value = unescapeText(field[2]);
|
||||
if (key === 'FN') card.name = [value];
|
||||
if (key === 'N') {
|
||||
const [, family, given] = /^((?:\\.|[^;\\])*)(?:;((?:\\.|[^;\\])*))?/.exec(field[2]);
|
||||
if (family) card.familyName = [unescapeText(family)];
|
||||
if (given) card.givenName = [unescapeText(given)];
|
||||
const fields = ['familyName','givenName','additionalName','honorificPrefix','honorificSuffix'];
|
||||
components(field[2], ';').slice(0, fields.length).forEach((part, i) => {
|
||||
if (part) card[fields[i]] = components(part, ',').map(unescapeText);
|
||||
});
|
||||
}
|
||||
if (key === 'TEL' || key === 'EMAIL') {
|
||||
if (key === 'NICKNAME' || key === 'CATEGORIES') {
|
||||
const name = key === 'NICKNAME' ? 'nickname' : 'category';
|
||||
(card[name] ||= []).push(...components(field[2], ',').map(unescapeText));
|
||||
}
|
||||
if (key === 'ORG' || key === 'NOTE' || key === 'TITLE') {
|
||||
const name = {ORG:'org', NOTE:'note', TITLE:'jobTitle'}[key];
|
||||
(card[name] ||= []).push(key === 'ORG' ? components(field[2], ';').map(unescapeText).join(' / ') : value);
|
||||
}
|
||||
if (['TEL','EMAIL','URL','ADR'].includes(key)) {
|
||||
const types = params.flatMap(param => {
|
||||
if (param.includes('=') && !/^TYPE=/i.test(param)) return [];
|
||||
return param.replace(/^TYPE=/i, '').replace(/"/g, '').toLowerCase().split(',');
|
||||
}).filter(Boolean).map(type => type === 'cell' ? 'mobile' : type);
|
||||
const clean = key === 'TEL' ? value.replace(/^tel:/i, '') : value.replace(/^mailto:/i, '');
|
||||
if (clean) card[key.toLowerCase()].push({value: clean, type: types.length ? types : ['other']});
|
||||
const preferred = types.includes('pref') || params.some(param => /^PREF=1$/i.test(param));
|
||||
const labels = types.filter(type => type !== 'pref');
|
||||
const entry = {type: labels.length ? labels : ['other']};
|
||||
if (preferred) entry.pref = true;
|
||||
if (key === 'ADR') {
|
||||
const [box, extended, street, locality, region, postalCode, countryName] =
|
||||
components(field[2], ';').map(unescapeText);
|
||||
Object.assign(entry, {streetAddress:[box, extended, street].filter(Boolean).join('\n'),
|
||||
locality, region, postalCode, countryName});
|
||||
} else {
|
||||
entry.value = key === 'TEL' ? value.replace(/^tel:/i, '') :
|
||||
key === 'EMAIL' ? value.replace(/^mailto:/i, '') : value;
|
||||
if (!entry.value) continue;
|
||||
}
|
||||
(card[key.toLowerCase()] ||= []).push(entry);
|
||||
}
|
||||
}
|
||||
if (card || !cards.length) throw Error('INVALID_VCARD');
|
||||
|
|
|
|||
Loading…
Reference in a new issue