You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
448 B

4 years ago
  1. module.exports = function escape(url) {
  2. if (typeof url !== 'string') {
  3. return url
  4. }
  5. // If url is already wrapped in quotes, remove them
  6. if (/^['"].*['"]$/.test(url)) {
  7. url = url.slice(1, -1);
  8. }
  9. // Should url be wrapped?
  10. // See https://drafts.csswg.org/css-values-3/#urls
  11. if (/["'() \t\n]/.test(url)) {
  12. return '"' + url.replace(/"/g, '\\"').replace(/\n/g, '\\n') + '"'
  13. }
  14. return url
  15. }