| 1 | 'use strict';
|
|---|
| 2 | const path = require('path');
|
|---|
| 3 | const valueParser = require('postcss-value-parser');
|
|---|
| 4 | const normalize = require('normalize-url');
|
|---|
| 5 |
|
|---|
| 6 | const multiline = /\\[\r\n]/;
|
|---|
| 7 | // eslint-disable-next-line no-useless-escape
|
|---|
| 8 | const escapeChars = /([\s\(\)"'])/g;
|
|---|
| 9 |
|
|---|
| 10 | // Scheme: https://tools.ietf.org/html/rfc3986#section-3.1
|
|---|
| 11 | // Absolute URL: https://tools.ietf.org/html/rfc3986#section-4.3
|
|---|
| 12 | const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;
|
|---|
| 13 | // Windows paths like `c:\`
|
|---|
| 14 | const WINDOWS_PATH_REGEX = /^[a-zA-Z]:\\/;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Originally in sindresorhus/is-absolute-url
|
|---|
| 18 | *
|
|---|
| 19 | * @param {string} url
|
|---|
| 20 | */
|
|---|
| 21 | function isAbsolute(url) {
|
|---|
| 22 | if (WINDOWS_PATH_REGEX.test(url)) {
|
|---|
| 23 | return false;
|
|---|
| 24 | }
|
|---|
| 25 | return ABSOLUTE_URL_REGEX.test(url);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * @param {string} url
|
|---|
| 30 | * @param {normalize.Options} options
|
|---|
| 31 | * @return {string}
|
|---|
| 32 | */
|
|---|
| 33 | function convert(url, options) {
|
|---|
| 34 | if (isAbsolute(url) || url.startsWith('//')) {
|
|---|
| 35 | let normalizedURL;
|
|---|
| 36 |
|
|---|
| 37 | try {
|
|---|
| 38 | normalizedURL = normalize(url, options);
|
|---|
| 39 | } catch (e) {
|
|---|
| 40 | normalizedURL = url;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return normalizedURL;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | // `path.normalize` always returns backslashes on Windows, need replace in `/`
|
|---|
| 47 | return path.normalize(url).replace(new RegExp('\\' + path.sep, 'g'), '/');
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * @param {import('postcss').AtRule} rule
|
|---|
| 52 | * @return {void}
|
|---|
| 53 | */
|
|---|
| 54 | function transformNamespace(rule) {
|
|---|
| 55 | rule.params = valueParser(rule.params)
|
|---|
| 56 | .walk((node) => {
|
|---|
| 57 | if (
|
|---|
| 58 | node.type === 'function' &&
|
|---|
| 59 | node.value.toLowerCase() === 'url' &&
|
|---|
| 60 | node.nodes.length
|
|---|
| 61 | ) {
|
|---|
| 62 | /** @type {valueParser.Node} */ (node).type = 'string';
|
|---|
| 63 | /** @type {any} */ (node).quote =
|
|---|
| 64 | node.nodes[0].type === 'string' ? node.nodes[0].quote : '"';
|
|---|
| 65 | node.value = node.nodes[0].value;
|
|---|
| 66 | }
|
|---|
| 67 | if (node.type === 'string') {
|
|---|
| 68 | node.value = node.value.trim();
|
|---|
| 69 | }
|
|---|
| 70 | return false;
|
|---|
| 71 | })
|
|---|
| 72 | .toString();
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * @param {import('postcss').Declaration} decl
|
|---|
| 77 | * @param {normalize.Options} opts
|
|---|
| 78 | * @return {void}
|
|---|
| 79 | */
|
|---|
| 80 | function transformDecl(decl, opts) {
|
|---|
| 81 | decl.value = valueParser(decl.value)
|
|---|
| 82 | .walk((node) => {
|
|---|
| 83 | if (node.type !== 'function' || node.value.toLowerCase() !== 'url') {
|
|---|
| 84 | return false;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | node.before = node.after = '';
|
|---|
| 88 |
|
|---|
| 89 | if (!node.nodes.length) {
|
|---|
| 90 | return false;
|
|---|
| 91 | }
|
|---|
| 92 | let url = node.nodes[0];
|
|---|
| 93 | let escaped;
|
|---|
| 94 |
|
|---|
| 95 | url.value = url.value.trim().replace(multiline, '');
|
|---|
| 96 |
|
|---|
| 97 | // Skip empty URLs
|
|---|
| 98 | // Empty URL function equals request to current stylesheet where it is declared
|
|---|
| 99 | if (url.value.length === 0) {
|
|---|
| 100 | /** @type {any} */ (url).quote = '';
|
|---|
| 101 |
|
|---|
| 102 | return false;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | if (/^data:(.*)?,/i.test(url.value)) {
|
|---|
| 106 | return false;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | if (!/^.+-extension:\//i.test(url.value)) {
|
|---|
| 110 | url.value = convert(url.value, opts);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | if (escapeChars.test(url.value) && url.type === 'string') {
|
|---|
| 114 | escaped = url.value.replace(escapeChars, '\\$1');
|
|---|
| 115 |
|
|---|
| 116 | if (escaped.length < url.value.length + 2) {
|
|---|
| 117 | url.value = escaped;
|
|---|
| 118 | /** @type {valueParser.Node} */ (url).type = 'word';
|
|---|
| 119 | }
|
|---|
| 120 | } else {
|
|---|
| 121 | url.type = 'word';
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | return false;
|
|---|
| 125 | })
|
|---|
| 126 | .toString();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /** @typedef {normalize.Options} Options */
|
|---|
| 130 | /**
|
|---|
| 131 | * @type {import('postcss').PluginCreator<Options>}
|
|---|
| 132 | * @param {Options} opts
|
|---|
| 133 | * @return {import('postcss').Plugin}
|
|---|
| 134 | */
|
|---|
| 135 | function pluginCreator(opts) {
|
|---|
| 136 | opts = Object.assign(
|
|---|
| 137 | {},
|
|---|
| 138 | {
|
|---|
| 139 | normalizeProtocol: false,
|
|---|
| 140 | sortQueryParameters: false,
|
|---|
| 141 | stripHash: false,
|
|---|
| 142 | stripWWW: false,
|
|---|
| 143 | stripTextFragment: false,
|
|---|
| 144 | },
|
|---|
| 145 | opts
|
|---|
| 146 | );
|
|---|
| 147 |
|
|---|
| 148 | return {
|
|---|
| 149 | postcssPlugin: 'postcss-normalize-url',
|
|---|
| 150 |
|
|---|
| 151 | OnceExit(css) {
|
|---|
| 152 | css.walk((node) => {
|
|---|
| 153 | if (node.type === 'decl') {
|
|---|
| 154 | return transformDecl(node, opts);
|
|---|
| 155 | } else if (
|
|---|
| 156 | node.type === 'atrule' &&
|
|---|
| 157 | node.name.toLowerCase() === 'namespace'
|
|---|
| 158 | ) {
|
|---|
| 159 | return transformNamespace(node);
|
|---|
| 160 | }
|
|---|
| 161 | });
|
|---|
| 162 | },
|
|---|
| 163 | };
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | pluginCreator.postcss = true;
|
|---|
| 167 | module.exports = pluginCreator;
|
|---|