From c06f7ee0a40255214c477e13d9b956a7e07c2900 Mon Sep 17 00:00:00 2001 From: littleblack111 Date: Sat, 14 Oct 2023 15:05:55 +0800 Subject: postcss,(plugins): nanocss, postcss-preset-env --- node_modules/postcss-normalize-url/src/index.js | 151 ++++++++++++++++++++ .../postcss-normalize-url/src/normalize.js | 153 +++++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 node_modules/postcss-normalize-url/src/index.js create mode 100644 node_modules/postcss-normalize-url/src/normalize.js (limited to 'node_modules/postcss-normalize-url/src') diff --git a/node_modules/postcss-normalize-url/src/index.js b/node_modules/postcss-normalize-url/src/index.js new file mode 100644 index 00000000..3542dcf8 --- /dev/null +++ b/node_modules/postcss-normalize-url/src/index.js @@ -0,0 +1,151 @@ +'use strict'; +const path = require('path'); +const valueParser = require('postcss-value-parser'); +const normalize = require('./normalize.js'); + +const multiline = /\\[\r\n]/; +// eslint-disable-next-line no-useless-escape +const escapeChars = /([\s\(\)"'])/g; + +// Scheme: https://tools.ietf.org/html/rfc3986#section-3.1 +// Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3 +const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/; +// Windows paths like `c:\` +const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/; + +/** + * Originally in sindresorhus/is-absolute-url + * + * @param {string} url + */ +function isAbsolute(url) { + if (WINDOWS_PATH_REGEX.test(url)) { + return false; + } + return ABSOLUTE_URL_REGEX.test(url); +} + +/** + * @param {string} url + * @return {string} + */ +function convert(url) { + if (isAbsolute(url) || url.startsWith('//')) { + let normalizedURL; + + try { + normalizedURL = normalize(url); + } catch (e) { + normalizedURL = url; + } + + return normalizedURL; + } + + // `path.normalize` always returns backslashes on Windows, need replace in `/` + return path.normalize(url).replace(new RegExp('\\' + path.sep, 'g'), '/'); +} + +/** + * @param {import('postcss').AtRule} rule + * @return {void} + */ +function transformNamespace(rule) { + rule.params = valueParser(rule.params) + .walk((node) => { + if ( + node.type === 'function' && + node.value.toLowerCase() === 'url' && + node.nodes.length + ) { + /** @type {valueParser.Node} */ (node).type = 'string'; + /** @type {any} */ (node).quote = + node.nodes[0].type === 'string' ? node.nodes[0].quote : '"'; + node.value = node.nodes[0].value; + } + if (node.type === 'string') { + node.value = node.value.trim(); + } + return false; + }) + .toString(); +} + +/** + * @param {import('postcss').Declaration} decl + * @return {void} + */ +function transformDecl(decl) { + decl.value = valueParser(decl.value) + .walk((node) => { + if (node.type !== 'function' || node.value.toLowerCase() !== 'url') { + return false; + } + + node.before = node.after = ''; + + if (!node.nodes.length) { + return false; + } + let url = node.nodes[0]; + let escaped; + + url.value = url.value.trim().replace(multiline, ''); + + // Skip empty URLs + // Empty URL function equals request to current stylesheet where it is declared + if (url.value.length === 0) { + /** @type {any} */ (url).quote = ''; + + return false; + } + + if (/^data:(.*)?,/i.test(url.value)) { + return false; + } + + if (!/^.+-extension:\//i.test(url.value)) { + url.value = convert(url.value); + } + + if (escapeChars.test(url.value) && url.type === 'string') { + escaped = url.value.replace(escapeChars, '\\$1'); + + if (escaped.length < url.value.length + 2) { + url.value = escaped; + /** @type {valueParser.Node} */ (url).type = 'word'; + } + } else { + url.type = 'word'; + } + + return false; + }) + .toString(); +} + +/** + * @type {import('postcss').PluginCreator} + * @return {import('postcss').Plugin} + */ +function pluginCreator() { + return { + postcssPlugin: 'postcss-normalize-url', + + OnceExit(css) { + css.walk((node) => { + if (node.type === 'decl') { + return transformDecl(node); + } else if ( + node.type === 'atrule' && + node.name.toLowerCase() === 'namespace' + ) { + return transformNamespace(node); + } + }); + }, + }; +} + +pluginCreator.postcss = true; +module.exports = pluginCreator; diff --git a/node_modules/postcss-normalize-url/src/normalize.js b/node_modules/postcss-normalize-url/src/normalize.js new file mode 100644 index 00000000..e6597e0d --- /dev/null +++ b/node_modules/postcss-normalize-url/src/normalize.js @@ -0,0 +1,153 @@ +'use strict'; +/* Derived from normalize-url https://github.com/sindresorhus/normalize-url/main/index.js by Sindre Sorhus */ + +// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs +const DATA_URL_DEFAULT_MIME_TYPE = 'text/plain'; +const DATA_URL_DEFAULT_CHARSET = 'us-ascii'; + +const supportedProtocols = new Set(['https:', 'http:', 'file:']); + +/** + * @param {string} urlString + * @return {boolean} */ +function hasCustomProtocol(urlString) { + try { + const { protocol } = new URL(urlString); + return protocol.endsWith(':') && !supportedProtocols.has(protocol); + } catch { + return false; + } +} + +/** + * @param {string} urlString + * @return {string} */ +function normalizeDataURL(urlString) { + const match = /^data:(?[^,]*?),(?[^#]*?)(?:#(?.*))?$/.exec( + urlString + ); + + if (!match) { + throw new Error(`Invalid URL: ${urlString}`); + } + + let { type, data, hash } = + /** @type {{type: string, data: string, hash: string}} */ (match.groups); + const mediaType = type.split(';'); + + let isBase64 = false; + if (mediaType[mediaType.length - 1] === 'base64') { + mediaType.pop(); + isBase64 = true; + } + + // Lowercase MIME type + const mimeType = mediaType.shift()?.toLowerCase() ?? ''; + const attributes = mediaType + .map( + /** @type {(string: string) => string} */ (attribute) => { + let [key, value = ''] = attribute + .split('=') + .map( + /** @type {(string: string) => string} */ (string) => string.trim() + ); + + // Lowercase `charset` + if (key === 'charset') { + value = value.toLowerCase(); + + if (value === DATA_URL_DEFAULT_CHARSET) { + return ''; + } + } + + return `${key}${value ? `=${value}` : ''}`; + } + ) + .filter(Boolean); + + const normalizedMediaType = [...attributes]; + + if (isBase64) { + normalizedMediaType.push('base64'); + } + + if ( + normalizedMediaType.length > 0 || + (mimeType && mimeType !== DATA_URL_DEFAULT_MIME_TYPE) + ) { + normalizedMediaType.unshift(mimeType); + } + + return `data:${normalizedMediaType.join(';')},${ + isBase64 ? data.trim() : data + }${hash ? `#${hash}` : ''}`; +} + +/** + * @param {string} urlString + * @return {string} + */ +function normalizeUrl(urlString) { + urlString = urlString.trim(); + + // Data URL + if (/^data:/i.test(urlString)) { + return normalizeDataURL(urlString); + } + + if (hasCustomProtocol(urlString)) { + return urlString; + } + + const hasRelativeProtocol = urlString.startsWith('//'); + const isRelativeUrl = !hasRelativeProtocol && /^\.*\//.test(urlString); + + // Prepend protocol + if (!isRelativeUrl) { + urlString = urlString.replace(/^(?!(?:\w+:)?\/\/)|^\/\//, 'http:'); + } + + const urlObject = new URL(urlString); + + // Remove duplicate slashes if not preceded by a protocol + if (urlObject.pathname) { + urlObject.pathname = urlObject.pathname.replace( + /(?