Skip to content

Commit

Permalink
Do not try to import data: tel: mailto: something: URLs (#50)
Browse files Browse the repository at this point in the history
* Ignore data URLs

* Add tests

* Ignore tel and mailto URLs

* Hoist  regexes into constants
  • Loading branch information
oliger authored and devongovett committed Dec 6, 2017
1 parent bb75349 commit 781b7ec
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/utils/is-url.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const isURL = require('is-url');

// Matches anchor (ie: #raptors)
const ANCHOR_REGEXP = /^#/;

// Matches scheme (ie: tel:, mailto:, data:)
const SCHEME_REGEXP = /^[a-z]*\:/i;

module.exports = function (url) {
return isURL(url) || /^#/.test(url);
return isURL(url) || ANCHOR_REGEXP.test(url) || SCHEME_REGEXP.test(url);
};
4 changes: 4 additions & 0 deletions test/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ describe('css', function () {
assert(/url\("[0-9a-f]+\.woff2"\)/.test(css));
assert(css.includes('url("http://google.com")'));
assert(css.includes('.index'));
assert(css.includes('url("data:image/gif;base64,quotes")'));
assert(css.includes('.quotes'));
assert(css.includes('url(data:image/gif;base64,no-quote)'));
assert(css.includes('.no-quote'));

assert(fs.existsSync(__dirname + '/dist/' + css.match(/url\("([0-9a-f]+\.woff2)"\)/)[1]));
});
Expand Down
8 changes: 8 additions & 0 deletions test/integration/css-url/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
.index {
background: url("http://google.com");
}

.quotes {
background: url("data:image/gif;base64,quotes");
}

.no-quote {
background: url(data:image/gif;base64,no-quote);
}
2 changes: 2 additions & 0 deletions test/integration/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<h1>Hello world</h1>
<p>Linking to <a href="other.html">another page</a></p>
<a href="#hash_link">Hash link</a>
<a href="mailto:someone@acme.com">Mailto link</a>
<a href="tel:+33636757575">Tel link</a>
<script src="index.js"></script>
<script src="https://unpkg.com/parcel-bundler"></script>
</body>
Expand Down

0 comments on commit 781b7ec

Please sign in to comment.