Skip to content

Commit

Permalink
feat: emit warning on invalid url
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 30, 2018
1 parent 947e666 commit f736ed0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
40 changes: 24 additions & 16 deletions lib/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,49 +20,55 @@ function walkUrls(parsed, callback) {
node.after = '';
/* eslint-enable */

if (url.trim().replace(/\\[\r\n]/g, '').length !== 0) {
callback(node, url);
}
callback(node, url);

// Do not traverse inside url
// eslint-disable-next-line consistent-return
return false;
});
}

function filterUrls(parsed, filter) {
const result = [];
function filterUrls(parsed, result, decl, filter) {
const urls = [];

walkUrls(parsed, (node, content) => {
if (!filter(content)) {
walkUrls(parsed, (node, url) => {
if (url.trim().replace(/\\[\r\n]/g, '').length === 0) {
result.warn(`Unable to find uri in '${decl.toString()}'`, {
node: decl,
});

return;
}

if (!filter(url)) {
return;
}

result.push(content);
urls.push(url);
});

return result;
return urls;
}

function walkDeclsWithUrl(css, filter) {
const result = [];
function walkDeclsWithUrl(css, result, filter) {
const items = [];

css.walkDecls((decl) => {
if (!/url\(/i.test(decl.value)) {
return;
}

const parsed = valueParser(decl.value);
const values = filterUrls(parsed, filter);
const values = filterUrls(parsed, result, decl, filter);

if (values.length === 0) {
return;
}

result.push({ decl, parsed, values });
items.push({ decl, parsed, values });
});

return result;
return items;
}

function flatten(array) {
Expand Down Expand Up @@ -92,9 +98,11 @@ function uniq(array) {
module.exports = postcss.plugin(
pluginName,
(options) =>
function process(css) {
function process(css, result) {
const urlItems = [];
const traversed = walkDeclsWithUrl(css, (value) => isUrlRequest(value));
const traversed = walkDeclsWithUrl(css, result, (value) =>
isUrlRequest(value)
);
const paths = uniq(flatten(traversed.map((item) => item.values)));

if (paths.length === 0) {
Expand Down
26 changes: 25 additions & 1 deletion test/__snapshots__/url-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -421,4 +421,28 @@ exports.push([module.id, \\".class {\\\\n background: url(\\" + escape(require(
"
`;
exports[`url option true: warnings 1`] = `Array []`;
exports[`url option true: warnings 1`] = `
Array [
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
Warning
(120:3) Unable to find uri in 'background: green url() xyz'],
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
Warning
(124:3) Unable to find uri in 'background: green url('') xyz'],
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
Warning
(128:3) Unable to find uri in 'background: green url("") xyz'],
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
Warning
(132:3) Unable to find uri in 'background: green url('') xyz'],
[ModuleWarning: Module Warning (from /home/evilebottnawi/IdeaProjects/css-loader/index.js):
Warning
(136:3) Unable to find uri in 'background: green url(
) xyz'],
]
`;

0 comments on commit f736ed0

Please sign in to comment.