rewrite-url.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. var path = require('path');
  2. var url = require('url');
  3. var isDataUriResource = require('../utils/is-data-uri-resource');
  4. var DOUBLE_QUOTE = '"';
  5. var SINGLE_QUOTE = '\'';
  6. var URL_PREFIX = 'url(';
  7. var URL_SUFFIX = ')';
  8. var PROTOCOL_LESS_PREFIX_PATTERN = /^[^\w\d]*\/\//;
  9. var QUOTE_PREFIX_PATTERN = /^["']/;
  10. var QUOTE_SUFFIX_PATTERN = /["']$/;
  11. var ROUND_BRACKETS_PATTERN = /[()]/;
  12. var URL_PREFIX_PATTERN = /^url\(/i;
  13. var URL_SUFFIX_PATTERN = /\)$/;
  14. var WHITESPACE_PATTERN = /\s/;
  15. var isWindows = process.platform == 'win32';
  16. function rebase(uri, rebaseConfig) {
  17. if (!rebaseConfig) {
  18. return uri;
  19. }
  20. if (isAbsolute(uri) && !isRemote(rebaseConfig.toBase)) {
  21. return uri;
  22. }
  23. if (isRemote(uri) || isSVGMarker(uri) || isInternal(uri) || isDataUriResource(uri)) {
  24. return uri;
  25. }
  26. if (isRemote(rebaseConfig.toBase)) {
  27. return url.resolve(rebaseConfig.toBase, uri);
  28. }
  29. return rebaseConfig.absolute
  30. ? normalize(absolute(uri, rebaseConfig))
  31. : normalize(relative(uri, rebaseConfig));
  32. }
  33. function isAbsolute(uri) {
  34. return path.isAbsolute(uri);
  35. }
  36. function isSVGMarker(uri) {
  37. return uri[0] == '#';
  38. }
  39. function isInternal(uri) {
  40. return /^\w+:\w+/.test(uri);
  41. }
  42. function isRemote(uri) {
  43. return /^[^:]+?:\/\//.test(uri) || PROTOCOL_LESS_PREFIX_PATTERN.test(uri);
  44. }
  45. function absolute(uri, rebaseConfig) {
  46. return path
  47. .resolve(path.join(rebaseConfig.fromBase || '', uri))
  48. .replace(rebaseConfig.toBase, '');
  49. }
  50. function relative(uri, rebaseConfig) {
  51. return path.relative(rebaseConfig.toBase, path.join(rebaseConfig.fromBase || '', uri));
  52. }
  53. function normalize(uri) {
  54. return isWindows ? uri.replace(/\\/g, '/') : uri;
  55. }
  56. function quoteFor(unquotedUrl) {
  57. if (unquotedUrl.indexOf(SINGLE_QUOTE) > -1) {
  58. return DOUBLE_QUOTE;
  59. } if (unquotedUrl.indexOf(DOUBLE_QUOTE) > -1) {
  60. return SINGLE_QUOTE;
  61. } if (hasWhitespace(unquotedUrl) || hasRoundBrackets(unquotedUrl)) {
  62. return SINGLE_QUOTE;
  63. }
  64. return '';
  65. }
  66. function hasWhitespace(url) {
  67. return WHITESPACE_PATTERN.test(url);
  68. }
  69. function hasRoundBrackets(url) {
  70. return ROUND_BRACKETS_PATTERN.test(url);
  71. }
  72. function rewriteUrl(originalUrl, rebaseConfig, pathOnly) {
  73. var strippedUrl = originalUrl
  74. .replace(URL_PREFIX_PATTERN, '')
  75. .replace(URL_SUFFIX_PATTERN, '')
  76. .trim();
  77. var unquotedUrl = strippedUrl
  78. .replace(QUOTE_PREFIX_PATTERN, '')
  79. .replace(QUOTE_SUFFIX_PATTERN, '')
  80. .trim();
  81. var quote = strippedUrl[0] == SINGLE_QUOTE || strippedUrl[0] == DOUBLE_QUOTE
  82. ? strippedUrl[0]
  83. : quoteFor(unquotedUrl);
  84. return pathOnly
  85. ? rebase(unquotedUrl, rebaseConfig)
  86. : URL_PREFIX + quote + rebase(unquotedUrl, rebaseConfig) + quote + URL_SUFFIX;
  87. }
  88. module.exports = rewriteUrl;