getUrl.js 597 B

1234567891011121314151617181920212223242526
  1. "use strict";
  2. module.exports = function (url, options) {
  3. if (!options) {
  4. options = {};
  5. }
  6. if (!url) {
  7. return url;
  8. }
  9. url = String(url.__esModule ? url.default : url);
  10. // If url is already wrapped in quotes, remove them
  11. if (/^['"].*['"]$/.test(url)) {
  12. url = url.slice(1, -1);
  13. }
  14. if (options.hash) {
  15. url += options.hash;
  16. }
  17. // Should url be wrapped?
  18. // See https://drafts.csswg.org/css-values-3/#urls
  19. if (/["'() \t\n]|(%20)/.test(url) || options.needQuotes) {
  20. return "\"".concat(url.replace(/"/g, '\\"').replace(/\n/g, "\\n"), "\"");
  21. }
  22. return url;
  23. };