/** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: Apache-2.0 */ /** @typedef {LH.Artifacts.FontSize['analyzedFailingNodesData'][0]} FailingNodeData */ import * as i18n from '../../lib/i18n/i18n.js'; import {Audit} from '../audit.js'; import {ViewportMeta} from '../../computed/viewport-meta.js'; const MINIMAL_PERCENTAGE_OF_LEGIBLE_TEXT = 60; const UIStrings = { /** Title of a Lighthouse audit that provides detail on the font sizes used on the page. This descriptive title is shown to users when the fonts used on the page are large enough to be considered legible. */ title: 'Document uses legible font sizes', /** Title of a Lighthouse audit that provides detail on the font sizes used on the page. This descriptive title is shown to users when there is a font that may be too small to be read by users. */ failureTitle: 'Document doesn\'t use legible font sizes', /** Description of a Lighthouse audit that tells the user *why* they need to use a larger font size. This is displayed after a user expands the section to see more. No character length limits. The last sentence starting with 'Learn' becomes link text to additional documentation. */ description: 'Font sizes less than 12px are too small to be legible and require mobile visitors to “pinch to zoom” in order to read. Strive to have >60% of page text ≥12px. [Learn more about legible font sizes](https://developer.chrome.com/docs/lighthouse/seo/font-size/).', /** Label for the audit identifying font sizes that are too small. */ displayValue: '{decimalProportion, number, extendedPercent} legible text', /** Explanatory message stating that there was a failure in an audit caused by a missing page viewport meta tag configuration. "viewport" and "meta" are HTML terms and should not be translated. */ explanationViewport: 'Text is illegible because there\'s no viewport meta tag optimized ' + 'for mobile screens.', /** Label for the table row which summarizes all failing nodes that were not fully analyzed. "Add'l" is shorthand for "Additional" */ additionalIllegibleText: 'Add\'l illegible text', /** Label for the table row which displays the percentage of nodes that have proper font size. */ legibleText: 'Legible text', /** Label for a column in a data table; entries will be css style rule selectors. */ columnSelector: 'Selector', /** Label for a column in a data table; entries will be the percent of page text a specific CSS rule applies to. */ columnPercentPageText: '% of Page Text', /** Label for a column in a data table; entries will be text font sizes. */ columnFontSize: 'Font Size', }; const str_ = i18n.createIcuMessageFn(import.meta.url, UIStrings); /** * @param {Array} fontSizeArtifact * @return {Array} */ function getUniqueFailingRules(fontSizeArtifact) { /** @type {Map} */ const failingRules = new Map(); fontSizeArtifact.forEach((failingNodeData) => { const {nodeId, cssRule, fontSize, textLength, parentNode} = failingNodeData; const artifactId = getFontArtifactId(cssRule, nodeId); const failingRule = failingRules.get(artifactId); if (!failingRule) { failingRules.set(artifactId, { nodeId, parentNode, cssRule, fontSize, textLength, }); } else { failingRule.textLength += textLength; } }); return [...failingRules.values()]; } /** * @param {Array=} attributes * @return {Map} */ function getAttributeMap(attributes = []) { const map = new Map(); for (let i = 0; i < attributes.length; i += 2) { const name = attributes[i]; const value = attributes[i + 1]; if (!name || !value) continue; const normalizedValue = value.trim(); if (normalizedValue) { map.set(name.toLowerCase(), normalizedValue); } } return map; } /** * TODO: return unique selector, like axe-core does, instead of just id/class/name of a single node * @param {FailingNodeData['parentNode']} parentNode * @return {string} */ function getSelector(parentNode) { const attributeMap = getAttributeMap(parentNode.attributes); if (attributeMap.has('id')) { return '#' + attributeMap.get('id'); } else { const attrClass = attributeMap.get('class'); if (attrClass) { return '.' + attrClass.split(/\s+/).join('.'); } } return parentNode.nodeName.toLowerCase(); } /** * @param {FailingNodeData['parentNode']} parentNode * @return {LH.Audit.Details.NodeValue} */ function nodeToTableNode(parentNode) { const attributes = parentNode.attributes || []; const attributesString = attributes.map((value, idx) => (idx % 2 === 0) ? ` ${value}` : `="${value}"` ).join(''); return { type: 'node', selector: parentNode.parentNode ? getSelector(parentNode.parentNode) : '', snippet: `<${parentNode.nodeName.toLowerCase()}${attributesString}>`, }; } /** * @param {string} baseURL * @param {FailingNodeData['cssRule']} styleDeclaration * @param {FailingNodeData['parentNode']} parentNode * @return {{source: LH.Audit.Details.UrlValue | LH.Audit.Details.SourceLocationValue | LH.Audit.Details.CodeValue, selector: string | LH.Audit.Details.NodeValue}} */ function findStyleRuleSource(baseURL, styleDeclaration, parentNode) { if (!styleDeclaration || styleDeclaration.type === 'Attributes' || styleDeclaration.type === 'Inline' ) { return { source: {type: 'url', value: baseURL}, selector: nodeToTableNode(parentNode), }; } if (styleDeclaration.parentRule && styleDeclaration.parentRule.origin === 'user-agent') { return { source: {type: 'code', value: 'User Agent Stylesheet'}, selector: styleDeclaration.parentRule.selectors.map(item => item.text).join(', '), }; } // Combine all the selectors for the associated style rule // example: .some-selector, .other-selector {...} => `.some-selector, .other-selector` let selector = ''; if (styleDeclaration.parentRule) { const rule = styleDeclaration.parentRule; selector = rule.selectors.map(item => item.text).join(', '); } if (styleDeclaration.stylesheet && !styleDeclaration.stylesheet.sourceURL) { // Dynamically injected into page. return { source: {type: 'code', value: 'dynamic'}, selector, }; } // !!range == has defined location in a source file (.css or .html) // sourceURL == stylesheet URL || raw value of magic `sourceURL` comment // hasSourceURL == flag that signals sourceURL is the raw value of a magic `sourceURL` comment, *not* a real resource if (styleDeclaration.stylesheet && styleDeclaration.range) { const {range, stylesheet} = styleDeclaration; // DevTools protocol does not provide the resource URL if there is a magic `sourceURL` comment. // `sourceURL` will be the raw value of the magic `sourceURL` comment, which likely refers to // a file at build time, not one that is served over the network that we could link to. const urlProvider = stylesheet.hasSourceURL ? 'comment' : 'network'; let line = range.startLine; let column = range.startColumn; // Add the startLine/startColumn of the