'use strict'; const csstree = require('css-tree'); const { referencesProps } = require('./_collections.js'); /** * @typedef {import('../lib/types').XastElement} XastElement * @typedef {import('../lib/types').PluginInfo} PluginInfo */ exports.type = 'visitor'; exports.name = 'prefixIds'; exports.active = false; exports.description = 'prefix IDs'; /** * extract basename from path * @type {(path: string) => string} */ const getBasename = (path) => { // extract everything after latest slash or backslash const matched = path.match(/[/\\]?([^/\\]+)$/); if (matched) { return matched[1]; } return ''; }; /** * escapes a string for being used as ID * @type {(string: string) => string} */ const escapeIdentifierName = (str) => { return str.replace(/[. ]/g, '_'); }; /** * @type {(string: string) => string} */ const unquote = (string) => { if ( (string.startsWith('"') && string.endsWith('"')) || (string.startsWith("'") && string.endsWith("'")) ) { return string.slice(1, -1); } return string; }; /** * prefix an ID * @type {(prefix: string, name: string) => string} */ const prefixId = (prefix, value) => { if (value.startsWith(prefix)) { return value; } return prefix + value; }; /** * prefix an #ID * @type {(prefix: string, name: string) => string | null} */ const prefixReference = (prefix, value) => { if (value.startsWith('#')) { return '#' + prefixId(prefix, value.slice(1)); } return null; }; /** * Prefixes identifiers * * @author strarsis * * @type {import('../lib/types').Plugin<{ * prefix?: boolean | string | ((node: XastElement, info: PluginInfo) => string), * delim?: string, * prefixIds?: boolean, * prefixClassNames?: boolean, * }>} */ exports.fn = (_root, params, info) => { const { delim = '__', prefixIds = true, prefixClassNames = true } = params; return { element: { enter: (node) => { /** * prefix, from file name or option * @type {string} */ let prefix = 'prefix' + delim; if (typeof params.prefix === 'function') { prefix = params.prefix(node, info) + delim; } else if (typeof params.prefix === 'string') { prefix = params.prefix + delim; } else if (params.prefix === false) { prefix = ''; } else if (info.path != null && info.path.length > 0) { prefix = escapeIdentifierName(getBasename(info.path)) + delim; } // prefix id/class selectors and url() references in styles if (node.name === 'style') { // skip empty