UNPKG

@realfavicongenerator/check-favicon

Version:
180 lines (179 loc) 7.61 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkTouchIcon = exports.getDuplicatedSizes = exports.checkTouchIconIcon = exports.checkTouchIconTitle = void 0; const types_1 = require("./types"); const helper_1 = require("./helper"); const checkTouchIconTitle = (baseUrl_1, head_1, ...args_1) => __awaiter(void 0, [baseUrl_1, head_1, ...args_1], void 0, function* (baseUrl, head, fetcher = helper_1.fetchFetcher) { const messages = []; let appTitle = undefined; if (!head) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.noHead, text: 'No <head> element' }); return { messages }; } const titleMarkup = head.querySelectorAll("meta[name='apple-mobile-web-app-title']"); if (titleMarkup.length === 0) { messages.push({ status: types_1.CheckerStatus.Warning, id: types_1.MessageId.noTouchWebAppTitle, text: 'No touch web app title declared' }); return { messages }; } if (titleMarkup.length > 1) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.multipleTouchWebAppTitles, text: `The touch web app title is declared ${titleMarkup.length} times` }); return { messages }; } if (!titleMarkup[0].getAttribute('content')) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.emptyTouchWebAppTitle, text: 'The touch web app title has no content' }); return { messages }; } appTitle = titleMarkup[0].getAttribute('content'); messages.push({ status: types_1.CheckerStatus.Ok, id: types_1.MessageId.touchWebAppTitleDeclared, text: `The touch web app title is "${appTitle}"` }); return { messages, appTitle }; }); exports.checkTouchIconTitle = checkTouchIconTitle; const checkTouchIconIcon = (baseUrl_2, head_2, ...args_2) => __awaiter(void 0, [baseUrl_2, head_2, ...args_2], void 0, function* (baseUrl, head, fetcher = helper_1.fetchFetcher) { const messages = []; let touchIcon = null; if (!head) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.noHead, text: 'No <head> element' }); return { messages, touchIcon }; } const iconMarkup = head.querySelectorAll("link[rel='apple-touch-icon']"); if (iconMarkup.length === 0) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.noTouchIcon, text: 'No touch icon declared' }); return { messages, touchIcon }; } messages.push({ status: types_1.CheckerStatus.Ok, id: types_1.MessageId.touchIconDeclared, text: 'The touch icon is declared' }); const duplicatedSizes = (0, exports.getDuplicatedSizes)(iconMarkup.map(icon => icon.getAttribute('sizes'))); if (duplicatedSizes.length > 0) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.duplicatedTouchIconSizes, text: `The touch icon sizes ${duplicatedSizes.map(s => s || '(no size)').join(', ')} are declared more than once` }); } const iconHref = iconMarkup[0].getAttribute('href'); if (!iconHref) { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.noTouchIconHref, text: 'The touch icon has no href' }); return { messages, touchIcon }; } const touchIconUrl = (0, helper_1.mergeUrlAndPath)(baseUrl, iconHref); const processor = { cannotGet: (status) => { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.touchIconCannotGet, text: `The touch icon cannot be fetched (${status})` }); }, downloadable: () => { messages.push({ status: types_1.CheckerStatus.Ok, id: types_1.MessageId.touchIconDownloadable, text: 'The touch icon is downloadable' }); }, icon404: () => { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.touchIcon404, text: `The touch icon at ${touchIconUrl} is not found` }); }, noHref: () => { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.noTouchIconHref, text: 'The touch icon markup has no href' }); }, notSquare: (width, height) => { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.touchIconNotSquare, text: `The touch icon is not square (${width}x${height})` }); }, rightSize: (width) => { messages.push({ status: types_1.CheckerStatus.Ok, id: types_1.MessageId.touchIconSquare, text: `The touch icon is square (${width}x${width})` }); }, square: (width) => { messages.push({ status: types_1.CheckerStatus.Ok, id: types_1.MessageId.touchIconSquare, text: `The touch icon is square (${width}x${width})` }); }, wrongSize: (width) => { messages.push({ status: types_1.CheckerStatus.Error, id: types_1.MessageId.touchIconWrongSize, text: `The touch icon has a wrong size (${width}x${width})` }); } }; touchIcon = yield (0, helper_1.checkIcon)(touchIconUrl, processor, fetcher, undefined); return { messages, touchIcon: touchIcon ? touchIcon.content : null }; }); exports.checkTouchIconIcon = checkTouchIconIcon; const getDuplicatedSizes = (sizes) => { const duplicated = sizes.filter((size, index) => sizes.indexOf(size, index + 1) >= 0); return duplicated.filter((size, index) => duplicated.indexOf(size) === index); }; exports.getDuplicatedSizes = getDuplicatedSizes; const checkTouchIcon = (baseUrl_3, head_3, ...args_3) => __awaiter(void 0, [baseUrl_3, head_3, ...args_3], void 0, function* (baseUrl, head, fetcher = helper_1.fetchFetcher) { const titleReport = yield (0, exports.checkTouchIconTitle)(baseUrl, head, fetcher); const iconReport = yield (0, exports.checkTouchIconIcon)(baseUrl, head, fetcher); return { messages: [...titleReport.messages, ...iconReport.messages], appTitle: titleReport.appTitle, touchIcon: iconReport.touchIcon }; }); exports.checkTouchIcon = checkTouchIcon;